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 += 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: ";

View File

@ -35,11 +35,10 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> 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;
}

View File

@ -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}"

View File

@ -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

View File

@ -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<cmSourceFile*>& sources = t.GetSourceFiles();
for(std::vector<cmSourceFile*>::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: ";

View File

@ -38,13 +38,19 @@
#endif
#ifndef CMAKE_NO_ANSI_STREAM_HEADERS
#include <fstream>
#include <iostream>
#include <strstream>
# include <fstream>
# include <iostream>
#else
#include <fstream.h>
#include <iostream.h>
#include <strstream.h>
# include <fstream.h>
# include <iostream.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
// 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

View File

@ -1068,12 +1068,11 @@ bool cmSystemTools::FilesDiffer(const char* source,
if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
statSource.st_size != static_cast<long>(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

View File

@ -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());

View File

@ -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;
}

View File

@ -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<std::string>& 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<std::string>& args)