-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.c
executable file
·247 lines (199 loc) · 6.33 KB
/
common.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "include/common.h"
/* the following code deals with the user interface */
static int total_searched=0;
static int total_inserted=0;
static int inserted=0;
static int found=0;
/* display an error message and exit the program */
void fatal(char *str) { puts(str); exit(1); }
/* copy a block of memory, a word at a time */
void node_cpy(uint32_t *dest, uint32_t *src, uint32_t bytes)
{
bytes=bytes>>2;
while(bytes != 0)
{
*dest++=*src++;
bytes--;
}
}
/* string compare routine */
int32_t scmp(const char *s1, const char *s2)
{
while(*s1 != '\0' && *s1 == *s2 )
{
s1++;
s2++;
}
return( *s1-*s2 );
}
/*
* scan an array of characters and replace '\n' characters
* with '\0'
*/
void set_terminator(char *buffer, int length)
{
register int32_t i=0;
for(; i<length; ++i)
{
if( *(buffer+i) == '\n' )
{
*(buffer+i) = '\0';
}
}
}
/* string length routine */
int32_t slen(char *word)
{
char *x=word;
for(; *x != '\0'; ++x);
return x-word;
}
void reset_counters()
{
total_searched=total_inserted=inserted=found=0;
}
int32_t get_inserted()
{
return inserted;
}
int32_t get_found()
{
return found;
}
/* access the data structure to insert the strings found in the
* filename that is provided as a parameter. The file supplied must
* be smaller than 2GB in size, otherwise, the code below has to be
* modified to support large files, i.e., open64(), lseek64().
* This should not be required, however, since the caller to this
* function should be designed to handle multiple files, to allow you
* to break a large file into smaller pieces.
*/
double perform_insertion(char *to_insert)
{
int32_t input_file=0;
int32_t return_value=0;
uint32_t input_file_size=0;
uint32_t read_in_so_far=0;
char *buffer=0;
char *buffer_start=0;
timer start, stop;
double insert_real_time=0.0;
/* open the file for reading */
if( (input_file=(int32_t) open(to_insert, O_RDONLY))<=0)
fatal(BAD_INPUT);
/* get the size of the file in bytes */
input_file_size=lseek(input_file, 0, SEEK_END);
/* allocate a buffer in memory to store the file */
if( (buffer = (char *)calloc(1, input_file_size+1 )) == NULL)
fatal(MEMORY_EXHAUSTED);
/* keep a pointer to the start of the buffer */
buffer_start=buffer;
/* read the file into memory and close the file pointer */
lseek(input_file, 0, SEEK_SET);
/* attempt to read the entire file into memory */
while(read_in_so_far < input_file_size)
{
return_value=read(input_file, buffer, input_file_size);
assert(return_value>=0);
read_in_so_far+=return_value;
}
close(input_file);
/* make sure that all strings are null terminated */
set_terminator(buffer, input_file_size);
/* start the timer for insertion */
gettimeofday(&start, NULL);
/* main insertion loop */
time_loop_insert:
/* insert the first null-terminated string in the buffer */
if(insert(buffer))
{
inserted++;
}
total_inserted++;
/* point to the next string in the buffer */
for(; *buffer != '\0'; buffer++);
buffer++;
/* if the buffer pointer has been incremented to beyond the size of the file,
* then all strings have been processed, and the insertion is complete.
*/
if(buffer - buffer_start >= input_file_size) goto insertion_complete;
goto time_loop_insert;
insertion_complete:
/* stop the insertion timer */
gettimeofday(&stop, NULL);
/* do the math to compute the time required for insertion */
insert_real_time = 1000.0 * ( stop.tv_sec - start.tv_sec ) + 0.001
* (stop.tv_usec - start.tv_usec );
insert_real_time = insert_real_time/1000.0;
/* free the temp buffer used to store the file in memory */
free(buffer_start);
/* return the elapsed insertion time */
return insert_real_time;
}
/* access the data structure to search for the strings found in the
* filename that is provided as a parameter. The file supplied must
* be smaller than 2GB in size, otherwise, the code below has to be
* modified to support large files, i.e., open64(), lseek64().
* This should not be required, however, since the caller to this
* function should be designed to handle multiple files, to allow you
* to break a large file into smaller pieces.
*/
double perform_search(char *to_search)
{
int32_t input_file=0;
int32_t return_value=0;
uint32_t input_file_size=0;
uint32_t read_in_so_far=0;
char *buffer=0;
char *buffer_start=0;
timer start, stop;
double search_real_time=0.0;
/* attempt to open the file for reading */
if( (input_file=(int32_t)open (to_search, O_RDONLY)) <= 0 ) fatal(BAD_INPUT);
/* get the size of the file */
input_file_size=lseek(input_file, 0, SEEK_END);
/* create a buffer to match the size of the file */
if( (buffer = (char *)calloc(1, input_file_size+1 )) == NULL)
fatal(MEMORY_EXHAUSTED);
/* read the file into memory */
buffer_start=buffer;
lseek(input_file, 0, SEEK_SET);
/* attempt to read the entire file into memory */
while(read_in_so_far < input_file_size)
{
return_value=read(input_file, buffer, input_file_size);
assert(return_value>=0);
read_in_so_far+=return_value;
}
close(input_file);
/* make sure each string is null terminated */
set_terminator(buffer, input_file_size);
/* start the timer for search */
gettimeofday(&start, NULL);
time_loop_search:
/* search for the first null terminated string in the buffer */
if(search(buffer))
{
found++;
}
total_searched++;
/* point to the next string in the buffer */
for(; *buffer != '\0'; buffer++);
buffer++;
/* if the buffer pointer is incremented to beyond the size of the file,
* then all strings have been processed
*/
if(buffer - buffer_start >= input_file_size) goto search_complete;
goto time_loop_search;
search_complete:
/* stop the search timer */
gettimeofday(&stop, NULL);
/* do the math to compute the total time required for search */
search_real_time = 1000.0 * ( stop.tv_sec - start.tv_sec ) + 0.001
* (stop.tv_usec - start.tv_usec );
search_real_time = search_real_time/1000.0;
/* free the file buffer */
free(buffer_start);
/* return the elapsed search time */
return search_real_time;
}