-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdfutil.c
More file actions
215 lines (206 loc) · 4.99 KB
/
dfutil.c
File metadata and controls
215 lines (206 loc) · 4.99 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* dfutil.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "prim.h"
#define DB_DFU if(DB[13])
/****************************************************************************
* Count cols and rows in data table file
*/
int DataTableDimsI(FILE *inPF,int hr,int *ncolPI,int *nrowPI)
{
char *cPC, bufS[BBUFF_SIZE+1];
int line,ncol,nc,nrow;
*ncolPI = *nrowPI = 0;
line = ncol = nrow = 0;
while(fgets(bufS,BBUFF_SIZE,inPF) != NULL)
{
if(line < hr) {
line++;
continue;
}
line++;
/****
* Count the columns; first row is special as others compare to this
*/
cPC = bufS;
PASS_BLANK(cPC);
nc = 0;
while(isgraph(INT(*cPC)))
{
nc++;
NEXT_WORD(cPC);
}
if(nrow == 0) {
ncol = nc;
}
else if(nc != ncol) {
printf("Missing table data on line %d\n",line);
printf("Expecting %d values, %d found\n",ncol,nc);
return(FALSE);
}
nrow++;
}
*ncolPI = ncol;
*nrowPI = nrow;
return(TRUE);
}
/*************************************************************************
* Wrappers for FileStatsI and FileLinesI
* ... someday nice to have functions determine if given file or name????
*/
int FilenameStatsI(char *fnameS, int *linePI, int *minPI, int *maxPI,
int *comPI, int *blankPI)
{
int n;
FILE *fPF;
if(!(fPF=OpenUFilePF(fnameS, "r", NULL))) {
return(-1);
}
n = FileStatsI(fPF, linePI, minPI, maxPI, comPI, blankPI);
FILECLOSE(fPF);
return(n);
}
/************************************************************************/
int FilenameLinesI(char *fnameS, int ig_com, int ig_blank)
{
int n;
FILE *fPF;
if(!(fPF=OpenUFilePF(fnameS, "r", NULL))) {
return(-1);
}
n = FileLinesI(fPF, ig_com, ig_blank);
FILECLOSE(fPF);
return(n);
}
/*************************************************************************
* Reads through a file collecting stats on line number and size as well
* as number of comment and blank lines
* Returns total number of characters read in file
*/
int FileStatsI(FILE *fPF, int *linePI, int *minPI, int *maxPI, int *comPI,
int *blankPI)
{
int c,n,p,g,line,min,max,com,blank;
off_t fpos;
if(!fPF) {
return(0);
}
line = n = 0;
fpos = ftell(fPF);
max = -TOO_BIG;
min = TOO_BIG;
com = blank = p = g = 0;
/***
* Char at a time
*/
while( (c=fgetc(fPF)) != EOF)
{
n++;
p++;
/***
* Comment line
*/
if( (p==1) && (c=='#') ) {
com++;
}
/***
* Graphic?
*/
if(isgraph(INT(c))) {
g++;
}
/***
* End of line?
*/
if(!ISLINE(c)) {
line++;
if(g==0) {
blank++;
}
min = MIN_NUM(min,p);
max = MAX_NUM(max,p);
g = p = 0;
}
}
/***
* Set values to any real pointers
*/
if(linePI) {
*linePI = line;
}
if(minPI) {
*minPI = min;
}
if(maxPI) {
*maxPI = max;
}
if(comPI) {
*comPI = com;
}
if(blankPI) {
*blankPI = blank;
}
fseek(fPF,fpos,0);
return(n);
}
/*************************************************************************
* Returns the number of lines in a file
* If ig_com is true, comment lines are removed from the count
* If ig_blank is true, blank lines are removed from the count
*/
int FileLinesI(FILE *fPF, int ig_com, int ig_blank)
{
int n,nc,nb;
if(!fPF) {
return(0);
}
FileStatsI(fPF,&n,NULL,NULL,&nc,&nb);
if(ig_com) {
n -= nc;
}
if(ig_blank) {
n -= nb;
}
return(n);
}
/*************************************************************************
* Eats up to first non-print character in file (i.e. this line)
* Returns number of chars read (i.e. line length)
* MOVES FILE AHEAD
*/
int EatOneLineI(FILE *fPF)
{
int n,c;
n = 0;
if(!fPF)
{ return(n); }
n = 0;
while( (c=fgetc(fPF)) != EOF)
{
if(!ISLINE(c)) {
break;
}
n++;
}
return(n);
}