44 size_t lenpre = strlen(pre),
46 return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
59 size_t lensuf = strlen(suf),
61 return lenstr < lensuf ? false : strncmp(str + lenstr - lensuf, suf,
78 size_t i, len = strlen(str);
101 size_t i, j, len_str = strlen(str),
102 len_chars = strlen(chars);
104 for (i=0; i<len_str; i++) {
105 for (j=0; j<len_chars; j++) {
106 count += (str[i] == chars[j]);
130 char **
str_split(
char *original,
const char *delims,
int *len_ret)
137 size_t len = strlen(original);
138 size_t n_delim = strlen(delims);
141 char all_delim[1 + n_delim];
142 for (i=0; i<n_delim; i++)
143 all_delim[i] = delims[i];
144 all_delim[n_delim] =
'\0';
156 result =
Calloc(
char *, count);
160 copy =
Calloc(
char, len + 1);
161 strcpy(copy, original);
162 token = strtok(copy, all_delim);
164 result[i] =
Calloc(
char, strlen(token) + 1);
165 strcpy(result[i], token);
168 token = strtok(NULL, all_delim);
196 char *
get_line(FILE *fid,
char *filename,
char *buffer)
199 if (retval == NULL) {
200 err(
"[GenSVM Error]: Error reading from file %s\n", filename);
224 retval = sscanf(buffer, fmt, &value);
226 err(
"[GenSVM Error]: No double read from file.\n");
245 retval = sscanf(buffer, fmt, &value);
247 err(
"[GenSVM Error]: No long read from file.\n");
272 start = buffer + offset;
274 value = strtod(start, &end);
276 all_doubles[i] = value;
308 start = buffer + offset;
310 value = strtol(start, &end, 10);
312 all_longs[i] = value;
#define Calloc(type, size)
void err(const char *fmt,...)
Parse a formatted string and write it to standard error.
bool str_contains_char(const char *str, const char c)
Check if a string contains a char.
#define GENSVM_MAX_LINE_LENGTH
char ** str_split(char *original, const char *delims, int *len_ret)
Split a string on delimiters and return an array of parts.
long all_doubles_str(char *buffer, long offset, double *all_doubles)
Read all doubles in a given buffer.
bool str_endswith(const char *str, const char *suf)
Check if a string ends with a suffix.
long all_longs_str(char *buffer, long offset, long *all_longs)
Read all longs in a given buffer.
long get_fmt_long(FILE *fid, char *filename, const char *fmt)
Read a long integer from file following a format.
char * get_line(FILE *fid, char *filename, char *buffer)
Read line to buffer.
int count_str_occurrences(const char *str, const char *chars)
Count the number of times a string contains any character of another.
void next_line(FILE *fid, char *filename)
Move to next line in file.
bool str_startswith(const char *str, const char *pre)
Check if a string starts with a prefix.
double get_fmt_double(FILE *fid, char *filename, const char *fmt)
Read a double from file following a format.
Header file for gensvm_print.c.
Header file for gensvm_strutil.c.