2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
14 /// смещение (в строках) строки str в буфере d_buf.
15 int stringOffset (char ** d_buf
, char *str
)
18 int len
= strlen (str
);
23 if (! strncmp (S
, str
, len
))
34 int fixPosition (tt_state_t
*tt
, int t_op_no
, DArray
* exp
)
43 d_buf
= tt
->fileData
+ tt
->attachment
[t_op_no
].offset
- 1;
44 for (i
= exp
->get_size () - 1; i
>= 0; --i
)
45 fix
+= stringOffset (&d_buf
, exp
->get (i
));
50 void print (tt_state_t
* tt
, DArray
* d_array
, DArray
* d_lines
)
53 line
= tt
->attachment
[ENV
->t_op_no
].pair_line
+ 1;
54 line
+= fixPosition (tt
, ENV
->t_op_no
, d_array
);
55 if (SB_FLGET (SB_FLTAGSTYLE
))
56 printf ("%s\t%s\t+%i\n", ENV
->cgrep_exp
,
59 printf ("%s +%i: %s", tt
->fileName
, line
, d_lines
->get (line
-1));
63 bool scan (char *S
, DArray
* d_array
)
69 size
= d_array
->get_size ();
70 for (i
= 0; i
< size
; i
++) {
71 one
= d_array
->get (i
);
83 int file (char * fileName
)
87 DArray
* d_lines
= NULL
;
91 if (ENV
->cgrep_exp
== NULL
)
94 tt
= CNEW (tt_state_t
, 1);
95 memset (tt
, 0, sizeof (tt_state_t
));
96 tt
->fileName
= strdup (fileName
);
98 if (THE_TT::do_tt_file (tt
) == NULL
) {
100 return -1; // broken file
107 d_array
= Dsplit (ENV
->cgrep_exp
, (char *) ",");
111 if (! SB_FLGET (SB_FLTAGSTYLE
)) {
112 d_lines
= new DArray (1024);
113 d_lines
->from_file (tt
->fileName
);
117 ch
= t_op (&d_ptr
, &d_out
);
121 if (! scan (d_out
, d_array
))
123 print (tt
, d_array
, d_lines
);
127 d_lines
->foreach (free
);
140 sprintf (m_buf
, "%s%i", ENV
->tmp_files
, N
);
141 m_file
= fopen (m_buf
, "r");
145 while (fgets (m_buf
, 512, m_file
)) {
155 int cgrep (EArray
* d_files
)
163 if (strchr (ENV
->cgrep_exp
, ' ') ||
164 strchr (ENV
->cgrep_exp
, '\t')) {
165 fprintf (stderr
, "You can't use spaces and tabs in expression.\n");
169 if (ENV
->max_proc
== 1) {
170 for (i
= 0; i
< d_files
->get_size (); ++i
)
171 file (d_files
->get (i
));
175 d_files
->strings_to_file (ENV
->tmp_files
);
177 for (i
= 0; i
< ENV
->max_proc
; ++i
) {
178 j
= ENV
->proc_list
->fork ();
183 while ((j
= ENV
->proc_list
->wait_all ()) && j
)