summaryrefslogtreecommitdiff
path: root/tests/splitter.c
blob: 25292f621b21e0279eb4674585c7f8bd24c34244 (plain) (blame)
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
char **line_splitter(char *line,int *rlen) {
 char **a;
 int i=0,j=0;
 int len;
 len=1;
 for(i=0;line[i] && line[i] == ' ';i++);//skip leading space
 for(;line[i];) {
  for(;line[i] && line[i] != ' ';i++);//skip rest of data
  for(;line[i] && line[i] == ' ';i++);//skip rest of space
  len++;
 }
 a=malloc(sizeof(char *) * len+1);
 a[len]=0;
 len=0;//reuse!
 for(i=0;line[i] && line[i] == ' ';i++);//skip leading space
 a[len]=line+i;
 for(;;) {
  for(;line[i] && line[i] != ' ';i++);//skip rest of data
  if(line[i]) {
   line[i]=0;
   i++;
  } else {
   //we're at the end! I guess return this shit.
   len++;
   a[len]=0;
   *rlen=len;
   return a;
  }
  for(;line[i] && line[i] == ' ';i++);//skip rest of space
  if(line[i]) {
   len++;
   a[len]=line+i;
  } else {
   len++;
   a[len]=0;
   *rlen=len;
   return a;
  }
 }
 *rlen=len;
 return a;
}

int main(int argc,char *argv[]) {
 char **a;
 int len;
 int i;
 a=line_splitter(strdup(argv[1]),&len);
 for(i=0;i<len;i++) {
  printf("a[%d]=\"%s\"\n",i,a[i]);
 }
 return 0;
}