-
Notifications
You must be signed in to change notification settings - Fork 3
/
text_parser.cpp
78 lines (69 loc) · 1.77 KB
/
text_parser.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
typedef unsigned int uint ;
#define HTAB 9
//**********************************************************************
void strip_newlines(char *rstr)
{
int slen = (int) strlen(rstr) ;
while (1) {
if (slen == 0)
break;
if (*(rstr+slen-1) == '\n' || *(rstr+slen-1) == '\r') {
slen-- ;
*(rstr+slen) = 0 ;
} else {
break;
}
}
}
//**********************************************************************
char *next_field(char *q)
{
while (*q != ' ' && *q != HTAB && *q != 0)
q++ ; // walk past non-spaces
while (*q == ' ' || *q == HTAB)
q++ ; // walk past all spaces
return q;
}
//**********************************************************************
int main(int argc, char **argv)
{
char fname[PATH_MAX+1] = "" ;
int idx ;
for (idx=1; idx<argc; idx++)
{
char *p = argv[idx];
strncpy(fname, p, PATH_MAX);
fname[PATH_MAX] = 0 ; // ensure NULL-term
}
if (fname[0] == 0)
{
puts("Usage: text_parser data_filename");
return 1;
}
FILE *fd = fopen(fname, "rt");
if (fd == NULL)
{
printf("%s: %s\n", fname, strerror(errno));
return 1;
}
#define MAX_READ_LEN 128
char inpstr[MAX_READ_LEN+1];
uint lcount = 0 ;
while (fgets(inpstr, MAX_READ_LEN, fd) != NULL)
{
strip_newlines(inpstr);
if (strlen(inpstr) == 0)
{
continue;
}
// a text string is ready now...
lcount++ ;
}
fclose(fd);
printf("%s: %u lines read\n", fname, lcount);
return 0;
} //lint !e818