From 07d35e662dc76fa7702d54f4a9b3ced3dac3c969 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 19 Jun 2002 15:21:49 -0400 Subject: ENH: Added cmStringStream class to wrap std::stringstream or std::strstream depending on the platform. The interface is that of std::stringstream, so no "ends" or "rdbuf()->freeze(0)" lines are needed. --- Source/cmBorlandMakefileGenerator.cxx | 8 +--- Source/cmCMakeMinimumRequired.cxx | 7 ++-- Source/cmConfigure.cmake.h.in | 1 + Source/cmConfigure.h.in | 1 + Source/cmNMakeMakefileGenerator.cxx | 7 +--- Source/cmStandardIncludes.h | 68 +++++++++++++++++++++++++++++---- Source/cmSystemTools.cxx | 19 ++++----- Source/cmUnixMakefileGenerator.cxx | 11 ++---- Source/cmVTKMakeInstantiatorCommand.cxx | 5 +-- Source/cmake.cxx | 16 ++++---- 10 files changed, 89 insertions(+), 54 deletions(-) diff --git a/Source/cmBorlandMakefileGenerator.cxx b/Source/cmBorlandMakefileGenerator.cxx index b064364..f996008 100644 --- a/Source/cmBorlandMakefileGenerator.cxx +++ b/Source/cmBorlandMakefileGenerator.cxx @@ -251,12 +251,10 @@ void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout, command += "-e"; command += target; command += " "; - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, name, t); - linklibs << std::ends; // then the linker options -L and libraries (any other order will fail!) command += linklibs.str(); - delete [] linklibs.str(); // then list of object files command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; std::string command2 = "implib -w "; @@ -353,11 +351,9 @@ void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout, { command += " -tWC "; } - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, 0, t); - linklibs << std::ends; command += linklibs.str(); - delete [] linklibs.str(); command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")"; std::string comment = "rule to build executable: "; diff --git a/Source/cmCMakeMinimumRequired.cxx b/Source/cmCMakeMinimumRequired.cxx index 6396532..4b7c9f5 100644 --- a/Source/cmCMakeMinimumRequired.cxx +++ b/Source/cmCMakeMinimumRequired.cxx @@ -35,11 +35,10 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector const& args) sscanf(args[1].c_str(), "%f", &reqVersion); if(reqVersion > version) { - std::strstream str; + cmStringStream str; str << "WARNING: This project requires version: " << args[1].c_str() << " of cmake.\n" - << "You are running version: " << version << std::ends; - cmSystemTools::Message(str.str()); - delete [] str.str(); + << "You are running version: " << version; + cmSystemTools::Message(str.str().c_str()); } return true; } diff --git a/Source/cmConfigure.cmake.h.in b/Source/cmConfigure.cmake.h.in index af1b410..ffff9e0 100644 --- a/Source/cmConfigure.cmake.h.in +++ b/Source/cmConfigure.cmake.h.in @@ -1,5 +1,6 @@ #cmakedefine CMAKE_NO_STD_NAMESPACE #cmakedefine CMAKE_NO_ANSI_STREAM_HEADERS +#cmakedefine CMAKE_NO_ANSI_STRING_STREAM #cmakedefine CMAKE_NO_ANSI_FOR_SCOPE #define CMAKE_ROOT_DIR "${CMake_SOURCE_DIR}" #define CMAKE_BUILD_DIR "${CMake_BINARY_DIR}" diff --git a/Source/cmConfigure.h.in b/Source/cmConfigure.h.in index 99595fc..bff2d77 100644 --- a/Source/cmConfigure.h.in +++ b/Source/cmConfigure.h.in @@ -1,4 +1,5 @@ #undef CMAKE_NO_STD_NAMESPACE #undef CMAKE_NO_ANSI_STREAM_HEADERS +#undef CMAKE_NO_ANSI_STRING_STREAM #undef CMAKE_NO_ANSI_FOR_SCOPE diff --git a/Source/cmNMakeMakefileGenerator.cxx b/Source/cmNMakeMakefileGenerator.cxx index f34c390..5059d9f 100644 --- a/Source/cmNMakeMakefileGenerator.cxx +++ b/Source/cmNMakeMakefileGenerator.cxx @@ -431,11 +431,9 @@ void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout, command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, name, t); - linklibs << std::ends; command += linklibs.str(); - delete [] linklibs.str(); const std::vector& sources = t.GetSourceFiles(); for(std::vector::const_iterator i = sources.begin(); @@ -541,9 +539,8 @@ void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout, command += " /subsystem:windows "; } - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, 0, t); - linklibs << std::ends; command += linklibs.str(); std::string comment = "rule to build executable: "; diff --git a/Source/cmStandardIncludes.h b/Source/cmStandardIncludes.h index 05505da..629f9c0 100644 --- a/Source/cmStandardIncludes.h +++ b/Source/cmStandardIncludes.h @@ -38,13 +38,19 @@ #endif #ifndef CMAKE_NO_ANSI_STREAM_HEADERS -#include -#include -#include +# include +# include #else -#include -#include -#include +# include +# include +#endif + +#if !defined(CMAKE_NO_ANSI_STRING_STREAM) +# include +#elif !defined(CMAKE_NO_ANSI_STREAM_HEADERS) +# include +#else +# include #endif // we must have stl with the standard include style @@ -100,7 +106,13 @@ using ::cerr; using ::cin; using ::ifstream; using ::ofstream; -using ::strstream; + +#if !defined(CMAKE_NO_ANSI_STRING_STREAM) + using ::stringstream; +#else + using ::strstream; +#endif + using ::endl; using ::ends; using ::flush; @@ -136,5 +148,45 @@ struct cmStdString : public std::string cmStdString(const StdString& s, size_type pos=0, size_type n=npos): StdString(s, pos, n) {} }; - + +// Define cmStringStream wrapper to hide differences between +// std::stringstream and the old strstream. +#if !defined(CMAKE_NO_ANSI_STRING_STREAM) +class cmStringStream: public std::stringstream +{ +public: + cmStringStream() {} +private: + cmStringStream(const cmStringStream&); + void operator=(const cmStringStream&); +}; +#else +class cmStrStreamCleanup +{ +public: + cmStrStreamCleanup(std::strstream& ostr): m_StrStream(ostr) {} + ~cmStrStreamCleanup() { m_StrStream.rdbuf()->freeze(0); } + static void IgnoreUnusedVariable(const cmStrStreamCleanup&) {} +protected: + std::strstream& m_StrStream; +}; + +class cmStringStream: public std::strstream +{ +public: + typedef std::strstream Superclass; + cmStringStream() {} + std::string str() + { + cmStrStreamCleanup cleanup(*this); + cmStrStreamCleanup::IgnoreUnusedVariable(cleanup); + const char* ptr = this->Superclass::str(); + return std::string(ptr, ptr+this->pcount()); + } +private: + cmStringStream(const cmStringStream&); + void operator=(const cmStringStream&); +}; +#endif + #endif diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index c357c3e..5730eb8 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -1068,12 +1068,11 @@ bool cmSystemTools::FilesDiffer(const char* source, if(statSource.st_size != static_cast(finSource.gcount()) || statSource.st_size != static_cast(finDestination.gcount())) { - std::strstream msg; + cmStringStream msg; msg << "FilesDiffer failed to read files (allocated: " << statSource.st_size << ", read source: " << finSource.gcount() - << ", read dest: " << finDestination.gcount() << std::ends; - cmSystemTools::Error(msg.str()); - delete [] msg.str(); + << ", read dest: " << finDestination.gcount(); + cmSystemTools::Error(msg.str().c_str()); delete [] source_buf; delete [] dest_buf; return false; @@ -1176,12 +1175,10 @@ void cmSystemTools::cmCopyFile(const char* source, if (statSource.st_size != statDestination.st_size) { - std::strstream msg; + cmStringStream msg; msg << "CopyFile failed to copy files (sizes differ, source: " - << statSource.st_size << " , dest: " << statDestination.st_size - << std::ends; - cmSystemTools::Error(msg.str()); - delete [] msg.str(); + << statSource.st_size << " , dest: " << statDestination.st_size; + cmSystemTools::Error(msg.str().c_str()); } } @@ -1427,7 +1424,7 @@ bool cmSystemTools::RunCommand(const char* command, if (WIFSIGNALED(retVal)) { retVal = WTERMSIG(retVal); - std::strstream error; + cmStringStream error; error << "\nProcess terminated due to "; switch (retVal) { @@ -1455,9 +1452,7 @@ bool cmSystemTools::RunCommand(const char* command, error << "signal " << retVal; break; } - error << std::ends; output += error.str(); - error.rdbuf()->freeze(0); } return false; #endif diff --git a/Source/cmUnixMakefileGenerator.cxx b/Source/cmUnixMakefileGenerator.cxx index 31e5085..c8ed941 100644 --- a/Source/cmUnixMakefileGenerator.cxx +++ b/Source/cmUnixMakefileGenerator.cxx @@ -655,11 +655,9 @@ void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout, libName = this->ConvertToOutputPath(libName.c_str()); command2 += libName + " \\\n"; command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, name, t); - linklibs << std::ends; command2 += linklibs.str(); - delete [] linklibs.str(); std::string customCommands = this->CreateTargetRules(t, name); const char* cc = 0; if(customCommands.size() > 0) @@ -699,11 +697,9 @@ void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout, libName = this->ConvertToOutputPath(libName.c_str()); command2 += libName + " \\\n"; command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t); - linklibs << std::ends; command2 += linklibs.str(); - delete [] linklibs.str(); std::string customCommands = this->CreateTargetRules(t, name); const char* cc = 0; if(customCommands.size() > 0) @@ -778,9 +774,8 @@ void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout, "$(CMAKE_C_COMPILER) $(CMAKE_C_SHLIB_LINK_FLAGS) $(CMAKE_C_FLAGS) "; } command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; - std::strstream linklibs; + cmStringStream linklibs; this->OutputLinkLibraries(linklibs, 0, t); - linklibs << std::ends; command += linklibs.str(); std::string outputFile = m_ExecutableOutputPath + name; command += " -o " + this->ConvertToOutputPath(outputFile.c_str()); diff --git a/Source/cmVTKMakeInstantiatorCommand.cxx b/Source/cmVTKMakeInstantiatorCommand.cxx index a8b3ed0..d649117 100644 --- a/Source/cmVTKMakeInstantiatorCommand.cxx +++ b/Source/cmVTKMakeInstantiatorCommand.cxx @@ -216,10 +216,9 @@ cmVTKMakeInstantiatorCommand std::string cmVTKMakeInstantiatorCommand::GenerateCreationFileName(unsigned int block) { - std::strstream nameStr; - nameStr << m_ClassName.c_str() << block << ".cxx" << std::ends; + cmStringStream nameStr; + nameStr << m_ClassName.c_str() << block << ".cxx"; std::string result = nameStr.str(); - delete [] nameStr.str(); return result; } diff --git a/Source/cmake.cxx b/Source/cmake.cxx index a24ddde..b93a62c 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -43,7 +43,7 @@ cmake::cmake() void cmake::Usage(const char* program) { - std::strstream errorStream; + cmStringStream errorStream; errorStream << "cmake version " << cmMakefile::GetMajorVersion() << "." << cmMakefile::GetMinorVersion() << "\n"; @@ -61,9 +61,9 @@ void cmake::Usage(const char* program) { errorStream << "\"" << i->c_str() << "\" "; } - errorStream << ")\n" << std::ends; + errorStream << ")\n"; - cmSystemTools::Error(errorStream.str()); + cmSystemTools::Error(errorStream.str().c_str()); } // Parse the args @@ -519,7 +519,7 @@ int cmake::Generate(const std::vector& args, bool buildMakefiles) void CMakeCommandUsage(const char* program) { - std::strstream errorStream; + cmStringStream errorStream; errorStream << "cmake version " << cmMakefile::GetMajorVersion() @@ -530,14 +530,14 @@ void CMakeCommandUsage(const char* program) << "Available commands: \n" << " copy file destination - copy file to destination (either file or directory)\n" << " remove file1 file2 ... - remove the file(s)\n" - << " time command [args] ... - run command and return elapsed time\n" + << " time command [args] ... - run command and return elapsed time\n"; #if defined(_WIN32) && !defined(__CYGWIN__) + errorStream << " write_regv key value - write registry value\n" - << " delete_regv key - delete registry value\n" + << " delete_regv key - delete registry value\n"; #endif - << std::ends; - cmSystemTools::Error(errorStream.str()); + cmSystemTools::Error(errorStream.str().c_str()); } int cmake::CMakeCommand(std::vector& args) -- cgit v0.12