]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BeagleBoardPkg/Tools/replace.c
Update the copyright notice format
[mirror_edk2.git] / BeagleBoardPkg / Tools / replace.c
old mode 100755 (executable)
new mode 100644 (file)
index d26d127..e35c394
@@ -2,9 +2,9 @@
 // Quick hack to work around not having sed, or any other reasonable \r
 // way to edit a file from a script on Windows......\r
 //\r
-// Copyright (c) 2010, Apple Inc. All rights reserved.\r
+// Copyright (c) 2010, Apple Inc. All rights reserved.<BR>\r
 //  \r
-//  All rights reserved. This program and the accompanying materials\r
+//  This program and the accompanying materials\r
 //  are licensed and made available under the terms and conditions of the BSD License\r
 //  which accompanies this distribution.  The full text of the license may be found at\r
 //  http://opensource.org/licenses/bsd-license.php\r
@@ -26,6 +26,16 @@ typedef struct {
   char  *Replace;\r
 } MATCH_PAIR;\r
 \r
+void\r
+Usage (char *Name)\r
+{\r
+  printf ("\n%s OldFile NewFile MatchString ReplaceString [MatchString2 ReplaceString2]*\n", Name);\r
+  printf ("    OldFile - Must be arg[1] File to search for MatchStrings\n");\r
+  printf ("    NewFile - Must be arg[2] File where MatchString has been replaced with ReplaceString\n");\r
+  printf ("    MatchString & ReplaceString. Required arguments.\n");\r
+  printf ("    More MatchString/ReplaceString pairs are supported.\n");\r
+}\r
+\r
 //\r
 // argv[1] - Old File\r
 // argv[2] - New File\r
@@ -44,10 +54,11 @@ main (int argc, char **argv)
   int Found;\r
 \r
   if (argc < 5) {\r
-    // Need at least two files and two strings\r
+    fprintf (stderr, "Need at least two files and one Match/Replacement string pair\n");\r
+    Usage (argv[0]);\r
     return -1;\r
   } else if ((argc % 2) == 0) {\r
-    // Match and Replace string must come in pairs\r
+    fprintf (stderr, "Match and Replace string must come in pairs\n");\r
     return -4;\r
   }\r
 \r
@@ -55,6 +66,7 @@ main (int argc, char **argv)
   fseek (In, 0, SEEK_END);\r
   InFileSize = ftell (In);\r
   if (InFileSize == 0) {\r
+    fprintf (stderr, "Could not open %s\n", argv[1]);\r
     return -6;\r
   }\r
   fseek (In, 0, SEEK_SET);\r
@@ -62,11 +74,11 @@ main (int argc, char **argv)
 \r
   Out = fopen (argv[2], "w+");\r
   if ((In == NULL) || (Out == NULL)) {\r
+    fprintf (stderr, "Could not open %s\n", argv[2]);\r
     return -2;\r
   }\r
 \r
   MaxMatch = (argc - 2)/2;\r
-  printf ("\nMaxMatch = %d:%d\n", MaxMatch, argc);\r
   Match = calloc (MaxMatch, sizeof (MATCH_PAIR));\r
   if (Match == NULL) {\r
     return -7;\r
@@ -76,7 +88,6 @@ main (int argc, char **argv)
     Match[n].Match   = argv[3 + n*2];\r
     Match[n].MatchSize = strlen (argv[3 + n*2]);\r
     Match[n].Replace = argv[3 + n*2 + 1];\r
-printf ("%s > %s\n", Match[n].Match, Match[n].Replace);\r
     if (Match[n].MatchSize > MaxLenKey) {\r
       // Max size of match/replace string pair\r
       MaxLenKey = Match[n].MatchSize;\r
@@ -91,6 +102,13 @@ printf ("%s > %s\n", Match[n].Match, Match[n].Replace);
     return -5;\r
   }\r
 \r
+  // Search for a match by reading every possition of the file\r
+  // into a buffer that is as big as the maximum search key size.\r
+  // Then we can search the keys for a match. If no match\r
+  // copy the old file character to the new file. If it is a match\r
+  // then copy the replacement string into the output file. \r
+  // This code assumes the file system is smart and caches the \r
+  // file in a buffer. So all the reads don't really hit the disk. \r
   InFilePos = 0;\r
   while (InFilePos < (InFileSize - MinLenKey)) {\r
     fseek (In, InFilePos, SEEK_SET);\r
@@ -98,9 +116,7 @@ printf ("%s > %s\n", Match[n].Match, Match[n].Replace);
     for (i = 0, Found = FALSE;i < MaxMatch; i++) {\r
       if (ReadCount >= Match[i].MatchSize) {\r
         if (!memcmp (Key, Match[i].Match, Match[i].MatchSize)) {\r
-          printf ("Found [%s] @ %u\n", Match[i].Match, InFilePos);\r
           InFilePos += (Match[i].MatchSize - 1);\r
-          printf ("InFilePos = %u", InFilePos);\r
           fputs (Match[i].Replace, Out);\r
           Found = TRUE;\r
           break;\r
@@ -114,10 +130,17 @@ printf ("%s > %s\n", Match[n].Match, Match[n].Replace);
     InFilePos++;\r
   }\r
 \r
+  // We stoped searching when we got to the point that we could no longer match.\r
+  // So the last few bytes of the file are not copied in the privous loop\r
+  fseek (In, InFilePos, SEEK_SET);\r
+  while ((c = fgetc (In)) != EOF) {\r
+    fputc (c, Out);\r
+  }\r
  \r
   fclose (In);\r
   fclose (Out);\r
   free (Key);\r
+  free (Match);\r
   return 0;\r
 }\r
 \r