static sdef(String, Sanitize, String name) { String out = String_Clone(name); String_ReplaceAll(&out, $("?"), $(" ")); String_ReplaceAll(&out, $(":"), $(" ")); String_ReplaceAll(&out, $("/"), $("-")); String_ReplaceAll(&out, $(" "), $(" ")); String_Copy(&out, String_Trim(out)); if (String_EndsWith(out, $("."))) { out.len--; } return out; }
static void ReadProfileIni(const char *filename) { char *source; char *key; char *keys; char buffer[120]; uint16 locsi; if (filename == NULL) return; if (!File_Exists(filename)) return; source = GFX_Screen_Get_ByIndex(SCREEN_1); memset(source, 0, 32000); File_ReadBlockFile(filename, source, GFX_Screen_GetSize_ByIndex(SCREEN_1)); keys = source + strlen(source) + 5000; *keys = '\0'; Ini_GetString("construct", NULL, keys, keys, 2000, source); for (key = keys; *key != '\0'; key += strlen(key) + 1) { ObjectInfo *oi = NULL; uint16 count; uint8 type; uint16 buildCredits; uint16 buildTime; uint16 fogUncoverRadius; uint16 availableCampaign; uint16 sortPriority; uint16 priorityBuild; uint16 priorityTarget; uint16 hitpoints; type = Unit_StringToType(key); if (type != UNIT_INVALID) { oi = &g_table_unitInfo[type].o; } else { type = Structure_StringToType(key); if (type != STRUCTURE_INVALID) oi = &g_table_structureInfo[type].o; } if (oi == NULL) continue; Ini_GetString("construct", key, buffer, buffer, 120, source); count = sscanf(buffer, "%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu", &buildCredits, &buildTime, &hitpoints, &fogUncoverRadius, &availableCampaign, &priorityBuild, &priorityTarget, &sortPriority); oi->buildCredits = buildCredits; oi->buildTime = buildTime; oi->hitpoints = hitpoints; oi->fogUncoverRadius = fogUncoverRadius; oi->availableCampaign = availableCampaign; oi->priorityBuild = priorityBuild; oi->priorityTarget = priorityTarget; if (count <= 7) continue; oi->sortPriority = (uint8)sortPriority; } if (g_debugGame) { for (locsi = 0; locsi < UNIT_MAX; locsi++) { ObjectInfo *oi = &g_table_unitInfo[locsi].o; sprintf(buffer, "%*s%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d", 15 - (int)strlen(oi->name), "", oi->buildCredits, oi->buildTime, oi->hitpoints, oi->fogUncoverRadius, oi->availableCampaign, oi->priorityBuild, oi->priorityTarget, oi->sortPriority); Ini_SetString("construct", oi->name, buffer, source); } for (locsi = 0; locsi < STRUCTURE_MAX; locsi++) { ObjectInfo *oi = &g_table_unitInfo[locsi].o; sprintf(buffer, "%*s%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d", 15 - (int)strlen(oi->name), "", oi->buildCredits, oi->buildTime, oi->hitpoints, oi->fogUncoverRadius, oi->availableCampaign, oi->priorityBuild, oi->priorityTarget, oi->sortPriority); Ini_SetString("construct", oi->name, buffer, source); } } *keys = '\0'; Ini_GetString("combat", NULL, keys, keys, 2000, source); for (key = keys; *key != '\0'; key += strlen(key) + 1) { uint16 damage; uint16 movingSpeedFactor; uint16 fireDelay; uint16 fireDistance; Ini_GetString("combat", key, buffer, buffer, 120, source); String_Trim(buffer); if (sscanf(buffer, "%hu,%hu,%hu,%hu", &fireDistance, &damage, &fireDelay, &movingSpeedFactor) < 4) continue; for (locsi = 0; locsi < UNIT_MAX; locsi++) { UnitInfo *ui = &g_table_unitInfo[locsi]; if (strcasecmp(ui->o.name, key) != 0) continue; ui->damage = damage; ui->movingSpeedFactor = movingSpeedFactor; ui->fireDelay = fireDelay; ui->fireDistance = fireDistance; break; } } if (!g_debugGame) return; for (locsi = 0; locsi < UNIT_MAX; locsi++) { const UnitInfo *ui = &g_table_unitInfo[locsi]; sprintf(buffer, "%*s%4d,%4d,%4d,%4d", 15 - (int)strlen(ui->o.name), "", ui->fireDistance, ui->damage, ui->fireDelay, ui->movingSpeedFactor); Ini_SetString("combat", ui->o.name, buffer, source); } }
char *Ini_GetString(const char *category, const char *key, const char *defaultValue, char *dest, uint16 length, char *source) { char *s; char buffer[1024]; uint16 catLength; char *current; char *ret; if (dest != NULL) { *dest = '\0'; /* Set the default value in case we jump out early */ if (defaultValue != NULL) strncpy(dest, defaultValue, length); dest[length - 1] = '\0'; } if (source == NULL) return NULL; sprintf(buffer, "[%s]", category); for (s = buffer; *s != '\0'; s++) *s = toupper(*s); catLength = strlen(buffer); ret = source; for (current = source; current != NULL; current++) { const char *end; current = strchr(current, '['); if (current == NULL) break; if (strncasecmp(current, buffer, catLength) != 0) continue; if (*(current - 1) != '\r' && *(current - 1) != '\n') continue; current += catLength; while (isspace((uint8)*current)) current++; /* Find the end of this block */ for (end = current; end != NULL; end++) { end = strchr(end, '['); if (end == NULL) break; if (*(end - 1) == '\r' || *(end - 1) == '\n') break; } /* If there is no other '[', take the last char of the file */ if (end == NULL) end = current + strlen(current); if (key != NULL) { uint16 keyLength = strlen(key); ret = current; while (true) { char *value; char *lineEnd; /* Check to see if there is nothing behind the key ('a' should not match 'aa') */ value = current + keyLength; while (isspace((uint8)*value)) value++; /* Now validate the size and if we match at all */ if (*value != '=' || strncasecmp(current, key, keyLength) != 0) { current = strchr(current, '\r'); if (current == NULL) break; while (isspace((uint8)*current)) current++; if (current > end) break; continue; } ret = current; /* Get the value */ current = value + 1; /* Find the end of the line */ lineEnd = strchr(current, '\r'); if (lineEnd == NULL) break; while (isspace((uint8)*lineEnd)) lineEnd++; if (lineEnd > end) break; /* Copy the value */ if (dest != NULL) { uint16 len = lineEnd - current; memcpy(dest, current, len); *(dest + len) = '\0'; String_Trim(dest); } return ret; } /* Failed to find the key. Return anyway. */ if (dest != NULL) *dest = '\0'; return NULL; } ret = current; if (dest == NULL) return ret; /* Read all the keys from this section */ while (true) { uint16 len; char *lineEnd; lineEnd = strchr(current, '='); if (lineEnd == NULL || lineEnd > end) break; len = lineEnd - current; memcpy(dest, current, len); *(dest + len) = '\0'; String_Trim(dest); dest += strlen(dest) + 1; /* Find the next line, ignoring all \r\n */ current = strchr(current, '\r'); if (current == NULL) break; while (isspace((uint8)*current)) current++; if (current > end) break; } *dest++ = '\0'; *dest++ = '\0'; return ret; } return NULL; }