X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fstr.c;h=d497078a585f1a994cf11be3a19e3ee0f894adbf;hb=0346f7d97515cdafb2caa6582224a8ad3f879ae8;hp=0a9d0d0f26d713fb64ce6de6b0e3f4cfa0a13377;hpb=36acd35beab37125d4083d98feaebec1d945e9b4;p=pspp-builds.git diff --git a/src/str.c b/src/str.c index 0a9d0d0f..d497078a 100644 --- a/src/str.c +++ b/src/str.c @@ -14,18 +14,18 @@ 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 +#include "str.h" +#include "error.h" #include #include #include #include "alloc.h" #include "error.h" #include "pool.h" -#include "str.h" /* sprintf() wrapper functions for convenience. */ @@ -77,7 +77,7 @@ 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; @@ -94,8 +94,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,136 +104,179 @@ mm_find_reverse (const char *haystack, size_t haystack_len, return 0; } -/* Compares S0 of length S0L to S1 of length S1L. The shorter string - is considered to be padded with spaces to the length of the - longer. */ +/* Compares the SIZE bytes in A to those in B, disregarding case, + and returns a strcmp()-type result. */ int -st_compare_pad (const char *s0, int s0l, const char *s1, int s1l) +buf_compare_case (const char *a_, const char *b_, size_t size) { - /* 254 spaces. */ - static char blanks[254] = - " " - " " - " " - " " - " "; - - int diff = s0l - s1l; - int r; + const unsigned char *a = (unsigned char *) a_; + const unsigned char *b = (unsigned char *) b_; - if (diff == 0) + while (size-- > 0) { - if (s0l == 0) - return 0; - return memcmp (s0, s1, s0l); + unsigned char ac = toupper (*a++); + unsigned char bc = toupper (*b++); + + if (ac != bc) + return ac > bc ? 1 : -1; } - else if (diff > 0) + + 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 +buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len) +{ + size_t min_len; + int result; + + min_len = a_len < b_len ? a_len : b_len; + result = memcmp (a, b, min_len); + if (result != 0) + return result; + else { - /* s0l > s1l */ - if (s1l) - { - r = memcmp (s0, s1, s1l); - if (r) - return r; - } - return memcmp (&s0[s1l], blanks, diff); + size_t idx; + + if (a_len < b_len) + { + for (idx = min_len; idx < b_len; idx++) + if (' ' != b[idx]) + return ' ' > b[idx] ? 1 : -1; + } + else + { + for (idx = min_len; idx < a_len; idx++) + if (a[idx] != ' ') + return a[idx] > ' ' ? 1 : -1; + } + return 0; } +} + +/* 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) +{ + return buf_compare_rpad (a, strlen (a), b, strlen (b)); +} + +/* 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 - /* diff<0 */ { - /* s0l < s1l */ - if (s0l) - { - r = memcmp (s0, s1, s0l); - if (r) - return r; - } - return memcmp (blanks, &s1[s0l], -diff); + memcpy (dst, src, src_len); + memset (&dst[src_len], ' ', dst_size - src_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. */ +/* 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_copy (char *dest, const char *src, size_t n) +buf_copy_str_lpad (char *dst, size_t dst_size, const char *src) { - size_t len; - - len = strlen (src); - if (len >= n) - memcpy (dest, src, n); + 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); + 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 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 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_bare_pad_len_copy (char *dest, const char *src, size_t n, size_t len) +buf_copy_rpad (char *dst, size_t dst_size, + const char *src, size_t src_size) { - if (len >= n) - memcpy (dest, src, n); + if (src_size >= dst_size) + memmove (dst, src, dst_size); else { - memcpy (dest, src, len); - memset (&dest[len], ' ', n - len); + memmove (dst, src, src_size); + memset (&dst[src_size], ' ', dst_size - src_size); } } -/* 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 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 -st_pad_copy (char *dest, const char *src, size_t n) +str_copy_rpad (char *dst, size_t dst_size, const char *src) { - size_t len; - - len = strlen (src); - if (len == n - 1) - strcpy (dest, src); - else if (len < n - 1) + 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'; } } + +/* Converts each character in S to uppercase. */ +void +str_uppercase (char *s) +{ + for (; *s != '\0'; s++) + *s = toupper ((unsigned char) *s); +} -/* Initializes ST inside pool POOL (which may be a null pointer) with - initial contents S. */ +/* Initializes ST with initial contents S. */ void -ds_create (struct pool *pool, struct string *st, const char *s) +ds_create (struct string *st, const char *s) { - st->pool = pool; st->length = strlen (s); - st->size = 8 + st->length * 2; - st->string = pool_malloc (pool, st->size + 1); + st->capacity = 8 + st->length * 2; + st->string = xmalloc (st->capacity + 1); strcpy (st->string, s); } -/* Initializes ST inside POOL (which may be null), making room for at - least SIZE characters. */ +/* Initializes ST, making room for at least CAPACITY characters. */ void -ds_init (struct pool *pool, struct string *st, size_t size) +ds_init (struct string *st, size_t capacity) { - st->pool = pool; st->length = 0; - if (size > 8) - st->size = size; + if (capacity > 8) + st->capacity = capacity; else - st->size = 8; - st->string = pool_malloc (pool, st->size + 1); + st->capacity = 8; + st->string = xmalloc (st->capacity + 1); } /* Replaces the contents of ST with STRING. STRING may overlap with @@ -241,17 +284,29 @@ ds_init (struct pool *pool, struct string *st, size_t size) void ds_replace (struct string *st, const char *string) { - char *s = st->string; - st->string = NULL; - ds_create (st->pool, st, string); - pool_free (st->pool, s); + size_t new_length = strlen (string); + if (new_length > st->capacity) + { + /* The new length is longer than the allocated length, so + there can be no overlap. */ + st->length = 0; + ds_concat (st, string, new_length); + } + else + { + /* Overlap is possible, but the new string will fit in the + allocated space, so we can just copy data. */ + st->length = new_length; + memmove (st->string, string, st->length); + } } /* Frees ST. */ void ds_destroy (struct string *st) { - pool_free (st->pool, st->string); + free (st->string); + st->string = NULL; } /* Truncates ST to zero length. */ @@ -261,29 +316,45 @@ ds_clear (struct string *st) st->length = 0; } -/* Ensures that ST can hold at least MIN_SIZE characters plus a null +/* Pad ST on the right with copies of PAD until ST is at least + LENGTH characters in size. If ST is initially LENGTH + characters or longer, this is a no-op. */ +void +ds_rpad (struct string *st, size_t length, char pad) +{ + assert (st != NULL); + if (st->length < length) + { + if (st->capacity < length) + ds_extend (st, length); + memset (&st->string[st->length], pad, length - st->length); + st->length = length; + } +} + +/* Ensures that ST can hold at least MIN_CAPACITY characters plus a null terminator. */ void -ds_extend (struct string *st, size_t min_size) +ds_extend (struct string *st, size_t min_capacity) { - if (min_size > st->size) + if (min_capacity > st->capacity) { - st->size *= 2; - if (st->size < min_size) - st->size = min_size * 2; + st->capacity *= 2; + if (st->capacity < min_capacity) + st->capacity = min_capacity * 2; - st->string = pool_realloc (st->pool, st->string, st->size + 1); + st->string = xrealloc (st->string, st->capacity + 1); } } -/* Shrink ST to the minimum size need to contain its content. */ +/* Shrink ST to the minimum capacity need to contain its content. */ void ds_shrink (struct string *st) { - if (st->size != st->length) + if (st->capacity != st->length) { - st->size = st->length; - st->string = pool_realloc (st->pool, st->string, st->size + 1); + st->capacity = st->length; + st->string = xrealloc (st->string, st->capacity + 1); } } @@ -305,21 +376,28 @@ ds_length (const struct string *st) /* Returns the allocation size of ST. */ size_t -ds_size (const struct string *st) +ds_capacity (const struct string *st) { - return st->size; + return st->capacity; } /* Returns the value of ST as a null-terminated string. */ char * -ds_value (const struct string *st) +ds_c_str (const struct string *st) { ((char *) st->string)[st->length] = '\0'; 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_value() has + This might not be an actual null character unless ds_c_str() has been called since the last modification to ST. */ char * ds_end (const struct string *st) @@ -329,7 +407,7 @@ ds_end (const struct string *st) /* Concatenates S onto ST. */ void -ds_concat (struct string *st, const char *s) +ds_puts (struct string *st, const char *s) { size_t s_len; @@ -343,47 +421,57 @@ ds_concat (struct string *st, const char *s) /* Concatenates LEN characters from BUF onto ST. */ void -ds_concat_buffer (struct string *st, const char *buf, size_t len) +ds_concat (struct string *st, const char *buf, size_t len) { ds_extend (st, st->length + len); memcpy (st->string + st->length, buf, len); st->length += len; } +void ds_vprintf (struct string *st, const char *format, va_list args); + + /* Formats FORMAT as a printf string and appends the result to ST. */ void ds_printf (struct string *st, const char *format, ...) +{ + va_list args; + + va_start (args, format); + ds_vprintf(st,format,args); + va_end (args); + +} + +/* Formats FORMAT as a printf string and appends the result to ST. */ +void +ds_vprintf (struct string *st, const char *format, va_list args) { /* Fscking glibc silently changed behavior between 2.0 and 2.1. Fsck fsck fsck. Before, it returned -1 on buffer overflow. Now, it returns the number of characters (not bytes) that would have been written. */ - va_list args; int avail, needed; - va_start (args, format); - avail = st->size - st->length + 1; + avail = st->capacity - st->length + 1; needed = vsnprintf (st->string + st->length, avail, format, args); - va_end (args); + if (needed >= avail) { ds_extend (st, st->length + needed); - va_start (args, format); vsprintf (st->string + st->length, format, args); - va_end (args); } else while (needed == -1) { - ds_extend (st, (st->size + 1) * 2); - avail = st->size - st->length + 1; - - va_start (args, format); + ds_extend (st, (st->capacity + 1) * 2); + avail = st->capacity - st->length + 1; + needed = vsnprintf (st->string + st->length, avail, format, args); - va_end (args); + } st->length += needed; @@ -391,21 +479,21 @@ ds_printf (struct string *st, const char *format, ...) /* Appends character CH to ST. */ void -ds_putchar (struct string *st, int ch) +ds_putc (struct string *st, int ch) { - if (st->length == st->size) + if (st->length == st->capacity) ds_extend (st, st->length + 1); st->string[st->length++] = ch; } -/* Reads a newline-terminated line from STREAM into ST. +/* Appends to ST a newline-terminated line read from STREAM. Newline is the last character of ST on return, unless an I/O error or end of file is encountered after reading some characters. Returns 1 if a line is successfully read, or 0 if no characters at all were read before an I/O error or end of file was encountered. */ int -ds_getline (struct string *st, FILE *stream) +ds_gets (struct string *st, FILE *stream) { int c; @@ -415,7 +503,7 @@ ds_getline (struct string *st, FILE *stream) for (;;) { - ds_putchar (st, c); + ds_putc (st, c); if (c == '\n') return 1; @@ -443,7 +531,7 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) /* Read the first line. */ ds_clear (st); where->line_number++; - if (!ds_getline (st, stream)) + if (!ds_gets (st, stream)) return 0; /* Read additional lines, if any. */ @@ -451,7 +539,7 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) { /* Remove trailing whitespace. */ { - char *s = ds_value (st); + char *s = ds_c_str (st); size_t len = ds_length (st); while (len > 0 && isspace ((unsigned char) s[len - 1])) @@ -460,13 +548,13 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) } /* Check for trailing \. Remove if found, bail otherwise. */ - if (ds_length (st) == 0 || ds_value (st)[ds_length (st) - 1] != '\\') + if (ds_length (st) == 0 || ds_c_str (st)[ds_length (st) - 1] != '\\') break; ds_truncate (st, ds_length (st) - 1); /* Append another line and go around again. */ { - int success = ds_getline (st, stream); + int success = ds_gets (st, stream); where->line_number++; if (!success) return 1; @@ -478,7 +566,7 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) char *cp; int quote = 0; - for (cp = ds_value (st); *cp; cp++) + for (cp = ds_c_str (st); *cp; cp++) if (quote) { if (*cp == quote) @@ -490,7 +578,7 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) quote = *cp; else if (*cp == '#') { - ds_truncate (st, cp - ds_value (st)); + ds_truncate (st, cp - ds_c_str (st)); break; } } @@ -500,31 +588,31 @@ ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where) /* Lengthed strings. */ -/* Creates a new lengthed string LS in POOL with contents as a copy of +/* Creates a new lengthed string LS with contents as a copy of S. */ void -ls_create (struct pool *pool, struct len_string *ls, const char *s) +ls_create (struct fixed_string *ls, const char *s) { ls->length = strlen (s); - ls->string = pool_alloc (pool, ls->length + 1); + ls->string = xmalloc (ls->length + 1); memcpy (ls->string, s, ls->length + 1); } -/* Creates a new lengthed string LS in POOL with contents as a copy of +/* Creates a new lengthed string LS with contents as a copy of BUFFER with length LEN. */ void -ls_create_buffer (struct pool *pool, struct len_string *ls, +ls_create_buffer (struct fixed_string *ls, const char *buffer, size_t len) { ls->length = len; - ls->string = pool_malloc (pool, len + 1); + ls->string = xmalloc (len + 1); memcpy (ls->string, buffer, len); ls->string[len] = '\0'; } /* 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; @@ -532,49 +620,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 in POOL backing LS. */ +/* Frees the memory backing LS. */ void -ls_destroy (struct pool *pool, struct len_string *ls) +ls_destroy (struct fixed_string *ls) { - pool_free (pool, ls->string); + 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_value (const struct len_string *ls) +ls_c_str (const struct fixed_string *ls) { return (char *) ls->string; } @@ -582,7 +670,7 @@ ls_value (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); }