1 |
gezelter |
2 |
#include <stdio.h> |
2 |
|
|
#include <math.h> |
3 |
|
|
#include <stdlib.h> |
4 |
|
|
#include <ctype.h> |
5 |
|
|
#include <string.h> |
6 |
|
|
#include "simError.h" |
7 |
|
|
#include "StringUtils.h" |
8 |
|
|
|
9 |
|
|
int fastForwardToBeginBlock(FILE* theFile, char* blockName ){ |
10 |
|
|
int foundText = 0; |
11 |
|
|
int lineNum; |
12 |
|
|
char* the_token; |
13 |
|
|
char* junk_token; |
14 |
|
|
char* eof_test; |
15 |
|
|
char readLine[500]; |
16 |
|
|
|
17 |
|
|
rewind( theFile ); |
18 |
|
|
lineNum = 0; |
19 |
|
|
|
20 |
|
|
eof_test = fgets( readLine, sizeof(readLine), theFile ); |
21 |
|
|
lineNum++; |
22 |
|
|
if( eof_test == NULL ){ |
23 |
|
|
sprintf( painCave.errMsg, |
24 |
|
|
"fastForwardToBeginBlock: File Is Empty!\n"); |
25 |
|
|
painCave.isFatal = 1; |
26 |
|
|
simError(); |
27 |
|
|
return -1; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
while( !foundText ){ |
31 |
|
|
|
32 |
|
|
if( eof_test == NULL ){ |
33 |
|
|
sprintf( painCave.errMsg, |
34 |
|
|
"fastForwardToBeginBlock: File Ended Unexpectedly!\n" |
35 |
|
|
"\tAt Line: %d\n", lineNum); |
36 |
|
|
painCave.isFatal = 1; |
37 |
|
|
simError(); |
38 |
|
|
return -1; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
if (isBeginLine(readLine)) { |
42 |
|
|
junk_token = strtok( readLine, " ,;\t" ); |
43 |
|
|
the_token = TrimSpaces(strtok( NULL, " ,;\t" )); |
44 |
|
|
foundText = !strcasecmp( blockName, the_token ); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
if( !foundText ){ |
48 |
|
|
eof_test = fgets( readLine, sizeof(readLine), theFile ); |
49 |
|
|
lineNum++; |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
if (eof_test == NULL) { |
53 |
|
|
sprintf( painCave.errMsg, |
54 |
|
|
"fastForwardToBeginBlock: File Ended Unexpectedly!\n" |
55 |
|
|
"\tAt Line: %d\n", lineNum); |
56 |
|
|
painCave.isFatal = 1; |
57 |
|
|
simError(); |
58 |
|
|
return -1; |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
return lineNum; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
int isBeginLine(char *line) { |
67 |
|
|
/* checks to see if the first token on the line is "begin" */ |
68 |
|
|
char *working_line; |
69 |
|
|
char *foo; |
70 |
|
|
|
71 |
|
|
working_line = strdup(line); |
72 |
|
|
|
73 |
|
|
foo = strtok(working_line, " ,;\t"); |
74 |
|
|
|
75 |
|
|
if (!strcasecmp(foo, "begin")) return 1; |
76 |
|
|
|
77 |
|
|
return 0; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
int isEndLine(char *line) { |
81 |
|
|
/* checks to see if the first token on the line is "end" */ |
82 |
|
|
char *working_line; |
83 |
|
|
char *foo; |
84 |
|
|
|
85 |
|
|
working_line = strdup(line); |
86 |
|
|
|
87 |
|
|
foo = strtok(working_line, " ,;\t"); |
88 |
|
|
|
89 |
|
|
if (!strcasecmp(foo, "end")) return 1; |
90 |
|
|
|
91 |
|
|
return 0; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Removes left and right spaces from a string |
96 |
|
|
* |
97 |
|
|
* @param str String to trim |
98 |
|
|
* |
99 |
|
|
* @return char* to the trimed string |
100 |
|
|
*/ |
101 |
|
|
char * TrimSpaces(char *str) { |
102 |
|
|
size_t len; |
103 |
|
|
char *right, *left; |
104 |
|
|
|
105 |
|
|
if (strlen(str) == 0) return(str); |
106 |
|
|
|
107 |
|
|
/* Trim whitespace from left side */ |
108 |
|
|
for (left = str; isspace(*left); left++); |
109 |
|
|
|
110 |
|
|
/* Trim whitespace from right side */ |
111 |
|
|
if ((len = strlen(left))) |
112 |
|
|
{ |
113 |
|
|
right = left + (len - 1); |
114 |
|
|
|
115 |
|
|
while (isspace(*right)) |
116 |
|
|
{ |
117 |
|
|
*right = '\0'; |
118 |
|
|
right--; |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/* Only do the str copy if their was spaces to the left */ |
123 |
|
|
if (left != str) |
124 |
|
|
strcpy(str, left); |
125 |
|
|
|
126 |
|
|
return (str); |
127 |
|
|
} |