File: chron_strs.c

package info (click to toggle)
chron 2.3-35-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 224 kB
  • ctags: 8
  • sloc: ansic: 127; makefile: 1
file content (54 lines) | stat: -rw-r--r-- 1,218 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <ctype.h>
#include <R.h>

/* count fields in vector of strings */

void
cnt_flds_str(char **strings, Sint *nstrings, char **sep, Sint
	     *white_space, Sint *counts)
{
    Sint n = *nstrings, whitespace = *white_space;
    Sint i, nsep;
    char *s, c_sep = **sep;
    int c, in_white_space;
  
    if(!whitespace) {
	/* sep is a one-char delimiter */
	for(i = 0; i < n; ++i) {
	    s = strings[i];
	    if(*s == '\0')
		continue;	/* empty string */
	    nsep = 0;
	    while((c = *s++) != '\0')
		if(c == c_sep)
		    ++nsep;
	    counts[i] = nsep + 1;
	}
	return;
    } 
    /* items are delimited by white space (new lines, tabs, spaces) */
    for(i = 0; i < n; ++i) {
	s = strings[i];
	if(*s == '\0')
	    continue;		/* empty string */
	while((c = *s++) != '\0')
	    /* skip initial white space */
	    if(!isspace(c))
		break;
	if(c == '\0')
	    continue;		/* only whitespace in string */
	in_white_space = nsep = 0;
	while(c != '\0') {
	    /* look for transitions into/out of white space */
	    if(in_white_space && !isspace(c)){
		++nsep;
		in_white_space = 0;
	    }
	    else if(!in_white_space && isspace(c))
		in_white_space = 1;
	    c = *s++;
	}
	counts[i] = nsep + 1;
    }
    return;
}