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

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

Code formatting + cosmetic fixes

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "util-string.h"
6#include <stdarg.h>
7#include "nonstd_stdio.h"
8#include "nonstd.h"
9#include <assert.h>
10#include <cstdlib> //malloc()
11#ifdef USE_VIRTFILE
12#include <common/virtfile/virtfile.h>
13#endif
14
15string ssprintf_va(const char* format, va_list ap)
16{
17        string s; //clang crashed when this declaration was in s=buf
18        int size = 256;
19        char* buf;
20        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)
21        // (does not apply to SString::sprintf, it does not have the va_list variant)
22
23        //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying
24#ifdef USE_VSCPRINTF
25        va_copy(ap_copy, ap);
26        size = _vscprintf(format, ap_copy) + 1; //+1 for terminating null character
27        va_end(ap_copy);
28#endif
29
30        while (1)
31        {
32                buf = (char*)malloc(size);
33                assert(buf != NULL);
34                va_copy(ap_copy, ap);
35                int n = vsnprintf(buf, size, format, ap_copy);
36                va_end(ap_copy);
37                if (n > -1 && n < size)
38                {
39                        s = buf;
40                        free(buf);
41                        return s;
42                }
43#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
44                if (n > -1)    /* glibc 2.1 */
45                        size = n + 1; /* precisely what is needed */
46                else           /* glibc 2.0 */
47#endif
48                        size *= 2;  /* twice the old size */
49                free(buf);
50        }
51}
52
53char* strmove(char *a, char *b) //strcpy that works well for overlapping strings ("Source and destination overlap")
54{
55        if (a == NULL || b == NULL)
56                return NULL;
57        memmove(a, b, strlen(b) + 1);
58        return a;
59}
60
61string ssprintf(const char* format, ...)
62{
63        va_list ap;
64        va_start(ap, format);
65        string ret = ssprintf_va(format, ap); //is it too wasteful? copying the string again... unless the compiler can handle it better
66        va_end(ap);
67        return ret;
68}
69
70string stripExt(const string& filename)
71{
72        size_t dot = filename.rfind('.');
73        if (dot == string::npos) return filename;
74        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
75        if ((sep == string::npos) || (sep < dot))
76                return filename.substr(0, dot);
77        return filename;
78}
79
80string stripFileDir(const string& filename)
81{
82        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
83        if (sep == string::npos) return filename;
84        return filename.substr(sep + 1);
85}
86
87string getFileExt(const string& filename)
88{
89        size_t dot = filename.rfind('.');
90        if (dot == string::npos) return string("");
91        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
92        if ((sep == string::npos) || (sep < dot))
93                return filename.substr(dot);
94        return string("");
95}
96
97string getFileDir(const string& filename)
98{
99        size_t slash = filename.rfind(PATH_SEPARATOR_CHAR);
100        if (slash == string::npos) return string("");
101        return filename.substr(0, slash);
102}
Note: See TracBrowser for help on using the repository browser.