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.

This commit is contained in:
Brad King 2002-06-19 15:21:49 -04:00
parent 27a2cad0fc
commit 07d35e662d
10 changed files with 89 additions and 54 deletions

View File

@ -251,12 +251,10 @@ void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
command += "-e"; command += "-e";
command += target; command += target;
command += " "; command += " ";
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, name, t); this->OutputLinkLibraries(linklibs, name, t);
linklibs << std::ends;
// then the linker options -L and libraries (any other order will fail!) // then the linker options -L and libraries (any other order will fail!)
command += linklibs.str(); command += linklibs.str();
delete [] linklibs.str();
// then list of object files // then list of object files
command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
std::string command2 = "implib -w "; std::string command2 = "implib -w ";
@ -353,11 +351,9 @@ void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout,
{ {
command += " -tWC "; command += " -tWC ";
} }
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, 0, t); this->OutputLinkLibraries(linklibs, 0, t);
linklibs << std::ends;
command += linklibs.str(); command += linklibs.str();
delete [] linklibs.str();
command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")"; command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
std::string comment = "rule to build executable: "; std::string comment = "rule to build executable: ";

View File

@ -35,11 +35,10 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args)
sscanf(args[1].c_str(), "%f", &reqVersion); sscanf(args[1].c_str(), "%f", &reqVersion);
if(reqVersion > version) if(reqVersion > version)
{ {
std::strstream str; cmStringStream str;
str << "WARNING: This project requires version: " << args[1].c_str() << " of cmake.\n" str << "WARNING: This project requires version: " << args[1].c_str() << " of cmake.\n"
<< "You are running version: " << version << std::ends; << "You are running version: " << version;
cmSystemTools::Message(str.str()); cmSystemTools::Message(str.str().c_str());
delete [] str.str();
} }
return true; return true;
} }

View File

@ -1,5 +1,6 @@
#cmakedefine CMAKE_NO_STD_NAMESPACE #cmakedefine CMAKE_NO_STD_NAMESPACE
#cmakedefine CMAKE_NO_ANSI_STREAM_HEADERS #cmakedefine CMAKE_NO_ANSI_STREAM_HEADERS
#cmakedefine CMAKE_NO_ANSI_STRING_STREAM
#cmakedefine CMAKE_NO_ANSI_FOR_SCOPE #cmakedefine CMAKE_NO_ANSI_FOR_SCOPE
#define CMAKE_ROOT_DIR "${CMake_SOURCE_DIR}" #define CMAKE_ROOT_DIR "${CMake_SOURCE_DIR}"
#define CMAKE_BUILD_DIR "${CMake_BINARY_DIR}" #define CMAKE_BUILD_DIR "${CMake_BINARY_DIR}"

View File

@ -1,4 +1,5 @@
#undef CMAKE_NO_STD_NAMESPACE #undef CMAKE_NO_STD_NAMESPACE
#undef CMAKE_NO_ANSI_STREAM_HEADERS #undef CMAKE_NO_ANSI_STREAM_HEADERS
#undef CMAKE_NO_ANSI_STRING_STREAM
#undef CMAKE_NO_ANSI_FOR_SCOPE #undef CMAKE_NO_ANSI_FOR_SCOPE

View File

@ -431,11 +431,9 @@ void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, name, t); this->OutputLinkLibraries(linklibs, name, t);
linklibs << std::ends;
command += linklibs.str(); command += linklibs.str();
delete [] linklibs.str();
const std::vector<cmSourceFile*>& sources = t.GetSourceFiles(); const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
for(std::vector<cmSourceFile*>::const_iterator i = sources.begin(); for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
@ -541,9 +539,8 @@ void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
command += " /subsystem:windows "; command += " /subsystem:windows ";
} }
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, 0, t); this->OutputLinkLibraries(linklibs, 0, t);
linklibs << std::ends;
command += linklibs.str(); command += linklibs.str();
std::string comment = "rule to build executable: "; std::string comment = "rule to build executable: ";

View File

@ -38,13 +38,19 @@
#endif #endif
#ifndef CMAKE_NO_ANSI_STREAM_HEADERS #ifndef CMAKE_NO_ANSI_STREAM_HEADERS
#include <fstream> # include <fstream>
#include <iostream> # include <iostream>
#include <strstream>
#else #else
#include <fstream.h> # include <fstream.h>
#include <iostream.h> # include <iostream.h>
#include <strstream.h> #endif
#if !defined(CMAKE_NO_ANSI_STRING_STREAM)
# include <sstream>
#elif !defined(CMAKE_NO_ANSI_STREAM_HEADERS)
# include <strstream>
#else
# include <strstream.h>
#endif #endif
// we must have stl with the standard include style // we must have stl with the standard include style
@ -100,7 +106,13 @@ using ::cerr;
using ::cin; using ::cin;
using ::ifstream; using ::ifstream;
using ::ofstream; using ::ofstream;
using ::strstream;
#if !defined(CMAKE_NO_ANSI_STRING_STREAM)
using ::stringstream;
#else
using ::strstream;
#endif
using ::endl; using ::endl;
using ::ends; using ::ends;
using ::flush; using ::flush;
@ -137,4 +149,44 @@ struct cmStdString : public std::string
StdString(s, pos, n) {} 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 #endif

View File

@ -1068,12 +1068,11 @@ bool cmSystemTools::FilesDiffer(const char* source,
if(statSource.st_size != static_cast<long>(finSource.gcount()) || if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
statSource.st_size != static_cast<long>(finDestination.gcount())) statSource.st_size != static_cast<long>(finDestination.gcount()))
{ {
std::strstream msg; cmStringStream msg;
msg << "FilesDiffer failed to read files (allocated: " msg << "FilesDiffer failed to read files (allocated: "
<< statSource.st_size << ", read source: " << finSource.gcount() << statSource.st_size << ", read source: " << finSource.gcount()
<< ", read dest: " << finDestination.gcount() << std::ends; << ", read dest: " << finDestination.gcount();
cmSystemTools::Error(msg.str()); cmSystemTools::Error(msg.str().c_str());
delete [] msg.str();
delete [] source_buf; delete [] source_buf;
delete [] dest_buf; delete [] dest_buf;
return false; return false;
@ -1176,12 +1175,10 @@ void cmSystemTools::cmCopyFile(const char* source,
if (statSource.st_size != statDestination.st_size) if (statSource.st_size != statDestination.st_size)
{ {
std::strstream msg; cmStringStream msg;
msg << "CopyFile failed to copy files (sizes differ, source: " msg << "CopyFile failed to copy files (sizes differ, source: "
<< statSource.st_size << " , dest: " << statDestination.st_size << statSource.st_size << " , dest: " << statDestination.st_size;
<< std::ends; cmSystemTools::Error(msg.str().c_str());
cmSystemTools::Error(msg.str());
delete [] msg.str();
} }
} }
@ -1427,7 +1424,7 @@ bool cmSystemTools::RunCommand(const char* command,
if (WIFSIGNALED(retVal)) if (WIFSIGNALED(retVal))
{ {
retVal = WTERMSIG(retVal); retVal = WTERMSIG(retVal);
std::strstream error; cmStringStream error;
error << "\nProcess terminated due to "; error << "\nProcess terminated due to ";
switch (retVal) switch (retVal)
{ {
@ -1455,9 +1452,7 @@ bool cmSystemTools::RunCommand(const char* command,
error << "signal " << retVal; error << "signal " << retVal;
break; break;
} }
error << std::ends;
output += error.str(); output += error.str();
error.rdbuf()->freeze(0);
} }
return false; return false;
#endif #endif

View File

@ -655,11 +655,9 @@ void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
libName = this->ConvertToOutputPath(libName.c_str()); libName = this->ConvertToOutputPath(libName.c_str());
command2 += libName + " \\\n"; command2 += libName + " \\\n";
command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, name, t); this->OutputLinkLibraries(linklibs, name, t);
linklibs << std::ends;
command2 += linklibs.str(); command2 += linklibs.str();
delete [] linklibs.str();
std::string customCommands = this->CreateTargetRules(t, name); std::string customCommands = this->CreateTargetRules(t, name);
const char* cc = 0; const char* cc = 0;
if(customCommands.size() > 0) if(customCommands.size() > 0)
@ -699,11 +697,9 @@ void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
libName = this->ConvertToOutputPath(libName.c_str()); libName = this->ConvertToOutputPath(libName.c_str());
command2 += libName + " \\\n"; command2 += libName + " \\\n";
command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t); this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
linklibs << std::ends;
command2 += linklibs.str(); command2 += linklibs.str();
delete [] linklibs.str();
std::string customCommands = this->CreateTargetRules(t, name); std::string customCommands = this->CreateTargetRules(t, name);
const char* cc = 0; const char* cc = 0;
if(customCommands.size() > 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) "; "$(CMAKE_C_COMPILER) $(CMAKE_C_SHLIB_LINK_FLAGS) $(CMAKE_C_FLAGS) ";
} }
command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") "; command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
std::strstream linklibs; cmStringStream linklibs;
this->OutputLinkLibraries(linklibs, 0, t); this->OutputLinkLibraries(linklibs, 0, t);
linklibs << std::ends;
command += linklibs.str(); command += linklibs.str();
std::string outputFile = m_ExecutableOutputPath + name; std::string outputFile = m_ExecutableOutputPath + name;
command += " -o " + this->ConvertToOutputPath(outputFile.c_str()); command += " -o " + this->ConvertToOutputPath(outputFile.c_str());

View File

@ -216,10 +216,9 @@ cmVTKMakeInstantiatorCommand
std::string std::string
cmVTKMakeInstantiatorCommand::GenerateCreationFileName(unsigned int block) cmVTKMakeInstantiatorCommand::GenerateCreationFileName(unsigned int block)
{ {
std::strstream nameStr; cmStringStream nameStr;
nameStr << m_ClassName.c_str() << block << ".cxx" << std::ends; nameStr << m_ClassName.c_str() << block << ".cxx";
std::string result = nameStr.str(); std::string result = nameStr.str();
delete [] nameStr.str();
return result; return result;
} }

View File

@ -43,7 +43,7 @@ cmake::cmake()
void cmake::Usage(const char* program) void cmake::Usage(const char* program)
{ {
std::strstream errorStream; cmStringStream errorStream;
errorStream << "cmake version " << cmMakefile::GetMajorVersion() errorStream << "cmake version " << cmMakefile::GetMajorVersion()
<< "." << cmMakefile::GetMinorVersion() << "\n"; << "." << cmMakefile::GetMinorVersion() << "\n";
@ -61,9 +61,9 @@ void cmake::Usage(const char* program)
{ {
errorStream << "\"" << i->c_str() << "\" "; errorStream << "\"" << i->c_str() << "\" ";
} }
errorStream << ")\n" << std::ends; errorStream << ")\n";
cmSystemTools::Error(errorStream.str()); cmSystemTools::Error(errorStream.str().c_str());
} }
// Parse the args // Parse the args
@ -519,7 +519,7 @@ int cmake::Generate(const std::vector<std::string>& args, bool buildMakefiles)
void CMakeCommandUsage(const char* program) void CMakeCommandUsage(const char* program)
{ {
std::strstream errorStream; cmStringStream errorStream;
errorStream errorStream
<< "cmake version " << cmMakefile::GetMajorVersion() << "cmake version " << cmMakefile::GetMajorVersion()
@ -530,14 +530,14 @@ void CMakeCommandUsage(const char* program)
<< "Available commands: \n" << "Available commands: \n"
<< " copy file destination - copy file to destination (either file or directory)\n" << " copy file destination - copy file to destination (either file or directory)\n"
<< " remove file1 file2 ... - remove the file(s)\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__) #if defined(_WIN32) && !defined(__CYGWIN__)
errorStream
<< " write_regv key value - write registry value\n" << " write_regv key value - write registry value\n"
<< " delete_regv key - delete registry value\n" << " delete_regv key - delete registry value\n";
#endif #endif
<< std::ends;
cmSystemTools::Error(errorStream.str()); cmSystemTools::Error(errorStream.str().c_str());
} }
int cmake::CMakeCommand(std::vector<std::string>& args) int cmake::CMakeCommand(std::vector<std::string>& args)