TPCCLIB
Loading...
Searching...
No Matches
readasciifile.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10/*****************************************************************************/
11#include "tpcextensions.h"
12/*****************************************************************************/
13
14/*****************************************************************************/
28 FILE *fp,
33 int *nonprintable
34) {
35 if(nonprintable!=NULL) *nonprintable=0;
36 if(fp==NULL) return(0);
37 size_t n, bn;
38 int c;
39 n=bn=0;
40 while((c=fgetc(fp))!=EOF && n<SIZE_MAX-1 && bn<2) {
41 if(iscntrl(c) && c!=13 && c!=10 && c!=9 && c!=169) {
42 bn++;
43 if(c!=0) {
44 if(nonprintable!=NULL) *nonprintable=1;
45 break;
46 }
47 }
48 n++;
49 }
50 rewind(fp);
51 if(nonprintable!=NULL && bn>1) *nonprintable=1;
52 return n;
53}
54/*****************************************************************************/
55
56/*****************************************************************************/
57#if(0) // NOT finalized, better to do with strings read line-by-line
58
67size_t asciiFileChrCount(
69 FILE *fp,
71 const char *s,
73 int omit_comments,
75 int omit_quoted
76) {
77 size_t n=0;
78 if(fp==NULL) return(n);
79 if(s==NULL || strnlen(s, 1)==0) return(n);
80 int c, j;
81 int comment_line=0;
82 int single_quotation=0;
83 int double_quotation=0;
84 while((c=fgetc(fp))!=EOF && n<SIZE_MAX-1) {
85 /* Stop if non-printable character */
86 if(iscntrl(c) && c!=13 && c!=10 && c!=9) break;
87 /* Check for quotations */
88 if(c=='\'') {
89 if(single_quotation==0 && strchr(cptr+1, '\'')!=NULL) single_quotation=1;
90 else single_quotation=0;
91 continue;
92 }
93 if(*cptr=='\"') {
94 if(double_quotation==0 && strchr(cptr+1, '\"')!=NULL) double_quotation=1;
95 else double_quotation=0;
96 continue;
97 }
98 if(single_quotation==1 || double_quotation==1) continue;
99
100
101 /* Is character included in s ? */
102 for(j=0; j<strlen(s); j++) if(c==(int)s[j]) n++;
103 n++;
104 }
105 rewind(fp);
106 return(n);
107}
108#endif
109/*****************************************************************************/
110
111/*****************************************************************************/
119 FILE *fp,
122 char *data,
124 size_t maxlen
125) {
126 /* Check the input */
127 if(fp==NULL || maxlen<2) return NULL;
128 /* Allocate memory if needed */
129 char *cptr;
130 if(data!=NULL) cptr=data; else {
131 cptr=malloc(maxlen*sizeof(char)); if(cptr==NULL) return NULL;
132 }
133 size_t i=0; int c;
134 while((c=fgetc(fp))!=EOF && i<maxlen-1) cptr[i++]=(char)c;
135 cptr[i]=(char)0; if(i==0) {if(data==NULL) free(cptr); cptr=NULL;}
136 return cptr;
137}
138/*****************************************************************************/
139
140/*****************************************************************************/
151 const char *line,
155 int *cont
156) {
157 if(cont!=NULL) *cont=0;
158 if(line==NULL) return 0;
159 char *cptr=(char*)line;
160 int i=strspn(cptr, " \t"); cptr+=i; if(cont!=NULL) *cont=i;
161 if(*cptr!='#') return 0;
162 if(cont==NULL) return 1;
163 cptr++; i=strspn(cptr, " \t"); *cont+=(i+1);
164 return 1;
165}
166/*****************************************************************************/
167
168/*****************************************************************************/
char * asciiFileRead(FILE *fp, char *data, size_t maxlen)
int asciiCommentLine(const char *line, int *cont)
size_t asciiFileSize(FILE *fp, int *nonprintable)
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
Header file for library libtpcextensions.