Mercurial > hg > index.fcgi > rcg > rcg-1
comparison rcg.c @ 14:669266246c6b
rcg.c: simplify string concatenation
author | paulo@thepaulopc |
---|---|
date | Fri, 12 Nov 2010 00:08:34 -0800 |
parents | 2fb87616ab65 |
children | acf12a2e872d |
comparison
equal
deleted
inserted
replaced
11:68f77fa02162 | 12:5152411bd5af |
---|---|
169 } | 169 } |
170 else | 170 else |
171 exit(args_error()); | 171 exit(args_error()); |
172 } | 172 } |
173 | 173 |
174 size_t CLR_START_len = strlen(CLR_START); | |
175 size_t clr_len = strlen(clr); | |
176 size_t CLR_END_len = strlen(CLR_END); | |
177 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); | |
178 | |
179 char *out = NULL; | 174 char *out = NULL; |
180 | 175 |
181 while (fgets(buf, BUFSIZE, stdin)) | 176 while (fgets(buf, BUFSIZE, stdin)) |
182 { | 177 { |
183 int so = -1; | 178 int so = -1; |
214 | 209 |
215 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA | 210 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA |
216 | 211 |
217 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) | 212 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) |
218 { | 213 { |
219 out_len += CLR_START_len + clr_len + CLR_END_len + CLR_CLEAR_len; | 214 size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR); |
215 | |
216 out_len += CLR_STRING_len; | |
220 out = realloc(out, out_len*sizeof(char)); | 217 out = realloc(out, out_len*sizeof(char)); |
221 if (!out) | 218 if (!out) |
222 exit(realloc_error()); | 219 exit(realloc_error()); |
223 strncpy(&out[out_pos], &buf[buf_pos], so); | 220 |
224 out_pos += so; | 221 sprintf(&out[out_pos], "%.*s%s%s%s%.*s%s", so, &buf[buf_pos], CLR_START, clr, CLR_END, match_len, &buf[buf_pos + so], CLR_CLEAR); |
225 buf_pos += so; | 222 |
226 strncpy(&out[out_pos], CLR_START, CLR_START_len); | 223 buf_pos += match_len + so; |
227 out_pos += CLR_START_len; | 224 out_pos += CLR_STRING_len + match_len + so; |
228 strncpy(&out[out_pos], clr, clr_len); | |
229 out_pos += clr_len; | |
230 strncpy(&out[out_pos], CLR_END, CLR_END_len); | |
231 out_pos += CLR_END_len; | |
232 strncpy(&out[out_pos], &buf[buf_pos], match_len); | |
233 out_pos += match_len; | |
234 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len); | |
235 out_pos += CLR_CLEAR_len; | |
236 | |
237 buf_pos += match_len; | |
238 } | 225 } |
239 else | 226 else |
240 { | 227 { |
241 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); | 228 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); |
242 out[out_len - 1] = '\0'; | 229 out[out_len - 1] = '\0'; |