src/tools/tStatFile.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 file by guru3/tank program
00026 
00027 */
00028 
00029 #include "tStatFile.h"
00030 #include "tDirectories.h"
00031 
00032 tStatFile::tStatFile(tString name, tList<tStatEntry> *inList)
00033 {
00034     fileName = name << ".txt";
00035     betweenWrites = 0;
00036 
00037     theList = inList;
00038 }
00039 
00040 void tStatFile::read()
00041 {
00042     if (!myFileD.is_open())
00043     {
00044         open();
00045     }
00046 
00047     while (myFileD.good() && !myFileD.eof())
00048     {
00049         //              std::cout << "file " << fileName << ": reading line\n";
00050         REAL t1;
00051         tString t2;
00052 
00053         myFileD >> t1;
00054         t2.ReadLine(myFileD);
00055 
00056         //              std::cout << "file " << fileName << ": " << t1 << "-" << t2 << ".\n";
00057 
00058         if (t2 != "")
00059         {
00060             tStatEntry *newone = new tStatEntry(t2, t1);
00061             theList->Add(newone, newone->id);
00062             //                  delete newone; should this be here?
00063         }
00064     }
00065 }
00066 
00067 void tStatFile::write()
00068 {
00069     betweenWrites++;
00070 
00071     if (betweenWrites == 20)
00072     {
00073         flushWrites();
00074         betweenWrites = 0;
00075     }
00076 }
00077 
00078 void tStatFile::open()
00079 {
00080     bool open = tDirectories::Var().Open(myFileD, fileName);
00081     if (!open)
00082     {
00083         std::ofstream t;
00084         tDirectories::Var().Open(t, fileName);
00085         t << std::flush;
00086         t.close();
00087         tDirectories::Var().Open(myFileD, fileName);
00088     }
00089 }
00090 
00091 void tStatFile::flushWrites()
00092 {
00093     for (int c = theList->Len() - 1; c >= 0; c--)
00094     {
00095         if ((*theList)[c]->toWrite == true)
00096         {
00097             //                  std::cout << "file " << fileName << ": un-buffering " << (*theList)[c]->getName() << " (" << c << ")" << std::endl;
00098 
00099             myFileD.clear();
00100             myFileD.seekg(0, std::ios::beg);
00101             //read thru each line in while loop !eof until the line matching the name is found or the end of file is hit
00102             //update the line or add the new one at the end. sorting can wait
00103             //fixed spacing for values important otherwise writing in place... tricky
00104 
00105             tString out;
00106             out << (*theList)[c]->getValue();
00107             out.SetPos(9, true);
00108             out << " " << (*theList)[c]->getName() << "\n";
00109 
00110             int found = 0;
00111             REAL t1;
00112             tString t2;
00113             while (!myFileD.eof())
00114             {
00115                 myFileD >> t1;
00116                 t2.ReadLine(myFileD);
00117                 if (t2 == (*theList)[c]->getName())
00118                 {
00119                     found = 1;
00120                     int pos = myFileD.tellg();
00121                     pos = pos - t2.Len() - 9;
00122                     myFileD.clear(); //in case last line?
00123                     myFileD.seekg(pos, std::ios::beg);
00124                     myFileD << out;
00125                 }
00126             }
00127 
00128             if (found == 0)
00129             {
00130                 myFileD.clear();
00131                 myFileD << out;
00132             }
00133             (*theList)[c]->toWrite = false;
00134         }
00135     }
00136 
00137     myFileD.flush();
00138 }
00139 
00140 void tStatFile::close()
00141 {
00142     if (myFileD.is_open())
00143     {
00144         myFileD.close();
00145     }
00146 }
00147 
00148 tStatFile::~tStatFile()
00149 {
00150     //  std::cout << fileName << ": deconstruction!\n";
00151     //necessary!
00152     flushWrites();
00153     close();
00154 }

Generated on Sat Mar 15 22:56:02 2008 for Armagetron Advanced by  doxygen 1.5.4