home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / core / file_parser.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-05-22  |  2.7 KB  |  130 lines

  1. /***************************************************************************
  2.  * file_parser.cpp  -  Image Settings Handler
  3.  *
  4.  * Copyright (C) 2005 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../core/globals.h"
  17. #include "../core/file_parser.h"
  18.  
  19. cFile_parser :: cFile_parser( void )
  20. {
  21.     error_count = 0;
  22. }
  23.  
  24. cFile_parser :: ~cFile_parser( void )
  25. {
  26.     //
  27. }
  28.  
  29. bool cFile_parser :: Parse( string &filename )
  30. {
  31.     error_count = 0;
  32.  
  33.     ifstream ifs( filename.c_str(), ios::in );
  34.     
  35.     if( !ifs )
  36.     {
  37.         printf( "Could not load data file : %s\n", filename.c_str() );
  38.         return 0;
  39.     }
  40.  
  41.     data_file = filename;
  42.  
  43.     char contents[500]; // maximum length of a line
  44.     
  45.     for( unsigned int i = 0; ifs.getline( contents, sizeof( contents ) ); i++ )
  46.     {
  47.         if( !Parse_Line( contents, i ) )
  48.         {
  49.             error_count++;
  50.         }
  51.     }
  52.  
  53.     return 1;
  54. }
  55.  
  56. bool cFile_parser :: Parse_Line( string command, int line )
  57. {
  58.     // linux support
  59.     while( command.find( '\r' ) != string::npos )
  60.     {
  61.         command.erase( command.find( '\r' ), 1 );
  62.     }
  63.  
  64.     // no tabs
  65.     while( command.find( '\t' ) != string::npos )
  66.     {
  67.         command.replace( command.find( '\t' ), 1, " " );
  68.     }
  69.  
  70.     // remove trailing spaces
  71.     while( command.find_last_of( ' ' ) == command.length() - 1  )
  72.     {
  73.         command.erase( command.find_last_of( ' ' ), 1 );
  74.     }
  75.  
  76.     // remove beginning spaces
  77.     while( command.find_first_of( ' ' ) == 0  )
  78.     {
  79.         command.erase( command.find_first_of( ' ' ), 1 );
  80.     }
  81.  
  82.     // ignore comments and empty lines
  83.     if( command.find_first_of( '#' ) == 0 || command.empty() )
  84.     {
  85.         // no error
  86.         return 1;
  87.     }
  88.  
  89.     string tempstr = command;
  90.     int count = 1;
  91.  
  92.     // Count spaces
  93.     while( tempstr.find( ' ' ) != string::npos  )
  94.     {
  95.         tempstr.erase( tempstr.find( ' ' ) , 1 );
  96.         count++;
  97.     }
  98.  
  99.     tempstr = command;
  100.     
  101.     string *parts = new string[ count + 1 ];
  102.     
  103.     int len;
  104.     int i = 0;
  105.  
  106.     while( count > 0 )
  107.     {
  108.         len = static_cast<int>(tempstr.find_first_of( ' ' ));
  109.         parts[i] = tempstr.substr( 0, len );
  110.         tempstr.erase( 0, len + 1 );
  111.         i++;
  112.         count--;
  113.     }
  114.  
  115.     parts[i] = tempstr;
  116.  
  117.     // Message handler
  118.     bool success = HandleMessage( parts, i, line );
  119.  
  120.     delete []parts;
  121.  
  122.     return success;
  123. }
  124.  
  125. bool cFile_parser :: HandleMessage( string *parts, unsigned int count, unsigned int line )
  126. {
  127.     // virtual
  128.     return 1;
  129. }
  130.