/home/enzo/treballs/fib/pfc/nanocomp/src/layoutDiskManager.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.8 $
00020 
00021 #include "layoutDiskManager.hpp"
00022 #include "flexDefines.hpp"
00023 #include <fstream>
00024 
00026 
00029 LayoutDiskManager::LayoutDiskManager(LayoutManager *controller)
00030 {
00031     this->controller = controller;
00032 }
00033 
00034 
00036 LayoutDiskManager::~LayoutDiskManager()
00037 {
00038 }
00039 
00040 
00042 
00047 bool LayoutDiskManager::saveLayout(LayoutConfig *layoutConfig, wxString fileName)
00048 {
00049     if (wxFile::Exists(fileName))
00050     {
00051         file.Open(fileName);
00052     }
00053     else
00054     {
00055         file.Create(fileName);   
00056     }
00057     file.Clear();
00058     if (!saveLayout(layoutConfig))
00059     {
00060         file.Close();
00061         return false;
00062     }
00063     if (!file.Write(wxTextFileType_Unix))
00064     {
00065         file.Close();
00066         return false;
00067     }
00068     return (file.Close());
00069 }
00070 
00071 
00073 
00079 bool LayoutDiskManager::saveLayout(LayoutConfig *layoutConfig)
00080 {
00081     bool result = true;
00082     file.AddLine(_("#Space file created by Nanocomp."));
00083     file.AddLine(_("#Do not edit this file if you don't know what are you doing."));
00084     file.AddLine(wxEmptyString);
00085     
00086     file.AddLine(wxString::Format(_("%d %d"), layoutConfig->getWidth(), layoutConfig->getHeight()));
00087     file.AddLine(wxEmptyString);
00088     result = result && saveGrid(layoutConfig->getGrid());
00089     
00090     file.AddLine(wxEmptyString);
00091     file.AddLine(_("#End of file"));
00092     return result;
00093 }
00094 
00095 
00097 
00101 bool LayoutDiskManager::saveGrid(Grid grid)
00102 {
00103     //See the CA space format for details
00104     wxChar inputs = 'a';
00105     wxChar outputs = 'A';
00106     wxString space = wxEmptyString;
00107     for(int i = 0; i < grid.getHeight(); i++)
00108     {
00109         wxString line = space;
00110         for(int j = 0; j < grid.getWidth(); j++)
00111         {
00112             wxString status;
00113             
00114             switch (grid(j, i))
00115             {
00116                 case nNOSPACE:
00117                 {
00118                     line = line + _("-");
00119                     break;
00120                 }
00121                 case nENABLED:
00122                 {
00123                     line = line + _("1");
00124                     break;
00125                 }
00126                 case nDISABLED:
00127                 {
00128                     line = line + _("0");
00129                     break;
00130                 }
00131                 case nINPUT:
00132                 {
00133                     line = line + inputs;
00134                     inputs++;
00135                     break;
00136                 }
00137                 case nOUTPUT:
00138                 {
00139                     line = line + outputs;
00140                     outputs++;
00141                     break;
00142                 }
00143                 case nDONTCARE:
00144                 {
00145                     line = line + _(".");
00146                     break;
00147                 }
00148                 default:
00149                 {
00150                 }
00151             }
00152             
00153             //Add the space between cells (except if last)
00154             if(j != grid.getWidth() - 1)
00155             {
00156                 for (int l = 0; l < CELL_SPACE; l++)
00157                 {
00158                     line += _(" ");
00159                 }
00160             }
00161         }
00162         file.AddLine(line);
00163         //Add the line tabulation for each line
00164         for (int k = 0; k < LINE_SPACE; k++)
00165         {
00166             space += _(" ");
00167         }
00168     }
00169     return true;
00170 }
00171 
00172 
00174 
00178 bool LayoutDiskManager::openLayout(wxString fileName)
00179 {
00180     LayoutConfig *layoutConfig;
00181     filebuf fb;
00182     if (!fb.open (fileName.mb_str(), ios::in))
00183     {
00184         return false;
00185     }
00186     istream *is = new istream(&fb);
00187     int result;
00188     int width;
00189     int height;
00190     FlexLexer* lexer = new yyFlexLexer();
00191     lexer->switch_streams(is, NULL);
00192     result = lexer->yylex();
00193     while (result != 0)
00194     {
00195         if (result != NUMBER)
00196         {
00197             //Number expected, error
00198             delete is;
00199             delete lexer;
00200             fb.close();
00201             return false;
00202         }
00203         else
00204         {
00205             width = atoi(lexer->YYText());
00206             if (lexer->yylex() != NUMBER) //Not another number
00207             {
00208                 //Number expected, error
00209                 delete is;
00210                 delete lexer;
00211                 fb.close();
00212                 return false;
00213             }
00214             else
00215             {
00216                 height = atoi(lexer->YYText());
00217                 layoutConfig = readLayout(width, height, lexer);
00218                 if (!layoutConfig)
00219                 {
00220                     delete is;
00221                     delete lexer;
00222                     fb.close();
00223                     return false;
00224                 }
00225             }
00226         }
00227         result = lexer->yylex();
00228     }
00229     fb.close();
00230     returnLayout(layoutConfig);
00231  
00232     return true;
00233 }
00234 
00235 
00237 
00245 LayoutConfig* LayoutDiskManager::readLayout(int width, int height, FlexLexer *lexer)
00246 {
00247     Grid cells(width, height);
00248     int inputs = 0;
00249     int outputs = 0;
00250     //Now we have to process width * height cells
00251     int result;
00252     for(int i = 0; i < width * height; i++)
00253     {
00254         switch (result = lexer->yylex())
00255         {
00256             case NUMBER: //Number, so must be 1 or 0, else error
00257                 {
00258                     if (atoi(lexer->YYText()) != 1 && atoi(lexer->YYText()) != 0)
00259                     {
00260                         //0 or 1 expected, file format error
00261                         return false;
00262                     }
00263                     else
00264                     {
00265                         if (atoi(lexer->YYText()) == 1)
00266                         {
00267                             cells(i - width * (i/width), i/width) = nENABLED;
00268                         }
00269                         else
00270                         {
00271                             cells(i - width * (i/width), i/width) = nDISABLED;
00272                         }
00273                     }
00274                     break;
00275                 }
00276             case POINT: 
00277                 {
00278                     cells(i - width * (i/width), i/width) = nDONTCARE;
00279                     break;
00280                 }
00281             case SLASH:
00282                 {
00283                     cells(i - width * (i/width), i/width) = nNOSPACE;
00284                     break;
00285                 }
00286             case UPPER:
00287                 {
00288                     cells(i - width * (i/width), i/width) = nOUTPUT;
00289                     outputs++;
00290                     break;
00291                 }
00292             case LOWER:
00293                 {
00294                     cells(i - width * (i/width), i/width) = nINPUT;
00295                     inputs++;
00296                     break;
00297                 }
00298             default: //Error
00299                 {
00300                     return false;
00301                 }
00302         }
00303     }
00304     
00305     //Now we know inputs and outputs
00306     LayoutConfig *layoutConfig = new LayoutConfig(width, height, inputs, outputs);
00307     
00308     
00309     for(int i = 0; i < height; i++)
00310     {
00311         for(int j = 0; j < width; j++)
00312         {
00313             layoutConfig->cellChanged(j, i, cells(j, i));
00314         }
00315     }
00316 
00317     return layoutConfig;
00318 }
00319 
00320 
00322 
00325 void LayoutDiskManager::returnLayout(LayoutConfig *layoutConfig)
00326 {
00327     controller->newLayout(layoutConfig);
00328 }

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