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 | |
---|
12 | string ssprintf_va(const char* format, va_list ap) |
---|
13 | { |
---|
14 | 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 |
---|
15 | long size=1000; |
---|
16 | char* buf; |
---|
17 | retry: |
---|
18 | buf=(char*)malloc(size); |
---|
19 | if (vsnprintf(buf,size,format,ap)>=size) {free(buf); size*=4; goto retry;} |
---|
20 | s=buf; |
---|
21 | free(buf); |
---|
22 | return s; |
---|
23 | } |
---|
24 | |
---|
25 | string ssprintf(const char* format, ...) |
---|
26 | { |
---|
27 | va_list ap; |
---|
28 | va_start(ap, format); |
---|
29 | string ret=ssprintf_va(format,ap); //is it too wasteful? copying the string again... unless the compiler can handle it better |
---|
30 | va_end(ap); |
---|
31 | return ret; |
---|
32 | } |
---|
33 | |
---|
34 | bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file) |
---|
35 | { |
---|
36 | MFILE *f=mfopen(filename,FOPEN_READ_BINARY); |
---|
37 | bool ok=f!=NULL; |
---|
38 | if (f) |
---|
39 | { |
---|
40 | mfseek(f,0,SEEK_END); |
---|
41 | long size=mftell(f); |
---|
42 | mfseek(f,0,SEEK_SET); |
---|
43 | data.resize(size); |
---|
44 | int przeczytane=mfread(&data[0],size,1,f); |
---|
45 | mfclose(f); |
---|
46 | ok&=przeczytane==1; |
---|
47 | } |
---|
48 | if (warn_on_missing_file && !ok) |
---|
49 | FMprintf("stl-util","readCompleteFile",FMLV_WARN,"Couldn't open file '%s'",filename); |
---|
50 | return ok; |
---|
51 | } |
---|
52 | |
---|
53 | bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file) |
---|
54 | { |
---|
55 | std::vector<char> data; |
---|
56 | if (readCompleteFile(filename,data,warn_on_missing_file)) |
---|
57 | { |
---|
58 | out=string(&data[0],data.size()); |
---|
59 | return true; |
---|
60 | } |
---|
61 | return false; |
---|
62 | } |
---|
63 | |
---|
64 | bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail) |
---|
65 | { |
---|
66 | MFILE *f=mfopen(filename,FOPEN_WRITE_BINARY); |
---|
67 | bool ok=f!=NULL; |
---|
68 | if (f) |
---|
69 | { |
---|
70 | int zapisane=mfwrite(text.c_str(),text.length(),1,f); |
---|
71 | mfclose(f); |
---|
72 | ok&=zapisane==1; |
---|
73 | } |
---|
74 | if (warn_on_fail && !ok) |
---|
75 | FMprintf("stl-util","writeCompleteFile",FMLV_WARN,"couldn't write file '%s'",filename); |
---|
76 | return ok; |
---|
77 | } |
---|
78 | |
---|
79 | bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail) |
---|
80 | { |
---|
81 | string s(&data[0],data.size()); |
---|
82 | return writeCompleteFile(filename, s, warn_on_fail); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | std::string stripExt(const std::string& filename) |
---|
88 | { |
---|
89 | int dot=filename.rfind('.'); |
---|
90 | if (dot==std::string::npos) return filename; |
---|
91 | int sep=filename.rfind(PATH_SEPARATOR_CHAR); |
---|
92 | if ((sep==std::string::npos)||(sep<dot)) |
---|
93 | return filename.substr(0,dot); |
---|
94 | return filename; |
---|
95 | } |
---|
96 | |
---|
97 | std::string getFileExt(const std::string& filename) |
---|
98 | { |
---|
99 | int dot=filename.rfind('.'); |
---|
100 | if (dot==std::string::npos) return string(""); |
---|
101 | int sep=filename.rfind(PATH_SEPARATOR_CHAR); |
---|
102 | if ((sep==std::string::npos)||(sep<dot)) |
---|
103 | return filename.substr(dot); |
---|
104 | return string(""); |
---|
105 | } |
---|