static int readFullRecord(char *errstr, char **ssp, char *buf, size_t sz, int *line_idx) { while(1) { if (sgets(buf, (int)sz, ssp) == NULL) { return EOF_CODE; } if (needSkip(buf)) { (*line_idx)++; continue; } size_t remain; char *line_end = findLineEnd(buf); char *ptr = line_end; char *open, *close; open = findChar(buf, '('); if (open != NULL) { *open = ' '; close = findChar(buf, ')'); if (close != NULL) { *close = ' '; } else { while (close == NULL) { *ptr++ = ' '; remain = sz - (ptr - buf); if (remain <= 0) { snprintf(errstr, ERR_STR_LEN, "Syntax error(line %d): the record is too long(more than %d)", *line_idx, RECORD_SIZE); return ERR_CODE; } if (sgets(ptr, remain, ssp) == NULL) { snprintf(errstr, ERR_STR_LEN, "syntax error(line: %d): no close parenthesis.", *line_idx); return ERR_CODE; } (*line_idx)++; if (needSkip(ptr)) continue; line_end = findLineEnd(ptr); close = findChar(ptr, ')'); ptr = line_end; } *close = ' '; } } break; } (*line_idx)++; // replace all invisible character to space. replaceInvisibleChar(buf); // LOG_DEBUG("read full record: %s", buf); return OK_CODE; }
static void processFile(Regex* re, SearchOutput* output, OrderedOutput::Chunk* chunk, HighlightBuffer& hlbuf, const char* path, size_t pathLength, const char* data, size_t size, unsigned int startLine) { const char* range = re->rangePrepare(data, size); const char* begin = range; const char* end = begin + size; unsigned int line = startLine; while (RegexMatch match = re->rangeSearch(begin, end - begin)) { // discard zero-length matches at the end (.* results in an extra line for every file part otherwise) if (match.data == end) break; // update line counter line += 1 + countLines(begin, match.data); // print match const char* lbeg = findLineStart(begin, match.data); const char* lend = findLineEnd(match.data + match.size, end); processMatch(re, output, chunk, hlbuf, path, pathLength, (lbeg - range) + data, lend - lbeg, line, lbeg, match.data - lbeg, match.size); // move to next line if (lend == end) break; begin = lend + 1; } re->rangeFinalize(range); }
template <typename Pred> static void filterRegex(const FilterEntries& entries, Regex* re, unsigned int limit, Pred pred) { const char* range = re->rangePrepare(entries.buffer, entries.bufferSize); const char* begin = range; const char* end = begin + entries.bufferSize; unsigned int matches = 0; while (RegexMatch match = re->rangeSearch(begin, end - begin)) { size_t matchOffset = match.data - range; // find first file entry with offset > matchOffset const FilterEntry* entry = std::upper_bound(entries.entries, entries.entries + entries.entryCount, matchOffset, [](size_t l, const FilterEntry& r) { return l < r.offset; }); // find last file entry with offset <= matchOffset assert(entry > entries.entries); entry--; // print match pred(entry - entries.entries); // move to next line const char* lend = findLineEnd(match.data + match.size, end); if (lend == end) break; begin = lend + 1; matches++; if (matches >= limit) break; } re->rangeFinalize(range); }
Tdim dim(char *txt, struct Tgraph * graph) { /* a linewidth mechanism were cool, i.e. automatic braking of the line */ /* baceline should jump current y down, x should be the maximum x of all lines */ /* a flag for linebreak should be placed, containing the y jump size */ /* so that the draw routines know when to add to y and reset x zo 0 */ int i; int len = strlen(txt); /* length of text passed * to parse */ Tdim our; /* the dimensions of our current object */ char *gpos; /* points to the tree node's text */ char *end; PRSDEF K; /* keynumber, result from the * keywordlookup */ our.x = 0; our.y = 1; our.baseline = 0; graph->children = 0; /* at the beginning the tree doesn't have * children. We must first find them */ graph->txt = (char *) malloc(len + 1); /* allocating the same * length is OK. Special * characters in output * are 2 chars * long--shorter than in * the input */ gpos = graph->txt; /* we setup now this pointer */ *gpos = 0; if (*(end=findLineEnd(txt))!='\0') { /* the current level contains one or more line ends */ /* the current level will become aan array of lines */ int nlines=0; char * start=txt; char **lines = (char **) malloc(sizeof(char *)); Tdim out; if (SYNTAX_ERR_FLAG==S_ERR) return out; *gpos = 1; /* See parsedef.h for the keyword * definitions */ gpos++; *gpos = (char) ARRAY; gpos++; *gpos = 0; newChild(graph); graph->down[graph->children - 1]->options = malloc((2)*sizeof(char)); graph->down[graph->children - 1]->options[0] = 'c'; /* default col alignment */ graph->down[graph->children - 1]->options[1] = '\0'; /* default col alignment */ /* count how many lines we have */ while (1) { lines =(char **) realloc(lines,(nlines + 1) * (sizeof(char *))); lines[nlines] = (char *) malloc(end - start + 1); strncpy(lines[nlines], start, end - start); lines[nlines][end - start] = '\0'; /* terminate the string */ nlines++; if (*end=='\0') break; start=end+1; end=findLineEnd(start); } /* fill the array with the lines */ #define Array (graph->down[graph->children-1]) Array->array = malloc(sizeof(Tarray)); Array->array->rows = nlines; Array->array->cols = 1; Array->array->rowy = (int *) calloc(nlines, sizeof(int)); Array->array->colx = (int *) calloc(1, sizeof(int)); for (i = 0; i < nlines; i++) { out = dim(lines[i], newChild(Array)); if (out.x > Array->array->colx[0]) Array->array->colx[0] = out.x; if (out.y > Array->array->rowy[i]) Array->array->rowy[i] = out.y; free(lines[i]); } free(lines); Array->dim.x = 0; Array->dim.x += Array->array->colx[0]; Array->dim.y = 0; for (i = 0; i < nlines; i++) Array->dim.y += Array->array->rowy[i]; Array->dim.y += Array->array->rows - 1; Array->dim.x += Array->array->cols - 1; Array->dim.baseline = Array->dim.y / 2; our.x += Array->dim.x; if (our.baseline < Array->dim.baseline) { our.y += Array->dim.baseline - our.baseline; our.baseline = Array->dim.baseline; } if (our.y < Array->dim.y) our.y = Array->dim.y; #undef Array graph->dim = our; return our; } for (i = 0; i < len; i++) { if(SYNTAX_ERR_FLAG==S_ERR) return our; if ((txt[i] != '\\') && (txt[i] != '_') && (txt[i] != '^')) { our.x++; *gpos = txt[i]; gpos++; *gpos = 0; } else { K = LookupKey(txt + i, Keys); switch (K) { case SUPER: i += dimSuperscript(txt + i, &gpos, &our, graph); break; case SUB: i += dimSubscript(txt + i, &gpos, &our, graph); break; case FRAC: i += dimFrac(txt + i, &gpos, &our, graph); break; case SQRT: i += dimSqrt(txt + i, &gpos, &our, graph); break; case OVERLINE: i += dimOverl(txt + i, &gpos, &our, graph); break; case UNDERLINE: i += dimUnderl(txt + i, &gpos, &our, graph); break; case LIMIT: i += dimLimit(txt + i, &gpos, &our, graph); break; case BRACES: i += dimBrace(txt + i, &gpos, &our, graph); break; case ARRAY: i += dimArray(txt + i, &gpos, &our, graph); break; case TO: i += dimTo(txt + i, &gpos, &our, graph); break; case LEADSTO: i += dimLeadsto(txt + i, &gpos, &our, graph); break; case SUM: i += dimSum(txt + i, &gpos, &our, graph); break; case PROD: i += dimProd(txt + i, &gpos, &our, graph); break; case INT: i += dimInt(txt + i, &gpos, &our, graph); break; case OINT: i += dimOint(txt + i, &gpos, &our, graph); break; case INFTY: strcat(gpos, "oo"); gpos += 2; our.x += 2; i += 5; break; case RCEIL: i += dimRceil(txt + i, &gpos, &our, graph); break; case LCEIL: i += dimLceil(txt + i, &gpos, &our, graph); break; case RFLOOR: i += dimRfloor(txt + i, &gpos, &our, graph); break; case LFLOOR: i += dimLfloor(txt + i, &gpos, &our, graph); break; case ESCAPE: i++; our.x++; *gpos = txt[i]; gpos++; *gpos = 0; break; case ERR: default: fprintf(stderr, "I screwed up in dim, this should never happen!\n"); exit(1); break; } } } graph->dim = our; return our; }