4 License: GNU General Public License (GPL) version 2
5 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
9 This file is part of J-Ben.
11 J-Ben is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License version 2
13 as published by the Free Software Foundation.
15 J-Ben 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 "file_utils.h"
27 #include "wx/strconv.h"
29 /* Tries to open the specified file. If successful, it'll read in its entire
30 contents into a wxString, and apply the specified conversion. */
31 int ReadEncodedFile(const wxChar
*filename
, const wxChar
*src_encoding
, wxString
& dest
) {
32 /* Read in the file. NOTE: a "b" flag MIGHT be necessary for
34 if(wxFile::Exists(filename
)) {
35 wxFFile
*in
= new wxFFile(filename
, _T("r"));
36 if(!in
->IsOpened()) { /* Bail if we couldn't read it */
38 return REF_FILE_OPEN_ERROR
;
41 /* Read in all the data - our files are "small", so we'll be lazy
43 /* NOTE: Workaround for SLOW GTK performance
44 provided by "doublemax" on wxforum.shadonet.com.
46 1. Standard C functions are used to get the raw file size
47 2. The file is read in using the wxFFile::Read() command instead of
48 ReadAll(). Read does not perform local encoding to Unicode
50 3. Use the wxString::GetWriteBuf function to get us a buffer to receive
51 the wide character conversion. File length * 2 is used, which isn't
52 very memory efficient, but is CPU-efficient - far more important.
53 4. wxCSConv::MB2WC is called to perform the conversion after the file
54 is loaded. This happens FAST. */
56 size_t length
= (size_t)in
->Length();
57 wxCharBuffer
buf(length
+1);
58 length
= in
->Read(buf
.data(), length
);
59 buf
.data()[length
]=_T('\0');
63 wxCSConv
transcoder(src_encoding
);
65 length
++; /* Incremented so we will include the null character as part of
67 transcoder
.MB2WC(dest
.GetWriteBuf(length
*2), buf
.data(), length
);
71 } else return REF_FILE_NOT_FOUND
;
74 /* Tries to open the specified file. If successful, it'll read in its entire
75 contents into a wxString, and apply the local-to-Unicode conversion.
76 (Yes, this is -another- ugly workaround for just doing a wxFFile::ReadAll.)
78 int ReadFile(const wxChar
*filename
, wxString
& dest
) {
79 /* Read in the file. NOTE: a "b" flag MIGHT be necessary for
81 if(wxFile::Exists(filename
)) {
82 wxFFile
*in
= new wxFFile(filename
, _T("r"));
83 if(!in
->IsOpened()) { /* Bail if we couldn't read it */
85 return REF_FILE_OPEN_ERROR
;
88 size_t length
= (size_t)in
->Length();
89 wxCharBuffer
buf(length
+1);
90 length
= in
->Read(buf
.data(), length
);
91 buf
.data()[length
]=_T('\0');
95 length
++; /* Incremented so we will include the null character as part of
97 wxConvCurrent
->MB2WC(dest
.GetWriteBuf(length
*2), buf
.data(), length
);
103 printf("Could not find file \"%ls\".\n", filename
);
105 return REF_FILE_NOT_FOUND
;