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

Last change on this file since 182 was 180, checked in by Maciej Komosinski, 11 years ago

Remove fixed length buffers

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1#include "stl-util.h"
2#include <stdarg.h>
3#include <stdlib.h>
4#include "nonstd_stdio.h"
5#include "nonstd.h"
6#include "framsg.h"
7
8string ssprintf_va(const char* format, va_list ap)
9{
10        string s; //clang crashed when this declaration was in s=buf - dreadful GOTO? - i doubt it... goto did not skip over any variable declarations in this case
11        long size=1000;
12        char* buf;
13retry:
14        buf=(char*)malloc(size);
15        if (vsnprintf(buf,size,format,ap)>=size) {free(buf); size*=4; goto retry;}
16        s=buf;
17        free(buf);
18        return s;
19}
20
21string ssprintf(const char* format, ...)
22{
23        va_list ap;
24        va_start(ap, format);
25        string ret=ssprintf_va(format,ap); //is it too wasteful? copying the string again... unless the compiler can handle it better
26        va_end(ap);
27        return ret;
28}
29
30bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
31{
32        MFILE *f=mfopen(filename,FOPEN_READ_BINARY);
33        bool ok=f!=NULL;
34        if (f)
35        {
36                mfseek(f,0,SEEK_END);
37                long size=mftell(f);
38                mfseek(f,0,SEEK_SET);
39                data.resize(size);
40                int przeczytane=mfread(&data[0],size,1,f);
41                mfclose(f);
42                ok&=przeczytane==1;
43        }
44        if (warn_on_missing_file && !ok)
45                FMprintf("stl-util","readCompleteFile",FMLV_WARN,"Couldn't open file '%s'",filename);
46        return ok;
47}
48
49bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file)
50{
51        std::vector<char> data;
52        if (readCompleteFile(filename,data,warn_on_missing_file))
53        {
54                out=string(&data[0],data.size());
55                return true;
56        }
57        return false;
58}
59
60bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
61{
62        MFILE *f=mfopen(filename,FOPEN_WRITE_BINARY);
63        bool ok=f!=NULL;
64        if (f)
65        {
66                int zapisane=mfwrite(text.c_str(),text.length(),1,f);
67                mfclose(f);
68                ok&=zapisane==1;
69        }
70        if (warn_on_fail && !ok)
71                FMprintf("stl-util","writeCompleteFile",FMLV_WARN,"couldn't write file '%s'",filename);
72        return ok;
73}
74
75bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)
76{
77        string s(&data[0],data.size());
78        return writeCompleteFile(filename, s, warn_on_fail);
79}
80
81
82
83std::string stripExt(const std::string& filename)
84{
85        int dot=filename.rfind('.');
86        if (dot==std::string::npos) return filename;
87        int sep=filename.rfind(PATH_SEPARATOR_CHAR);
88        if ((sep==std::string::npos)||(sep<dot))
89                return filename.substr(0,dot);
90        return filename;
91}
92
93std::string getFileExt(const std::string& filename)
94{
95        int dot=filename.rfind('.');
96        if (dot==std::string::npos) return string("");
97        int sep=filename.rfind(PATH_SEPARATOR_CHAR);
98        if ((sep==std::string::npos)||(sep<dot))
99                return filename.substr(dot);
100        return string("");
101}
Note: See TracBrowser for help on using the repository browser.