X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Flibgame%2Fmisc.c;h=7450c3a35fd4942d4e0a19a039738b9c997b2650;hb=9bda353724b6dc9176d0dfaf28ef9f1ff4084d4b;hp=02358aa292a3803890ca42b4526f679af0884cce;hpb=7078f3af87a1898999f8e2bbfef6c3895d5f7316;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 02358aa2..7450c3a3 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -620,12 +620,12 @@ static unsigned int mainCounter(int mode) return current_ms - base_ms; } -void InitCounter() // set counter back to zero +void InitCounter(void) // set counter back to zero { mainCounter(INIT_COUNTER); } -unsigned int Counter() // get milliseconds since last call of InitCounter() +unsigned int Counter(void) // get milliseconds since last call of InitCounter() { return mainCounter(READ_COUNTER); } @@ -1084,11 +1084,31 @@ char *getBasePath(char *filename) } +// ---------------------------------------------------------------------------- +// various memory functions +// ---------------------------------------------------------------------------- + +void *getMemCopy(const void *m, size_t size) +{ + void *m_copy; + + if (m == NULL) + return NULL; + + m_copy = checked_malloc(size); + memcpy(m_copy, m, size); + + return m_copy; +} + + // ---------------------------------------------------------------------------- // various string functions // ---------------------------------------------------------------------------- -char *getStringCat2WithSeparator(char *s1, char *s2, char *sep) +char *getStringCat2WithSeparator(const char *s1, + const char *s2, + const char *sep) { if (s1 == NULL || s2 == NULL || sep == NULL) return NULL; @@ -1101,7 +1121,10 @@ char *getStringCat2WithSeparator(char *s1, char *s2, char *sep) return complete_string; } -char *getStringCat3WithSeparator(char *s1, char *s2, char *s3, char *sep) +char *getStringCat3WithSeparator(const char *s1, + const char *s2, + const char *s3, + const char *sep) { if (s1 == NULL || s2 == NULL || s3 == NULL || sep == NULL) return NULL; @@ -1115,17 +1138,17 @@ char *getStringCat3WithSeparator(char *s1, char *s2, char *s3, char *sep) return complete_string; } -char *getStringCat2(char *s1, char *s2) +char *getStringCat2(const char *s1, const char *s2) { return getStringCat2WithSeparator(s1, s2, ""); } -char *getStringCat3(char *s1, char *s2, char *s3) +char *getStringCat3(const char *s1, const char *s2, const char *s3) { return getStringCat3WithSeparator(s1, s2, s3, ""); } -char *getPath2(char *path1, char *path2) +char *getPath2(const char *path1, const char *path2) { #if defined(PLATFORM_ANDROID) // workaround for reading from assets directory -- skip "." subdirs in path @@ -1138,7 +1161,7 @@ char *getPath2(char *path1, char *path2) return getStringCat2WithSeparator(path1, path2, STRING_PATH_SEPARATOR); } -char *getPath3(char *path1, char *path2, char *path3) +char *getPath3(const char *path1, const char *path2, const char *path3) { #if defined(PLATFORM_ANDROID) // workaround for reading from assets directory -- skip "." subdirs in path @@ -1166,12 +1189,12 @@ static char *getPngOrPcxIfNotExists(char *filename) return filename; } -char *getImg2(char *path1, char *path2) +char *getImg2(const char *path1, const char *path2) { return getPngOrPcxIfNotExists(getPath2(path1, path2)); } -char *getImg3(char *path1, char *path2, char *path3) +char *getImg3(const char *path1, const char *path2, const char *path3) { return getPngOrPcxIfNotExists(getPath3(path1, path2, path3)); } @@ -1215,6 +1238,18 @@ char *getStringCopyNStatic(const char *s, int n) return s_copy; } +char *getStringToUpper(const char *s) +{ + char *s_copy = checked_malloc(strlen(s) + 1); + char *s_ptr = s_copy; + + while (*s) + *s_ptr++ = toupper(*s++); + *s_ptr = '\0'; + + return s_copy; +} + char *getStringToLower(const char *s) { char *s_copy = checked_malloc(strlen(s) + 1); @@ -1227,14 +1262,224 @@ char *getStringToLower(const char *s) return s_copy; } -void setString(char **old_value, char *new_value) +void setString(char **old_value, const char *new_value) { checked_free(*old_value); *old_value = getStringCopy(new_value); } -boolean strEqual(char *s1, char *s2) +char **getSplitStringArray(const char *s, const char *separators, int max_tokens) +{ + const char *s_ptr, *s_last = s; + byte separator_table[256] = { FALSE }; + int num_tokens; + char **tokens = NULL; + + if (s == NULL) + return NULL; + + if (separators == NULL) + return NULL; + + if (max_tokens < 1) + max_tokens = INT_MAX; + + // if string is empty, return empty array + if (*s == '\0') + { + tokens = checked_malloc(sizeof(char *)); + tokens[0] = NULL; + + return tokens; + } + + // initialize separator table for all characters in separators string + for (s_ptr = separators; *s_ptr != '\0'; s_ptr++) + separator_table[*(byte *)s_ptr] = TRUE; + + // count number of tokens in string + for (num_tokens = 1, s_ptr = s; *s_ptr != '\0'; s_ptr++) + if (separator_table[*(byte *)s_ptr] && num_tokens < max_tokens) + num_tokens++; + + // allocate array for determined number of tokens + tokens = checked_malloc((num_tokens + 1) * sizeof(char *)); + + // copy all but last separated sub-strings to array + for (num_tokens = 0, s_ptr = s; *s_ptr != '\0'; s_ptr++) + { + if (separator_table[*(byte *)s_ptr] && num_tokens + 1 < max_tokens) + { + tokens[num_tokens++] = getStringCopyN(s_last, s_ptr - s_last); + s_last = s_ptr + 1; + } + } + + // copy last separated sub-string to array + tokens[num_tokens++] = getStringCopyN(s_last, s_ptr - s_last); + + // terminate array + tokens[num_tokens] = NULL; + + return tokens; +} + +int getStringArrayLength(char **s_array) +{ + int num_strings = 0; + + if (s_array == NULL) + return 0; + + while (s_array[num_strings] != NULL) + num_strings++; + + return num_strings; +} + +void freeStringArray(char **s_array) +{ + int i; + + if (s_array == NULL) + return; + + for (i = 0; s_array[i] != NULL; i++) + checked_free(s_array[i]); + + checked_free(s_array); +} + +char *getUnescapedString(const char *s) +{ + const char *s_ptr = s; + const char *octal_ptr; + char *s_unescaped; + char *s_unescaped_ptr; + + if (s == NULL) + return NULL; + + s_unescaped = checked_malloc(strlen(s) + 1); + s_unescaped_ptr = s_unescaped; + + while (*s_ptr != '\0') + { + if (*s_ptr == '\\') + { + s_ptr++; + + switch (*s_ptr) + { + case '\0': + Warn("getUnescapedString: trailing \\"); + goto out; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + *s_unescaped_ptr = 0; + octal_ptr = s_ptr; + + while (s_ptr < octal_ptr + 3 && *s_ptr >= '0' && *s_ptr <= '7') + { + *s_unescaped_ptr = (*s_unescaped_ptr * 8) + (*s_ptr - '0'); + s_ptr++; + } + + s_unescaped_ptr++; + s_ptr--; + break; + + case 'b': + *s_unescaped_ptr++ = '\b'; + break; + + case 'f': + *s_unescaped_ptr++ = '\f'; + break; + + case 'n': + *s_unescaped_ptr++ = '\n'; + break; + + case 'r': + *s_unescaped_ptr++ = '\r'; + break; + + case 't': + *s_unescaped_ptr++ = '\t'; + break; + + case 'v': + *s_unescaped_ptr++ = '\v'; + break; + + default: + /* also handles \" and \\ */ + *s_unescaped_ptr++ = *s_ptr; + break; + } + } + else + { + *s_unescaped_ptr++ = *s_ptr; + } + + s_ptr++; + } + + out: + *s_unescaped_ptr = '\0'; + + return s_unescaped; +} + +char *chugString(char *s) +{ + if (s == NULL) + return NULL; + + char *start; + + for (start = (char *)s; *start && isspace(*start); start++) + ; + + memmove(s, start, strlen(start) + 1); + + return s; +} + +char *chompString(char *s) +{ + if (s == NULL) + return NULL; + + int len = strlen(s); + + while (len--) + { + if (isspace(s[len])) + s[len] = '\0'; + else + break; + } + + return s; +} + +char *stripString(char *s) +{ + return chugString(chompString(s)); +} + +boolean strEqual(const char *s1, const char *s2) { return (s1 == NULL && s2 == NULL ? TRUE : s1 == NULL && s2 != NULL ? FALSE : @@ -1242,7 +1487,7 @@ boolean strEqual(char *s1, char *s2) strcmp(s1, s2) == 0); } -boolean strEqualN(char *s1, char *s2, int n) +boolean strEqualN(const char *s1, const char *s2, int n) { return (s1 == NULL && s2 == NULL ? TRUE : s1 == NULL && s2 != NULL ? FALSE : @@ -1250,7 +1495,7 @@ boolean strEqualN(char *s1, char *s2, int n) strncmp(s1, s2, n) == 0); } -boolean strEqualCase(char *s1, char *s2) +boolean strEqualCase(const char *s1, const char *s2) { return (s1 == NULL && s2 == NULL ? TRUE : s1 == NULL && s2 != NULL ? FALSE : @@ -1258,7 +1503,7 @@ boolean strEqualCase(char *s1, char *s2) strcasecmp(s1, s2) == 0); } -boolean strEqualCaseN(char *s1, char *s2, int n) +boolean strEqualCaseN(const char *s1, const char *s2, int n) { return (s1 == NULL && s2 == NULL ? TRUE : s1 == NULL && s2 != NULL ? FALSE : @@ -1266,7 +1511,7 @@ boolean strEqualCaseN(char *s1, char *s2, int n) strncasecmp(s1, s2, n) == 0); } -boolean strPrefix(char *s, char *prefix) +boolean strPrefix(const char *s, const char *prefix) { return (s == NULL && prefix == NULL ? TRUE : s == NULL && prefix != NULL ? FALSE : @@ -1274,7 +1519,7 @@ boolean strPrefix(char *s, char *prefix) strncmp(s, prefix, strlen(prefix)) == 0); } -boolean strSuffix(char *s, char *suffix) +boolean strSuffix(const char *s, const char *suffix) { return (s == NULL && suffix == NULL ? TRUE : s == NULL && suffix != NULL ? FALSE : @@ -1283,7 +1528,7 @@ boolean strSuffix(char *s, char *suffix) strcmp(&s[strlen(s) - strlen(suffix)], suffix) == 0); } -boolean strPrefixLower(char *s, char *prefix) +boolean strPrefixLower(const char *s, const char *prefix) { char *s_lower = getStringToLower(s); boolean match = strPrefix(s_lower, prefix); @@ -1293,7 +1538,7 @@ boolean strPrefixLower(char *s, char *prefix) return match; } -boolean strSuffixLower(char *s, char *suffix) +boolean strSuffixLower(const char *s, const char *suffix) { char *s_lower = getStringToLower(s); boolean match = strSuffix(s_lower, suffix); @@ -1303,7 +1548,7 @@ boolean strSuffixLower(char *s, char *suffix) return match; } -boolean isURL(char *s) +boolean isURL(const char *s) { while (*s && *s >= 'a' && *s <= 'z') s++; @@ -3072,7 +3317,7 @@ void freeDirectoryEntry(DirectoryEntry *dir_entry) // functions for checking files and filenames // ---------------------------------------------------------------------------- -boolean directoryExists(char *dir_name) +boolean directoryExists(const char *dir_name) { if (dir_name == NULL) return FALSE; @@ -3100,7 +3345,7 @@ boolean directoryExists(char *dir_name) return success; } -boolean fileExists(char *filename) +boolean fileExists(const char *filename) { if (filename == NULL) return FALSE; @@ -3124,7 +3369,7 @@ boolean fileExists(char *filename) } #if 0 -static boolean fileHasPrefix(char *basename, char *prefix) +static boolean fileHasPrefix(const char *basename, const char *prefix) { static char *basename_lower = NULL; int basename_length, prefix_length; @@ -3147,7 +3392,7 @@ static boolean fileHasPrefix(char *basename, char *prefix) } #endif -static boolean fileHasSuffix(char *basename, char *suffix) +static boolean fileHasSuffix(const char *basename, const char *suffix) { static char *basename_lower = NULL; int basename_length, suffix_length;