* Get the text of a message. * @param str Destination of the text (usually 256 characters long). * @param id Identification of the resource. * @param num Number of the string in the resource. * @bug Make the function safe (should never overwrite data outside \a str, * handle case where last line of file is not terminated with new-line) * @bug Out of range \a num seems not correctly han
| 177 | * @todo Why do we copy the text? Can we not return its address instead? |
| 178 | */ |
| 179 | void Micropolis::getIndString(char *str, int id, short num) |
| 180 | { |
| 181 | StringTable *tp, *st; |
| 182 | |
| 183 | // Try to find requested string table in already loaded files. |
| 184 | tp = stringTables; |
| 185 | st = NULL; |
| 186 | while (tp != NULL) { |
| 187 | if (tp->id == id) { |
| 188 | st = tp; |
| 189 | break; |
| 190 | } |
| 191 | tp = tp->next; |
| 192 | } |
| 193 | |
| 194 | if (st == NULL) { |
| 195 | // String table is not loaded yet -> get it |
| 196 | |
| 197 | // Create new string table |
| 198 | st = (StringTable *)newPtr(sizeof(StringTable)); |
| 199 | assert(st != NULL); |
| 200 | |
| 201 | st->id = id; |
| 202 | Resource *r = getResource("stri", id); |
| 203 | Quad size = r->size; |
| 204 | char *buf = r->buf; |
| 205 | |
| 206 | // Count number of lines in loaded file, terminate each line |
| 207 | Quad lines = 0; |
| 208 | for (Quad i = 0; i < size; i++) { |
| 209 | if (buf[i] == '\n') { |
| 210 | buf[i] = '\0'; |
| 211 | lines++; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // XXX What about termination of last line? |
| 216 | |
| 217 | st->lines = lines; |
| 218 | st->strings = (char **)newPtr(size * sizeof(char *)); |
| 219 | assert(st->strings != NULL); |
| 220 | |
| 221 | // Store starting points of texts in st->strings array |
| 222 | for (Quad i = 0; i < lines; i++) { |
| 223 | st->strings[i] = buf; |
| 224 | buf += strlen(buf) + 1; |
| 225 | } |
| 226 | |
| 227 | st->next = stringTables; |
| 228 | stringTables = st; |
| 229 | } |
| 230 | |
| 231 | // st points to the (possibly just loaded) string table |
| 232 | assert(num >= 1 && num <= st->lines); // Stay in range of the file |
| 233 | |
| 234 | strcpy(str, st->strings[num - 1]); |
| 235 | } |
| 236 |
nothing calls this directly
no outgoing calls
no test coverage detected