107 } |
107 } |
108 |
108 |
109 // ============================================================================= |
109 // ============================================================================= |
110 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
110 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
111 // ============================================================================= |
111 // ============================================================================= |
112 ulong loadFileContents (FILE* fp, OpenFile* load, std::vector<LDObject*>* copies, ulong* idx) { |
112 std::vector<LDObject*> loadFileContents (FILE* fp, ulong* numWarnings) { |
113 char line[1024]; |
113 char line[1024]; |
114 vector<str> lines; |
114 vector<str> lines; |
115 ulong numWarnings = 0; |
115 vector<LDObject*> objs; |
|
116 ulong lnum = 0; |
|
117 |
|
118 if (numWarnings) |
|
119 *numWarnings = 0; |
116 |
120 |
117 while (fgets (line, sizeof line, fp)) { |
121 while (fgets (line, sizeof line, fp)) { |
118 // Trim the trailing newline |
122 // Trim the trailing newline |
119 str zLine = line; |
123 str data = line; |
120 while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r') |
124 while (data[~data - 1] == '\n' || data[~data - 1] == '\r') |
121 zLine -= 1; |
125 data -= 1; |
122 |
126 |
123 lines.push_back (zLine); |
127 LDObject* obj = parseLine (data); |
124 } |
128 assert (obj != null); |
125 |
|
126 if (idx) |
|
127 *idx = load->objects.size (); |
|
128 |
|
129 ulong lnum = 1; |
|
130 for (str line : lines) { |
|
131 LDObject* obj = parseLine (line); |
|
132 |
|
133 if (copies) |
|
134 copies->push_back (obj->clone ()); |
|
135 |
|
136 load->objects.push_back (obj); |
|
137 |
129 |
138 // Check for parse errors and warn about tthem |
130 // Check for parse errors and warn about tthem |
139 if (obj->getType() == OBJ_Gibberish) { |
131 if (obj->getType() == OBJ_Gibberish) { |
140 logf (LOG_Warning, "Couldn't parse line #%lu: %s\n", |
132 logf (LOG_Warning, "Couldn't parse line #%lu: %s\n", |
141 lnum, static_cast<LDGibberish*> (obj)->zReason.chars()); |
133 lnum, static_cast<LDGibberish*> (obj)->zReason.chars()); |
142 |
134 |
143 logf (LOG_Warning, "- Line was: %s\n", line.chars()); |
135 logf (LOG_Warning, "- Line was: %s\n", data.chars()); |
144 numWarnings++; |
136 |
145 } |
137 if (numWarnings) |
146 |
138 (*numWarnings)++; |
|
139 } |
|
140 |
|
141 objs.push_back (obj); |
147 lnum++; |
142 lnum++; |
148 } |
143 } |
149 |
144 |
150 return numWarnings; |
145 return objs; |
151 } |
146 } |
152 |
147 |
153 // ============================================================================= |
148 // ============================================================================= |
154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
155 // ============================================================================= |
150 // ============================================================================= |
170 return null; |
165 return null; |
171 } |
166 } |
172 |
167 |
173 OpenFile* load = new OpenFile; |
168 OpenFile* load = new OpenFile; |
174 load->zFileName = path; |
169 load->zFileName = path; |
175 ulong numWarnings = loadFileContents (fp, load); |
170 ulong numWarnings; |
|
171 std::vector<LDObject*> objs = loadFileContents (fp, &numWarnings); |
|
172 |
|
173 for (LDObject* obj : objs) |
|
174 load->objects.push_back (obj); |
176 |
175 |
177 fclose (fp); |
176 fclose (fp); |
178 g_LoadedFiles.push_back (load); |
177 g_LoadedFiles.push_back (load); |
179 |
178 |
180 logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n", |
179 logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n", |