LCOV - code coverage report
Current view: top level - util - SettingHandler.cpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 33 178 18.5 %
Date: 2020-06-30 00:58:45 Functions: 8 16 50.0 %
Branches: 50 557 9.0 %

           Branch data     Line data    Source code
       1                 :            : 
       2                 :            : #include <cassert>
       3                 :            : #include <cstdio>
       4                 :            : #include <cstring>
       5                 :            : 
       6                 :            : #include <iostream>
       7                 :            : 
       8                 :            : #include "SettingHandler.hpp"
       9                 :            : #include "CubitDefines.h"
      10                 :            : #include "CubitString.hpp"
      11                 :            : #include "CubitUtil.hpp"
      12                 :            : #include "CubitMessage.hpp"
      13                 :            : #include "AppUtil.hpp"
      14                 :            : #include "CubitFile.hpp"
      15                 :            : 
      16                 :            : 
      17                 :            : SettingHandler* SettingHandler::instance_ = NULL;
      18                 :            : 
      19                 :      30590 : SettingHandler* SettingHandler::instance()
      20                 :            : {       
      21         [ +  + ]:      30590 :   if (!instance_)
      22                 :            :     {   
      23         [ +  - ]:        874 :       instance_ = new SettingHandler;
      24         [ -  + ]:        874 :       if (!instance_)
      25                 :            :         {
      26                 :          0 :           std::cerr << " *** Unable to instantiate setting_handler object ***" << std::endl;
      27                 :          0 :           exit(1);
      28                 :            :         }
      29                 :            :     }
      30                 :      30590 :   return instance_;
      31                 :            : }
      32                 :            : 
      33                 :            : //Default Constructor
      34                 :       1748 : SettingHandler::SettingHandler() 
      35                 :            : {
      36                 :        874 :   debug_flags_added = CUBIT_FALSE;
      37                 :        874 : } 
      38                 :            : 
      39                 :          0 : void SettingHandler::delete_instance() 
      40                 :            : {
      41         [ #  # ]:          0 :   if (instance_)
      42                 :            :   {
      43         [ #  # ]:          0 :     delete instance_;
      44                 :          0 :     instance_ = 0;
      45                 :            :   }
      46                 :          0 : }
      47                 :            : 
      48                 :          0 : SettingHandler::~SettingHandler() 
      49                 :            : {
      50         [ #  # ]:          0 :     std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
      51         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
      52                 :            : 
      53 [ #  # ][ #  # ]:          0 :   for( ; iter != end; ++iter)
                 [ #  # ]
      54 [ #  # ][ #  # ]:          0 :     delete (*iter).second;
                 [ #  # ]
      55         [ #  # ]:          0 :   mSettingsList.clear();
      56                 :          0 : }
      57                 :            : 
      58                 :       8740 : void SettingHandler::add_setting(const char* name, void (*setFn) (int), 
      59                 :            :                                 int (*getFn)())
      60                 :            : {
      61         [ +  - ]:       8740 :   CubitString cs = name;
      62                 :            :   //Check to see if a setting with the same name already exists
      63 [ +  - ][ +  - ]:       8740 :   if (mSettingsList.find(cs) != mSettingsList.end()) {
         [ +  - ][ -  + ]
      64 [ #  # ][ #  # ]:          0 :     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
                 [ #  # ]
      65         [ #  # ]:          0 :                 << "This is a bug.  Please report!\n";
      66                 :          0 :     exit(-1);
      67                 :            :   }
      68 [ +  - ][ +  - ]:       8740 :   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
         [ +  - ][ +  - ]
      69 [ +  - ][ +  - ]:       8740 :   mSettingsList[cs] = setting;
      70                 :       8740 : }
      71                 :            : 
      72                 :            : 
      73                 :       6118 : void SettingHandler::add_setting(const char* name, void (*setFn) (double), 
      74                 :            :                                 double (*getFn)())
      75                 :            : {
      76         [ +  - ]:       6118 :   CubitString cs = name;
      77                 :            :   //Check to see if a setting with the same name already exists
      78 [ +  - ][ +  - ]:       6118 :   if (mSettingsList.find(cs) != mSettingsList.end()) {
         [ +  - ][ -  + ]
      79 [ #  # ][ #  # ]:          0 :     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
                 [ #  # ]
      80         [ #  # ]:          0 :                 << "This is a bug.  Please report!\n";
      81                 :          0 :     exit(-1);
      82                 :            :     
      83                 :            :   }
      84 [ +  - ][ +  - ]:       6118 :   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
         [ +  - ][ +  - ]
      85 [ +  - ][ +  - ]:       6118 :   mSettingsList[cs] = setting;
      86                 :       6118 : }
      87                 :            : 
      88                 :            : 
      89                 :      13110 : void SettingHandler::add_setting(const char* name, void (*setFn) (CubitBoolean), 
      90                 :            :                                 CubitBoolean (*getFn)())
      91                 :            : {
      92         [ +  - ]:      13110 :    CubitString cs = name;
      93                 :            :   //Check to see if a setting with the same name already exists
      94 [ +  - ][ +  - ]:      13110 :   if (mSettingsList.find(cs) != mSettingsList.end()) {
         [ +  - ][ -  + ]
      95 [ #  # ][ #  # ]:          0 :     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
                 [ #  # ]
      96         [ #  # ]:          0 :                 << "This is a bug.  Please report!\n";
      97                 :          0 :     exit(-1);
      98                 :            :     
      99                 :            :   }
     100 [ +  - ][ +  - ]:      13110 :   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
         [ +  - ][ +  - ]
     101 [ +  - ][ +  - ]:      13110 :   mSettingsList[cs] = setting;
     102                 :      13110 : }
     103                 :            : 
     104                 :            : 
     105                 :       2622 : void SettingHandler::add_setting(const char* name, void (*setFn) (CubitString), 
     106                 :            :                                 CubitString (*getFn)())
     107                 :            : {
     108         [ +  - ]:       2622 :    CubitString cs = name;
     109                 :            :   //Check to see if a setting with the same name already exists
     110 [ +  - ][ +  - ]:       2622 :   if (mSettingsList.find(cs) != mSettingsList.end()) {
         [ +  - ][ -  + ]
     111 [ #  # ][ #  # ]:          0 :     std::cerr << "The " << name << " setting has the same name as another setting.\n" 
                 [ #  # ]
     112         [ #  # ]:          0 :                 << "This is a bug.  Please report!\n";
     113                 :          0 :     exit(-1);
     114                 :            :     
     115                 :            :   }
     116 [ +  - ][ +  - ]:       2622 :   SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
         [ +  - ][ +  - ]
     117 [ +  - ][ +  - ]:       2622 :   mSettingsList[cs] = setting;
     118                 :       2622 : }
     119                 :            : 
     120                 :          0 : void SettingHandler::save_settings(const char *filename)
     121                 :            : {
     122 [ #  # ][ #  # ]:          0 :   CubitFile file(filename, "w");
                 [ #  # ]
     123                 :            :   
     124 [ #  # ][ #  # ]:          0 :   if (!file) {
     125 [ #  # ][ #  # ]:          0 :     std::cerr << "File " << filename << " could not be opened.  Settings not saved." << std::endl;
         [ #  # ][ #  # ]
     126                 :          0 :     return;
     127                 :            :    }
     128                 :            : 
     129                 :            :   //Put a header at the top of the file
     130 [ #  # ][ #  # ]:          0 :   fprintf(file.file(), "#Setting Name                                 Setting Value\n");
     131 [ #  # ][ #  # ]:          0 :   fprintf(file.file(), "#------------------------------------------------------------\n");
     132                 :            : 
     133                 :            :   // Get starting and ending iterators from the map
     134         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
     135         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
     136                 :            :   
     137                 :            :   // Step through the map and build a file to hold the settings
     138 [ #  # ][ #  # ]:          0 :   while(start != end) {
         [ #  # ][ #  # ]
     139         [ #  # ]:          0 :     SettingHolder* setting = (*start).second;
     140                 :            : 
     141                 :            :     //Integer settings
     142         [ #  # ]:          0 :     if (setting->setting_type == 0) {
     143 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
         [ #  # ][ #  # ]
     144                 :            :     }
     145                 :            :     
     146                 :            :     //Double settings
     147         [ #  # ]:          0 :     else if (setting->setting_type == 1) {
     148 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
         [ #  # ][ #  # ]
     149                 :            :     }
     150                 :            : 
     151                 :            :     //Boolean settings
     152         [ #  # ]:          0 :     else if (setting->setting_type == 2) {
     153 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
         [ #  # ][ #  # ]
     154                 :            :     }
     155                 :            :     
     156                 :            :     //String settings
     157         [ #  # ]:          0 :     else if (setting->setting_type == 3) {
     158 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     159                 :            :     }
     160                 :            : 
     161                 :            :     else
     162                 :            :     {
     163                 :          0 :       assert(false);
     164                 :            :       PRINT_ERROR("Error with SettingHolder type!  Please report.");
     165                 :            :     }
     166                 :            : 
     167         [ #  # ]:          0 :     ++start;
     168                 :          0 :   }
     169                 :            : }
     170                 :            : 
     171                 :          0 : void SettingHandler::print_settings()
     172                 :            : {
     173                 :            :   int rows, cols;
     174 [ #  # ][ #  # ]:          0 :   if (!AppUtil::instance()->get_terminal_size(rows, cols))
                 [ #  # ]
     175                 :          0 :     cols = 71;
     176                 :          0 :   --cols;
     177                 :            : 
     178                 :            :   // Get starting and ending iterators from the map
     179         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
     180         [ #  # ]:          0 :   const std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
     181                 :            :   
     182                 :            :   // Step through the map and build a file to hold the settings
     183 [ #  # ][ #  # ]:          0 :   for ( ; iter != end; ++iter ) 
                 [ #  # ]
     184                 :            :   {
     185         [ #  # ]:          0 :     SettingHolder* setting = iter->second;
     186         [ #  # ]:          0 :     int vallen = cols - setting->name.length() - 1;
     187         [ #  # ]:          0 :     if (vallen < 1) vallen = 1;
     188   [ #  #  #  #  :          0 :     switch( setting->setting_type )
                      # ]
     189                 :            :     {
     190                 :            :       case 0:
     191 [ #  # ][ #  # ]:          0 :         PRINT_INFO("%s %*d\n", setting->name.c_str(), vallen,
         [ #  # ][ #  # ]
                 [ #  # ]
     192         [ #  # ]:          0 :             (setting->get_int_function)());
     193                 :          0 :         break;
     194                 :            :       case 1:
     195 [ #  # ][ #  # ]:          0 :         PRINT_INFO("%s %*f\n", setting->name.c_str(), vallen,
         [ #  # ][ #  # ]
                 [ #  # ]
     196         [ #  # ]:          0 :             (setting->get_double_function)());
     197                 :          0 :         break;
     198                 :            :       case 2:
     199 [ #  # ][ #  # ]:          0 :         PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     200         [ #  # ]:          0 :             (setting->get_bool_function)() ? "true" : "false");
     201                 :          0 :         break;
     202                 :            :       case 3:
     203 [ #  # ][ #  # ]:          0 :         PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     204         [ #  # ]:          0 :             (setting->get_string_function)().c_str());
     205                 :          0 :         break;
     206                 :            :       default:
     207 [ #  # ][ #  # ]:          0 :         PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen,
         [ #  # ][ #  # ]
     208         [ #  # ]:          0 :             "ERROR : UNKNOWN SETTING TYPE");
     209                 :            :     }
     210                 :            :   }
     211                 :          0 : }
     212                 :            : 
     213                 :          0 : void SettingHandler::save_settings()
     214                 :            : {
     215                 :            :   
     216                 :          0 :   const char* default_filename = "cubit.settings";
     217                 :            : 
     218 [ #  # ][ #  # ]:          0 :   CubitFile file(default_filename, "w");
                 [ #  # ]
     219                 :            :   
     220 [ #  # ][ #  # ]:          0 :   if (!file) {
     221 [ #  # ][ #  # ]:          0 :     std::cerr << "File " << default_filename << " could not be opened.  Settings not saved." << std::endl;
         [ #  # ][ #  # ]
     222                 :          0 :     return;
     223                 :            :    }
     224                 :            : 
     225                 :            :   //Put a header at the top of the file
     226 [ #  # ][ #  # ]:          0 :   fprintf(file.file(), "#Setting Name                                 Setting Value\n");
     227 [ #  # ][ #  # ]:          0 :   fprintf(file.file(), "#------------------------------------------------------------\n");
     228                 :            : 
     229                 :            :   // Get starting and ending iterators from the map
     230         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
     231         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
     232                 :            : 
     233                 :            :   // Step through the map and build a file to hold the settings
     234 [ #  # ][ #  # ]:          0 :   while(start != end) {
         [ #  # ][ #  # ]
     235         [ #  # ]:          0 :     SettingHolder* setting = (*start).second;
     236                 :            : 
     237                 :            :     //Integer settings
     238         [ #  # ]:          0 :     if (setting->setting_type == 0) {
     239 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
         [ #  # ][ #  # ]
     240                 :            :     }
     241                 :            :     
     242                 :            :     //Double settings
     243         [ #  # ]:          0 :     else if (setting->setting_type == 1) {
     244 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
         [ #  # ][ #  # ]
     245                 :            :     }
     246                 :            : 
     247                 :            :     //Boolean settings
     248         [ #  # ]:          0 :     else if (setting->setting_type == 2) {
     249 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
         [ #  # ][ #  # ]
     250                 :            :     }
     251                 :            :     
     252                 :            :     //String settings
     253         [ #  # ]:          0 :     else if (setting->setting_type == 3) {
     254 [ #  # ][ #  # ]:          0 :       fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     255                 :            :     }
     256                 :            : 
     257                 :            :     else
     258                 :            :     {
     259                 :          0 :       assert(false);
     260                 :            :       PRINT_ERROR("Error with SettingHolder type!  Please report.");
     261                 :            :     }
     262                 :            : 
     263         [ #  # ]:          0 :     ++start;
     264                 :          0 :   }
     265                 :            : }
     266                 :            : 
     267                 :            : 
     268                 :          0 : void SettingHandler::restore_settings(const char* filename)
     269                 :            : {
     270                 :            : 
     271                 :            :   //Open the file for reading
     272 [ #  # ][ #  # ]:          0 :   CubitFile file(filename, "r");
                 [ #  # ]
     273                 :            : 
     274 [ #  # ][ #  # ]:          0 :   if (file) {
     275 [ #  # ][ #  # ]:          0 :     std::cerr << "File " << filename << " could not be opened.  Settings not restored." << std::endl;
         [ #  # ][ #  # ]
     276                 :          0 :     return;
     277                 :            :    }
     278                 :            : 
     279                 :            :   //Read the first 2 lines of the file, we know they are not settings
     280                 :            :   //so just get rid of them
     281                 :            :   char junk[100];
     282 [ #  # ][ #  # ]:          0 :   fgets(junk, 100, file.file());
     283 [ #  # ][ #  # ]:          0 :   fgets(junk, 100, file.file());
     284                 :            : 
     285                 :            :   char name[51]; //Allocate 50 bytes for the characters and 1 for a null termination
     286 [ #  # ][ #  # ]:          0 :   CubitString cubit_name; //A CubitString that will be used for checking if the setting
         [ #  # ][ #  # ]
     287                 :            :                           //is in the map.
     288                 :            :   char value[120]; //This will be used when getting the value of a setting
     289                 :            :   SettingHolder* setting;
     290                 :          0 :   int int_value = -1;
     291                 :          0 :   double double_value = 0.0;
     292                 :          0 :   CubitBoolean bool_value = CUBIT_FALSE;
     293 [ #  # ][ #  # ]:          0 :   CubitString string_value = "";
     294                 :            : 
     295 [ #  # ][ #  # ]:          0 :   while (!feof(file.file())) {
     296                 :            : 
     297                 :            :   //Get the setting name.  This will be in the first 50 characters of a line
     298 [ #  # ][ #  # ]:          0 :   fgets(name, 51, file.file());
     299                 :            : 
     300 [ #  # ][ #  # ]:          0 :   cubit_name = name;
                 [ #  # ]
     301         [ #  # ]:          0 :   cubit_name.trim();
     302                 :            : 
     303                 :            :   //Find the setting in the map
     304         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator temp;
     305                 :            : 
     306         [ #  # ]:          0 :   temp = mSettingsList.find(cubit_name);
     307                 :            :   
     308                 :            :     //Read in the rest of the line no matter what
     309 [ #  # ][ #  # ]:          0 :   fgets(value, 120, file.file());
     310                 :            :     
     311 [ #  # ][ #  # ]:          0 :   if (temp == mSettingsList.end()) {
                 [ #  # ]
     312 [ #  # ][ #  # ]:          0 :     std::cerr << "Setting " << cubit_name.c_str() << " was not found." << std::endl;  
         [ #  # ][ #  # ]
                 [ #  # ]
     313                 :            :   }
     314                 :            :   else {
     315         [ #  # ]:          0 :     setting = (*temp).second;
     316                 :            :     //Integer Settings
     317         [ #  # ]:          0 :     if (setting->setting_type == 0) {
     318                 :          0 :       int_value = atoi(value);
     319         [ #  # ]:          0 :       (setting->set_int_function)(int_value);
     320                 :            :     }
     321                 :            :     
     322                 :            :     //Double Settings
     323         [ #  # ]:          0 :     if (setting->setting_type == 1) {
     324                 :          0 :       double_value = atof(value);
     325         [ #  # ]:          0 :       (setting->set_double_function)(double_value);
     326                 :            :     }    
     327                 :            : 
     328                 :            :     //Boolean Settings
     329         [ #  # ]:          0 :     if (setting->setting_type == 2) {
     330                 :            : //      bool_value = (CubitBoolean)(atoi(value));
     331                 :          0 :       bool_value = (atoi(value)) ? true : false;
     332         [ #  # ]:          0 :       (setting->set_bool_function)(bool_value);
     333                 :            :     }    
     334                 :            :     
     335                 :            :     //CubitString Settings
     336         [ #  # ]:          0 :     if (setting->setting_type == 3) {
     337 [ #  # ][ #  # ]:          0 :       string_value = value;
                 [ #  # ]
     338 [ #  # ][ #  # ]:          0 :       (setting->set_string_function)(string_value);
                 [ #  # ]
     339                 :            :     }     
     340                 :            : 
     341                 :            :   }
     342                 :            : 
     343                 :          0 :   } //End while
     344                 :            : 
     345                 :            : }
     346                 :            : 
     347                 :          0 : SettingHolder* SettingHandler::get_setting_holder(CubitString name)
     348                 :            : { 
     349         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator it = mSettingsList.find(name);
     350 [ #  # ][ #  # ]:          0 :   return it  ==  mSettingsList.end()  ? NULL : it->second;
         [ #  # ][ #  # ]
     351                 :            : }
     352                 :            :  
     353                 :          0 : void SettingHandler::get_settings_list(std::vector< std::pair<CubitString, SettingHolder*> > &list)
     354                 :            : {
     355                 :            :   // return a list of the settings
     356         [ #  # ]:          0 :   std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
     357 [ #  # ][ #  # ]:          0 :   while(iter != mSettingsList.end())
                 [ #  # ]
     358                 :            :   {
     359         [ #  # ]:          0 :     std::pair<CubitString, SettingHolder*>  tmp_pair;
     360 [ #  # ][ #  # ]:          0 :     CubitString key = (*iter).first;
                 [ #  # ]
     361         [ #  # ]:          0 :     tmp_pair.first = key;
     362         [ #  # ]:          0 :     tmp_pair.second = (*iter).second;
     363         [ #  # ]:          0 :     list.push_back( (tmp_pair) );
     364         [ #  # ]:          0 :     iter++;
     365         [ #  # ]:          0 :   }
     366 [ +  - ][ +  - ]:       6540 : }
     367                 :            : 
     368                 :            : 
     369                 :            : 
     370                 :            : 
     371                 :            : 
     372                 :            : 
     373                 :            : 

Generated by: LCOV version 1.11