GenSVM
gensvm_strutil.c
Go to the documentation of this file.
1 
31 #include "gensvm_strutil.h"
32 #include "gensvm_print.h"
33 
42 bool str_startswith(const char *str, const char *pre)
43 {
44  size_t lenpre = strlen(pre),
45  lenstr = strlen(str);
46  return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
47 }
48 
57 bool str_endswith(const char *str, const char *suf)
58 {
59  size_t lensuf = strlen(suf),
60  lenstr = strlen(str);
61  return lenstr < lensuf ? false : strncmp(str + lenstr - lensuf, suf,
62  lensuf) == 0;
63 }
64 
76 bool str_contains_char(const char *str, const char c)
77 {
78  size_t i, len = strlen(str);
79  for (i=0; i<len; i++)
80  if (str[i] == c)
81  return true;
82  return false;
83 }
84 
99 int count_str_occurrences(const char *str, const char *chars)
100 {
101  size_t i, j, len_str = strlen(str),
102  len_chars = strlen(chars);
103  int count = 0;
104  for (i=0; i<len_str; i++) {
105  for (j=0; j<len_chars; j++) {
106  count += (str[i] == chars[j]);
107  }
108  }
109  return count;
110 }
111 
130 char **str_split(char *original, const char *delims, int *len_ret)
131 {
132  char *copy = NULL,
133  *token = NULL,
134  **result = NULL;
135  int i, count;
136 
137  size_t len = strlen(original);
138  size_t n_delim = strlen(delims);
139 
140  // add the null terminator to the delimiters
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';
145 
146  // number of occurances of the delimiters
147  count = count_str_occurrences(original, delims);
148 
149  // extra count in case there is a delimiter at the end
150  count += (str_contains_char(delims, original[len - 1]));
151 
152  // extra count for the null terminator
153  count++;
154 
155  // allocate the result array
156  result = Calloc(char *, count);
157 
158  // tokenize a copy of the original string and keep the splits
159  i = 0;
160  copy = Calloc(char, len + 1);
161  strcpy(copy, original);
162  token = strtok(copy, all_delim);
163  while (token) {
164  result[i] = Calloc(char, strlen(token) + 1);
165  strcpy(result[i], token);
166  i++;
167 
168  token = strtok(NULL, all_delim);
169  }
170  free(copy);
171 
172  *len_ret = i;
173 
174  return result;
175 }
176 
183 void next_line(FILE *fid, char *filename)
184 {
185  char buffer[GENSVM_MAX_LINE_LENGTH];
186  get_line(fid, filename, buffer);
187 }
188 
196 char *get_line(FILE *fid, char *filename, char *buffer)
197 {
198  char *retval = fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
199  if (retval == NULL) {
200  err("[GenSVM Error]: Error reading from file %s\n", filename);
201  }
202  return retval;
203 }
204 
217 double get_fmt_double(FILE *fid, char *filename, const char *fmt)
218 {
219  char buffer[GENSVM_MAX_LINE_LENGTH];
220  double value = NAN;
221  int retval;
222 
223  get_line(fid, filename, buffer);
224  retval = sscanf(buffer, fmt, &value);
225  if (retval == 0)
226  err("[GenSVM Error]: No double read from file.\n");
227  return value;
228 }
229 
238 long get_fmt_long(FILE *fid, char *filename, const char *fmt)
239 {
240  char buffer[GENSVM_MAX_LINE_LENGTH];
241  long value = 0;
242  int retval;
243 
244  get_line(fid, filename, buffer);
245  retval = sscanf(buffer, fmt, &value);
246  if (retval == 0)
247  err("[GenSVM Error]: No long read from file.\n");
248  return value;
249 }
250 
265 long all_doubles_str(char *buffer, long offset, double *all_doubles)
266 {
267  double value;
268  long i = 0;
269  char *start = NULL,
270  *end = NULL;
271 
272  start = buffer + offset;
273  while (true) {
274  value = strtod(start, &end);
275  if (start != end) {
276  all_doubles[i] = value;
277  i++;
278  } else
279  break;
280  start = end;
281  end = NULL;
282  }
283 
284  return i;
285 }
286 
301 long all_longs_str(char *buffer, long offset, long *all_longs)
302 {
303  long value;
304  long i = 0;
305  char *start = NULL,
306  *end = NULL;
307 
308  start = buffer + offset;
309  while (true) {
310  value = strtol(start, &end, 10);
311  if (start != end) {
312  all_longs[i] = value;
313  i++;
314  } else
315  break;
316  start = end;
317  end = NULL;
318  }
319 
320  return i;
321 }
#define Calloc(type, size)
Definition: gensvm_memory.h:40
void err(const char *fmt,...)
Parse a formatted string and write it to standard error.
Definition: gensvm_print.c:84
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.