Merge branch 'master' of git+ssh://repo.or.cz/srv/git/jben
[jben.git] / src / file_utils.cpp
blob6b7bb948935eb58be46e952029424dca6bfa80d0
1 /*
2 Project: J-Ben
3 Author: Paul Goins
4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
8 File: file_utils.cpp
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Include platform-specific files here */
25 #ifdef __WIN32__
26 #include <windows.h>
27 #else
28 #include <unistd.h>
29 #endif
31 /* Normal includes go here */
32 #include "file_utils.h"
33 #include "encoding_convert.h"
34 #include "string_utils.h"
35 #include <fstream>
36 #include <sstream>
37 #include <zlib.h>
38 #include <boost/format.hpp>
39 using namespace std;
41 /* Tries to open the specified file. If successful, it'll read in its entire
42 contents into a wstring, and apply the specified conversion. */
43 int ReadEncodedFile(const char *filename, wstring& dest,
44 const char *src_encoding) {
45 ifstream ifile(filename, ios::in | ios::binary);
46 stringbuf data;
47 if(ifile.is_open()) {
48 while(!ifile.eof() && !ifile.fail())
49 ifile >> &data;
50 ifile.close();
51 if(ifile.fail()) return REF_FILE_OPEN_ERROR;
53 dest = ConvertString<char, wchar_t>
54 (data.str().c_str(), src_encoding, wcType.c_str());
55 return REF_SUCCESS;
57 } else return REF_FILE_NOT_FOUND;
60 /* Platform-independent getcwd function. */
61 string GetCWD() {
62 string s;
64 /* Yes, the size for the buffer used in
65 this function is maybe overkill. */
66 #ifdef __WIN32__
67 wchar_t *buffer = new wchar_t[0x10000];
68 GetCurrentDirectoryW(0x10000, buffer);
69 s = utfconv_wm(buffer);
70 #else
71 char *buffer = new char[0x10000];
72 if(getcwd(buffer, 0x10000))
73 s = buffer;
74 #endif
76 delete[] buffer;
77 return s;
80 bool FileExists(const char *filename) {
81 bool result=false;
82 ifstream f(filename);
83 if(f.is_open()) {
84 result=true;
85 f.close();
87 return result;
90 bool ReadFileIntoString(const string& filename,
91 string& destination, int bufSize) {
92 ifstream ifs(filename.c_str(), ios::binary | ios::in);
93 if(ifs.is_open()) {
94 char *buffer = new char[bufSize];
95 while((!ifs.fail()) && (!ifs.eof())) {
96 ifs.read(buffer, bufSize - 1);
97 buffer[ifs.gcount()] = 0;
98 destination += buffer;
100 ifs.close();
101 delete[] buffer;
102 return true;
104 return false;
107 bool ReadGzipIntoString(const string& filename,
108 string& destination, int bufSize) {
109 gzFile f = gzopen(filename.c_str(), "rb");
110 if(f) {
111 char *buffer = new char[bufSize];
112 int bytesRead = gzread(f, (void*)buffer, bufSize - 1);
113 while(bytesRead != 0) {
114 if(bytesRead == -1) {
115 el.Push(
116 EL_Error,
117 (boost::format("An error occurred: probably %s is not a "
118 "valid gzip file.")
119 % filename).str());
120 gzclose(f);
121 return false;
124 buffer[bytesRead] = 0;
125 destination += buffer;
126 bytesRead = gzread(f, (void*)buffer, bufSize - 1);
128 gzclose(f);
129 delete[] buffer;
130 return true;
132 return false;
135 void GetGzipName(const string& srcName, string& gzName, string& stdName) {
136 int len = srcName.length();
137 if(ToLower(srcName.substr(len-3)) == ".gz") {
138 gzName = srcName;
139 stdName = srcName.substr(0, len-3);
140 } else {
141 stdName = gzName = srcName;
142 gzName += ".gz";