summaryrefslogtreecommitdiffstats
path: root/Source/cmFileCommand.cxx
diff options
context:
space:
mode:
authorAlexander Neundorf <neundorf@kde.org>2008-01-02 21:46:38 (GMT)
committerAlexander Neundorf <neundorf@kde.org>2008-01-02 21:46:38 (GMT)
commit7b54af713dd9ab72335dabb161afba8cd2cc4cc8 (patch)
tree0c4d597a31fc4326dc73ceeafb1e1745ba32e94c /Source/cmFileCommand.cxx
parent2625b0498b42d55f2bbb6adea3df1f0b9cf9f165 (diff)
downloadCMake-7b54af713dd9ab72335dabb161afba8cd2cc4cc8.zip
CMake-7b54af713dd9ab72335dabb161afba8cd2cc4cc8.tar.gz
CMake-7b54af713dd9ab72335dabb161afba8cd2cc4cc8.tar.bz2
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
Diffstat (limited to 'Source/cmFileCommand.cxx')
-rw-r--r--Source/cmFileCommand.cxx95
1 files changed, 73 insertions, 22 deletions
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx
index 092dfb1..1a12ddf 100644
--- a/Source/cmFileCommand.cxx
+++ b/Source/cmFileCommand.cxx
@@ -205,15 +205,38 @@ bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
return false;
}
- std::string fileName = args[1];
- if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
+ cmCommandArgumentsHelper argHelper;
+ 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 += "/" + 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);
+#endif
+
if ( !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;
}
- // if there a limit?
+ // is there a limit?
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());
}
+ // is there an offset?
+ long offset = 0;
+ if (offsetArg.GetString().size() > 0)
+ {
+ offset = atoi(offsetArg.GetCString());
+ }
+
+ file.seekg(offset);
+
std::string output;
- std::string line;
- bool has_newline = false;
- while (sizeLimit != 0 &&
- cmSystemTools::GetLineFromStream(file, line, &has_newline,
- sizeLimit) )
+
+ if (hexOutputArg.IsEnabled())
{
- if (sizeLimit > 0)
+ // Convert part of the file into hex code
+ int c;
+ while((sizeLimit != 0) && (c = file.get(), file))
{
- sizeLimit = sizeLimit - static_cast<long>(line.size());
- if (has_newline)
+ char hex[4];
+ sprintf(hex, "%x", c&0xff);
+ output += hex;
+ if (sizeLimit > 0)
{
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());