/[pcsx2_0.9.7]/trunk/pcsx2/File_Reader.h
ViewVC logotype

Contents of /trunk/pcsx2/File_Reader.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 3206 byte(s)
committing r3113 initial commit again...
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 using namespace std;
19
20 class File_Reader {
21 private:
22 char buff[2048];
23 template<class T> T _read() {
24 if (fs->eof()) throw 1;
25 T t; (*fs) >> t;
26 if (fs->fail()) throw 1;
27 return t;
28 }
29 public:
30 fstream* fs;
31 File_Reader(const string& filename) {
32 fs = new fstream(filename.c_str(), ios_base::in);
33 }
34 virtual ~File_Reader() throw() {
35 if (fs) fs->close();
36 delete fs;
37 }
38 template<class T> void read(T &t) {
39 long pos = fs->tellp();
40 string s( _read<string>() );
41 if (s.length() >= 2) {
42 if (s[0] == '/' && s[1] == '/') {
43 fs->seekp(pos);
44 fs->getline(buff, sizeof(buff));
45 read(t);
46 return;
47 }
48 }
49 fs->seekp(pos);
50 t = _read<T>();
51 }
52 void readRaw(void* ptr, int size) {
53 u8* p = (u8*)ptr;
54 for (int i = 0; i < size; i++) {
55 p[i] = _read<u8>();
56 }
57 }
58 void ignoreLine() {
59 fs->getline(buff, sizeof(buff));
60 }
61 string getLine() {
62 if (fs->eof()) throw 1;
63 fs->getline(buff, sizeof(buff));
64 if (fs->fail()) throw 1;
65
66 string ret(buff);
67 int eol = ret.rfind("\r");
68 if (eol != string::npos) ret = ret.substr(0, eol);
69
70 return ret;
71 }
72 template<class T> void readLine(T& str) {
73 if (fs->eof()) throw 1;
74 fs->getline(buff, sizeof(buff));
75 if (fs->fail()) throw 1;
76 string t(buff);
77 str = t;
78 }
79 };
80
81 class File_Writer {
82 public:
83 ScopedPtr<fstream> fs;
84 File_Writer(const string& filename) {
85 fs = new fstream(filename.c_str(), ios_base::out);
86 }
87 virtual ~File_Writer() throw() { }
88
89 template<class T> void write(const T& t) {
90 (*fs) << t;
91 }
92 void writeRaw(const void* ptr, int size) {
93 const u8* p = (u8*)ptr;
94 for (int i = 0; i < size; i++) {
95 write(p[i]);
96 }
97 }
98 };
99
100 class String_Stream {
101 private:
102 char buff[2048];
103 public:
104 stringstream* ss;
105 String_Stream() {
106 ss = new stringstream(stringstream::in | stringstream::out);
107 }
108 String_Stream(const string& str) {
109 ss = new stringstream(str, stringstream::in | stringstream::out);
110 }
111 virtual ~String_Stream() throw() {
112 delete ss;
113 }
114 template<class T> void write(const T& t) {
115 (*ss) << t;
116 }
117 template<class T> void read(T& t) {
118 (*ss) >> t;
119 }
120 string toString() {
121 return ss->str();
122 }
123 string getLine() {
124 ss->getline(buff, sizeof(buff));
125 return buff;
126 }
127 wxString getLineWX() {
128 ss->getline(buff, sizeof(buff));
129 return wxString(fromUTF8(buff));
130 }
131 bool finished() {
132 return ss->eof() || ss->fail();
133 }
134 };
135
136 static bool fileExists(const string& file) {
137 FILE *f = fopen(file.c_str(), "r");
138 if (!f) return false;
139 fclose(f);
140 return true;
141 }

  ViewVC Help
Powered by ViewVC 1.1.22