file.cpp

changeset 140
2e8c1626aef7
parent 130
ec4b30b166fe
child 141
184d117e1b12
equal deleted inserted replaced
139:5e31a96adaa2 140:2e8c1626aef7
107 } 107 }
108 108
109 // ============================================================================= 109 // =============================================================================
110 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 110 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
111 // ============================================================================= 111 // =============================================================================
112 ulong loadFileContents (FILE* fp, OpenFile* load, std::vector<LDObject*>* copies, ulong* idx) {
113 char line[1024];
114 vector<str> lines;
115 ulong numWarnings = 0;
116
117 while (fgets (line, sizeof line, fp)) {
118 // Trim the trailing newline
119 str zLine = line;
120 while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r')
121 zLine -= 1;
122
123 lines.push_back (zLine);
124 }
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
138 // Check for parse errors and warn about tthem
139 if (obj->getType() == OBJ_Gibberish) {
140 logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
141 lnum, static_cast<LDGibberish*> (obj)->zReason.chars());
142
143 logf (LOG_Warning, "- Line was: %s\n", line.chars());
144 numWarnings++;
145 }
146
147 lnum++;
148 }
149
150 return numWarnings;
151 }
152
153 // =============================================================================
154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
155 // =============================================================================
112 OpenFile* openDATFile (str path, bool search) { 156 OpenFile* openDATFile (str path, bool search) {
113 logf ("Opening %s...\n", path.chars()); 157 logf ("Opening %s...\n", path.chars());
114 158
115 // Convert the file name to lowercase since some parts contain uppercase 159 // Convert the file name to lowercase since some parts contain uppercase
116 // file names. I'll assume here that the library will always use lowercase 160 // file names. I'll assume here that the library will always use lowercase
125 logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno)); 169 logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno));
126 return null; 170 return null;
127 } 171 }
128 172
129 OpenFile* load = new OpenFile; 173 OpenFile* load = new OpenFile;
130 ulong numWarnings = 0;
131
132 load->zFileName = path; 174 load->zFileName = path;
133 175 ulong numWarnings = loadFileContents (fp, load);
134 vector<str> lines;
135
136 {
137 char line[1024];
138 while (fgets (line, sizeof line, fp)) {
139 // Trim the trailing newline
140 str zLine = line;
141 while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r')
142 zLine -= 1;
143
144 lines.push_back (zLine);
145 }
146 }
147 176
148 fclose (fp); 177 fclose (fp);
149
150 for (str line : lines) {
151 LDObject* obj = parseLine (line);
152 load->objects.push_back (obj);
153
154 // Check for parse errors and warn about tthem
155 if (obj->getType() == OBJ_Gibberish) {
156 logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
157 (&line - &(lines[0])),
158 static_cast<LDGibberish*> (obj)->zReason.chars());
159
160 logf (LOG_Warning, "- Line was: %s\n", line.chars());
161 numWarnings++;
162 }
163 }
164
165 g_LoadedFiles.push_back (load); 178 g_LoadedFiles.push_back (load);
166 179
167 logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n", 180 logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n",
168 path.chars(), numWarnings, PLURAL (numWarnings)); 181 path.chars(), numWarnings, PLURAL (numWarnings));
169 182

mercurial