From: borutr Date: Sun, 15 Jun 2003 18:59:22 +0000 (+0000) Subject: added functions fputStrSet(), appendStrSet(), joinStrSet(); X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=8518bef327102806ce21008f3f668320f956a8e3;p=fw%2Fsdcc added functions fputStrSet(), appendStrSet(), joinStrSet(); removed functions addToList(), join(), joinn(), pathCharsEquivalent(), pathCharTransform(), fixupPath(), getPathDifference() git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2698 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/src/SDCCutil.c b/src/SDCCutil.c index 134bc283..381d53d0 100644 --- a/src/SDCCutil.c +++ b/src/SDCCutil.c @@ -27,6 +27,7 @@ #include #endif #include +#include "dbuf.h" #include "SDCCglobl.h" #include "SDCCmacro.h" #include "SDCCutil.h" @@ -49,164 +50,65 @@ populateStringHash(const char **pin) return pret; } -/** Given an array of string pointers and another string, adds the - string to the end of the list. The end of the list is assumed to - be the first NULL pointer. +/** Prints elements of the set to the file, each element on new line */ void -addToList (const char **list, const char *str) +fputStrSet(FILE *fp, set *list) { - /* This is the bad way to do things :) */ - while (*list) - list++; - *list = Safe_strdup (str); - if (!*list) - { - werror (E_OUT_OF_MEM, __FILE__, 0); - exit (1); - } - *(++list) = NULL; -} - -/** Given an array of string pointers returns a string containing all - of the strings seperated by spaces. The returned string is on the - heap. The join stops when a NULL pointer is hit. -*/ -char * -join(const char **pplist) -{ - buffer[0] = 0; - - while (*pplist) - { - strncatz(buffer, *pplist, PATH_MAX); - strncatz(buffer, " ", PATH_MAX); - pplist++; - } - - return buffer; -} - -/** Given an array of string pointers, returns a string containing all - of the strings seperated by spaces. The returned string is on the - heap. n is the number of strings in the list. -*/ -char * -joinn(char **pplist, int n) -{ - buffer[0] = 0; - - while (n--) - { - strncatz(buffer, *pplist, PATH_MAX); - strncatz(buffer, " ", PATH_MAX); - pplist++; - } - - return buffer; -} + const char *s; -/** Returns TRUE if for the host the two path characters are - equivalent. -*/ -static bool -pathCharsEquivalent(char c1, char c2) -{ -#if NATIVE_WIN32 - /* win32 is case insensitive */ - if (tolower(c1) == tolower(c2)) - { - return TRUE; - } - /* And / is equivalent to \ */ - else if (c1 == '/' && c2 == '\\') - { - return TRUE; - } - else if (c1 == '\\' && c2 == '/') - { - return TRUE; - } - else - { - return FALSE; - } -#else - /* Assume a Unix host where they must match exactly. */ - return c1 == c2; -#endif -} - -static char -pathCharTransform(char c) -{ -#if NATIVE_WIN32 - if (c == '/') - { - return DIR_SEPARATOR_CHAR; - } - else - { - return c; - } -#else - return c; -#endif + for (s = setFirstItem(list); s != NULL; s = setNextItem(list)) { + fputs(s, fp); + fputc('\n', fp); + } } -/** Fixes up a potentially mixed path to the proper representation for - the host. Fixes up in place. +/** Prepend / append given strings to each item of string set. The result is in a + new string set. */ -static char * -fixupPath(char *pin) +set * +appendStrSet(set *list, const char *pre, const char *post) { - char *p = pin; - - while (*p) - { - *p = pathCharTransform(*p); - p++; - } - *p = '\0'; + set *new_list = NULL; + const char *item; + struct dbuf_s dbuf; + + for (item = setFirstItem(list); item != NULL; item = setNextItem(list)) { + dbuf_init(&dbuf, PATH_MAX); + if (pre != NULL) + dbuf_append(&dbuf, pre, strlen(pre)); + dbuf_append(&dbuf, item, strlen(item)); + if (post != NULL) + dbuf_append(&dbuf, post, strlen(post)); + addSet(&new_list, (void *)dbuf_c_str(&dbuf)); + dbuf_detach(&dbuf); + } - return pin; + return new_list; } -/** Returns the characters in p2 past the last matching characters in - p1. +/** Given a set returns a string containing all of the strings seperated + by spaces. The returned string is on the heap. */ -char * -getPathDifference (char *pinto, const char *p1, const char *p2) +const char * +joinStrSet(set *list) { - char *p = pinto; + const char *s; + struct dbuf_s dbuf; -#if NATIVE_WIN32 - /* win32 can have a path at the start. */ - if (strchr(p2, ':')) - { - p2 = strchr(p2, ':')+1; - } -#endif + dbuf_init(&dbuf, PATH_MAX); - while (*p1 != '\0' && *p2 != '\0') + for (s = setFirstItem(list); s != NULL; s = setNextItem(list)) { - if (pathCharsEquivalent(*p1, *p2) == FALSE) - { - break; - } - p1++; - p2++; + dbuf_append(&dbuf, s, strlen(s)); + dbuf_append(&dbuf, " ", 1); } - while (*p2) - { - *p++ = *p2++; - } - *p = '\0'; - return fixupPath(pinto); + s = dbuf_c_str(&dbuf); + dbuf_detach(&dbuf); + return s; } - /** Given a file with path information in the binary files directory, returns the directory component. Used for discovery of bin directory of SDCC installation. Returns NULL if the path is @@ -325,7 +227,7 @@ char *strncpyz(char *dest, const char *src, size_t n) assert(n > 0); --n; - // paranoia... + /* paranoia... */ if (strlen(src) > n) { fprintf(stderr, "strncpyz prevented buffer overrun!\n"); @@ -350,7 +252,7 @@ char *strncatz(char *dest, const char *src, size_t n) maxToCopy = n - destLen - 1; - // paranoia... + /* paranoia... */ if (strlen(src) + destLen >= n) { fprintf(stderr, "strncatz prevented buffer overrun!\n"); diff --git a/src/SDCCutil.h b/src/SDCCutil.h index 6ac097d2..fbfc7a04 100644 --- a/src/SDCCutil.h +++ b/src/SDCCutil.h @@ -33,33 +33,24 @@ */ hTab *populateStringHash(const char **pin); -/** Given an array of string pointers and another string, adds the - string to the end of the list. The end of the list is assumed to - be the first NULL pointer. +/** Prints elements of the set to the file, each element on new line */ -void addToList (const char **list, const char *str); +void fputStrSet(FILE *fp, set *list); -/** Given an array of string pointers returns a string containing all - of the strings seperated by spaces. The returned string is on the - heap. The join stops when a NULL pointer is hit. +/** Prepend / append given strings to each item of string set. The result is in a + new string set. */ -char *join(const char **pplist); +set *appendStrSet(set *list, const char *pre, const char *post); -/** Given an array of string pointers, returns a string containing all - of the strings seperated by spaces. The returned string is on the - heap. n is the number of strings in the list. +/** Given a set returns a string containing all of the strings seperated + by spaces. The returned string is on the heap. */ -char *joinn(char **pplist, int n); +const char *joinStrSet(set *list); -/** Returns the characters in the path p2 past the last matching characters in - p1. Processes in a host dependent way. For example, on win32 the - test is case insensitive and treats '/' and '\' as the same. -*/ -char *getPathDifference (char *pinto, const char *p1, const char *p2); - -/** Given an array of string pointers, returns a string containing all - of the strings seperated by spaces. The returned string is on the - heap. n is the number of strings in the list. +/** Given a file with path information in the binary files directory, + returns the directory component. Used for discovery of bin + directory of SDCC installation. Returns NULL if the path is + impossible. */ char *getBinPath (const char *prel); @@ -118,7 +109,4 @@ size_t SDCCsnprintf(char *, size_t, const char *, ...); # error "Need at least one of snprintf, vsnprintf, vsprintf!" # endif - - #endif -