Major directory structure changes, plus a bug fix.
[jben.git] / src / file_utils.cpp
blob70d2877692893f7a59adeb747c974b23829f3eb4
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 __WXMSW__
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 <fstream>
35 #include <sstream>
36 using namespace std;
38 /* Tries to open the specified file. If successful, it'll read in its entire
39 contents into a wstring, and apply the specified conversion. */
40 int ReadEncodedFile(const char *filename, wstring& dest,
41 const char *src_encoding) {
42 ifstream ifile(filename, ios::in | ios::binary);
43 stringbuf data;
44 if(ifile.is_open()) {
45 while(!ifile.eof() && !ifile.fail())
46 ifile >> &data;
47 ifile.close();
48 if(ifile.fail()) return REF_FILE_OPEN_ERROR;
50 dest = ConvertString<char, wchar_t>
51 (data.str().c_str(), src_encoding, wcType.c_str());
52 return REF_SUCCESS;
54 } else return REF_FILE_NOT_FOUND;
57 /* Platform-independent getcwd function. */
58 string GetCWD() {
59 string s;
61 /* Yes, the size for the buffer used in
62 this function is maybe overkill. */
63 #ifdef __WXMSW__
64 wchar_t *buffer = new wchar_t[0x10000];
65 GetCurrentDirectoryW(0x10000, buffer);
66 s = utfconv_wm(buffer);
67 #else
68 char *buffer = new char[0x10000];
69 if(getcwd(buffer, 0x10000))
70 s = buffer;
71 #endif
73 delete[] buffer;
74 return s;
77 bool FileExists(const char *filename) {
78 bool result=false;
79 ifstream f(filename);
80 if(f.is_open()) {
81 result=true;
82 f.close();
84 return result;