[cmake-commits] alex committed cmFileCommand.h 1.30 1.31 cmFileCommand.cxx 1.90 1.91

cmake-commits at cmake.org cmake-commits at cmake.org
Wed Jan 2 16:46:41 EST 2008


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv13817

Modified Files:
	cmFileCommand.h cmFileCommand.cxx 
Log Message:
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


Index: cmFileCommand.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmFileCommand.h,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- cmFileCommand.h	10 Oct 2007 15:47:43 -0000	1.30
+++ cmFileCommand.h	2 Jan 2008 21:46:38 -0000	1.31
@@ -68,7 +68,7 @@
     return
       "  file(WRITE 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"
       "       [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n"
       "       [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n"
@@ -93,7 +93,9 @@
       "because it will lead to an infinite loop. Use configure_file if you "
       "want to generate input files to CMake.\n"
       "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 "
       "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 "

Index: cmFileCommand.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmFileCommand.cxx,v
retrieving revision 1.90
retrieving revision 1.91
diff -u -d -r1.90 -r1.91
--- cmFileCommand.cxx	2 Jan 2008 20:17:56 -0000	1.90
+++ cmFileCommand.cxx	2 Jan 2008 21:46:38 -0000	1.91
@@ -205,15 +205,38 @@
     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 @@
     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());



More information about the Cmake-commits mailing list