ConfigFile.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "core/ConfigCmdManager.h"
  2. #include "core/ConfigFile.h"
  3. #include "core/Logger.h"
  4. #include "core/Utilites.h"
  5. void
  6. ConfigFile::Execute( const Ogre::String& name )
  7. {
  8. // Open the configuration file
  9. std::ifstream fp;
  10. // Always open in binary mode
  11. fp.open( name.c_str(), std::ios::in | std::ios::binary );
  12. if( !fp )
  13. {
  14. LOG_WARNING( "Config file\"" + name + "\" not found!" );
  15. }
  16. else
  17. {
  18. Ogre::String command = "";
  19. while( true )
  20. {
  21. char c = fp.get(); // get character from file
  22. if( fp.good() == false )
  23. {
  24. break;
  25. }
  26. if( c == '\r' || ( c == '\n' && command.size() == 0 ) )
  27. {
  28. }
  29. else if( c == '\n' && command.size() > 0 )
  30. {
  31. Ogre::StringVector params = StringTokenise( command );
  32. if( params.size() > 0 )
  33. {
  34. // handle command
  35. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find( params[ 0 ] );
  36. if( cmd != NULL )
  37. {
  38. cmd->GetHandler()( params );
  39. }
  40. else
  41. {
  42. LOG_ERROR( "Can't find command \"" + params[ 0 ] + "\"." );
  43. }
  44. }
  45. command = "";
  46. }
  47. else if( c >= 0 ) // use only ascii characters
  48. {
  49. command += c;
  50. }
  51. }
  52. if( command.size() > 0 )
  53. {
  54. Ogre::StringVector params = StringTokenise( command );
  55. if( params.size() > 0 )
  56. {
  57. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find( params[ 0 ] );
  58. if( cmd != NULL )
  59. {
  60. cmd->GetHandler()( params );
  61. }
  62. else
  63. {
  64. LOG_ERROR( "Can't find command \"" + params[ 0 ] + "\"." );
  65. }
  66. }
  67. }
  68. fp.close(); // close file
  69. }
  70. }