/home/enzo/treballs/fib/pfc/nanocomp/src/forbiddenPatternDiskManager.cpp

Go to the documentation of this file.
00001 /*
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   This program is distributed in the hope that it will be useful,       *
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00011  *   GNU General Public License for more details.                          *
00012  *                                                                         *
00013  *   You should have received a copy of the GNU General Public License     *
00014  *   along with this program; if not, write to the                         *
00015  *   Free Software Foundation, Inc.,                                       *
00016  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00017                                                                           */
00018 
00019 // $Revision: 1.9 $
00020 
00021 #include "forbiddenPatternDiskManager.hpp"
00022 #include "flexDefines.hpp"
00023 #include <list>
00024 #include <fstream>
00025 
00026 
00028 
00031 ForbiddenPatternDiskManager::ForbiddenPatternDiskManager(ForbiddenPatternManager *controller)
00032 {
00033     this->controller = controller;
00034 }
00035 
00036 
00038 ForbiddenPatternDiskManager::~ForbiddenPatternDiskManager()
00039 {
00040 }
00041 
00042 
00044 
00049 bool ForbiddenPatternDiskManager::saveFPs(list<ForbiddenPattern*> FPs, wxString fileName)
00050 {
00051     if (wxFile::Exists(fileName))
00052     {
00053         file.Open(fileName);
00054     }
00055     else
00056     {
00057         file.Create(fileName);   
00058     }
00059     file.Clear();
00060     if (!saveFPList(FPs))
00061     {
00062         file.Close();
00063         return false;
00064     }
00065     if (!file.Write(wxTextFileType_Unix))
00066     {
00067         file.Close();
00068         return false;
00069     }
00070     return (file.Close());
00071 }
00072 
00073 
00075 
00079 bool ForbiddenPatternDiskManager::saveFPList(list<ForbiddenPattern*> FPs)
00080 {
00081     bool result = true;
00082     int FPId = 1;
00083     file.AddLine(_("#Forbidden Pattern file created by Nanocomp."));
00084     file.AddLine(_("#Do not edit this file if you don't know what are you doing."));
00085     file.AddLine(wxEmptyString);
00086     for(list<ForbiddenPattern*>::iterator i = FPs.begin(); i != FPs.end(); i++)
00087     {
00088         file.AddLine(wxString::Format(_("#Forbidden Pattern %d"), FPId));
00089         result = result && saveFP((*i));
00090         FPId++;
00091     }
00092     file.AddLine(_("#End of file"));
00093     return result;
00094 }
00095 
00096 
00098 
00102 bool ForbiddenPatternDiskManager::saveFP(ForbiddenPattern *fp)
00103 {
00104     file.AddLine(wxString::Format(_("%d %d"), fp->getWidth(), fp->getHeight()));
00105     file.AddLine(wxEmptyString);
00106     file.AddLine(_("!"));
00107     saveGrid(fp->getGrid());
00108     file.AddLine(wxEmptyString);
00109     return true;    
00110 }
00111 
00112 
00114 
00118 bool ForbiddenPatternDiskManager::saveGrid(Grid grid)
00119 {
00120     wxString space = wxEmptyString;
00121     //Save the matrix. See the CA format documentation
00122     //for details.
00123     for(int i = 0; i < grid.getHeight(); i++)
00124     {
00125         wxString line = space;
00126         for(int j = 0; j < grid.getWidth(); j++)
00127         {
00128             wxString status;
00129              switch (grid(j, i))
00130             {
00131                 case nDONTCARE:
00132                 {
00133                     line = line + _(".");
00134                     break;
00135                 }
00136                 case nENABLED:
00137                 {
00138                     line = line + _("1");
00139                     break;
00140                 }
00141                 case nDISABLED:
00142                 {
00143                     line = line + _("0");
00144                     break;
00145                 }
00146                 default:
00147                 {
00148                 }
00149             }
00150 
00151             //Add the space between cells (except if last)
00152             if(j != grid.getWidth() - 1)
00153             {
00154                 for (int l = 0; l < CELL_SPACE; l++)
00155                 {
00156                     line += _(" ");
00157                 }
00158             }
00159         }
00160         file.AddLine(line);
00161         //Add the line tabulation for each line
00162         for (int k = 0; k < LINE_SPACE; k++)
00163         {
00164             space += _(" ");
00165         }
00166     }
00167     return true;
00168 }
00169 
00170 
00172 
00177 bool ForbiddenPatternDiskManager::openFPs(wxString fileName)
00178 {
00179     //TODO change couts to GUI error messages
00180     //List where we'll store the patterns found
00181     list<ForbiddenPattern *> *FPList = new list<ForbiddenPattern *>();
00182     filebuf fb;
00183     if (!fb.open (fileName.mb_str(), ios::in))
00184     {
00185         cout << "Error opening file" << endl;
00186         return false;
00187     }
00188     istream *is = new istream(&fb);
00189     //Flex result
00190     int result;
00191     //Pattern width and height
00192     int width;
00193     int height;
00194     //Flex lexer
00195     FlexLexer* lexer = new yyFlexLexer();
00196     lexer->switch_streams(is, NULL);
00197     result = lexer->yylex();
00198     //We read until EOF
00199     while (result != 0)
00200     {
00201         if (result != NUMBER)
00202         {
00203             cout << "Number expected, file format error" << endl;
00204             //Clear all dynamic memory used since we have an error
00205             delete FPList;
00206             delete is;
00207             delete lexer;
00208             fb.close();
00209             return false;
00210         }
00211         else
00212         {
00213             //We've read the width
00214             width = atoi(lexer->YYText());
00215             if (lexer->yylex() != NUMBER) //Not another number
00216             {
00217                 cout << "Number expected, file format error" << endl;
00218                 //Clear all dynamic memory used since we have an error
00219                 delete FPList;
00220                 delete is;
00221                 delete lexer;
00222                 fb.close();
00223                 return false;
00224             }
00225             else
00226             {
00227                 //We've read the height
00228                 height = atoi(lexer->YYText());
00229                 if (lexer->yylex() != NEGATION)
00230                     {
00231                         cout << "! expected, file format error" << endl;
00232                         //Clear all dynamic memory used since we have an error
00233                         delete FPList;
00234                         delete is;
00235                         delete lexer;
00236                         fb.close();
00237                         return false;
00238                     }
00239                 else
00240                 {
00241                     //We try to read a grid
00242                     if (!readFP(width, height, FPList, lexer))//Wrong rule format
00243                     {
00244                         //Clear all dynamic memory used since we have an error
00245                         delete FPList;
00246                         delete is;
00247                         delete lexer;
00248                         fb.close();
00249                         return false;
00250                     }
00251                 }
00252             }
00253             result = lexer->yylex();
00254         }
00255     }
00256     fb.close();
00257     //We return the patterns read to be added to the controller
00258     returnFPs(FPList);
00259     delete FPList;
00260     return true;
00261 }
00262 
00263 
00265 
00273 bool ForbiddenPatternDiskManager::readFP(int width, int height, list<ForbiddenPattern *> *FPList, FlexLexer *lexer)
00274 {
00275     ForbiddenPattern *fp = new ForbiddenPattern(width, height);
00276     //Now we have to process width * height cells
00277     int result;
00278     for(int i = 0; i < width * height; i++)
00279     {
00280         switch (result = lexer->yylex())
00281         {
00282             case NUMBER: //Number, so must be 1 or 0, else error
00283                 {
00284                     if (atoi(lexer->YYText()) > 1)
00285                     {
00286                         cout << "-, 1 or 0 expected, file format error" << endl;
00287                         delete fp;
00288                         return false;
00289                     }
00290                     else
00291                     {
00292                         if (atoi(lexer->YYText()))
00293                         {
00294                             cout << "Cell at position " << i/width << ", " << i - width * (i/width) << endl;
00295                             fp->cellChanged(i - width * (i/width), i/width, nENABLED);
00296                         }
00297                         else
00298                         {
00299                             cout << "No Cell at position " << i/width << ", " << i - width * (i/width) << endl;
00300                             fp->cellChanged(i - width * (i/width), i/width, nDISABLED);
00301                         }
00302                     }
00303                     break;
00304                 }
00305             case POINT: 
00306                 {
00307                     cout << "Don't care at position " << i/width << ", " << i - width * (i/width) << endl;
00308                     fp->cellChanged(i - width * (i/width), i/width, nDONTCARE);
00309                     break;
00310                 }
00311             default: //Error
00312                 {
00313                     cout << "-, 1 or 0 expected, file format error" << endl;
00314                     delete fp;
00315                     return false;
00316                 }
00317         }
00318     }
00319     FPList->push_back(fp);
00320     return true;
00321 }
00322 
00323 
00325 
00330 void ForbiddenPatternDiskManager::returnFPs(list<ForbiddenPattern *> *FPList)
00331 {
00332     for(list<ForbiddenPattern *>::iterator i = FPList->begin(); i != FPList->end(); i++)
00333     {
00334         controller->newFP((*i));
00335     }   
00336 }

Generated on Fri Sep 1 23:55:13 2006 for NanoComp by  doxygen 1.4.6