Debian Codegraph @

skycpt/TextFile.cpp

raw symbols SCIP
1 #include "stdafx.h"
2 #include "TextFile.h"
3
4 TextFile::TextFile(char *name) {
5 FILE *inf = fopen(name, "r");
6 if (!inf) {
7 printf("Unable to open file %s\n", name);
8 getchar();
9 }
10 _lines = NULL;
11 read(inf);
12 fclose(inf);
13 }
14
15 uint32 crop(char *line) {
16 char *start = line;
17 while (*line)
18 if ((*line == 0xA) || (*line == 0xD))
19 *line = '\0';
20 else if ((line[0] == '/') && (line[1] == '/'))
21 *line = '\0';
22 else
23 line++;
24
25 while (((*(line - 1) == ' ') || ((*(line - 1)) == '\t')) && (line > start)) {
26 line--;
27 *line = '\0';
28 }
29 return (uint32)(line - start);
30 }
31
32 char *TextFile::giveLine(uint32 num) {
33 if (num >= _numLines)
34 return NULL;
35 else
36 return _lines[num];
37 }
38
39 void TextFile::read(FILE *inf) {
40 char *line = (char*)malloc(4096);
41 _lines = (char**)malloc(4096 * sizeof(char *));
42 _numLines = 0;
43 uint32 linesMax = 4096;
44 while (fgets(line, 4096, inf)) {
45 if (_numLines >= linesMax) {
46 _lines = (char**)realloc(_lines, linesMax * 2 * sizeof(char *));
47 linesMax *= 2;
48 }
49 char *start = line;
50 while ((*start == '\t') || (*start == ' '))
51 start++;
52 if (memcmp(start, "(int16)", 7) == 0) {
53 start += 7;
54 while ((*start == '\t') || (*start == ' '))
55 start++;
56 }
57 uint32 length = crop(start);
58 _lines[_numLines] = (char*)malloc(length + 1);
59 memcpy(_lines[_numLines], start, length + 1);
60 _numLines++;
61 }
62 free(line);
63 }
64
65 int32 TextFile::findLine(KmpSearch *kmp, uint32 fromLine) {
66 for (uint32 cnt = fromLine; cnt < _numLines; cnt++) {
67 if (kmp->search(_lines[cnt]))
68 return cnt;
69 }
70 return -1;
71 }