Debian Codegraph @

sstring.c

raw symbols SCIP
1 /* -*- Mode: C; c-file-style: "stroustrup" -*- */
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include "sstring.h"
6
7
8 /* strtok/strtok_r is a disaster, so here's a more sane one */
9
10 /* if *tok is NULL space is allocated; if the *tok is not NULL, and
11 * the token is too large it is truncated; tok_len returns the length,
12 * a pointer to **tok is also returned
13 * if tok is NULL do not return the parsed value
14 */
15
16 char* sstrtok( const char* str, const char* delim, char** tok, int tok_max,
17 int* tok_len, char** s )
18 {
19 char *end = NULL ;
20 int use = 0 ;
21
22 if ( delim == NULL ) { return NULL; }
23 if ( str != NULL ) { *s = (char*)str; }
24 if ( **s == '\0' ) { return NULL; } /* consumed last token */
25 end = strpbrk( *s, delim );
26 if ( end == NULL ) {
27 *tok_len = strlen(*s);
28 } else {
29 *tok_len = end - *s;
30 }
31
32 if ( tok ) {
33 if ( tok_max > 0 ) {
34 use = tok_max < *tok_len ? tok_max : *tok_len;
35 } else {
36 use = *tok_len;
37 }
38 if ( !*tok ) { *tok = malloc( use+1 ); }
39 sstrncpy( *tok, *s, use );
40 }
41 *s += *tok_len + ( end == NULL ? 0 : 1 );
42 return tok ? *tok : "";
43 }
44
45 void stolower( char* str ) {
46 if ( !str ) { return; }
47 for ( ; *str; str++ ) {
48 *str = tolower( *str );
49 }
50 }
51