00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "DataStack.h"
00021 #include "GameManager.h"
00022
00023
00024
00025
00026 u32 DataStack::mIDCount = 0;
00027
00028
00029 DataStack::DataStack(const std::string &name)
00030 : mName(name)
00031 {
00032 mID = mIDCount++;
00033 }
00034
00035
00036 DataStack::~DataStack()
00037 {
00038 clear();
00039 }
00040
00041
00042 void DataStack::init()
00043 {
00044 }
00045
00046
00047 void DataStack::clear()
00048 {
00049 removeVars();
00050 }
00051
00052
00053 u32 DataStack::getID() const
00054 {
00055 return mID;
00056 }
00057
00058
00059 const std::string& DataStack::getName() const
00060 {
00061 return mName;
00062 }
00063
00064
00065 u32 DataStack::getSize() const
00066 {
00067 return mVars.size();
00068 }
00069
00070
00071 std::string DataStack::getVar(const std::string &name)
00072 {
00073
00074 std::map<std::string, std::string>::iterator it;
00075 it = mVars.find(name);
00076
00077
00078 if(it != mVars.end())
00079 return it->second;
00080
00081
00082 else return NULL;
00083 }
00084
00085
00086 void DataStack::removeVars()
00087 {
00088 mVars.clear();
00089 }
00090
00091
00092 bool DataStack::removeVar(const std::string &name)
00093 {
00094 std::map<std::string, std::string>::iterator it;
00095 it = mVars.find(name);
00096
00097 if(it != mVars.end())
00098 {
00099 mVars.erase(it);
00100 return true;
00101 }
00102
00103 else return false;
00104 }
00105
00106
00107 bool DataStack::saveBencode(const std::string &fileName)
00108 {
00109
00110 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00111
00112
00113 IWriteFile *file = fileSystem->createAndWriteFile(fileName.c_str());
00114
00115
00116 std::stringstream fBuffer;
00117
00118
00119 fBuffer << "d3:fidi2419e5:ftype3:bdse";
00120
00121
00122 fBuffer << "d";
00123
00124 std::map<std::string, std::string>::iterator it;
00125 for(it = mVars.begin(); it != mVars.end(); it++)
00126 {
00127 fBuffer << it->first.size() << ":" << it->first;
00128 fBuffer << it->second.size() << ":" << it->second;
00129 }
00130
00131 fBuffer << "e";
00132
00133
00134 file->write(fBuffer.str().c_str(), fBuffer.str().size());
00135
00136
00137 file->drop();
00138
00139 return true;
00140 }
00141
00142
00143 bool DataStack::loadBencode(const std::string &fileName)
00144 {
00145
00146 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00147
00148
00149 if(!fileSystem->existFile(fileName.c_str()))
00150 return false;
00151
00152
00153 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00154
00155 unsigned long fSize = file->getSize();
00156 c8 *fBuffer = new c8[fSize+1];
00157 file->read(fBuffer, fSize);
00158 file->drop();
00159
00160
00161 std::string fHeader = std::string(fBuffer).substr(0, 25);
00162
00163 if(fHeader != "d3:fidi2419e5:ftype3:bdse")
00164 return false;
00165
00166
00167 removeVars();
00168
00169
00170 std::stringstream bCountStream;
00171 unsigned long bCount = 0;
00172
00173 s32 state = 0;
00174 std::string key;
00175 std::string value;
00176
00177 for(unsigned long i = 25; i < fSize; i++)
00178 {
00179 if(bCount != 0)
00180 {
00181 if(state == 0)
00182 key += fBuffer[i];
00183
00184 else if(state == 1)
00185 value += fBuffer[i];
00186
00187 bCount--;
00188 }
00189
00190 else
00191 {
00192 if(!key.empty())
00193 state = 1;
00194
00195 if(!value.empty())
00196 {
00197 mVars[key] = value;
00198 key.clear();
00199 value.clear();
00200 state = 0;
00201 }
00202
00203 if(fBuffer[i] >= '0' && fBuffer[i] <= '9')
00204 bCountStream << fBuffer[i];
00205 }
00206
00207 if(fBuffer[i] == ':')
00208 {
00209 bCountStream >> bCount;
00210 bCountStream.clear();
00211 }
00212 }
00213
00214
00215 delete[] fBuffer;
00216
00217
00218 return true;
00219 }
00220
00221
00222 bool DataStack::saveXML(const std::string &fileName)
00223 {
00224
00225 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00226
00227
00228 IWriteFile *file = fileSystem->createAndWriteFile(fileName.c_str());
00229
00230
00231 std::stringstream fBuffer;
00232
00233
00234 fBuffer << "<?xml version=\"1.0\"?>\n";
00235
00236
00237 fBuffer << "<datastack>\n";
00238
00239 std::map<std::string, std::string>::iterator it;
00240 for(it = mVars.begin(); it != mVars.end(); it++)
00241 fBuffer << " <var name=\"" << it->first << "\" value=\"" << it->second << "\" />\n";
00242
00243 fBuffer << "</datastack>";
00244
00245
00246 file->write(fBuffer.str().c_str(), fBuffer.str().size());
00247
00248
00249 file->drop();
00250
00251 return true;
00252 }
00253
00254
00255 bool DataStack::loadXML(const std::string &fileName)
00256 {
00257
00258 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00259
00260
00261 if(!fileSystem->existFile(fileName.c_str()))
00262 return false;
00263
00264
00265 IXMLReader *file = fileSystem->createXMLReader(fileName.c_str());
00266
00267 if(!file)
00268 return false;
00269
00270
00271 bool isDataStack = false;
00272
00273 while(file && file->read())
00274 {
00275 switch(file->getNodeType())
00276 {
00277 case io::EXN_ELEMENT:
00278
00279
00280 if(stringw("datastack") == file->getNodeName())
00281 isDataStack = true;
00282
00283
00284 if(isDataStack)
00285 {
00286 if(stringw("var") == file->getNodeName())
00287 {
00288 stringc name = file->getAttributeValue(L"name");
00289 stringc value = file->getAttributeValue(L"value");
00290 mVars[name.c_str()] = value.c_str();
00291 }
00292 }
00293
00294 break;
00295
00296 default:
00297 break;
00298 }
00299 }
00300
00301 if(!isDataStack)
00302 return false;
00303
00304
00305 file->drop();
00306
00307 return true;
00308 }
00309
00310