source: cpp/common/util-string.cpp @ 1000

Last change on this file since 1000 was 913, checked in by Maciej Komosinski, 5 years ago

Added a few utility/helper functions

  • Property svn:eol-style set to native
File size: 4.7 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[860]2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[196]4
[860]5#include "util-string.h"
[180]6#include <stdarg.h>
7#include "nonstd_stdio.h"
8#include "nonstd.h"
[220]9#include <assert.h>
[862]10#include <cstdlib> //malloc()
[247]11#ifdef USE_VIRTFILE
[382]12#include <common/virtfile/virtfile.h>
[247]13#endif
[892]14#ifdef __ANDROID__
15#include <android/log.h> //only needed to print error messages related to a workaround for Android bug
16#endif
[180]17
18string ssprintf_va(const char* format, va_list ap)
19{
[220]20        string s; //clang crashed when this declaration was in s=buf
[319]21        int size = 256;
[180]22        char* buf;
[247]23        va_list ap_copy; // "va_list ap" can only by used once by printf-type functions as they advance the current argument pointer (crashed on linux x86_64)
24        // (does not apply to SString::sprintf, it does not have the va_list variant)
[220]25
26        //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying
27#ifdef USE_VSCPRINTF
[257]28        va_copy(ap_copy, ap);
[247]29        size = _vscprintf(format, ap_copy) + 1; //+1 for terminating null character
30        va_end(ap_copy);
[220]31#endif
32
[246]33        while (1)
34        {
35                buf = (char*)malloc(size);
36                assert(buf != NULL);
[257]37                va_copy(ap_copy, ap);
[247]38                int n = vsnprintf(buf, size, format, ap_copy);
39                va_end(ap_copy);
[892]40
41#ifdef __ANDROID__
42                //Workaround for Android bug. /system/lib64/libc.so? maybe only arm 64-bit? "If an encoding error occurs, a negative number is returned". On some devices keeps returning -1 forever.
43                //https://github.com/android-ndk/ndk/issues/879 but unfortunately during google play tests (Firebase Test Lab) this problem turned out to be not limited to Chinese devices and occurred in Mate 9, Galaxy S9, Pixel, Pixel 2, Moto Z (even with the en_GB locale; the locale is not important but the problem seem to be utf8 non-ascii chars in the format string).
44                if (n < 0 && size >= (1 << 24)) //wants more than 16M
45                {
46                        buf[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
47                        __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "Giving up due to Android bug: vsnprintf() wants more than %d bytes, it used %zu bytes, for format='%s'", size, strlen(buf), format);
48                        //in my tests, it always used 0 bytes, so it produced a 0-length string: ""
49                        va_copy(ap_copy, ap);
50                        n = vsprintf(buf, format, ap_copy); //hoping 16M is enough
51                        va_end(ap_copy);
52                        __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", buf);
53                        if (n < 0) //vsprintf was also buggy. If we were strict, we should abort the app now.
54                        {
[897]55                                strcpy(buf, "[STR_ERR] "); //a special prefix just to indicate the returned string is incorrect
56                                strcat(buf, format); //append and return the original formatting string
57                                __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, using the incorrect resulting string: '%s'", buf);
[892]58                        }
[897]59                        n = strlen(buf); //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
[892]60                }
61#endif
62
[246]63                if (n > -1 && n < size)
[220]64                {
[246]65                        s = buf;
[220]66                        free(buf);
67                        return s;
[246]68                }
[220]69#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
70                if (n > -1)    /* glibc 2.1 */
[867]71                        size = n + 1; /* precisely what is needed */
[220]72                else           /* glibc 2.0 */
73#endif
74                        size *= 2;  /* twice the old size */
75                free(buf);
[246]76        }
[180]77}
78
[913]79bool str_starts_with(const char *str, const char *prefix)
80{
81        return strncmp(str,prefix,strlen(prefix))==0;
82}
83
[257]84char* strmove(char *a, char *b) //strcpy that works well for overlapping strings ("Source and destination overlap")
85{
86        if (a == NULL || b == NULL)
87                return NULL;
88        memmove(a, b, strlen(b) + 1);
89        return a;
90}
91
[180]92string ssprintf(const char* format, ...)
93{
94        va_list ap;
95        va_start(ap, format);
[246]96        string ret = ssprintf_va(format, ap); //is it too wasteful? copying the string again... unless the compiler can handle it better
[180]97        va_end(ap);
98        return ret;
99}
100
[246]101string stripExt(const string& filename)
[180]102{
[319]103        size_t dot = filename.rfind('.');
[246]104        if (dot == string::npos) return filename;
[319]105        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
[246]106        if ((sep == string::npos) || (sep < dot))
107                return filename.substr(0, dot);
[180]108        return filename;
109}
110
[460]111string stripFileDir(const string& filename)
112{
113        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
114        if (sep == string::npos) return filename;
[867]115        return filename.substr(sep + 1);
[460]116}
117
[246]118string getFileExt(const string& filename)
[180]119{
[319]120        size_t dot = filename.rfind('.');
[246]121        if (dot == string::npos) return string("");
[319]122        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
[246]123        if ((sep == string::npos) || (sep < dot))
[180]124                return filename.substr(dot);
125        return string("");
126}
[246]127
128string getFileDir(const string& filename)
129{
[319]130        size_t slash = filename.rfind(PATH_SEPARATOR_CHAR);
[246]131        if (slash == string::npos) return string("");
132        return filename.substr(0, slash);
133}
Note: See TracBrowser for help on using the repository browser.