annotate src/readpstlog.c @ 70:b12f4e50e2e8

Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile. Signed/unsigned cleanup from 'CFLAGS=-Wextra ./configure' Reindent vbuf.c to make it readable.
author Carl Byington <carl@five-ten-sg.com>
date Sun, 11 May 2008 10:04:33 -0700
parents 034641c26ab9
children 987aa872294e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
1 #include "define.h"
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
2
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 #include <stdio.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 #include <ctype.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 #include <string.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 #ifndef _WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 # include <unistd.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 #ifndef __GNUC__
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 # include "XGetopt.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 #define BUF_SIZE 4096
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 int usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 size_t get(void * buf, int size, unsigned int count, FILE *fp);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20 int split_args(char *args, int **targ);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 int is_in(int a, int *b, int c);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23 int main(int argc, char** argv) {
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
24 int identity = 0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
25 int level = 0;
46
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 43
diff changeset
26 off_t *i = NULL;
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
27 int x, ptr, stop=0, flag;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
28 char *fname, *buf, rec_type;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
29 unsigned char version;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
30 int *show_type=NULL, show_size=0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
31 int *ex_type=NULL, ex_size=0;
70
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
32 unsigned int funcname=0, filename=0, text=0, end=0, dtype=0, line=0;
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
33 int c;
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
34 char ch;
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
35 FILE *fp;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
36 struct pst_debug_file_rec_m mfile_rec;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
37 struct pst_debug_file_rec_l lfile_rec;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
38 char format = 'D'; // default
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
39 while ((c = getopt(argc, argv, "f:t:x:")) != -1) {
70
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
40 ch = c;
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
41 switch(ch) {
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
42 case 'f':
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
43 // change the output format
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
44 format = toupper(optarg[0]);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
45 break;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
46 case 't':
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
47 //change the type of statements shown
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
48 show_size = split_args(optarg, &show_type);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
49 // type = atoi(optarg);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
50 break;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
51 case 'x':
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
52 // change the type of statements excluded
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
53 ex_size = split_args(optarg, &ex_type);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
54 break;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
55 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
56 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
57 if (argc > optind) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
58 fname = argv[optind++];
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
59 } else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
60 usage();
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
61 exit(2);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
62 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
64 fp = fopen(fname, "rb");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
65 if (fp == NULL) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
66 printf("Error. couldn't open debug file\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
67 return 2;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
68 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
69 if (get(&version, sizeof(char), 1, fp)==0) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
70 printf("Error. could not read version byte from front of file");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
71 return 3;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
72 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
74 if (version > DEBUG_VERSION) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
75 printf("Version number is higher than the format I know about.");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
76 return 4;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
77 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
79 buf = (char*) xmalloc(BUF_SIZE);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
81 while (!stop) {
48
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
82 off_t temp;
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
83 if (fread(&temp, sizeof(off_t), 1, fp)<=0) break;
48
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
84 x = (int)temp;
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
85 ptr = 0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
86 if (x > 0) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
87 if (i) free(i);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
88 i = (off_t*)xmalloc(sizeof(off_t)*(x+1));
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
89 // plus 1 cause we want to read the offset of the next index
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
90 if (get(i, sizeof(off_t), x+1, fp)==0) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
91 // we have reached the end of the debug file
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
92 printf("oh dear. we must now end\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
93 break;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
94 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
95 while (ptr < x) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
96 fseek(fp, i[ptr++], SEEK_SET);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
97 get(&rec_type, 1, sizeof(char), fp);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
98 if (rec_type == 'L') {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
99 get(&lfile_rec, sizeof(lfile_rec), 1, fp);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
100 funcname=lfile_rec.funcname;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
101 filename=lfile_rec.filename;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
102 text = lfile_rec.text;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
103 end = lfile_rec.end;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
104 dtype = lfile_rec.type;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
105 line = lfile_rec.line;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
106 } else if (rec_type == 'M') {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
107 get(&mfile_rec, sizeof(mfile_rec), 1, fp);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
108 funcname = mfile_rec.funcname;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
109 filename = mfile_rec.filename;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
110 text = mfile_rec.text;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
111 end = mfile_rec.end;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
112 dtype = mfile_rec.type;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
113 line = mfile_rec.line;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
114 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
115 if (dtype == DEBUG_FUNCENT_NO) level++;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
116 if ((show_type == NULL || is_in(dtype, show_type, show_size)) &&
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
117 (ex_type == NULL || !is_in(dtype, ex_type, ex_size))) {
70
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
118 unsigned int c = 0;
b12f4e50e2e8 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents: 52
diff changeset
119 flag = 0;
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
120 while (c < end) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
121 int ii = (level-1) * 4;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
122 if (ii < 0) ii = 0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
123 if (ii > 64) ii = 64;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
124 char indent[ii+1];
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
125 memset(indent, ' ', ii);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
126 indent[ii] = '\0';
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
127 if (c + (BUF_SIZE-1) < end) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
128 get(buf, 1, BUF_SIZE-1, fp);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
129 buf[BUF_SIZE-1] = '\0';
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
130 c += BUF_SIZE-1;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
131 } else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
132 get(buf, 1, end-c, fp);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
133 buf[end-c] = '\0';
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
134 c = end;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
135 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
136 if (flag == 0) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
137 if (format == 'I') { // indented text format
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
138 char *b = buf+text;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
139 identity++;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
140 //printf("%s %d %s/%s[%d]: ", indent, identity, &buf[filename], &buf[funcname], line);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
141 printf("%s %s/%s[%d]: ", indent, &buf[filename], &buf[funcname], line);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
142 while (b) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
143 char *p = strchr(b, '\n');
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
144 if (p) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
145 *p = '\0';
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
146 printf("%s\n%s ", b, indent);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
147 b = p + 1;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
148 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
149 else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
150 printf("%s", b);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
151 b = NULL;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
152 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
153 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
154 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
155 else if (format == 'T') { // text format
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
156 printf("%s/%s[%d]: %s", &buf[filename], &buf[funcname], line, &buf[text]);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
157 } else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
158 printf("Type: %d\nFile[line]: %s[%d]\nFunction:%s\nText:%s", dtype,
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
159 &buf[filename], line, &buf[funcname], &buf[text]);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
160 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
161 flag = 1;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
162 } else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
163 if (format == 'I') {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
164 char *b = buf;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
165 while (b) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
166 char *p = strchr(b, '\n');
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
167 if (p) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
168 *p = '\0';
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
169 printf("%s\n%s ", b, indent);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
170 b = p + 1;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
171 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
172 else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
173 printf("%s", b);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
174 b = NULL;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
175 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
176 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
177 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
178 else printf("%s", buf);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
179 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
180 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
181 printf("\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
182 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
183 if (dtype == DEBUG_FUNCRET_NO) level--;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
184 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
185 if (fseek(fp, i[ptr], SEEK_SET)==-1) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
186 printf("finished\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
187 break;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
188 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
189 } else {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
190 printf("...no more items\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
191 break;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
192 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
193 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
194 free(buf);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
195 fclose(fp);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
196 if (format == 'I') printf("final indent level = %i\n", level);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
197 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
198 }
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
199
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
200 size_t get(void *buf, int size, unsigned int count, FILE *fp) {
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
201 size_t z;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
202 if ((z=fread(buf, size, count, fp)) < count) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
203 printf("Read Failed! (size=%d, count=%d,z=%ld)\n", size, count, (long)z);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
204 exit(1);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
205 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
206 return z;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
207 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
208
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
209 int usage() {
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
210 printf("readlog -t[show_type] -x[exclude_type] -f[format] filename\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
211 printf("\tformat:\n\t\tt: text log format\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
212 printf("\t\ti: indented text log format\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
213 printf("\tshow_type:\n\t\tcomma separated list of types to show "
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
214 "[ie, 2,4,1,6]\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
215 printf("\texclude_type:\n\t\tcomma separated list of types to exclude "
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
216 "[ie, 1,5,3,7]\n");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
217 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
218 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
219
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
220
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
221 int split_args(char *args, int **targ) {
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
222 int count = 1, *i, x, z;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
223 char *tmp = args, *y;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
224 if (*targ != NULL) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
225 free(*targ);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
226 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
227 // find the number of tokens in the string. Starting
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
228 // from 1 cause there will always be one
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
229 while ((tmp = strchr(tmp, ',')) != NULL) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
230 tmp++; count++;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
231 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
232 *targ = (int*)xmalloc(count * sizeof(int));
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
233 i = *targ; // for convienience
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
234 tmp = args;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
235 z = 0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
236 for (x = 0; x < count; x++) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
237 y = strtok(tmp, ",");
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
238 tmp = NULL; // must be done after first call
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
239 if (y != NULL) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
240 i[x] = atoi(y);
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
241 z++;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
242 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
243 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
244 return z;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
245 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
246
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
247
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
248 // checks to see if the first arg is in the array of the second arg,
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
249 // the size of which is specified with the third arg. If the second
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
250 // arg is NULL, then it is obvious that it is not there.
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
251 int is_in(int a, int *b, int c){
52
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
252 int d = 0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
253 if (b == NULL || c == 0) { // no array or no items in array
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
254 return 0;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
255 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
256 while (d < c) {
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
257 if (a == b[d]) return 1;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
258 d++;
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
259 }
034641c26ab9 code cleanup
carl
parents: 48
diff changeset
260 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
261 }