Debian Codegraph @

source/code/sago/SagoMisc.cpp

raw symbols SCIP
1 /*
2 Copyright (c) 2015 Poul Sander
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "SagoMisc.hpp"
26 #include <physfs.h>
27 #include <iostream>
28 #include <iconv.h>
29 #include <string.h>
30
31 #if PHYSFS_VER_MAJOR < 3
32 #define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z)
33 #define PHYSFS_writeBytes(X,Y,Z) PHYSFS_write(X,Y,1,Z)
34 #endif
35
36 namespace sago {
37
38
39 std::vector<std::string> GetFileList(const char* dir) {
40 std::vector<std::string> ret;
41 char** rc = PHYSFS_enumerateFiles(dir);
42 for (char** i = rc; *i != NULL; i++) {
43 ret.push_back(*i);
44 }
45 PHYSFS_freeList(rc);
46 return ret;
47 }
48
49 bool FileExists(const char* filename) {
50 return PHYSFS_exists(filename);
51 }
52
53 void ReadBytesFromFile(const char* filename, std::unique_ptr<char[]>& dest, unsigned int& bytes) {
54 bytes = 0;
55 if (!PHYSFS_exists(filename)) {
56 std::cerr << "ReadBytesFromFile - File does not exists: " << filename << "\n";
57 return;
58 }
59 PHYSFS_file* myfile = PHYSFS_openRead(filename);
60 unsigned int m_size = PHYSFS_fileLength(myfile);
61 std::unique_ptr<char[]> m_data(new char[m_size]);
62 int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size);
63 if (length_read != (int)m_size) {
64 PHYSFS_close(myfile);
65 std::cerr << "Error: Curropt data file: " << filename << "\n";
66 return;
67 }
68 PHYSFS_close(myfile);
69 std::swap(m_data, dest);
70 bytes = m_size;
71 }
72
73 std::string GetFileContent(const char* filename) {
74 std::string ret;
75 if (!PHYSFS_exists(filename)) {
76 std::cerr << "GetFileContent - File does not exists: " << filename << "\n";
77 return ret;
78 }
79 unsigned int m_size = 0;
80 std::unique_ptr<char[]> m_data;
81 ReadBytesFromFile(filename, m_data, m_size);
82 //Now create a std::string
83 char* inbuf = m_data.get();
84 ret = std::string(inbuf, inbuf+m_size);
85 return ret;
86 }
87
88 static void CreatePathToFile(const std::string& path) {
89 size_t end_of_path = path.find_last_of("/");
90 if (end_of_path == std::string::npos) {
91 //No path
92 return;
93 }
94 std::string path2dir = path.substr(0, end_of_path);
95 PHYSFS_mkdir(path2dir.c_str());
96 }
97
98 void WriteFileContent(const char* filename, const std::string& content) {
99 CreatePathToFile(filename);
100 PHYSFS_file* myfile = PHYSFS_openWrite(filename);
101 if (!myfile) {
102 #if PHYSFS_VER_MAJOR > 2
103 PHYSFS_ErrorCode code = PHYSFS_getLastErrorCode();
104 std::cerr << "Failed to open file for writing, " << PHYSFS_getErrorByCode(code) << " (" << code << ")\n";
105 #else
106 std::cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << "\n";
107 #endif
108 return;
109 }
110 PHYSFS_writeBytes(myfile, content.c_str(), sizeof(char)*content.length());
111 PHYSFS_close(myfile);
112 }
113
114 long int StrToLong(const char* c_string) {
115 auto ret = strtol(c_string, nullptr, 10);
116 return ret;
117 }
118
119 }