strutils

strutils —

Synopsis




#define     SLASH
#define     SLASHSTR
#define     CTRLZ
int         string_is_blank                 (const char *s);
int         has_suffix                      (const char *str,
                                             const char *sfx);
int         numeric_string                  (const char *str);
int         count_fields                    (const char *s);
double      dot_atof                        (const char *s);
int         dotpos                          (const char *str);
int         slashpos                        (const char *str);
char*       delchar                         (int c,
                                             char *str);
int         haschar                         (char c,
                                             const char *s);
int         lastchar                        (char c,
                                             const char *s);
int         ends_with_backslash             (const char *s);
int         gretl_varchar_spn               (const char *s);
char*       gretl_trunc                     (char *str,
                                             size_t n);
char*       gretl_delete                    (char *str,
                                             int idx,
                                             int count);
char*       gretl_strdup                    (const char *src);
char*       gretl_strndup                   (const char *src,
                                             size_t n);
char*       gretl_strdup_printf             (const char *template,
                                             ...);
char*       gretl_word_strdup               (const char *src,
                                             const char **ptr);
char**      gretl_string_split              (const char *s,
                                             int *n);
char*       gretl_str_expand                (char **orig,
                                             const char *add,
                                             const char *sep);
double      gretl_double_from_string        (const char *s,
                                             const char **ptr);
char*       charsub                         (char *str,
                                             char find,
                                             char repl);
char*       shift_string_left               (char *str,
                                             size_t move);
char*       lower                           (char *str);
void        clear                           (char *str,
                                             int len);
char*       chopstr                         (char *str);
char*       switch_ext                      (char *targ,
                                             const char *src,
                                             char *ext);
int         get_base                        (char *targ,
                                             const char *src,
                                             char c);
int         equation_get_lhs_and_rhs        (const char *s,
                                             char **plh,
                                             char **prh);
int         top_n_tail                      (char *str);
char*       tailstrip                       (char *str);
char*       compress_spaces                 (char *s);
char*       space_to_score                  (char *s);
char*       safecpy                         (char *targ,
                                             const char *src,
                                             int n);
char**      strings_array_new               (int nstrs);
int         strings_array_add               (char ***pS,
                                             int *n,
                                             const char *p);
char**      strings_array_new_with_length   (int nstrs,
                                             int len);
char**      strings_array_dup               (char **strs,
                                             int n);
void        free_strings_array              (char **strs,
                                             int nstrs);
char*       get_obs_string                  (char *obs,
                                             int t,
                                             const DATAINFO *pdinfo);
char*       get_full_obs_string             (char *obs,
                                             int t,
                                             const DATAINFO *pdinfo);
double      obs_str_to_double               (const char *obs);
char*       colonize_obs                    (char *obs);
void        modify_date_for_csv             (char *s,
                                             int pd);
void        csv_obs_to_prn                  (int t,
                                             const DATAINFO *pdinfo,
                                             PRN *prn);
const char* print_time                      (const time_t *timep);
int         gretl_xml_validate              (const char *s);
char*       gretl_xml_encode                (const char *str);
int         gretl_xml_encode_to_buf         (char *targ,
                                             const char *src,
                                             int n);
void        unescape_url                    (char *url);
char*       make_varname_unique             (char *vname,
                                             int v,
                                             DATAINFO *pdinfo);
int         fix_varname_duplicates          (DATAINFO *pdinfo);
char*       append_dir                      (char *fname,
                                             const char *dir);
char*       build_path                      (char *targ,
                                             const char *dirname,
                                             const char *fname,
                                             const char *ext);
const char* path_last_element               (const char *path);
int*        varname_match_list              (const DATAINFO *pdinfo,
                                             const char *pattern);

Description

Details

SLASH

#define     SLASH


SLASHSTR

#define     SLASHSTR


CTRLZ

#define CTRLZ 26


string_is_blank ()

int         string_is_blank                 (const char *s);

s : the string to examine.
Returns : 1 if the string is NULL, of length zero, or contains nothing but space characters, otherwise returns 0.

has_suffix ()

int         has_suffix                      (const char *str,
                                             const char *sfx);

str : the string to check.
sfx : the suffix to check for.
Returns : 1 if str ends with sfx (on a case-insensitive comparison), 0 otherwise.

numeric_string ()

int         numeric_string                  (const char *str);

str : the string to examine.
Returns : 1 if the given str is numeric, otherwise 0.

count_fields ()

int         count_fields                    (const char *s);

s : the string to process.
Returns : the number of space-separated fields in s.

dot_atof ()

double      dot_atof                        (const char *s);

s : the string to convert.
Returns : the double-precision numeric interpretation of s, where the decimal point character is forced to be '.', regardless of the current locale.

dotpos ()

int         dotpos                          (const char *str);

str : the string to examine.
Returns : the integer position of the last "." within str, or strlen(str) in case a dot is not found, or the string ends with a (backward or forward) slash.

slashpos ()

int         slashpos                        (const char *str);

str : the string to examine.
Returns : the integer position of the last SLASH within str, or 0 in case a SLASH is not found.

delchar ()

char*       delchar                         (int c,
                                             char *str);

Deletes all instances of c within str.

c : the character to delete.
str : the string from which to delete c.
Returns : the possibly modified string.

haschar ()

int         haschar                         (char c,
                                             const char *s);

c : the character to look for.
s : the string to examine.
Returns : the first position of c in s, or -1 if c is not found.

lastchar ()

int         lastchar                        (char c,
                                             const char *s);

c : the character to look for.
s : the string to examine.
Returns : 1 if c is the last character in s, 0 otherwise

ends_with_backslash ()

int         ends_with_backslash             (const char *s);

s : the string to examine.
Returns : 1 if the last non-space character in s is a backslash, otherwise 0.

gretl_varchar_spn ()

int         gretl_varchar_spn               (const char *s);

s : the string to examine.
Returns : the length of the intial segment of s which consists of characters that are valid in a gretl variable or object name, namely a-z, A-Z, 0-9 and _, starting with a letter.

gretl_trunc ()

char*       gretl_trunc                     (char *str,
                                             size_t n);

Truncates the given str to the specified length.

str : the string to truncate.
n : the desired length of the truncated string.
Returns : the possibly truncated string.

gretl_delete ()

char*       gretl_delete                    (char *str,
                                             int idx,
                                             int count);

Deletes count characters from str, starting at position indx.

str : the string to process.
idx : the starting point for deleting characters.
count : the number of characters to delete.
Returns : the modified string.

gretl_strdup ()

char*       gretl_strdup                    (const char *src);

src : the string to duplicate.
Returns : an allocated copy of src, or NULL on error.

gretl_strndup ()

char*       gretl_strndup                   (const char *src,
                                             size_t n);

src : the string to be copied.
n : the maximum number of characters to copy.
Returns : an allocated copy of at most n characters from src, or NULL on error.

gretl_strdup_printf ()

char*       gretl_strdup_printf             (const char *template,
                                             ...);

Print the arguments according to format.

template : as in printf().
... : arguments to be printed.
Returns : allocated result of the printing, or NULL on failure.

gretl_word_strdup ()

char*       gretl_word_strdup               (const char *src,
                                             const char **ptr);

Copies the first 'word' found in src, where a word is defined as consisting of alphanumeric characters and the underscore. If ptr is not NULL, on exit it points at the next position in src after the copied word.

src : the source string.
ptr : location to receive end of word pointer, or NULL.
Returns : the allocated word or NULL in case no word is found, or if allocation fails.

gretl_string_split ()

char**      gretl_string_split              (const char *s,
                                             int *n);

Parses s into a set of zero or more substrings, separated by one or more spaces, and creates an array of those substrings. On sucessful exit, n holds the number of substrings.

s : the source string.
n : location to receive the number of substrings.
Returns : the allocated array or NULL in case of failure.

gretl_str_expand ()

char*       gretl_str_expand                (char **orig,
                                             const char *add,
                                             const char *sep);

Creates a newly allocated string built by concatenating orig and add, with sep interpolated unless sep is NULL, and replaces the content of orig with the new string. As a special case, if orig is NULL, or if the content of orig is NULL, we just duplicate add.

orig : pointer to the base string.
add : the string to be added.
sep : string to be interpolated, or NULL.
Returns : the reallocated string, or NULL on failure. In case of failure the content of orig is freed, if orig is not NULL, to avoid memory leakage.

gretl_double_from_string ()

double      gretl_double_from_string        (const char *s,
                                             const char **ptr);

Tries to parse the first 'word' found in s as a floating-point number, and returns either the number or NADBL on failure. If the parsing was successful and if ptr is not NULL, on exit it points at the next position in s after the just-parsed numeric portion.

s : the source string.
ptr : location to receive end of field pointer, or NULL.
Returns : floating-point value or NADBL.

charsub ()

char*       charsub                         (char *str,
                                             char find,
                                             char repl);

Replaces all occurrences of find with repl in str.

str : the string to operate on.
find : the character to replace.
repl : the replacement character.
Returns : the (possibly modified) string.

shift_string_left ()

char*       shift_string_left               (char *str,
                                             size_t move);

Shifts the content of str left by move places, dropping leading bytes as needed.

str : the string to process.
move : the number of places to shift.
Returns : the modified string.

lower ()

char*       lower                           (char *str);

Converts any upper case characters in str to lower case.

str : the string to transform.
Returns : the possibly modified string.

clear ()

void        clear                           (char *str,
                                             int len);

Sets all bytes in str to 0.

str : the string to clear.
len : the length of the string to be cleared.

chopstr ()

char*       chopstr                         (char *str);

Removes both leading and trailing space from a string.

str : the string to process.
Returns : the possibly modified string.

switch_ext ()

char*       switch_ext                      (char *targ,
                                             const char *src,
                                             char *ext);

For processing filenames: copies src to targ, minus any existing filename extension, and adds to targ the specified extension.

targ : the target or output string (must be pre-allocated).
src : the source or input string.
ext : the extension or suffix to attach.
Returns : the output string, targ.

get_base ()

int         get_base                        (char *targ,
                                             const char *src,
                                             char c);

If c is found in src, puts into targ the portion of src up to and including the last occurrence of c within src.

targ : the target or output string (must be pre-allocated).
src : the source or input string.
c : the "base marker" character.
Returns : 1 if c is found in str, otherwise 0.

equation_get_lhs_and_rhs ()

int         equation_get_lhs_and_rhs        (const char *s,
                                             char **plh,
                                             char **prh);

Given a string s, parse it into a left-hand side and a right-hand side, separated by an equals sign. Return in plh and prh allocated copies of the respective sides, with any leading or trailing white space trimmed.

s : equation in string form.
plh : pointer to receive left-hand side expression.
prh : pointer to receive right-hand side expression.
Returns : 0 on success, 1 on error.

top_n_tail ()

int         top_n_tail                      (char *str);

Drop leading space and trailing space and newline from string, then replace a trailing backslash (if any) with a space.

str : the string to process.
Returns : 1 if a trailing backslash was found, otherwise 0.

tailstrip ()

char*       tailstrip                       (char *str);

Drop trailing space (and newline if any) from string.

str : the string to process.
Returns : the modified string.

compress_spaces ()

char*       compress_spaces                 (char *s);

Reduce multiple contiguous space characters to single spaces within s.

s : the string to process.
Returns : the compressed string.

space_to_score ()

char*       space_to_score                  (char *s);

Replace any spaces with underscores in s.

s : the string to process.
Returns : the (possibly) modified string.

safecpy ()

char*       safecpy                         (char *targ,
                                             const char *src,
                                             int n);

Copies at most n characters from src to targ, and ensures that targ[n] is a NUL byte.

targ : target or output string (must be pre-allocated).
src : source or input string.
n : maximum length of target string.
Returns : the output string.

strings_array_new ()

char**      strings_array_new               (int nstrs);

Allocates storage for nstrs strings and initalizes all to NULL.

nstrs : number of strings in array.
Returns : the allocated array, or NULL on failure.

strings_array_add ()

int         strings_array_add               (char ***pS,
                                             int *n,
                                             const char *p);

Allocates storage for an extra member of S and adds a copy of string p in the last position. On success, the content of n is incremented by 1.

pS : pointer to strings array.
n : location of present number of strings in array.
p : string to add to array.
Returns : 0 on success, E_ALLOC on failure.

strings_array_new_with_length ()

char**      strings_array_new_with_length   (int nstrs,
                                             int len);

Allocates storage for nstrs strings, each of them len bytes long. The first byte of each string is initialized to 0.

nstrs : number of strings in array.
len : number of bytes per string.
Returns : the allocated array, or NULL on failure.

strings_array_dup ()

char**      strings_array_dup               (char **strs,
                                             int n);

strs : array of strings to be copied.
n : number of strings in array.
Returns : an allocated copy of strs, or NULL on failure.

free_strings_array ()

void        free_strings_array              (char **strs,
                                             int nstrs);

Frees each allocated string in strs, then frees strs itself. Checks that strs is not NULL before proceeding.

strs : array of allocated strings.
nstrs : number of strings in array.

get_obs_string ()

char*       get_obs_string                  (char *obs,
                                             int t,
                                             const DATAINFO *pdinfo);

obs : char array big enough to hold the observation (OBSLEN).
t : zero-based observation number.
pdinfo : pointer to dataset information.
Returns : the observation string corresponding to t.

get_full_obs_string ()

char*       get_full_obs_string             (char *obs,
                                             int t,
                                             const DATAINFO *pdinfo);

obs : char array big enough to hold the observation (OBSLEN).
t : zero-based observation number.
pdinfo : pointer to dataset information.
Returns : the observation string corresponding to t, using a four-digit year in case of dated daily data.

obs_str_to_double ()

double      obs_str_to_double               (const char *obs);

obs : string representation of observation number.
Returns : the floating-point counterpart of obs.

colonize_obs ()

char*       colonize_obs                    (char *obs);

Converts a decimal point in obs to a colon.

obs : string representation of observation number.
Returns : the (possibly) modified obs string.

modify_date_for_csv ()

void        modify_date_for_csv             (char *s,
                                             int pd);

s :
pd :

csv_obs_to_prn ()

void        csv_obs_to_prn                  (int t,
                                             const DATAINFO *pdinfo,
                                             PRN *prn);

t :
pdinfo :
prn :

print_time ()

const char* print_time                      (const time_t *timep);

timep : time to print.
Returns : pointer to a static string containing a locale-dependent representation of timep. In English, this will be in the format Y/m/d H:M.

gretl_xml_validate ()

int         gretl_xml_validate              (const char *s);

s : string to be tested.
Returns : 1 if s is acceptable for insertion into an XML file as is, 0 if it contains special characters that need to be escaped. See also gretl_xml_encode().

gretl_xml_encode ()

char*       gretl_xml_encode                (const char *str);

str : NUL-terminated source string.
Returns : an allocated re-write of str, with characters that are special in XML encoded as character entities. See also gretl_xml_validate().

gretl_xml_encode_to_buf ()

int         gretl_xml_encode_to_buf         (char *targ,
                                             const char *src,
                                             int n);

Writes into targ a version of src in which characters that are special in XML are encoded as character entities. See also gretl_xml_encode() for the case where the encoding of src is of unknown size at compile time.

targ : target buffer.
src : NUL-terminated source string.
n : size of targ in bytes.
Returns : 0 on success or 1 if the encoded version of src is longer than n bytes (allowing for NUL termination), in which case the conversion is not done.

unescape_url ()

void        unescape_url                    (char *url);

url : string representing a URL.

make_varname_unique ()

char*       make_varname_unique             (char *vname,
                                             int v,
                                             DATAINFO *pdinfo);

Given a tenative name for a new variable, check that it is not a duplicate of an existing varname. If it is, modify the new name so that it becomes unique. The ID number v is required so that, if the variable has already been added to the dataset, its name does not appear to conflict with itself! If the name to be tested is not associated with an existing variable, pass 0 for v.

vname : tentative name for variable.
v : the ID number for the new variable.
pdinfo : dataset information.
Returns : the (possibly modified) variable name.

fix_varname_duplicates ()

int         fix_varname_duplicates          (DATAINFO *pdinfo);

pdinfo :
Returns :

append_dir ()

char*       append_dir                      (char *fname,
                                             const char *dir);

fname :
dir :
Returns :

build_path ()

char*       build_path                      (char *targ,
                                             const char *dirname,
                                             const char *fname,
                                             const char *ext);

Writes to targ a full path composed of dirname, fname and (optionally) ext. This function ensures that an appropriate separator is inserted between dirname and fname, if dirname is not already terminated with such a separator.

targ : target string to write to (must be pre-allocated).
dirname : first part of path.
fname : filename.
ext : filename extension to be appended (or NULL).
Returns : the target string, targ.

path_last_element ()

const char* path_last_element               (const char *path);

path : path to work on.
Returns : a pointer to the last element of path, that is, the element following the last path separator character, if any. If path does not contain a separator, path itself is returned. Note that the return value may be the empty string, if path ends with a separator.

varname_match_list ()

int*        varname_match_list              (const DATAINFO *pdinfo,
                                             const char *pattern);

pdinfo :
pattern :
Returns :