| Libgretl Reference Manual |
|---|
strutilsstrutils — |
#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);
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. |
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.
|
int numeric_string (const char *str);
str : |
the string to examine. |
| Returns : | 1 if the given str is numeric, otherwise 0.
|
int count_fields (const char *s);
s : |
the string to process. |
| Returns : | the number of space-separated fields in s.
|
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.
|
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.
|
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. |
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.
|
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
|
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.
|
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.
|
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. |
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. |
char* gretl_strdup (const char *src);
src : |
the string to duplicate. |
| Returns : | an allocated copy of src, or NULL on error.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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. |
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. |
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. |
char* lower (char *str);
Converts any upper case characters in str to lower case.
str : |
the string to transform. |
| Returns : | the possibly modified string. |
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. |
char* chopstr (char *str);
Removes both leading and trailing space from a string.
str : |
the string to process. |
| Returns : | the possibly modified string. |
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.
|
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.
|
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. |
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. |
char* tailstrip (char *str);
Drop trailing space (and newline if any) from string.
str : |
the string to process. |
| Returns : | the modified string. |
char* compress_spaces (char *s);
Reduce multiple contiguous space characters to single spaces
within s.
s : |
the string to process. |
| Returns : | the compressed string. |
char* space_to_score (char *s);
Replace any spaces with underscores in s.
s : |
the string to process. |
| Returns : | the (possibly) modified string. |
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. |
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.
|
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.
|
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.
|
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.
|
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. |
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.
|
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.
|
double obs_str_to_double (const char *obs);
obs : |
string representation of observation number. |
| Returns : | the floating-point counterpart of 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. |
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.
|
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().
|
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().
|
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.
|
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. |
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.
|
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.
|
int* varname_match_list (const DATAINFO *pdinfo, const char *pattern);
pdinfo : |
|
pattern : |
|
| Returns : |
| << gretl_utils | gretl_list >> |