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)
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 */
31 /* Normal includes go here */
32 #include "file_utils.h"
33 #include "encoding_convert.h"
34 #include "string_utils.h"
38 #include <boost/format.hpp>
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
);
48 while(!ifile
.eof() && !ifile
.fail())
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());
57 } else return REF_FILE_NOT_FOUND
;
60 /* Platform-independent getcwd function. */
64 /* Yes, the size for the buffer used in
65 this function is maybe overkill. */
67 wchar_t *buffer
= new wchar_t[0x10000];
68 GetCurrentDirectoryW(0x10000, buffer
);
69 s
= utfconv_wm(buffer
);
71 char *buffer
= new char[0x10000];
72 if(getcwd(buffer
, 0x10000))
80 bool FileExists(const char *filename
) {
90 bool ReadFileIntoString(const string
& filename
,
91 string
& destination
, int bufSize
) {
92 ifstream
ifs(filename
.c_str(), ios::binary
| ios::in
);
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
;
107 bool ReadGzipIntoString(const string
& filename
,
108 string
& destination
, int bufSize
) {
109 gzFile f
= gzopen(filename
.c_str(), "rb");
111 char *buffer
= new char[bufSize
];
112 int bytesRead
= gzread(f
, (void*)buffer
, bufSize
- 1);
113 while(bytesRead
!= 0) {
114 if(bytesRead
== -1) {
117 (boost::format("An error occurred: probably %s is not a "
124 buffer
[bytesRead
] = 0;
125 destination
+= buffer
;
126 bytesRead
= gzread(f
, (void*)buffer
, bufSize
- 1);
135 void GetGzipName(const string
& srcName
, string
& gzName
, string
& stdName
) {
136 int len
= srcName
.length();
137 if(ToLower(srcName
.substr(len
-3)) == ".gz") {
139 stdName
= srcName
.substr(0, len
-3);
141 stdName
= gzName
= srcName
;