1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <cassert><--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#include <cstdio>
#include <cstring>

#include <iostream>

#include "SettingHandler.hpp"
#include "CubitDefines.h"
#include "CubitString.hpp"
#include "CubitUtil.hpp"
#include "CubitMessage.hpp"
#include "AppUtil.hpp"
#include "CubitFile.hpp"


SettingHandler* SettingHandler::instance_ = NULL;

SettingHandler* SettingHandler::instance()
{	
  if (!instance_)
    {	
      instance_ = new SettingHandler;
      if (!instance_)
	{
	  std::cerr << " *** Unable to instantiate setting_handler object ***" << std::endl;
	  exit(1);
	}
    }
  return instance_;
}

//Default Constructor
SettingHandler::SettingHandler() 
{
  debug_flags_added = CUBIT_FALSE;
} 

void SettingHandler::delete_instance() 
{
  if (instance_)
  {
    delete instance_;
    instance_ = 0;
  }
}

SettingHandler::~SettingHandler() 
{
    std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
  std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();

  for( ; iter != end; ++iter)
    delete (*iter).second;
  mSettingsList.clear();
}

void SettingHandler::add_setting(const char* name, void (*setFn) (int), 
				int (*getFn)())
{
  CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
		<< "This is a bug.  Please report!\n";
    exit(-1);
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}


void SettingHandler::add_setting(const char* name, void (*setFn) (double), 
				double (*getFn)())
{
  CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
		<< "This is a bug.  Please report!\n";
    exit(-1);
    
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}


void SettingHandler::add_setting(const char* name, void (*setFn) (CubitBoolean), 
				CubitBoolean (*getFn)())
{
   CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
		<< "This is a bug.  Please report!\n";
    exit(-1);
    
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}


void SettingHandler::add_setting(const char* name, void (*setFn) (CubitString), 
				CubitString (*getFn)())
{
   CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
		<< "This is a bug.  Please report!\n";
    exit(-1);
    
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}

void SettingHandler::save_settings(const char *filename)
{<--- The function 'save_settings' is never used.
  CubitFile file(filename, "w");
  
  if (!file) {
    std::cerr << "File " << filename << " could not be opened.  Settings not saved." << std::endl;
    return;
   }

  //Put a header at the top of the file
  fprintf(file.file(), "#Setting Name                                 Setting Value\n");
  fprintf(file.file(), "#------------------------------------------------------------\n");

  // Get starting and ending iterators from the map
  std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
  std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
  
  // Step through the map and build a file to hold the settings
  while(start != end) {
    SettingHolder* setting = (*start).second;

    //Integer settings
    if (setting->setting_type == 0) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
    }
    
    //Double settings
    else if (setting->setting_type == 1) {
      fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
    }

    //Boolean settings
    else if (setting->setting_type == 2) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
    }
    
    //String settings
    else if (setting->setting_type == 3) {
      fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
    }

    else
    {
      assert(false);
      PRINT_ERROR("Error with SettingHolder type!  Please report.");
    }

    ++start;
  }
}

void SettingHandler::print_settings()
{<--- The function 'print_settings' is never used.
  int rows, cols;
  if (!AppUtil::instance()->get_terminal_size(rows, cols))
    cols = 71;
  --cols;

  // Get starting and ending iterators from the map
  std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
  const std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
  
  // Step through the map and build a file to hold the settings
  for ( ; iter != end; ++iter ) 
  {
    SettingHolder* setting = iter->second;
    int vallen = cols - setting->name.length() - 1;
    if (vallen < 1) vallen = 1;
    switch( setting->setting_type )
    {
      case 0:
        PRINT_INFO("%s %*d\n", setting->name.c_str(), vallen,
            (setting->get_int_function)());
        break;
      case 1:
        PRINT_INFO("%s %*f\n", setting->name.c_str(), vallen,
            (setting->get_double_function)());
        break;
      case 2:
        PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
            (setting->get_bool_function)() ? "true" : "false");
        break;
      case 3:
        PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
            (setting->get_string_function)().c_str());
        break;
      default:
        PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen,
            "ERROR : UNKNOWN SETTING TYPE");
    }
  }
}

void SettingHandler::save_settings()
{
  
  const char* default_filename = "cubit.settings";

  CubitFile file(default_filename, "w");
  
  if (!file) {
    std::cerr << "File " << default_filename << " could not be opened.  Settings not saved." << std::endl;
    return;
   }

  //Put a header at the top of the file
  fprintf(file.file(), "#Setting Name                                 Setting Value\n");
  fprintf(file.file(), "#------------------------------------------------------------\n");

  // Get starting and ending iterators from the map
  std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
  std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();

  // Step through the map and build a file to hold the settings
  while(start != end) {
    SettingHolder* setting = (*start).second;

    //Integer settings
    if (setting->setting_type == 0) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
    }
    
    //Double settings
    else if (setting->setting_type == 1) {
      fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
    }

    //Boolean settings
    else if (setting->setting_type == 2) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
    }
    
    //String settings
    else if (setting->setting_type == 3) {
      fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
    }

    else
    {
      assert(false);
      PRINT_ERROR("Error with SettingHolder type!  Please report.");
    }

    ++start;
  }
}


void SettingHandler::restore_settings(const char* filename)
{<--- The function 'restore_settings' is never used.

  //Open the file for reading
  CubitFile file(filename, "r");

  if (file) {
    std::cerr << "File " << filename << " could not be opened.  Settings not restored." << std::endl;
    return;
   }

  //Read the first 2 lines of the file, we know they are not settings
  //so just get rid of them
  char junk[100];
  fgets(junk, 100, file.file());
  fgets(junk, 100, file.file());

  char name[51]; //Allocate 50 bytes for the characters and 1 for a null termination
  CubitString cubit_name; //A CubitString that will be used for checking if the setting
                          //is in the map.
  char value[120]; //This will be used when getting the value of a setting
  SettingHolder* setting;
  int int_value = -1;
  double double_value = 0.0;
  CubitBoolean bool_value = CUBIT_FALSE;
  CubitString string_value = "";

  while (!feof(file.file())) {

  //Get the setting name.  This will be in the first 50 characters of a line
  fgets(name, 51, file.file());

  cubit_name = name;
  cubit_name.trim();

  //Find the setting in the map
  std::map<CubitString, SettingHolder*>::iterator temp;

  temp = mSettingsList.find(cubit_name);
  
    //Read in the rest of the line no matter what
  fgets(value, 120, file.file());
    
  if (temp == mSettingsList.end()) {
    std::cerr << "Setting " << cubit_name.c_str() << " was not found." << std::endl;  
  }
  else {
    setting = (*temp).second;
    //Integer Settings
    if (setting->setting_type == 0) {
      int_value = atoi(value);
      (setting->set_int_function)(int_value);
    }
    
    //Double Settings
    if (setting->setting_type == 1) {
      double_value = atof(value);
      (setting->set_double_function)(double_value);
    }    

    //Boolean Settings
    if (setting->setting_type == 2) {
//      bool_value = (CubitBoolean)(atoi(value));
      bool_value = (atoi(value)) ? true : false;
      (setting->set_bool_function)(bool_value);
    }    
    
    //CubitString Settings
    if (setting->setting_type == 3) {
      string_value = value;
      (setting->set_string_function)(string_value);
    }     

  }

  } //End while

}

SettingHolder* SettingHandler::get_setting_holder(CubitString name)
{ <--- The function 'get_setting_holder' is never used.
  std::map<CubitString, SettingHolder*>::iterator it = mSettingsList.find(name);
  return it  ==  mSettingsList.end()  ? NULL : it->second;
}
 
void SettingHandler::get_settings_list(std::vector< std::pair<CubitString, SettingHolder*> > &list)
{<--- The function 'get_settings_list' is never used.
  // return a list of the settings
  std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
  while(iter != mSettingsList.end())
  {
    std::pair<CubitString, SettingHolder*>  tmp_pair;
    CubitString key = (*iter).first;
    tmp_pair.first = key;
    tmp_pair.second = (*iter).second;
    list.push_back( (tmp_pair) );
    iter++;
  }<--- Prefer prefix ++/-- operators for non-primitive types.
}