TPCCLIB
Loading...
Searching...
No Matches
readfile.c File Reference

Functions for reading ASCII data files. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

void str_token_list_init (STR_TOKEN_LIST *lst)
 
void str_token_list_empty (STR_TOKEN_LIST *lst)
 
int str_token_list_add (STR_TOKEN_LIST *lst, char *new_item)
 
int str_token_list_del (STR_TOKEN_LIST *lst, int item)
 
int str_token_list_read (const char *filename, STR_TOKEN_LIST *lst)
 
int textfileReadLines (const char *filename, STR_TOKEN_LIST *lst)
 
int readStrtokens (const char *filename, char ***toklist)
 
int asciiCommentLine (const char *line, int *cont)
 

Detailed Description

Functions for reading ASCII data files.

Author
Vesa Oikonen

Definition in file readfile.c.

Function Documentation

◆ asciiCommentLine()

int asciiCommentLine ( const char * line,
int * cont )

Check if ASCII text line starts with comment character '#'. Comment character is searched from the first non-space character (space characters here include spaces and tabs).

Returns
1 if this is comment line and 0 if not.
Author
Vesa Oikonen
Parameters
linePointer to string containing one line of ASCII text file
contOptional pointer which is set to the index of line where the first non-space character after the comment character starts. If line does not start with comment character, then this will point to the first non-space character of the line.
Enter NULL if not needed.

Definition at line 246 of file readfile.c.

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}

Referenced by sifRead().

◆ readStrtokens()

int readStrtokens ( const char * filename,
char *** toklist )

Read list of string tokens from specified file Remember to free the memory of string list.

Returns
Returns the nr of tokens or <0 in case of an error.
Parameters
filenameName of file to read
toklistPointer to list of strings read and allocated here

Definition at line 181 of file readfile.c.

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}

◆ str_token_list_add()

int str_token_list_add ( STR_TOKEN_LIST * lst,
char * new_item )

Put a string in STR_TOKEN_LIST.

Returns
Returns 0, if successful, and <>0 in case of an error.
Parameters
lstList that has to be initialized beforehand.
new_itemString that is added to list.

Definition at line 42 of file readfile.c.

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}

Referenced by str_token_list_read(), and textfileReadLines().

◆ str_token_list_del()

int str_token_list_del ( STR_TOKEN_LIST * lst,
int item )

Remove the specified string item from the STR_TOKEN_LIST.

Returns
Returns 0, if successful, and <>0 in case of an error.
Parameters
lstList that has to be initialized beforehand.
itemItem number to remove (1..item_nr).

Definition at line 70 of file readfile.c.

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}

Referenced by sifRead().

◆ str_token_list_empty()

void str_token_list_empty ( STR_TOKEN_LIST * lst)

Free memory allocated for STR_TOKEN_LIST. All contents are destroyed.

Parameters
lstPointer to list to be emptied.

Definition at line 26 of file readfile.c.

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}

Referenced by roi_read(), sifRead(), str_token_list_read(), and textfileReadLines().

◆ str_token_list_init()

void str_token_list_init ( STR_TOKEN_LIST * lst)

Initiate STR_TOKEN_LIST structure. This should be called once before first use.

Parameters
lstPointer to list to be initiated.

Definition at line 13 of file readfile.c.

16 {
17 memset(lst, 0, sizeof(STR_TOKEN_LIST));
18 lst->list_size=0; lst->token_nr=0; lst->tok=NULL;
19}

Referenced by roi_read(), and sifRead().

◆ str_token_list_read()

int str_token_list_read ( const char * filename,
STR_TOKEN_LIST * lst )

Read all string tokens from text file into STR_TOKEN_LIST. List needs to be initialized. Previous contents are deleted.

Returns
Returns 0, if successful, and <>0 in case of an error.
Parameters
filenameName of text file to read
lstToken list is allocated by this function.

Definition at line 93 of file readfile.c.

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}
void str_token_list_empty(STR_TOKEN_LIST *lst)
Definition readfile.c:26
int str_token_list_add(STR_TOKEN_LIST *lst, char *new_item)
Definition readfile.c:42

◆ textfileReadLines()

int textfileReadLines ( const char * filename,
STR_TOKEN_LIST * lst )

Read all lines from text file into STR_TOKEN_LIST. List needs to be initialized. Previous contents are deleted.

Returns
Returns 0, if successful, and <>0 in case of an error.
See also
str_token_list_init, str_token_list_empty
Parameters
filenameName of text file to read
lstToken list is allocated by this function.

Definition at line 136 of file readfile.c.

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}
char * strTokenDup(const char *s1, const char *s2, int *next)
Definition strext.c:89

Referenced by roi_read(), and sifRead().