From: <pst...@us...> - 2011-07-26 18:23:30
|
Revision: 841 http://jazzplusplus.svn.sourceforge.net/jazzplusplus/?rev=841&view=rev Author: pstieber Date: 2011-07-26 18:23:24 +0000 (Tue, 26 Jul 2011) Log Message: ----------- 1. Updated the char* version of JZConfiguration::Get to use a string reference. 2. Changed JZConfiguration::Get and JZConfiguration::Put to use C++ stream I/O and std::string instead of C FILE pointers and C-style strings. Modified Paths: -------------- trunk/jazz/src/Configuration.cpp trunk/jazz/src/Configuration.h Modified: trunk/jazz/src/Configuration.cpp =================================================================== --- trunk/jazz/src/Configuration.cpp 2011-03-29 02:36:45 UTC (rev 840) +++ trunk/jazz/src/Configuration.cpp 2011-07-26 18:23:24 UTC (rev 841) @@ -543,7 +543,7 @@ //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -bool JZConfiguration::Get(int Entry, char* value) +bool JZConfiguration::Get(int Entry, string& Value) { assert((Entry >= 0) && (Entry < NumConfigNames)); @@ -553,42 +553,64 @@ return false; } - FILE *fd = fopen(FileName.c_str(), "r"); - const string& name = GetName(Entry); + ifstream Ifs(FileName.c_str()); + if (!Ifs) + { + return false; + } - int len = name.length(); - char buf[1000]; - bool found = false; - while (!found && fgets(buf, sizeof(buf), fd) != NULL) + const string& Name = GetName(Entry); + + string::size_type Length = Name.length(); + + string Line; + + bool Found = false; + while (!Found && !Ifs.eof() && getline(Ifs, Line)) { - if (strncmp(buf, name.c_str(), len) == 0) + // Search the beginning of the string for the name. + string::size_type Start = Line.find(Name); + if (Start == 0) { - while (isspace(buf[len])) + // Skip white space to find the value. + Start += Length; + while (isspace(Line[Start])) { - len++; + ++Start; } - int end = strlen(buf) - 1; - while (end > 0 && isspace(buf[end])) + + // Find the end of the value. + string::size_type End = Line.length() - 1; + while (End > 0 && isspace(Line[End])) { - buf[end--] = 0; + --End; } - strcpy(value, buf + len); - found = true; + + Value = Line.substr(Start, End - Start + 1); + + // Indicate a value was found. + Found = true; } } - fclose(fd); - return found; + + Ifs.close(); + + return Found; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -bool JZConfiguration::Get(int Entry, int& value) +bool JZConfiguration::Get(int Entry, int& Value) { - char buf[512]; - if (Get(Entry, buf)) + string String; + if (Get(Entry, String)) { - sscanf(buf, " %d ", &value); - return true; + istringstream Iss(String); + Iss >> Value; + if (!Iss.fail()) + { + return true; + } } return false; } @@ -615,35 +637,39 @@ string TempFileName(FileName); TempFileName.append(".tmp"); ofstream Os(TempFileName.c_str()); -// ofstream Os(TempFileName.GetFullPath()); if (!Os) { return false; } - FILE* inp = fopen(FileName.c_str(), "r"); + ifstream Ifs(FileName.c_str()); + if (!Ifs) + { + return false; + } + const string& ValueName = GetName(Index); - int len = ValueName.length(); - char buf[1000]; - bool found = false; - while (fgets(buf, sizeof(buf), inp) != NULL) + string Line; + bool Found = false; + while (!Ifs.eof() && getline(Ifs, Line)) { - if (strncmp(buf, ValueName.c_str(), len) == 0) + string::size_type Start = Line.find(ValueName); + if (Start == 0) { Os << ValueName << ' ' << ValueString << endl; - found = true; + Found = true; } else { - Os << buf; + Os << Line << endl; } } - if (!found) + if (!Found) { Os << ValueName << ' ' << ValueString << endl; } - fclose(inp); + Ifs.close(); Os.close(); ::wxRemoveFile(FileName.c_str()); @@ -845,7 +871,7 @@ } else if (pVector && isdigit(InputLine[0])) { - // Read named value entries. + // Read named entries. // Voice names if (pVector == &mVoiceNames) Modified: trunk/jazz/src/Configuration.h =================================================================== --- trunk/jazz/src/Configuration.h 2011-03-29 02:36:45 UTC (rev 840) +++ trunk/jazz/src/Configuration.h 2011-07-26 18:23:24 UTC (rev 841) @@ -239,7 +239,7 @@ const int& GetValue(const char* pName) const; const int& GetValue(int Index) const; - bool Get(int Entry, char* pValue); + bool Get(int Entry, std::string& Value); bool Get(int Entry, int& Value); bool Put(int Entry, const std::string& ValueString); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |