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

Last change on this file since 875 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
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
[180]14
15string ssprintf_va(const char* format, va_list ap)
16{
[220]17        string s; //clang crashed when this declaration was in s=buf
[319]18        int size = 256;
[180]19        char* buf;
[247]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)
[220]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
[257]25        va_copy(ap_copy, ap);
[247]26        size = _vscprintf(format, ap_copy) + 1; //+1 for terminating null character
27        va_end(ap_copy);
[220]28#endif
29
[246]30        while (1)
31        {
32                buf = (char*)malloc(size);
33                assert(buf != NULL);
[257]34                va_copy(ap_copy, ap);
[247]35                int n = vsnprintf(buf, size, format, ap_copy);
36                va_end(ap_copy);
[246]37                if (n > -1 && n < size)
[220]38                {
[246]39                        s = buf;
[220]40                        free(buf);
41                        return s;
[246]42                }
[220]43#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
44                if (n > -1)    /* glibc 2.1 */
[867]45                        size = n + 1; /* precisely what is needed */
[220]46                else           /* glibc 2.0 */
47#endif
48                        size *= 2;  /* twice the old size */
49                free(buf);
[246]50        }
[180]51}
52
[257]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
[180]61string ssprintf(const char* format, ...)
62{
63        va_list ap;
64        va_start(ap, format);
[246]65        string ret = ssprintf_va(format, ap); //is it too wasteful? copying the string again... unless the compiler can handle it better
[180]66        va_end(ap);
67        return ret;
68}
69
[246]70string stripExt(const string& filename)
[180]71{
[319]72        size_t dot = filename.rfind('.');
[246]73        if (dot == string::npos) return filename;
[319]74        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
[246]75        if ((sep == string::npos) || (sep < dot))
76                return filename.substr(0, dot);
[180]77        return filename;
78}
79
[460]80string stripFileDir(const string& filename)
81{
82        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
83        if (sep == string::npos) return filename;
[867]84        return filename.substr(sep + 1);
[460]85}
86
[246]87string getFileExt(const string& filename)
[180]88{
[319]89        size_t dot = filename.rfind('.');
[246]90        if (dot == string::npos) return string("");
[319]91        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
[246]92        if ((sep == string::npos) || (sep < dot))
[180]93                return filename.substr(dot);
94        return string("");
95}
[246]96
97string getFileDir(const string& filename)
98{
[319]99        size_t slash = filename.rfind(PATH_SEPARATOR_CHAR);
[246]100        if (slash == string::npos) return string("");
101        return filename.substr(0, slash);
102}
Note: See TracBrowser for help on using the repository browser.