NIREP

DisplayAttributes.cpp

Go to the documentation of this file.
00001 #include "DisplayAttributes.h"
00002 #include <algorithm>
00003 
00004 #define WIDGETLIST 1
00005 
00006 
00009 DisplayAttributes::DisplayAttributes(const std::string& fileName)
00010 {
00011   Init();
00012   error = Read(fileName);
00013 }
00014 
00015 void DisplayAttributes::Init()
00016 {
00017   for(DisplayAttributeCmdList::iterator i = DisplayAttributeCmd.begin();i != DisplayAttributeCmd.end();i++) {
00018     std::vector< std::vector<std::string> > temp;
00019     attributes[i->second.c_str()] = temp;
00020   }
00021   this->rowDimension = 0;
00022   this->colDimension = 0;
00023 }
00024 
00025 
00028 bool DisplayAttributes::Read(const std::string& fileName)
00029 {
00030         std::ifstream file (fileName.c_str());
00031   int list = 0;
00032   if (file.is_open())
00033   {
00034     std::string line;
00035     
00036     while (! file.eof() )
00037     {
00038       getline (file,line);
00039 
00040       // Convert line to all lower case for comparison
00041       std::transform(line.begin(), line.end(), line.begin(), tolower);
00042 
00043       if(line.size() > 0)
00044       {
00045 
00046           //Run through an if statment.  If we have not hit
00047           //the begin WidgetList then we are going to be
00048           //looking for a begin widgetList or columnSize
00049           //or rowSize.  If we have hit the beginWidgetList
00050           //then either look for the End WidgetList or
00051           //insert the items into the vector at the locations
00052           //specified in the file.  In the file, there should
00053           //be a line like:
00054           //  W1,1 = view(_A1, Transverse)
00055           //  This example will place a view widget at 
00056           //row 1, column 1.
00057           if(list == 0)
00058           {
00059             if(!line.compare("begin displayattributes"))
00060             {
00061               list = WIDGETLIST;
00062             }
00063           }
00064           else if(list == WIDGETLIST) {
00065             if(!line.compare("end displayattributes")) {
00066               list =0;
00067             }
00068             else {
00069               size_t foundTwo = line.find_first_of("(");
00070 
00071               if( foundTwo == std::string::npos ) {
00072                 wxString temp = wxT("Error: Missing left parenthese in the line: ");
00073                 temp.Append(wxString::Format (wxT("%s"), line.c_str()));
00074                 wxLogError(temp);
00075                 return true;
00076               }
00077 
00078               size_t foundThree = line.find_first_of(")");
00079 
00080               if( foundTwo == std::string::npos ) {
00081                 wxString temp = wxT("Error: Missing right parenthese in the line: ");
00082                 temp.Append(wxString::Format (wxT("%s"), line.c_str()));
00083                 wxLogError(temp);
00084                 return true;
00085               }
00086 
00087 
00088               std::string id(line, 0,foundTwo);
00089               wxString wxIDTwo(id.c_str(), wxConvUTF8);
00090               wxIDTwo.Trim();
00091               wxIDTwo.Trim(false);
00092               id = wxIDTwo.ToAscii();
00093 
00094               //Get the function that the widget is supposed to do
00095               std::string function(line, foundTwo+1,foundThree-foundTwo-1);
00096               wxString wxFunction(function.c_str(), wxConvUTF8);
00097               wxFunction.Trim();
00098               wxFunction.Trim(false);
00099               function = wxFunction.ToAscii();
00100               //first we have to have a char * to pass to strtok
00101               //Since we are using strings, and c_str() returns
00102               //a const char*, we are going to copy the data from
00103               //c_str to a new variable called strtokStringToExpand
00104               //which will be passed to strtok to tokenize bassed
00105               //on ";"
00106               char * strtokStringToExpand = new char[function.size()+1]();
00107               memcpy(strtokStringToExpand,function.c_str(),function.size()+1);
00108               char* inputAttributes = strtok (strtokStringToExpand,";");
00109               std::vector<std::string> tempAttributes;
00110               //keep tokenizing until we hit the end of the string
00111               while (inputAttributes != NULL)
00112               {
00113                 tempAttributes.push_back(inputAttributes);
00114                 //get the next tokenized part
00115                 inputAttributes = strtok (NULL, ";");
00116               }
00117               if(this->attributes.find(id) != this->attributes.end() )
00118               {
00119                 this->attributes.find(id)->second.push_back(tempAttributes);
00120                 if(!id.compare(DisplayAttributeCmd.find("Rowsize")->second))
00121                   this->SetRowDimension( atoi(tempAttributes.at(0).c_str() ) );
00122                 else if(!id.compare(DisplayAttributeCmd.find("Columnsize")->second))
00123                   this->SetColDimension( atoi(tempAttributes.at(0).c_str() ) );
00124               }
00125               else
00126               {
00127                 std::vector< std::vector< std::string > > temp;
00128                 this->attributes[id] =  temp;
00129                 this->attributes.find(id)->second.push_back(tempAttributes);
00130               }
00131               delete []strtokStringToExpand;
00132             }
00133           }
00134           
00135       }
00136     }
00137     file.close();
00138     if(this->rowDimension != 0 && this->colDimension != 0)
00139      return false;
00140     else if(this->rowDimension == 0) {
00141       wxString temp = wxT("Error: Row Dimension was not set in file: ");
00142       temp.Append(wxString::Format (wxT("%s\n"), fileName.c_str()));
00143       temp.Append((wxString::Format (wxT("please use the command: %s\n"), DisplayAttributeCmd.find("Rowsize")->second.c_str() ) )  );
00144       wxLogError(temp);
00145       return true;
00146     }
00147     else if(this->colDimension == 0) {
00148       wxString temp = wxT("Error: Column Dimension was not set in file: ");
00149       temp.Append(wxString::Format (wxT("%s\n"), fileName.c_str()));
00150       temp.Append((wxString::Format (wxT("please use the command: %s\n"), DisplayAttributeCmd.find("Columnsize")->second.c_str() ) ) );
00151       wxLogError(temp);
00152       return true;
00153     }
00154   }
00155 
00156   wxString temp = wxT("Error: Could not open file: ");
00157   temp.Append(wxString::Format (wxT("%s"), fileName.c_str()));
00158   wxLogError(temp);
00159   return true;
00160 }
00161 
00164 void DisplayAttributes::Write(const std::string& fileName)
00165 {
00166 }
00167 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines