Main Page Namespace List Class Hierarchy Alphabetical List Data Structures File List Namespace Members Data Fields Globals

# ExCFile.cpp

Go to the documentation of this file.

    00001 /*
    00002  * ExNihilo 3D Engine
    00003  *
    00004  *  This program is free software; you can redistribute it and/or modify
    00005  *  it under the terms of the GNU General Public License as published by
    00006  *  the Free Software Foundation; either version 2 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  This program is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020  * $Id: ExCFile.cpp,v 1.11 2002/08/25 15:27:49 data Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExCFile.h"
    00025
    00026 void ExCFile::setOpenMode (int mode) {
    00027         switch (mode) {
    00028                 case RO: this->mode = fstream::in; break;
    00029                 case WO: this->mode = fstream::out; break;
    00030                 case WOA: this->mode = fstream::out | fstream::app; break;
    00031                 case RW: this->mode = fstream::in | fstream::out; break;
    00032                 case RWA: this->mode = fstream::in | fstream::out | fstream::app; break;
    00033                 default: this->mode = fstream::in; break;
    00034         }
    00035 }
    00036
    00037 ios_base::openmode ExCFile::getOpenMode (int mode) {
    00038         switch (mode) {
    00039                 case RO: return ios::in; break;
    00040                 case WO: return ios::out; break;
    00041                 case WOA: return ios::out | ios::app; break;
    00042                 case RW: return ios::in | ios::out; break;
    00043                 case RWA: return ios::in | ios::out | ios::app; break;
    00044                 default: return ios::in; break;
    00045         }
    00046 }
    00047
    00048 bool ExCFile::add (std::string f) {
    00049         ofstream p;
    00050         p.open (filename.data (), ios::app);
    00051         if (p.fail ()) {
    00052                 std::cerr << "Erreur ouverture" << std::endl;
    00053                 return false;
    00054         }
    00055         p << f << std::endl;
    00056         p.close ();
    00057         return true;
    00058 }
    00059
    00060 bool ExCFile::addLine (void) {
    00061         ofstream p;
    00062         p.open (filename.data (), ios::app);
    00063         if (p.fail ()) {
    00064                 std::cerr << "Erreur ouverture" << std::endl;
    00065                 return false;
    00066         }
    00067         p << std::endl;
    00068         p.close ();
    00069         return true;
    00070 }
    00071
    00072 ExCFile::~ExCFile () {
    00073 }
    00074
    00075 bool ExCFile::exist (void) {
    00076         fstream file;
    00077         file.open (filename.data(), getOpenMode (RO));
    00078         if (! file.fail ()) {
    00079                 file.close();
    00080                 return true;
    00081         }
    00082         return false;
    00083 }
    00084
    00085 vector<string> ExCFile::getContent (void) {
    00086         string d;
    00087         vector<string> v;
    00088         fstream file;
    00089         file.open (filename.data (), getOpenMode (RO));
    00090         if (! file.fail ()) {
    00091                 while (! std::getline (file, d).eof ()) {
    00092                         v.push_back (d);
    00093                 }
    00094         }
    00095         file.close ();
    00096         return v;
    00097 }
    00098
    00099 ExCFileReader::ExCFileReader (string s) : ExCFile (s, RO) {
    00100         file_data = new ExCFileData;
    00101 }
    00102
    00103 ExCFileData ExCFileReader::getContent (void) {
    00104 Guard(ExCFileData ExCFileReader::getContent (void))
    00105         fstream file;
    00106         if (file_data->getType() == FILE_EMPTY) {
    00107                 file.open (filename.data(), getOpenMode (RO));
    00108                 if (file.is_open ()) {
    00109                         string d;
    00110                         ExCFileDataAction ds;
    00111                         bool first = true;
    00112                         string action;
    00113                         while (! getline (file, d).eof ()) {
    00114                                 if (first == false && (file_data->getType() != FILE_EMPTY || file_data->getType() != FILEUNKNOWN)) {
    00115                                         action = d.substr (0, d.find ("#"));
    00116                                         #ifdef DEBUG_HIGH
    00117                                         cout << "end of -> " << action.find ("end of") << endl;
    00118                                         #endif
    00119                                         if (action.find ("end of") != 0) {
    00120                                                 ds.setAction (action);
    00121                                                 #ifdef DEBUG_HIGH
    00122                                                 cout << "action -> " << d.substr (0, d.find ("#")) << endl;
    00123                                                 #endif
    00124                                                 if (file_data->getType() == FILE_COMMAND) {
    00125                                                         string dds = d.substr (d.find ("#") + 1, d.length());
    00126                                                         string dds1 = dds.substr (0, d.find ("#"));
    00127                                                         ds.setCommand (atoi ((dds1).data()));
    00128                                                         #ifdef DEBUG_HIGH
    00129                                                         cout << "command -> " << atoi (dds1.data()) << endl;
    00130                                                         #endif
    00131                                                         ds.setParam (dds.substr (dds.find ("#") + 1, d.length()));
    00132                                                         #ifdef DEBUG_HIGH
    00133                                                         cout << "param -> " << dds.substr (dds.find ("#") + 1, d.length()) << endl;
    00134                                                         #endif
    00135                                                 } else {
    00136                                                         string dds = d.substr (d.find ("#") + 1, d.length());
    00137                                                         ds.setCommand (atoi ((dds).data()));
    00138                                                         #ifdef DEBUG_HIGH
    00139                                                         cout << "command -> " << atoi (d.substr (d.find ("#") + 1, d.length()).data()) << endl;
    00140                                                         #endif
    00141                                                 }
    00142                                                 file_data->addElement (ds);
    00143                                         }
    00144                                 } else {
    00145                                         // erk
    00146                                         int t = FILEUNKNOWN;
    00147                                         if (d == "#exec#") t = FILE_BATCH;
    00148                                         else if (d == "#command#") t = FILE_COMMAND;
    00149                                         else if (d == "#console#") t = FILE_CONSOLE;
    00150                                         else if (d == "#set#") t = FILE_SET;
    00151                                         else if (d == "#particulesystem#") t = FILE_PARTICULE;
    00152                                         else if (d == "#interface#") t = FILE_INTERFACE;
    00153                                         file_data->setType (t);
    00154                                         #ifdef DEBUG_HIGH
    00155                                         cout << "type -> " << d << " " << t << endl;
    00156                                         #endif
    00157                                         first = false;
    00158                                 }
    00159                         }
    00160                         file.close ();
    00161                 } else { cout << "pika plop !" << endl; }
    00162         } else { cout << "plok ? plop !" << endl; }
    00163         return *file_data;
    00164 UnGuard
    00165 }
    00166
    00167 ExCFileWriter::ExCFileWriter (string s) : ExCFile (s, WO) {};
    00168
    00169 void ExCFileWriter::setContent (ExCFileData s) {
    00170         ofstream file;
    00171         file.open (filename.data (), getOpenMode (WOA));
    00172         vector<ExCFileDataAction> d = s.getContent();
    00173         vector<ExCFileDataAction>::iterator i = d.begin ();
    00174         string f = "#";
    00175         switch (s.getType ()) {
    00176                 case FILE_BATCH: f += "exec"; break;
    00177                 case FILE_COMMAND: f += "command"; break;
    00178                 case FILE_CONSOLE: f += "console"; break;
    00179                 case FILE_SET: f += "set"; break;
    00180                 case FILE_PARTICULE: f += "particulesystem"; break;
    00181                 case FILE_INTERFACE: f += "interface"; break;
    00182         }
    00183         f += "#";
    00184         file << f << endl;
    00185
    00186         ExCFileDataAction plop;
    00187         while (i != d.end ()) {
    00188                 plop = *i++;
    00189                 file << plop.getAction() << "#" << plop.getCommand() << endl;
    00190         }
    00191         file.close ();
    00192 }