cgma
SettingHandler.cpp
Go to the documentation of this file.
00001 
00002 #include <cassert>
00003 #include <cstdio>
00004 #include <cstring>
00005 
00006 #include <iostream>
00007 
00008 #include "SettingHandler.hpp"
00009 #include "CubitDefines.h"
00010 #include "CubitString.hpp"
00011 #include "CubitUtil.hpp"
00012 #include "CubitMessage.hpp"
00013 #include "AppUtil.hpp"
00014 #include "CubitFile.hpp"
00015 
00016 
00017 SettingHandler* SettingHandler::instance_ = NULL;
00018 
00019 SettingHandler* SettingHandler::instance()
00020 {   
00021   if (!instance_)
00022     {   
00023       instance_ = new SettingHandler;
00024       if (!instance_)
00025     {
00026       std::cerr << " *** Unable to instantiate setting_handler object ***" << std::endl;
00027       exit(1);
00028     }
00029     }
00030   return instance_;
00031 }
00032 
00033 //Default Constructor
00034 SettingHandler::SettingHandler() 
00035 {
00036   debug_flags_added = CUBIT_FALSE;
00037 } 
00038 
00039 void SettingHandler::delete_instance() 
00040 {
00041   if (instance_)
00042   {
00043     delete instance_;
00044     instance_ = 0;
00045   }
00046 }
00047 
00048 SettingHandler::~SettingHandler() 
00049 {
00050     std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
00051   std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
00052 
00053   for( ; iter != end; ++iter)
00054     delete (*iter).second;
00055   mSettingsList.clear();
00056 }
00057 
00058 void SettingHandler::add_setting(const char* name, void (*setFn) (int), 
00059                 int (*getFn)())
00060 {
00061   CubitString cs = name;
00062   //Check to see if a setting with the same name already exists
00063   if (mSettingsList.find(cs) != mSettingsList.end()) {
00064     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
00065         << "This is a bug.  Please report!\n";
00066     exit(-1);
00067   }
00068   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
00069   mSettingsList[cs] = setting;
00070 }
00071 
00072 
00073 void SettingHandler::add_setting(const char* name, void (*setFn) (double), 
00074                 double (*getFn)())
00075 {
00076   CubitString cs = name;
00077   //Check to see if a setting with the same name already exists
00078   if (mSettingsList.find(cs) != mSettingsList.end()) {
00079     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
00080         << "This is a bug.  Please report!\n";
00081     exit(-1);
00082     
00083   }
00084   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
00085   mSettingsList[cs] = setting;
00086 }
00087 
00088 
00089 void SettingHandler::add_setting(const char* name, void (*setFn) (CubitBoolean), 
00090                 CubitBoolean (*getFn)())
00091 {
00092    CubitString cs = name;
00093   //Check to see if a setting with the same name already exists
00094   if (mSettingsList.find(cs) != mSettingsList.end()) {
00095     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
00096         << "This is a bug.  Please report!\n";
00097     exit(-1);
00098     
00099   }
00100   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
00101   mSettingsList[cs] = setting;
00102 }
00103 
00104 
00105 void SettingHandler::add_setting(const char* name, void (*setFn) (CubitString), 
00106                 CubitString (*getFn)())
00107 {
00108    CubitString cs = name;
00109   //Check to see if a setting with the same name already exists
00110   if (mSettingsList.find(cs) != mSettingsList.end()) {
00111     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
00112         << "This is a bug.  Please report!\n";
00113     exit(-1);
00114     
00115   }
00116   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
00117   mSettingsList[cs] = setting;
00118 }
00119 
00120 void SettingHandler::save_settings(const char *filename)
00121 {
00122   CubitFile file(filename, "w");
00123   
00124   if (!file) {
00125     std::cerr << "File " << filename << " could not be opened.  Settings not saved." << std::endl;
00126     return;
00127    }
00128 
00129   //Put a header at the top of the file
00130   fprintf(file.file(), "#Setting Name                                 Setting Value\n");
00131   fprintf(file.file(), "#------------------------------------------------------------\n");
00132 
00133   // Get starting and ending iterators from the map
00134   std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
00135   std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
00136   
00137   // Step through the map and build a file to hold the settings
00138   while(start != end) {
00139     SettingHolder* setting = (*start).second;
00140 
00141     //Integer settings
00142     if (setting->setting_type == 0) {
00143       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
00144     }
00145     
00146     //Double settings
00147     else if (setting->setting_type == 1) {
00148       fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
00149     }
00150 
00151     //Boolean settings
00152     else if (setting->setting_type == 2) {
00153       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
00154     }
00155     
00156     //String settings
00157     else if (setting->setting_type == 3) {
00158       fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
00159     }
00160 
00161     else
00162     {
00163       assert(false);
00164       PRINT_ERROR("Error with SettingHolder type!  Please report.");
00165     }
00166 
00167     ++start;
00168   }
00169 }
00170 
00171 void SettingHandler::print_settings()
00172 {
00173   int rows, cols;
00174   if (!AppUtil::instance()->get_terminal_size(rows, cols))
00175     cols = 71;
00176   --cols;
00177 
00178   // Get starting and ending iterators from the map
00179   std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
00180   const std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
00181   
00182   // Step through the map and build a file to hold the settings
00183   for ( ; iter != end; ++iter ) 
00184   {
00185     SettingHolder* setting = iter->second;
00186     int vallen = cols - setting->name.length() - 1;
00187     if (vallen < 1) vallen = 1;
00188     switch( setting->setting_type )
00189     {
00190       case 0:
00191         PRINT_INFO("%s %*d\n", setting->name.c_str(), vallen,
00192             (setting->get_int_function)());
00193         break;
00194       case 1:
00195         PRINT_INFO("%s %*f\n", setting->name.c_str(), vallen,
00196             (setting->get_double_function)());
00197         break;
00198       case 2:
00199         PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
00200             (setting->get_bool_function)() ? "true" : "false");
00201         break;
00202       case 3:
00203         PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
00204             (setting->get_string_function)().c_str());
00205         break;
00206       default:
00207         PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen,
00208             "ERROR : UNKNOWN SETTING TYPE");
00209     }
00210   }
00211 }
00212 
00213 void SettingHandler::save_settings()
00214 {
00215   
00216   const char* default_filename = "cubit.settings";
00217 
00218   CubitFile file(default_filename, "w");
00219   
00220   if (!file) {
00221     std::cerr << "File " << default_filename << " could not be opened.  Settings not saved." << std::endl;
00222     return;
00223    }
00224 
00225   //Put a header at the top of the file
00226   fprintf(file.file(), "#Setting Name                                 Setting Value\n");
00227   fprintf(file.file(), "#------------------------------------------------------------\n");
00228 
00229   // Get starting and ending iterators from the map
00230   std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
00231   std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
00232 
00233   // Step through the map and build a file to hold the settings
00234   while(start != end) {
00235     SettingHolder* setting = (*start).second;
00236 
00237     //Integer settings
00238     if (setting->setting_type == 0) {
00239       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
00240     }
00241     
00242     //Double settings
00243     else if (setting->setting_type == 1) {
00244       fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
00245     }
00246 
00247     //Boolean settings
00248     else if (setting->setting_type == 2) {
00249       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
00250     }
00251     
00252     //String settings
00253     else if (setting->setting_type == 3) {
00254       fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
00255     }
00256 
00257     else
00258     {
00259       assert(false);
00260       PRINT_ERROR("Error with SettingHolder type!  Please report.");
00261     }
00262 
00263     ++start;
00264   }
00265 }
00266 
00267 
00268 void SettingHandler::restore_settings(const char* filename)
00269 {
00270 
00271   //Open the file for reading
00272   CubitFile file(filename, "r");
00273 
00274   if (file) {
00275     std::cerr << "File " << filename << " could not be opened.  Settings not restored." << std::endl;
00276     return;
00277    }
00278 
00279   //Read the first 2 lines of the file, we know they are not settings
00280   //so just get rid of them
00281   char junk[100];
00282   fgets(junk, 100, file.file());
00283   fgets(junk, 100, file.file());
00284 
00285   char name[51]; //Allocate 50 bytes for the characters and 1 for a null termination
00286   CubitString cubit_name; //A CubitString that will be used for checking if the setting
00287                           //is in the map.
00288   char value[120]; //This will be used when getting the value of a setting
00289   SettingHolder* setting;
00290   int int_value = -1;
00291   double double_value = 0.0;
00292   CubitBoolean bool_value = CUBIT_FALSE;
00293   CubitString string_value = "";
00294 
00295   while (!feof(file.file())) {
00296 
00297   //Get the setting name.  This will be in the first 50 characters of a line
00298   fgets(name, 51, file.file());
00299 
00300   cubit_name = name;
00301   cubit_name.trim();
00302 
00303   //Find the setting in the map
00304   std::map<CubitString, SettingHolder*>::iterator temp;
00305 
00306   temp = mSettingsList.find(cubit_name);
00307   
00308     //Read in the rest of the line no matter what
00309   fgets(value, 120, file.file());
00310     
00311   if (temp == mSettingsList.end()) {
00312     std::cerr << "Setting " << cubit_name.c_str() << " was not found." << std::endl;  
00313   }
00314   else {
00315     setting = (*temp).second;
00316     //Integer Settings
00317     if (setting->setting_type == 0) {
00318       int_value = atoi(value);
00319       (setting->set_int_function)(int_value);
00320     }
00321     
00322     //Double Settings
00323     if (setting->setting_type == 1) {
00324       double_value = atof(value);
00325       (setting->set_double_function)(double_value);
00326     }    
00327 
00328     //Boolean Settings
00329     if (setting->setting_type == 2) {
00330 //      bool_value = (CubitBoolean)(atoi(value));
00331       bool_value = (atoi(value)) ? true : false;
00332       (setting->set_bool_function)(bool_value);
00333     }    
00334     
00335     //CubitString Settings
00336     if (setting->setting_type == 3) {
00337       string_value = value;
00338       (setting->set_string_function)(string_value);
00339     }     
00340 
00341   }
00342 
00343   } //End while
00344 
00345 }
00346 
00347 SettingHolder* SettingHandler::get_setting_holder(CubitString name)
00348 { 
00349   std::map<CubitString, SettingHolder*>::iterator it = mSettingsList.find(name);
00350   return it  ==  mSettingsList.end()  ? NULL : it->second;
00351 }
00352  
00353 void SettingHandler::get_settings_list(std::vector< std::pair<CubitString, SettingHolder*> > &list)
00354 {
00355   // return a list of the settings
00356   std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
00357   while(iter != mSettingsList.end())
00358   {
00359     std::pair<CubitString, SettingHolder*>  tmp_pair;
00360     CubitString key = (*iter).first;
00361     tmp_pair.first = key;
00362     tmp_pair.second = (*iter).second;
00363     list.push_back( (tmp_pair) );
00364     iter++;
00365   }
00366 }
00367 
00368 
00369 
00370 
00371 
00372 
00373 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines