/[anoncvs]/projects/roguelike/DataFile.cpp


UCC Code Repository

Contents of /projects/roguelike/DataFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations) (download)
Sat Apr 15 15:02:52 2006 UTC (15 years ago) by banana
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +2 -2 lines
Windows frontend finished (though it is rudimentary)

1 #include "DataFile.h"
2 #include "port.h"
3 #include "core.h"
4 #include <sstream>
5 using namespace std;
6
7 /**********
8 * Generic *
9 **********/
10
11 DataFile::DataFile(string category, int id) {
12 stringstream path;
13 path << category << '/' << id;
14
15 wchar_t *context, *idx, *data = port_open_data(path.str().c_str());
16
17 /* For debug purposes, write out the data we read in */
18 char logfile[50];
19 sprintf(logfile, "%s-%d.log", category.c_str(), id);
20 port_save_user(logfile, data);
21 /* end debug bit */
22
23 const wchar_t *sep = L"\r\n";
24 #ifdef WINDOWS
25 for (idx = wcstok(data, sep); idx != NULL; idx = wcstok(NULL, sep))
26 #else
27 for (idx = wcstok(data, sep, &context); idx != NULL; idx = wcstok(NULL, sep, &context))
28 #endif
29 lines.push_back(idx);
30 }
31
32 wstring DataFile::operator[](int line) {
33 return lines[line];
34 }
35
36 /**********
37 * Parsers *
38 **********/
39
40 bool DataFile::parseBool(int line) {
41 return (lines[line][0] == L'1');
42 }
43
44 unsigned int DataFile::parseHex(int line) {
45 return wcstol(lines[line].c_str(), NULL, 16);
46 }
47
48 wchar_t DataFile::parseChar(int line) {
49 return lines[line][0];
50 }
51
52 wstring DataFile::parseString(int line) {
53 return lines[line];
54 }
55
56 int DataFile::parseGridColumns(int line) {
57 return lines[line].length();
58 }
59
60 int DataFile::parseGridRows(int line) {
61 return lines.size() - line;
62 }
63
64 /***************
65 * Interpreters *
66 ***************/
67
68 Terrain *DataFile::asTerrain() {
69 if (lines.size() < 4 || lines[1].length() != 1 || lines[2].length() > 8 || lines[3].length() != 1)
70 return NULL; //Not a valid terrain file
71
72 return new Terrain(parseString(0), parseChar(1), parseHex(2), parseBool(3));
73 }
74
75 World *DataFile::asWorld() {
76 vector<vector<int> > terrain;
77
78 for (vector<wstring>::iterator i = ++(lines.begin()); i != lines.end(); i++) {
79 vector<int> v;
80
81 for (unsigned x = 0; x < i->length(); x++) {
82 char id = wctob((*i)[x]);
83 if (id == ' ') //allow space for empty
84 id = '0';
85
86 v.push_back(id - '0');
87 }
88
89 terrain.push_back(v);
90 }
91
92 return new World(parseString(0), parseGridColumns(1), parseGridRows(1), terrain);
93 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26