From f8c982cf786362385048071bb35bf826fa1516c4 Mon Sep 17 00:00:00 2001 From: Ken Martin Date: Thu, 1 Mar 2007 15:53:09 -0500 Subject: [PATCH] ENH: added LIMIT on file read --- Source/cmFileCommand.cxx | 28 +++++++++++++++++++++++++--- Source/cmFileCommand.h | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index d5d1eb27f..a97802c52 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -190,9 +190,10 @@ bool cmFileCommand::HandleWriteCommand(std::vector const& args, //---------------------------------------------------------------------------- bool cmFileCommand::HandleReadCommand(std::vector const& args) { - if ( args.size() != 3 ) + if ( args.size() < 3 ) { - this->SetError("READ must be called with two additional arguments"); + this->SetError("READ must be called with at least two additional " + "arguments"); return false; } @@ -214,11 +215,32 @@ bool cmFileCommand::HandleReadCommand(std::vector const& args) return false; } + // if there a limit? + long sizeLimit = -1; + if (args.size() >= 5 && args[3] == "LIMIT") + { + sizeLimit = atof(args[4].c_str()); + } + std::string output; std::string line; bool has_newline = false; - while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) ) + while (sizeLimit != 0 && + cmSystemTools::GetLineFromStream(file, line, &has_newline, + sizeLimit) ) { + if (sizeLimit > 0) + { + sizeLimit -= line.size(); + if (has_newline) + { + sizeLimit--; + } + if (sizeLimit < 0) + { + sizeLimit = 0; + } + } output += line; if ( has_newline ) { diff --git a/Source/cmFileCommand.h b/Source/cmFileCommand.h index 73323a688..ed4320723 100644 --- a/Source/cmFileCommand.h +++ b/Source/cmFileCommand.h @@ -66,7 +66,7 @@ public: return " FILE(WRITE filename \"message to write\"... )\n" " FILE(APPEND filename \"message to write\"... )\n" - " FILE(READ filename variable)\n" + " FILE(READ filename variable [LIMIT numBytes])\n" " FILE(GLOB variable [RELATIVE path] [globbing expressions]...)\n" " FILE(GLOB_RECURSE variable [RELATIVE path] \n" " [globbing expressions]...)\n"