X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fstr.c;h=081ffea5a74d36290c05450b3d5e52e04cd8fa90;hb=4a73877b4d56caed03d383fb4d38347a9774046f;hp=9d2d0b3c4023226cb08b57dcb9f4b222057b8347;hpb=92bfefccd465052e492f669ce561aa25b0110283;p=pspp-builds.git diff --git a/src/str.c b/src/str.c index 9d2d0b3c..081ffea5 100644 --- a/src/str.c +++ b/src/str.c @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include #include "str.h" @@ -25,7 +25,6 @@ #include #include "alloc.h" #include "error.h" -#include "pool.h" /* sprintf() wrapper functions for convenience. */ @@ -77,10 +76,10 @@ nvsprintf (char *buf, const char *format, va_list args) /* Reverses the order of NBYTES bytes at address P, thus converting between little- and big-endian byte orders. */ void -mm_reverse (void *p, size_t nbytes) +buf_reverse (char *p, size_t nbytes) { - unsigned char *h = p, *t = &h[nbytes - 1]; - unsigned char temp; + char *h = p, *t = &h[nbytes - 1]; + char temp; nbytes /= 2; while (nbytes--) @@ -94,8 +93,8 @@ mm_reverse (void *p, size_t nbytes) /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length HAYSTACK_LEN. Returns a pointer to the needle found. */ char * -mm_find_reverse (const char *haystack, size_t haystack_len, - const char *needle, size_t needle_len) +buf_find_reverse (const char *haystack, size_t haystack_len, + const char *needle, size_t needle_len) { int i; for (i = haystack_len - needle_len; i >= 0; i--) @@ -104,11 +103,31 @@ mm_find_reverse (const char *haystack, size_t haystack_len, return 0; } +/* Compares the SIZE bytes in A to those in B, disregarding case, + and returns a strcmp()-type result. */ +int +buf_compare_case (const char *a_, const char *b_, size_t size) +{ + const unsigned char *a = (unsigned char *) a_; + const unsigned char *b = (unsigned char *) b_; + + while (size-- > 0) + { + unsigned char ac = toupper (*a++); + unsigned char bc = toupper (*b++); + + if (ac != bc) + return ac > bc ? 1 : -1; + } + + return 0; +} + /* Compares A of length A_LEN to B of length B_LEN. The shorter string is considered to be padded with spaces to the length of the longer. */ int -st_compare_pad (const char *a, size_t a_len, const char *b, size_t b_len) +buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len) { size_t min_len; int result; @@ -137,63 +156,120 @@ st_compare_pad (const char *a, size_t a_len, const char *b, size_t b_len) } } -/* Copies SRC to DEST, truncating to N characters or right-padding - with spaces to N characters as necessary. Does not append a null - character. SRC must be null-terminated. */ -void -st_bare_pad_copy (char *dest, const char *src, size_t n) +/* Compares strin A to string B. The shorter string is + considered to be padded with spaces to the length of the + longer. */ +int +str_compare_rpad (const char *a, const char *b) { - size_t len; + return buf_compare_rpad (a, strlen (a), b, strlen (b)); +} - len = strlen (src); - if (len >= n) - memcpy (dest, src, n); +/* Copies string SRC to buffer DST, of size DST_SIZE bytes. + DST is truncated to DST_SIZE bytes or padded on the right with + spaces as needed. */ +void +buf_copy_str_rpad (char *dst, size_t dst_size, const char *src) +{ + size_t src_len = strlen (src); + if (src_len >= dst_size) + memcpy (dst, src, dst_size); else { - memcpy (dest, src, len); - memset (&dest[len], ' ', n - len); + memcpy (dst, src, src_len); + memset (&dst[src_len], ' ', dst_size - src_len); } } -/* Copies SRC to DEST, truncating SRC to N characters or right-padding - with spaces to N characters if necessary. Does not append a null - character. SRC must be LEN characters long but does not need to be - null-terminated. */ +/* Copies string SRC to buffer DST, of size DST_SIZE bytes. + DST is truncated to DST_SIZE bytes or padded on the left with + spaces as needed. */ void -st_bare_pad_len_copy (char *dest, const char *src, size_t n, size_t len) +buf_copy_str_lpad (char *dst, size_t dst_size, const char *src) { - if (len >= n) - memmove (dest, src, n); + size_t src_len = strlen (src); + if (src_len >= dst_size) + memcpy (dst, src, dst_size); else { - memmove (dest, src, len); - memset (&dest[len], ' ', n - len); + size_t pad_cnt = dst_size - src_len; + memset (&dst[0], ' ', pad_cnt); + memcpy (dst + pad_cnt, src, src_len); } } -/* Copies SRC to DEST, truncating SRC to N-1 characters or - right-padding with spaces to N-1 characters if necessary. Always - appends a null character. */ +/* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes. + DST is truncated to DST_SIZE bytes or padded on the right with + spaces as needed. */ void -st_pad_copy (char *dest, const char *src, size_t n) +buf_copy_rpad (char *dst, size_t dst_size, + const char *src, size_t src_size) { - size_t len; + if (src_size >= dst_size) + memmove (dst, src, dst_size); + else + { + memmove (dst, src, src_size); + memset (&dst[src_size], ' ', dst_size - src_size); + } +} - len = strlen (src); - if (len == n - 1) - strcpy (dest, src); - else if (len < n - 1) +/* Copies string SRC to string DST, which is in a buffer DST_SIZE + bytes long. + Truncates DST to DST_SIZE - 1 characters or right-pads with + spaces to DST_SIZE - 1 characters if necessary. */ +void +str_copy_rpad (char *dst, size_t dst_size, const char *src) +{ + size_t src_len = strlen (src); + if (src_len < dst_size - 1) { - memcpy (dest, src, len); - memset (&dest[len], ' ', n - 1 - len); - dest[n - 1] = 0; + memcpy (dst, src, src_len); + memset (&dst[src_len], ' ', dst_size - 1 - src_len); } else + memcpy (dst, src, dst_size - 1); + dst[dst_size - 1] = 0; +} + +/* Copies SRC to DST, which is in a buffer DST_SIZE bytes long. + Truncates DST to DST_SIZE - 1 characters, if necessary. */ +void +str_copy_trunc (char *dst, size_t dst_size, const char *src) +{ + size_t src_len = strlen (src); + assert (dst_size > 0); + if (src_len + 1 < dst_size) + memcpy (dst, src, src_len + 1); + else { - memcpy (dest, src, n - 1); - dest[n - 1] = 0; + memcpy (dst, src, dst_size - 1); + dst[dst_size - 1] = '\0'; } } + +/* Copies buffer SRC, of SRC_LEN bytes, + to DST, which is in a buffer DST_SIZE bytes long. + Truncates DST to DST_SIZE - 1 characters, if necessary. */ +void +str_copy_buf_trunc (char *dst, size_t dst_size, + const char *src, size_t src_size) +{ + size_t dst_len; + assert (dst_size > 0); + + dst_len = src_size < dst_size ? src_size : dst_size - 1; + memcpy (dst, src, dst_len); + dst[dst_len] = '\0'; +} + +/* Converts each character in S to uppercase. */ +void +str_uppercase (char *s) +{ + for (; *s != '\0'; s++) + *s = toupper ((unsigned char) *s); +} /* Initializes ST with initial contents S. */ void @@ -244,6 +320,7 @@ void ds_destroy (struct string *st) { free (st->string); + st->string = NULL; } /* Truncates ST to zero length. */ @@ -326,6 +403,13 @@ ds_c_str (const struct string *st) return st->string; } +/* Returns the string data inside ST. */ +char * +ds_data (const struct string *st) +{ + return st->string; +} + /* Returns a pointer to the null terminator ST. This might not be an actual null character unless ds_c_str() has been called since the last modification to ST. */ @@ -383,7 +467,9 @@ ds_vprintf (struct string *st, const char *format, va_list args) been written. */ int avail, needed; + va_list a1; + va_copy(a1, args); avail = st->capacity - st->length + 1; needed = vsnprintf (st->string + st->length, avail, format, args); @@ -392,17 +478,22 @@ ds_vprintf (struct string *st, const char *format, va_list args) { ds_extend (st, st->length + needed); - vsprintf (st->string + st->length, format, args); + vsprintf (st->string + st->length, format, a1); } else while (needed == -1) { + va_list a2; + va_copy(a2, a1); + ds_extend (st, (st->capacity + 1) * 2); avail = st->capacity - st->length + 1; - needed = vsnprintf (st->string + st->length, avail, format, args); + needed = vsnprintf (st->string + st->length, avail, format, a2); + va_end(a2); } + va_end(a1); st->length += needed; } @@ -521,7 +612,7 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) /* Creates a new lengthed string LS with contents as a copy of S. */ void -ls_create (struct len_string *ls, const char *s) +ls_create (struct fixed_string *ls, const char *s) { ls->length = strlen (s); ls->string = xmalloc (ls->length + 1); @@ -531,7 +622,7 @@ ls_create (struct len_string *ls, const char *s) /* Creates a new lengthed string LS with contents as a copy of BUFFER with length LEN. */ void -ls_create_buffer (struct len_string *ls, +ls_create_buffer (struct fixed_string *ls, const char *buffer, size_t len) { ls->length = len; @@ -542,7 +633,7 @@ ls_create_buffer (struct len_string *ls, /* Sets the fields of LS to the specified values. */ void -ls_init (struct len_string *ls, const char *string, size_t length) +ls_init (struct fixed_string *ls, const char *string, size_t length) { ls->string = (char *) string; ls->length = length; @@ -550,49 +641,49 @@ ls_init (struct len_string *ls, const char *string, size_t length) /* Copies the fields of SRC to DST. */ void -ls_shallow_copy (struct len_string *dst, const struct len_string *src) +ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src) { *dst = *src; } /* Frees the memory backing LS. */ void -ls_destroy (struct len_string *ls) +ls_destroy (struct fixed_string *ls) { free (ls->string); } /* Sets LS to a null pointer value. */ void -ls_null (struct len_string *ls) +ls_null (struct fixed_string *ls) { ls->string = NULL; } /* Returns nonzero only if LS has a null pointer value. */ int -ls_null_p (const struct len_string *ls) +ls_null_p (const struct fixed_string *ls) { return ls->string == NULL; } /* Returns nonzero only if LS is a null pointer or has length 0. */ int -ls_empty_p (const struct len_string *ls) +ls_empty_p (const struct fixed_string *ls) { return ls->string == NULL || ls->length == 0; } /* Returns the length of LS, which must not be null. */ size_t -ls_length (const struct len_string *ls) +ls_length (const struct fixed_string *ls) { return ls->length; } /* Returns a pointer to the character string in LS. */ char * -ls_c_str (const struct len_string *ls) +ls_c_str (const struct fixed_string *ls) { return (char *) ls->string; } @@ -600,7 +691,7 @@ ls_c_str (const struct len_string *ls) /* Returns a pointer to the null terminator of the character string in LS. */ char * -ls_end (const struct len_string *ls) +ls_end (const struct fixed_string *ls) { return (char *) (ls->string + ls->length); }