-
Notifications
You must be signed in to change notification settings - Fork 0
/
fct_grep.c
164 lines (139 loc) · 4.27 KB
/
fct_grep.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
/*
* fct_grep.c
*
* Created on: 8 dec. 2012
* Author: Hugo
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "read_file.h"
#include "search.h"
#include "fifo_context.h"
#include "fct_grep.h"
int run_grep_onafile(char *afilepath, struct arguments *args) {
FILE *file = NULL;
if (open_file(afilepath, &file)) {
exit(EXIT_FAILURE);
}
//default size for one line (debug value:50 , prod value: 1000)
size_t defaultsize = 50;
//init pointer for line buffer
char *currentline = NULL;
//start the main loop
int read_error_line;
unsigned int match_count = 0, line_count = 0;
long byte_offset = 0;
long current_byte_offset = 0;
fpos_t current_endline_pos;
Fifo_linecontext *before_line_pos = NULL;
//fpos_t before_line_pos;
//unsigned int count_B = 0;
unsigned int line_print_count = 0;
fifo_savenextline(&before_line_pos, file, args->opt_B_lines); //save the pos before for context
if (args->opt_b)
byte_offset = ftell(file);
read_error_line =
(!args->opt_z) ?
get_line_file(file, ¤tline, &defaultsize) :
get_line_file_nd(file, ¤tline, &defaultsize);
while (!read_error_line) {
fgetpos(file, ¤t_endline_pos); //save the start of the nextline
if (args->opt_n)
line_count++;
if (search_simple_regex(currentline, args->pattern, args)) {
match_count++;
if (!(args->opt_B == 1 || args->opt_C == 1 || args->opt_A == 1)) {
printline(currentline, line_count, byte_offset, args, afilepath);
} else {
//Print with context
if(args->opt_B == 1 || args->opt_C == 1){
current_byte_offset = ftell(file);
fsetpos(file, before_line_pos->data);
line_print_count = 0;
read_error_line =
(!args->opt_z) ?
get_line_file(file, ¤tline,
&defaultsize) :
get_line_file_nd(file, ¤tline,
&defaultsize);
while ((line_print_count <= (args->opt_B_lines))
&& !read_error_line) {
line_print_count++;
printline(currentline, line_count, byte_offset, args, afilepath);
if (current_byte_offset == ftell(file))
break;
read_error_line =
(!args->opt_z) ?
get_line_file(file, ¤tline,
&defaultsize) :
get_line_file_nd(file, ¤tline,
&defaultsize);
}
}
if(args->opt_A == 1 && args->opt_C == 0) printline(currentline, line_count, byte_offset, args, afilepath);
if(args->opt_C == 1 || args->opt_A == 1){
line_print_count = 0;
read_error_line =
(!args->opt_z) ?
get_line_file(file, ¤tline,
&defaultsize) :
get_line_file_nd(file, ¤tline,
&defaultsize);
while ((line_print_count < (args->opt_A_lines))
&& !read_error_line) {
line_print_count++;
printline(currentline, line_count, byte_offset, args, afilepath);
read_error_line =
(!args->opt_z) ?
get_line_file(file, ¤tline,
&defaultsize) :
get_line_file_nd(file, ¤tline,
&defaultsize);
}
}
fsetpos(file, ¤t_endline_pos);
}
}
if (args->opt_m && match_count >= args->opt_m_count)
break;
if (args->opt_b)
byte_offset = ftell(file);
if (args->opt_B == 1 || args->opt_C == 1) {
fifo_savenextline(&before_line_pos, file, args->opt_B_lines);
}
read_error_line =
(!args->opt_z) ?
get_line_file(file, ¤tline, &defaultsize) :
get_line_file_nd(file, ¤tline, &defaultsize);
}
//option count
if(args->opt_c && match_count>0) printcount(afilepath,match_count);
//end the main loop
fifo_clear(&before_line_pos);
fclose(file);
free(currentline);
currentline = NULL;
return 0;
}
int printline(char *aline, unsigned int line_count, long byte_offset,
struct arguments *args,char *filepath) {
if(args->opt_c) return 1;
char printout_n[100] = "";
char printout_b[100] = "";
//Simple Print out without context
if (args->opt_n) {
snprintf(printout_n, 100, "%u:", line_count);
}
if (args->opt_b) {
snprintf(printout_b, 100, "%lu:", byte_offset);
}
fprintf(stdout,"%s%s%s%s%s\n", (args->opt_H) ? filepath : "",
(args->opt_H) ? ":" : "", printout_n, printout_b, aline);
//End Simple Print out without context
return 1;
}
int printcount(char *filepath, unsigned int match_count){
fprintf(stdout,"%s:%u\n",filepath,match_count);
return 1;
}