diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2010-10-26 23:36:46 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2010-10-26 23:36:46 +0300 |
commit | 21e0e5cd482620bc73ff221b7ce26b41f97d7cee (patch) | |
tree | 849055ff1ae0f02d9bd99233e7adc2801c31606a /build | |
parent | b2a688ee00922811b6e77852bd43b7d0f5b0e0c0 (diff) | |
download | rpm-21e0e5cd482620bc73ff221b7ce26b41f97d7cee.tar.gz rpm-21e0e5cd482620bc73ff221b7ce26b41f97d7cee.tar.bz2 rpm-21e0e5cd482620bc73ff221b7ce26b41f97d7cee.zip |
Bury StringBuf inside librpmbuild internals
Diffstat (limited to 'build')
-rw-r--r-- | build/misc.c | 79 | ||||
-rw-r--r-- | build/rpmbuild_internal.h | 1 | ||||
-rw-r--r-- | build/rpmbuild_misc.h | 42 |
3 files changed, 122 insertions, 0 deletions
diff --git a/build/misc.c b/build/misc.c index d2d05baac..16be0351f 100644 --- a/build/misc.c +++ b/build/misc.c @@ -5,9 +5,88 @@ #include <ctype.h> #include <stdlib.h> +#include <rpm/rpmstring.h> #include "build/rpmbuild_misc.h" #include "debug.h" +#define BUF_CHUNK 1024 + +struct StringBufRec { + char *buf; + char *tail; /* Points to first "free" char */ + int allocated; + int free; +}; + +StringBuf newStringBuf(void) +{ + StringBuf sb = xmalloc(sizeof(*sb)); + + sb->free = sb->allocated = BUF_CHUNK; + sb->buf = xcalloc(sb->allocated, sizeof(*sb->buf)); + sb->buf[0] = '\0'; + sb->tail = sb->buf; + + return sb; +} + +StringBuf freeStringBuf(StringBuf sb) +{ + if (sb) { + sb->buf = _free(sb->buf); + sb = _free(sb); + } + return sb; +} + +void truncStringBuf(StringBuf sb) +{ + sb->buf[0] = '\0'; + sb->tail = sb->buf; + sb->free = sb->allocated; +} + +void stripTrailingBlanksStringBuf(StringBuf sb) +{ + while (sb->free != sb->allocated) { + if (! risspace(*(sb->tail - 1))) + break; + sb->free++; + sb->tail--; + } + sb->tail[0] = '\0'; +} + +char * getStringBuf(StringBuf sb) +{ + return sb->buf; +} + +void appendStringBufAux(StringBuf sb, const char *s, int nl) +{ + int l; + + l = strlen(s); + /* If free == l there is no room for NULL terminator! */ + while ((l + nl + 1) > sb->free) { + sb->allocated += BUF_CHUNK; + sb->free += BUF_CHUNK; + sb->buf = xrealloc(sb->buf, sb->allocated); + sb->tail = sb->buf + (sb->allocated - sb->free); + } + + /* FIX: shrug */ + strcpy(sb->tail, s); + sb->tail += l; + sb->free -= l; + if (nl) { + sb->tail[0] = '\n'; + sb->tail[1] = '\0'; + sb->tail++; + sb->free--; + } +} + uint32_t parseUnsignedNum(const char * line, uint32_t * res) { char * s1 = NULL; diff --git a/build/rpmbuild_internal.h b/build/rpmbuild_internal.h index ebc0c09e5..8eb75e1b6 100644 --- a/build/rpmbuild_internal.h +++ b/build/rpmbuild_internal.h @@ -3,6 +3,7 @@ #include <rpm/rpmbuild.h> #include <rpm/rpmutil.h> +#include "build/rpmbuild_misc.h" struct TriggerFileEntry { int index; diff --git a/build/rpmbuild_misc.h b/build/rpmbuild_misc.h index 2c126be97..9450f13da 100644 --- a/build/rpmbuild_misc.h +++ b/build/rpmbuild_misc.h @@ -76,6 +76,48 @@ gid_t getGidS(const char * gname); RPM_GNUC_INTERNAL void handleComments(char * s); +/** \ingroup rpmstring + */ +typedef struct StringBufRec *StringBuf; + +/** \ingroup rpmstring + */ +RPM_GNUC_INTERNAL +StringBuf newStringBuf(void); + +/** \ingroup rpmstring + */ +RPM_GNUC_INTERNAL +StringBuf freeStringBuf( StringBuf sb); + +/** \ingroup rpmstring + */ +RPM_GNUC_INTERNAL +void truncStringBuf(StringBuf sb); + +/** \ingroup rpmstring + */ +RPM_GNUC_INTERNAL +char * getStringBuf(StringBuf sb); + +/** \ingroup rpmstring + */ +RPM_GNUC_INTERNAL +void stripTrailingBlanksStringBuf(StringBuf sb); + +/** \ingroup rpmstring + */ +#define appendStringBuf(sb, s) appendStringBufAux(sb, s, 0) + +/** \ingroup rpmstring + */ +#define appendLineStringBuf(sb, s) appendStringBufAux(sb, s, 1) + +/** \ingroup rpmstring + */ +RPM_GNUC_INTERNAL +void appendStringBufAux(StringBuf sb, const char * s, int nl); + /** \ingroup rpmbuild * Parse an unsigned number. * @param line from spec file |