X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fstr.c;h=db17cea6dead784c3e8612ccbfa98efafeb8d1e5;hb=ed7bce25787929340a3f264f00dde7c979e571a9;hp=265c2a230d53e0798cea632e1507d23f25e4cad5;hpb=b321086267ad1014dc5d09886396cde30f094437;p=pspp diff --git a/src/str.c b/src/str.c index 265c2a230d..db17cea6de 100644 --- a/src/str.c +++ b/src/str.c @@ -194,6 +194,31 @@ st_pad_copy (char *dest, const char *src, size_t n) dest[n - 1] = 0; } } + +/* Copies SRC to DST, truncating DST to N-1 characters if + necessary. Always appends a null character. */ +void +st_trim_copy (char *dst, const char *src, size_t n) +{ + size_t len = strlen (src); + assert (n > 0); + if (len + 1 < n) + memcpy (dst, src, len + 1); + else + { + memcpy (dst, src, n - 1); + dst[n - 1] = '\0'; + } +} + + +/* Converts each character in S to uppercase. */ +void +st_uppercase (char *s) +{ + for (; *s != '\0'; s++) + *s = toupper ((unsigned char) *s); +} /* Initializes ST with initial contents S. */ void @@ -528,7 +553,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); @@ -538,7 +563,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; @@ -549,7 +574,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; @@ -557,49 +582,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; } @@ -607,7 +632,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); }