ENH: add the keywords OFFSET and HEX to the FILE() command, using OFFSET an

offset can be specified where the reading starts, and using HEX the data can
be converted into a hex string, so binary data can be compared with text
functions
-add docs for LIMIT, OFFSET and HEX

Alex
This commit is contained in:
Alexander Neundorf 2008-01-02 16:46:38 -05:00
parent 2625b0498b
commit 7b54af713d
2 changed files with 78 additions and 25 deletions

View File

@ -205,15 +205,38 @@ bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
return false; return false;
} }
std::string fileName = args[1]; cmCommandArgumentsHelper argHelper;
if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) ) cmCommandArgumentGroup group;
cmCAString readArg (&argHelper, "READ");
cmCAString fileNameArg (&argHelper, 0);
cmCAString resultArg (&argHelper, 0);
cmCAString offsetArg (&argHelper, "OFFSET", &group);
cmCAString limitArg (&argHelper, "LIMIT", &group);
cmCAEnabler hexOutputArg (&argHelper, "HEX", &group);
readArg.Follows(0);
fileNameArg.Follows(&readArg);
resultArg.Follows(&fileNameArg);
group.Follows(&resultArg);
argHelper.Parse(&args, 0);
std::string fileName = fileNameArg.GetString();
if ( !cmsys::SystemTools::FileIsFullPath(fileName.c_str()) )
{ {
fileName = this->Makefile->GetCurrentDirectory(); fileName = this->Makefile->GetCurrentDirectory();
fileName += "/" + args[1]; fileName += "/" + fileNameArg.GetString();
} }
std::string variable = args[2]; std::string variable = resultArg.GetString();
// Open the specified file.
#if defined(_WIN32) || defined(__CYGWIN__)
std::ifstream file(fileName.c_str(), std::ios::in | (hexOutputArg.IsEnabled()?std::ios::binary:0));
#else
std::ifstream file(fileName.c_str(), std::ios::in); std::ifstream file(fileName.c_str(), std::ios::in);
#endif
if ( !file ) if ( !file )
{ {
std::string error = "Internal CMake error when trying to open file: "; std::string error = "Internal CMake error when trying to open file: ";
@ -223,36 +246,64 @@ bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
return false; return false;
} }
// if there a limit? // is there a limit?
long sizeLimit = -1; long sizeLimit = -1;
if (args.size() >= 5 && args[3] == "LIMIT") if (limitArg.GetString().size() > 0)
{ {
sizeLimit = atoi(args[4].c_str()); sizeLimit = atoi(limitArg.GetCString());
} }
std::string output; // is there an offset?
std::string line; long offset = 0;
bool has_newline = false; if (offsetArg.GetString().size() > 0)
while (sizeLimit != 0 &&
cmSystemTools::GetLineFromStream(file, line, &has_newline,
sizeLimit) )
{ {
if (sizeLimit > 0) offset = atoi(offsetArg.GetCString());
}
file.seekg(offset);
std::string output;
if (hexOutputArg.IsEnabled())
{
// Convert part of the file into hex code
int c;
while((sizeLimit != 0) && (c = file.get(), file))
{ {
sizeLimit = sizeLimit - static_cast<long>(line.size()); char hex[4];
if (has_newline) sprintf(hex, "%x", c&0xff);
output += hex;
if (sizeLimit > 0)
{ {
sizeLimit--; sizeLimit--;
} }
if (sizeLimit < 0)
{
sizeLimit = 0;
}
} }
output += line; }
if ( has_newline ) else
{
std::string line;
bool has_newline = false;
while (sizeLimit != 0 &&
cmSystemTools::GetLineFromStream(file, line, &has_newline,
sizeLimit) )
{ {
output += "\n"; if (sizeLimit > 0)
{
sizeLimit = sizeLimit - static_cast<long>(line.size());
if (has_newline)
{
sizeLimit--;
}
if (sizeLimit < 0)
{
sizeLimit = 0;
}
}
output += line;
if ( has_newline )
{
output += "\n";
}
} }
} }
this->Makefile->AddDefinition(variable.c_str(), output.c_str()); this->Makefile->AddDefinition(variable.c_str(), output.c_str());

View File

@ -68,7 +68,7 @@ public:
return return
" file(WRITE filename \"message to write\"... )\n" " file(WRITE filename \"message to write\"... )\n"
" file(APPEND filename \"message to write\"... )\n" " file(APPEND filename \"message to write\"... )\n"
" file(READ filename variable [LIMIT numBytes])\n" " file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n"
" file(STRINGS filename variable [LIMIT_COUNT num]\n" " file(STRINGS filename variable [LIMIT_COUNT num]\n"
" [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n" " [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n"
" [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n" " [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n"
@ -93,7 +93,9 @@ public:
"because it will lead to an infinite loop. Use configure_file if you " "because it will lead to an infinite loop. Use configure_file if you "
"want to generate input files to CMake.\n" "want to generate input files to CMake.\n"
"READ will read the content of a file and store it into the " "READ will read the content of a file and store it into the "
"variable.\n" "variable. It will start at the given offset and read up to numBytes. "
"If the argument HEX is given, the binary data will be converted to "
"hexadecimal representation and this will be stored in the variable.\n"
"STRINGS will parse a list of ASCII strings from a file and " "STRINGS will parse a list of ASCII strings from a file and "
"store it in a variable. Binary data in the file are ignored. Carriage " "store it in a variable. Binary data in the file are ignored. Carriage "
"return (CR) characters are ignored. It works also for Intel Hex and " "return (CR) characters are ignored. It works also for Intel Hex and "