Pol  Revision:1cb77e0
pol/readcfg.cpp
Go to the documentation of this file.
00001 /*
00002 History
00003 =======
00004 
00005 
00006 Notes
00007 =======
00008 
00009 */
00010 
00011 #include "../clib/cfgelem.h"
00012 #include "../clib/cfgfile.h"
00013 #include "../clib/clib.h"
00014 #include "../clib/fileutil.h"
00015 #include "../clib/logfacility.h"
00016 #include "../clib/stlutil.h"
00017 
00018 #include "item/itemdesc.h"
00019 #include "menu.h"
00020 #include "polcfg.h"
00021 
00022 #include <stdexcept>
00023 #include <string>
00024 
00025 #ifdef _MSC_VER
00026 #pragma warning(disable:4996) // deprecation warning stricmp
00027 #endif
00028 
00029 namespace Pol {
00030   namespace Core {
00031     void read_menus( void )
00032     {
00033       if ( !Clib::FileExists( "config/menus.cfg" ) )
00034       {
00035         if ( config.loglevel > 1 )
00036           INFO_PRINT << "File config/menus.cfg not found, skipping.\n";
00037         return;
00038       }
00039 
00040       Clib::ConfigFile cf( "config/menus.cfg" );
00041 
00042       Clib::ConfigElem elem;
00043 
00044       while ( cf.read( elem ) )
00045       {
00046         std::string menu_name;
00047         std::string menu_title;
00048         std::string propname;
00049         std::string value;
00050 
00051         if ( stricmp( elem.type(), "ItemMenu" ) != 0 )
00052           continue;
00053 
00054         menu_name = elem.remove_string( "NAME" );
00055         menu_title = elem.remove_string( "TITLE" );
00056 
00057         menus.push_back( Menu() );
00058         Menu* menu = &menus.back();
00059 
00060         menu->menu_id = static_cast<unsigned short>( menus.size() );
00061         strzcpy( menu->name, menu_name.c_str(), sizeof menu->name );
00062         strzcpy( menu->title, menu_title.c_str(), sizeof menu->title );
00063 
00064         while ( elem.remove_first_prop( &propname, &value ) )
00065         {
00066           if ( stricmp( propname.c_str(), "SubMenu" ) == 0 ||
00067                stricmp( propname.c_str(), "Entry" ) == 0 )
00068           {
00069             std::string submenu_name;
00070             std::string objtype_str;
00071             std::string title;
00072 
00073             /*
00074             char *submenu_name = NULL;
00075             char *objtype_str = NULL;
00076             char *title = NULL;
00077             */
00078 
00079             if ( stricmp( propname.c_str(), "SubMenu" ) == 0 )
00080             {
00081               ISTRINGSTREAM is( value );
00082               is >> submenu_name >> objtype_str;
00083 
00084               // skipws
00085               // is.eatwhite();
00086 
00087               std::getline( is, title ); // title.getline( is );
00088 
00089               // FIXME: remove leading spaces (better way? please? anyone?)
00090               while ( isspace( title[0] ) )
00091                 title.erase( 0, 1 );
00092 
00093               //char s[100];
00094               //is.ignore( std::numeric_limits<int>::max(), ' ' );
00095               //is.ignore( std::numeric_limits<int>::max(), '\t' );
00096               //is.getline( s, sizeof s );
00097               //title = s;
00098               /*
00099               submenu_name = strtok( value, " \t," );
00100               objtype_str = strtok( NULL, " \t," );
00101               title = strtok( NULL, "" ); // title is the rest of the line
00102               while ((title != NULL) && *title && isspace(*title)) // skip leading spaces
00103               title++;
00104               */
00105             }
00106             else // Entry
00107             {
00108               submenu_name = "";
00109               ISTRINGSTREAM is( value );
00110               is >> objtype_str; // FIXME objtype_str ends up having the "," on the end
00111 
00112               std::getline( is, title );
00113               // FIXME: remove leading spaces (better way? please? anyone?)
00114               while ( isspace( title[0] ) )
00115                 title.erase( 0, 1 );
00116             }
00117 
00118             if ( objtype_str == "" )
00119             {
00120               ERROR_PRINT << "Entry in menu " << menu->name
00121                 << " must provide at least an object type\n";
00122               throw std::runtime_error( "Data error in MENUS.CFG" );
00123             }
00124             u32 objtype = (u32)strtoul( objtype_str.c_str(), NULL, 0 );
00125             if ( objtype == 0 ) // 0 specified, or text
00126             {
00127               ERROR_PRINT << "Entry in menu " << menu->name
00128                 << " cannot specify [" << objtype_str << "] as an Object Type.\n";
00129               throw std::runtime_error( "Data error in MENUS.CFG" );
00130             }
00131             if ( ( stricmp( propname.c_str(), "SubMenu" ) == 0 ) &&
00132                  ( submenu_name == "" ) )
00133             {
00134               ERROR_PRINT << "SubMenu in menu " << menu->name
00135                 << " needs format: Objtype, Title, SubMenuName [got '" << value << "']\n";
00136               throw std::runtime_error("Data error in MENUS.CFG");
00137             }
00138             if ( ( stricmp( propname.c_str(), "Entry" ) == 0 ) &&
00139                  ( submenu_name != "" ) )
00140             {
00141               ERROR_PRINT << "Entry in menu " << menu->name
00142                 << " must not specify SubMenuName [got '" << value << "']\n";
00143               throw std::runtime_error("Data error in MENUS.CFG");
00144             }
00145 
00146             menu->menuitems_.push_back( MenuItem() );
00147             MenuItem* mi = &menu->menuitems_.back();
00148 
00149             mi->objtype_ = objtype;
00150             mi->graphic_ = Items::getgraphic( objtype );
00151             mi->color_ = Items::getcolor( objtype );
00152 
00153             if ( title != "" )
00154               strzcpy( mi->title, title.c_str(), sizeof mi->title );
00155             else
00156               mi->title[0] = '\0';
00157 
00158             if ( submenu_name != "" )
00159               strzcpy( mi->submenu_name, submenu_name.c_str(), sizeof mi->submenu_name );
00160             else
00161               mi->submenu_name[0] = '\0';
00162 
00163             mi->submenu_id = 0;
00164           }
00165           else
00166           {
00167             ERROR_PRINT << "Unexpected property in menu " << menu->name << ": " << propname << "\n";
00168             throw std::runtime_error("Data error in MENUS.CFG");
00169           }
00170         }
00171       }
00172 
00173 
00174       for ( unsigned menuidx = 0; menuidx < menus.size(); menuidx++ )
00175       {
00176         Menu *menu = &menus[menuidx];
00177         for ( unsigned itemidx = 0; itemidx < menu->menuitems_.size(); itemidx++ )
00178         {
00179           MenuItem *mi = &menu->menuitems_[itemidx];
00180 
00181           if ( mi->submenu_name[0] )
00182           {
00183             Menu* found = find_menu( mi->submenu_name );
00184             if ( found )
00185             {
00186               mi->submenu_id = found->menu_id;
00187             }
00188             else
00189             {
00190               INFO_PRINT << "Unable to find SubMenu " << mi->submenu_name << "\n";
00191             }
00192           }
00193         }
00194       }
00195 
00196       //TailoringMenu = find_menu( "Tailoring" );
00197       //ItemCreationMenu = find_menu( "ItemCreation" );
00198     }
00199   }
00200 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines