| 12 | #include "string.h" |
| 13 | |
| 14 | char *Ini_GetString(const char *category, const char *key, const char *defaultValue, char *dest, uint16 length, char *source) |
| 15 | { |
| 16 | char *s; |
| 17 | char buffer[1024]; |
| 18 | uint16 catLength; |
| 19 | char *current; |
| 20 | char *ret; |
| 21 | |
| 22 | if (dest != NULL) { |
| 23 | *dest = '\0'; |
| 24 | /* Set the default value in case we jump out early */ |
| 25 | if (defaultValue != NULL) strncpy(dest, defaultValue, length); |
| 26 | dest[length - 1] = '\0'; |
| 27 | } |
| 28 | |
| 29 | if (source == NULL) return NULL; |
| 30 | |
| 31 | sprintf(buffer, "[%s]", category); |
| 32 | for (s = buffer; *s != '\0'; s++) *s = toupper(*s); |
| 33 | catLength = (uint16)strlen(buffer); |
| 34 | |
| 35 | ret = source; |
| 36 | |
| 37 | for (current = source; current != NULL; current++) { |
| 38 | const char *end; |
| 39 | |
| 40 | current = strchr(current, '['); |
| 41 | if (current == NULL) break; |
| 42 | |
| 43 | if (strncasecmp(current, buffer, catLength) != 0) continue; |
| 44 | if (current != source && *(current - 1) != '\r' && *(current - 1) != '\n') continue; |
| 45 | |
| 46 | current += catLength; |
| 47 | while (isspace((uint8)*current)) current++; |
| 48 | |
| 49 | /* Find the end of this block */ |
| 50 | for (end = current; end != NULL; end++) { |
| 51 | end = strchr(end, '['); |
| 52 | if (end == NULL) break; |
| 53 | |
| 54 | if (*(end - 1) == '\r' || *(end - 1) == '\n') break; |
| 55 | } |
| 56 | |
| 57 | /* If there is no other '[', take the last char of the file */ |
| 58 | if (end == NULL) end = current + strlen(current); |
| 59 | |
| 60 | if (key != NULL) { |
| 61 | uint16 keyLength = (uint16)strlen(key); |
| 62 | |
| 63 | ret = current; |
| 64 | |
| 65 | while (current < end) { |
| 66 | char *value; |
| 67 | char *lineEnd; |
| 68 | |
| 69 | /* Check to see if there is nothing behind the key ('a' should not match 'aa') */ |
| 70 | value = current + keyLength; |
| 71 | while (isspace((uint8)*value)) value++; |
no test coverage detected