Added integrated KanjiPad, accessible via Kanji->Find menu.
[jben.git] / file_utils.cpp
blobb670166e61313c43d3f3b8a380757ed4b778ff4e
1 /*
2 Project: J-Ben
3 Author: Paul Goins
4 License: GNU General Public License (GPL) version 2
5 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
7 File: file_utils.cpp
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"
25 #include "wx/file.h"
26 #include "wx/ffile.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
33 Windows! */
34 if(wxFile::Exists(filename)) {
35 wxFFile *in = new wxFFile(filename, _T("r"));
36 if(!in->IsOpened()) { /* Bail if we couldn't read it */
37 delete in;
38 return REF_FILE_OPEN_ERROR;
41 /* Read in all the data - our files are "small", so we'll be lazy
42 and "read all". */
43 /* NOTE: Workaround for SLOW GTK performance
44 provided by "doublemax" on wxforum.shadonet.com.
45 What happens:
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
49 conversion.
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');
60 in->Close();
61 delete in;
63 wxCSConv transcoder(src_encoding);
65 length++; /* Incremented so we will include the null character as part of
66 the copied string. */
67 transcoder.MB2WC(dest.GetWriteBuf(length*2), buf.data(), length);
68 dest.UngetWriteBuf();
70 return REF_SUCCESS;
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
80 Windows! */
81 if(wxFile::Exists(filename)) {
82 wxFFile *in = new wxFFile(filename, _T("r"));
83 if(!in->IsOpened()) { /* Bail if we couldn't read it */
84 delete in;
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');
92 in->Close();
93 delete in;
95 length++; /* Incremented so we will include the null character as part of
96 the copied string. */
97 wxConvCurrent->MB2WC(dest.GetWriteBuf(length*2), buf.data(), length);
98 dest.UngetWriteBuf();
100 return REF_SUCCESS;
101 } else {
102 #ifdef DEBUG
103 printf("Could not find file \"%ls\".\n", filename);
104 #endif
105 return REF_FILE_NOT_FOUND;