-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
134 lines (112 loc) · 2.21 KB
/
search.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRBUFSIZ 256
#if !defined(NDEBUG)
#define DBG(x) x
#define RUN(x)
#else
#define DBG(x)
#define RUN(x) x
#endif
#ifdef true
#undef true
#endif
#ifdef false
#undef false
#endif
#define true 1
#define false 0
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
typedef unsigned int bool;
char *usage = "\n\
\n\
search string file\n\
\n\
Assumes that the file was generated by kabi-parser.\n\
\n";
const char *EXPSTR = "EXPORTED";
const char *ARGSTR = "ARG";
const char *RETSTR = "RETURN";
const char *NSTSTR = "NESTED";
struct flocations {
fpos_t currentpos;
fpos_t deepestpos;
fpos_t shallowpos;
int currentlevel;
int deepestlevel;
int shallowlevel;
int count;
};
static FILE *open_file(char *filename)
{
FILE *fh = fopen(filename, "r");
if (fh)
return fh;
printf("\nCould not open file: %s\n\n", filename);
exit(1);
}
static bool check_args(int argc)
{
if(argc == 3)
return true;
puts(usage);
exit(1);
}
static int get_level(char *line)
{
int level;
if (!(strstr(line, NSTSTR)))
return 0;
sscanf(line, " NESTED: %d ", &level);
return level;
}
static int update_flocations
(struct flocations *flocs,
fpos_t curpos,
char *line)
{
int level = get_level(line);
if (level > flocs->deepestlevel) {
flocs->deepestlevel = level;
flocs->deepestpos = curpos;
}
else if (level < flocs->shallowlevel) {
flocs->shallowlevel = level;
flocs->shallowpos = curpos;
}
return level;
}
// While searching through the file:
// . count the number of times the object is found
// . keep fpos on the most deeply nested and least deeply nested
// occurrances.
// . keep fpos of the current occurance
//
int main(int argc, char **argv)
{
char strbuf[STRBUFSIZ];
char *filename;
char *searchstr;
FILE *fh;
struct flocations flocs;
fpos_t curpos;
memset(&flocs, 0, sizeof(struct flocations));
check_args(argc);
searchstr = argv[1];
filename = argv[2];
fh = open_file(filename);
while (!feof(fh)) {
fgetpos(fh, &curpos);
fgets(strbuf, STRBUFSIZ-1, fh);
if (strstr(strbuf, searchstr))
update_flocations(&flocs, curpos, strbuf);
}
fclose(fh);
return 0;
}