TPCCLIB
Loading...
Searching...
No Matches
readfile.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpcmisc.h"
7/*****************************************************************************/
8
9/*****************************************************************************/
16) {
17 memset(lst, 0, sizeof(STR_TOKEN_LIST));
18 lst->list_size=0; lst->token_nr=0; lst->tok=NULL;
19}
20/*****************************************************************************/
21
22/*****************************************************************************/
29) {
30 for(int i=0; i<lst->list_size; i++)
31 if(lst->tok[i]!=NULL) free(lst->tok[i]);
32 if(lst->tok!=NULL) free(lst->tok);
33 lst->tok=NULL;
34 lst->list_size=0; lst->token_nr=0;
35}
36/*****************************************************************************/
37
38/*****************************************************************************/
44 STR_TOKEN_LIST *lst,
46 char *new_item
47) {
48 int i;
49 const int add_nr=10;
50
51 if(lst==NULL || new_item==NULL || strlen(new_item)<1) return(1);
52 if(lst->list_size<=lst->token_nr) {
53 lst->tok=realloc(lst->tok, sizeof(char*)*(lst->list_size+add_nr));
54 if(lst->tok==NULL) return(2);
55 for(i=lst->list_size; i<lst->list_size+add_nr; i++) lst->tok[i]=NULL;
56 lst->list_size+=add_nr;
57 }
58 lst->tok[lst->token_nr]=strdup(new_item);
59 if(lst->tok[lst->token_nr]==NULL) return(3);
60 lst->token_nr++;
61
62 return(0);
63}
64/*****************************************************************************/
65
66/*****************************************************************************/
72 STR_TOKEN_LIST *lst,
74 int item
75) {
76 //printf("str_token_list_del(list with %d items, %d)\n", lst->token_nr, item);
77 if(lst==NULL || item<1 || item>lst->token_nr) return(1);
78 if(lst->tok[item-1]!=NULL) free(lst->tok[item-1]);
79 for(int i=item; i<lst->token_nr; i++) {
80 //printf(" index %d -> %d\n", i, i-1);
81 lst->tok[i-1]=lst->tok[i]; lst->tok[i]=NULL;
82 }
83 lst->token_nr--;
84 return(0);
85}
86/*****************************************************************************/
87
88/*****************************************************************************/
95 const char *filename,
97 STR_TOKEN_LIST *lst
98) {
99 int i, ret, nr=0;
100 char *allfile, *cptr;
101 FILE *fp;
102
103 if(lst==NULL || filename==NULL || strlen(filename)<1) return(1);
105
106 /* Open file */
107 fp=fopen(filename, "r"); if(fp==NULL) return(2);
108 /* Get file size */
109 nr=0; while((ret=fgetc(fp))!=EOF) nr++; rewind(fp);
110 if(nr<1) {fclose(fp); return(0);}
111 /* Allocate memory for file contents */
112 allfile=(char*)malloc((nr+1)*sizeof(char));
113 if(allfile==NULL) {fclose(fp); return(3);}
114 /* Read file contents */
115 i=0; while((ret=fgetc(fp))!=EOF && i<nr) allfile[i++]=(char)ret;
116 fclose(fp); allfile[i]=(char)0;
117 /* and then fill the list */
118 cptr=strtok(allfile, " ;,|\t\n\r");
119 if(cptr==NULL) {free(allfile); return(4);}
120 do {
121 if(str_token_list_add(lst, cptr)) {free(allfile); return(10);}
122 cptr=strtok(NULL, " ;,|\t\n\r");
123 } while(cptr!=NULL);
124 free(allfile);
125
126 return(0);
127}
128/*****************************************************************************/
129
130/*****************************************************************************/
138 const char *filename,
140 STR_TOKEN_LIST *lst
141) {
142 int i, ret, nr=0;
143 char *allfile, *cptr, *line;
144 FILE *fp;
145
146 if(lst==NULL || filename==NULL || strlen(filename)<1) return(1);
148
149 /* Open file */
150 fp=fopen(filename, "r"); if(fp==NULL) return(2);
151 /* Get file size */
152 nr=0; while((ret=fgetc(fp))!=EOF) nr++; rewind(fp);
153 if(nr<1) {fclose(fp); return(0);} //printf("nr=%d\n", nr);
154 /* Allocate memory for file contents */
155 allfile=(char*)malloc((nr+1)*sizeof(char));
156 if(allfile==NULL) {fclose(fp); return(3);}
157 /* Read file contents */
158 i=0; while((ret=fgetc(fp))!=EOF && i<nr) allfile[i++]=(char)ret;
159 fclose(fp); allfile[i]=(char)0;
160 /* and then fill the list */
161 int npos=0; cptr=allfile;
162 line=strTokenDup(cptr, "\n\r\0", &npos);
163 if(line==NULL) {free(allfile); return(4);}
164 do {
165 //printf("line='%s'\n", line);
166 if(str_token_list_add(lst, line)) {free(allfile); free(line); return(10);}
167 free(line); if(npos==0) break; cptr=cptr+npos;
168 line=strTokenDup(cptr, "\n\r\0", &npos);
169 } while(line!=NULL);
170 free(allfile);
171
172 return(0);
173}
174/*****************************************************************************/
175
176/*****************************************************************************/
183 const char *filename,
185 char ***toklist
186) {
187 int i, ret, nr=0;
188 char *allfile, *cptr;
189 static char **list;
190 FILE *fp;
191
192 /* Check arguments */
193 if(strlen(filename)<1) return(-1);
194 /* Open file */
195 fp=fopen(filename, "r"); if(fp==NULL) return(-2);
196 /* Get file size */
197 nr=0; while((ret=fgetc(fp))!=EOF) nr++; rewind(fp);
198 if(nr<1) {fclose(fp); return(0);}
199 /* Allocate memory for file contents */
200 allfile=(char*)malloc((nr+1)*sizeof(char));
201 if(allfile==NULL) {fclose(fp); return(-4);}
202 /* Read file contents */
203 i=0; while((ret=fgetc(fp))!=EOF && i<nr) allfile[i++]=(char)ret;
204 fclose(fp); allfile[i]=(char)0;
205 /* Get the number of tokens */
206 cptr=allfile; nr=0;
207 while(1) {
208 i=strspn(cptr, " ;,|\t\n\r"); cptr+=i;
209 i=strcspn(cptr, " ;,|\t\n\r"); cptr+=i;
210 if(i==0 || *cptr==0) break; else nr++;
211 }
212 /*printf("Nr of tokens in %s: %d\n", filename, nr);*/
213 /* Allocate memory for array of strings */
214 list=(char**)malloc(nr*sizeof(char*));
215 if(list==NULL) {free(allfile); return(-5);}
216 /* and then fill the list */
217 cptr=strtok(allfile, " ;,|\t\n\r");
218 for(i=0; i<nr; i++) {
219 if(cptr==NULL) {
220 for(--i; i>=0; i--) free(list[i]);
221 free(list); free(allfile);
222 return(-8);
223 }
224 list[i]=(char*)malloc( (strlen(cptr)+1)*sizeof(char) );
225 if(list[i]==NULL) {
226 for(--i; i>=0; i--) free(list[i]);
227 free(list); free(allfile);
228 return(-9);
229 }
230 strcpy(list[i], cptr);
231 cptr=strtok(NULL, " ;,|\t\n\r");
232 }
233 free(allfile);
234 *toklist=list;
235 return(nr);
236}
237/*****************************************************************************/
238
239/*****************************************************************************/
248 const char *line,
254 int *cont
255) {
256 if(cont!=NULL) *cont=0;
257 if(line==NULL) return 0;
258 char *cptr=(char*)line;
259 int i=strspn(cptr, " \t"); cptr+=i; if(cont!=NULL) *cont=i;
260 if(*cptr!='#') return 0;
261 if(cont==NULL) return 1;
262 cptr++; i=strspn(cptr, " \t"); *cont+=(i+1);
263 return 1;
264}
265/*****************************************************************************/
266
267/*****************************************************************************/
Header file for libtpcmisc.
char * strTokenDup(const char *s1, const char *s2, int *next)
Definition strext.c:89
void str_token_list_empty(STR_TOKEN_LIST *lst)
Definition readfile.c:26
int str_token_list_del(STR_TOKEN_LIST *lst, int item)
Definition readfile.c:70
int textfileReadLines(const char *filename, STR_TOKEN_LIST *lst)
Definition readfile.c:136
void str_token_list_init(STR_TOKEN_LIST *lst)
Definition readfile.c:13
int str_token_list_add(STR_TOKEN_LIST *lst, char *new_item)
Definition readfile.c:42
int str_token_list_read(const char *filename, STR_TOKEN_LIST *lst)
Definition readfile.c:93
int asciiCommentLine(const char *line, int *cont)
Definition readfile.c:246
int readStrtokens(const char *filename, char ***toklist)
Definition readfile.c:181