Main Page   Modules   Compound List   File List   Compound Members   File Members   Related Pages  

lib/stringbuf.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include "stringbuf.h"
00008 #include "debug.h"
00009 
00010 #define BUF_CHUNK 1024
00011 
00012 struct StringBufRec {
00013     /*@owned@*/ char *buf;
00014     /*@dependent@*/ char *tail;     /* Points to first "free" char */
00015     int allocated;
00016     int free;
00017 };
00018 
00019 StringBuf newStringBuf(void)
00020 {
00021     StringBuf sb = xmalloc(sizeof(struct StringBufRec));
00022 
00023     sb->free = sb->allocated = BUF_CHUNK;
00024     sb->buf = xcalloc(sb->allocated, sizeof(*sb->buf));
00025     sb->buf[0] = '\0';
00026     sb->tail = sb->buf;
00027     
00028     return sb;
00029 }
00030 
00031 void freeStringBuf(StringBuf sb)
00032 {
00033     if (! sb) {
00034         return;
00035     }
00036     
00037     free(sb->buf);
00038     free(sb);
00039 }
00040 
00041 void truncStringBuf(StringBuf sb)
00042 {
00043     sb->buf[0] = '\0';
00044     sb->tail = sb->buf;
00045     sb->free = sb->allocated;
00046 }
00047 
00048 void stripTrailingBlanksStringBuf(StringBuf sb)
00049 {
00050     while (sb->free != sb->allocated) {
00051         if (! isspace(*(sb->tail - 1))) {
00052             break;
00053         }
00054         sb->free++;
00055         sb->tail--;
00056     }
00057     sb->tail[0] = '\0';
00058 }
00059 
00060 char *getStringBuf(StringBuf sb)
00061 {
00062     return sb->buf;
00063 }
00064 
00065 void appendStringBufAux(StringBuf sb, const char *s, int nl)
00066 {
00067     int l;
00068 
00069     l = strlen(s);
00070     /* If free == l there is no room for NULL terminator! */
00071     while ((l + nl + 1) > sb->free) {
00072         sb->allocated += BUF_CHUNK;
00073         sb->free += BUF_CHUNK;
00074         sb->buf = xrealloc(sb->buf, sb->allocated);
00075         sb->tail = sb->buf + (sb->allocated - sb->free);
00076     }
00077     
00078     strcpy(sb->tail, s);
00079     sb->tail += l;
00080     sb->free -= l;
00081     if (nl) {
00082         sb->tail[0] = '\n';
00083         sb->tail[1] = '\0';
00084         sb->tail++;
00085         sb->free--;
00086     }
00087 }

Generated at Sun Apr 8 18:43:01 2001 for rpm by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000