📄 inifile.cc
字号:
//// inifile.cc - A general inifile class//// author: J.I.v.Hemert// last updated : 08-10-1997//// This file implements the class IniFileC, which can be used to read in// values (strings, doubles and integers) from an external inifile. It// still has some constraints. Labels cannot be longer than 300 characters// and the number of labels cannot exceed 50. The inifile should consist of// lines like this : "value = 0". The spaces around the equalsign must be// there. Empty lines are ignored, just as lines starting with an #.//#include "inifile.h"void IniFileC::Open (StringT filename)// Open the inifile and read in all the labels with// corresponding values.{ int i = 0; char equalsine; nroflabels = 0; strcpy (name, filename); filedata.open (filename); if (!filedata.is_open ()) { cerr << "Error: The inifile \"" << filename << "\" could not be opened" << endl; exit (1); } // Read in all the label and their values while ((i < MAXLABELS) && (!filedata.eof ())) { filedata >> labels[nroflabels]; // Kill comments of the form: # comment if (labels[nroflabels][0] == '#') { do filedata.get (equalsine); while (equalsine != '\n'); } else if (labels[nroflabels][0] != '\0') { filedata >> equalsine; filedata >> data[nroflabels]; nroflabels++; } i++; } if (!filedata.eof ()) cerr << "Warning: file \"" << name << "\" contains more lines then could be read, the maximum of lines allowed is " << MAXLABELS << endl; filedata.close ();} // Open ()char * IniFileC::ReadString (StringT label)// Return a _pointer_ to the string corresponding to the value// belonging to the label.{ int i = 0; while ((strcmp (label, labels[i]) != 0) && (i < nroflabels)) i++; if (i < nroflabels) return (data[i]); else { cerr << "label " << label << " not found in \"" << name << "\"" << endl; return (NULL); }} // ReadString ()double IniFileC::ReadDouble (StringT label)// Return a _double_ beloning to the label.{ int i = 0; while ((strcmp (label, labels[i]) != 0) && (i < nroflabels)) i++; if (i < nroflabels) return (atof (data[i])); else { cerr << "label " << label << " not found in \"" << name << "\"" << endl; return (0); }} // ReadDouble ()int IniFileC::ReadInt (StringT label)// Return a _integer_ beloning to the label.{ int i = 0; while ((strcmp (label, labels[i]) != 0) && (i < nroflabels)) i++; if (i < nroflabels) return (atoi(data[i])); else { cerr << "label " << label << " not found in \"" << name << "\"" << endl; return (0); }} // ReadInt ()bool IniFileC::ReadBool (StringT label)// Return a _boolean_ beloning to the label.{ int i; char * tmp; tmp = (char *) malloc (sizeof (char) * MAXSTRINGLENGTH); strcpy (tmp, label); i = ReadInt (tmp); free (tmp); switch (i) { case 0: return (false); case 1: return (true); default: cerr << "Warning: label \"" << label << "\" in \"" << name << "\" doesn't have a boolean (0 or 1) value, choosing false" << endl; return (false); }} // ReadBool ()// eof inifile.cc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -