source: cpp/common/stl-util.cpp @ 238

Last change on this file since 238 was 220, checked in by Maciej Komosinski, 11 years ago
  • moved vsnprintf-related #defines to nonstd.h
  • ssprintf_va() now works with non-C99 conformant vsnprintf(): when possible, it obtains the required number of characters in a single call instead of iterating
  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include "stl-util.h"
6#include <stdarg.h>
7#include <stdlib.h>
8#include "nonstd_stdio.h"
9#include "nonstd.h"
10#include "framsg.h"
11#include <assert.h>
12
13string ssprintf_va(const char* format, va_list ap)
14{
15        string s; //clang crashed when this declaration was in s=buf
16        long size=256;
17        char* buf;
18
19        //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying
20#ifdef USE_VSCPRINTF
21        size=_vscprintf(format, ap);
22#endif
23
24        while(1)
25                {
26                buf=(char*)malloc(size);
27                assert(buf!=NULL);
28                int n=vsnprintf(buf,size,format,ap);
29                if (n > -1 && n < size)
30                        {
31                        s=buf;
32                        free(buf);
33                        return s;
34                        }
35#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
36                if (n > -1)    /* glibc 2.1 */
37                        size = n+1; /* precisely what is needed */
38                else           /* glibc 2.0 */
39#endif
40                        size *= 2;  /* twice the old size */
41                free(buf);
42                }
43}
44
45string ssprintf(const char* format, ...)
46{
47        va_list ap;
48        va_start(ap, format);
49        string ret=ssprintf_va(format,ap); //is it too wasteful? copying the string again... unless the compiler can handle it better
50        va_end(ap);
51        return ret;
52}
53
54bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
55{
56        MFILE *f=mfopen(filename,FOPEN_READ_BINARY);
57        bool ok=f!=NULL;
58        if (f)
59        {
60                mfseek(f,0,SEEK_END);
61                long size=mftell(f);
62                mfseek(f,0,SEEK_SET);
63                data.resize(size);
64                int przeczytane=mfread(&data[0],size,1,f);
65                mfclose(f);
66                ok&=przeczytane==1;
67        }
68        if (warn_on_missing_file && !ok)
69                FMprintf("stl-util","readCompleteFile",FMLV_WARN,"Couldn't open file '%s'",filename);
70        return ok;
71}
72
73bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file)
74{
75        std::vector<char> data;
76        if (readCompleteFile(filename,data,warn_on_missing_file))
77        {
78                out=string(&data[0],data.size());
79                return true;
80        }
81        return false;
82}
83
84bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
85{
86        MFILE *f=mfopen(filename,FOPEN_WRITE_BINARY);
87        bool ok=f!=NULL;
88        if (f)
89        {
90                int zapisane=mfwrite(text.c_str(),text.length(),1,f);
91                mfclose(f);
92                ok&=zapisane==1;
93        }
94        if (warn_on_fail && !ok)
95                FMprintf("stl-util","writeCompleteFile",FMLV_WARN,"couldn't write file '%s'",filename);
96        return ok;
97}
98
99bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)
100{
101        string s(&data[0],data.size());
102        return writeCompleteFile(filename, s, warn_on_fail);
103}
104
105
106
107std::string stripExt(const std::string& filename)
108{
109        int dot=filename.rfind('.');
110        if (dot==std::string::npos) return filename;
111        int sep=filename.rfind(PATH_SEPARATOR_CHAR);
112        if ((sep==std::string::npos)||(sep<dot))
113                return filename.substr(0,dot);
114        return filename;
115}
116
117std::string getFileExt(const std::string& filename)
118{
119        int dot=filename.rfind('.');
120        if (dot==std::string::npos) return string("");
121        int sep=filename.rfind(PATH_SEPARATOR_CHAR);
122        if ((sep==std::string::npos)||(sep<dot))
123                return filename.substr(dot);
124        return string("");
125}
Note: See TracBrowser for help on using the repository browser.