]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BeagleBoardPkg/Tools/replace.c
Update input of disasmembler to support IfThen construct. Add prototype dos script...
[mirror_edk2.git] / BeagleBoardPkg / Tools / replace.c
diff --git a/BeagleBoardPkg/Tools/replace.c b/BeagleBoardPkg/Tools/replace.c
new file mode 100755 (executable)
index 0000000..d26d127
--- /dev/null
@@ -0,0 +1,123 @@
+//\r
+// 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
+//  \r
+//  All rights reserved. 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
+//\r
+//  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+//  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+//\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <limits.h>\r
+\r
+#define TRUE  1\r
+#define FALSE 0\r
+\r
+typedef struct {\r
+  char  *Match;\r
+  int   MatchSize;\r
+  char  *Replace;\r
+} MATCH_PAIR;\r
+\r
+//\r
+// argv[1] - Old File\r
+// argv[2] - New File\r
+// argv[3+n] - Match String\r
+// argv[4+n] - Replace string \r
+int\r
+main (int argc, char **argv)\r
+{\r
+  FILE *In, *Out;\r
+  char *Key, *Replace;\r
+  int  c, i, n, Len, MaxLenKey = 0, MinLenKey = INT_MAX;\r
+  unsigned long  InFileSize, InFilePos;\r
+  MATCH_PAIR *Match;\r
+  int MaxMatch;\r
+  int ReadCount;\r
+  int Found;\r
+\r
+  if (argc < 5) {\r
+    // Need at least two files and two strings\r
+    return -1;\r
+  } else if ((argc % 2) == 0) {\r
+    // Match and Replace string must come in pairs\r
+    return -4;\r
+  }\r
+\r
+  In  = fopen (argv[1], "r");\r
+  fseek (In, 0, SEEK_END);\r
+  InFileSize = ftell (In);\r
+  if (InFileSize == 0) {\r
+    return -6;\r
+  }\r
+  fseek (In, 0, SEEK_SET);\r
+\r
+\r
+  Out = fopen (argv[2], "w+");\r
+  if ((In == NULL) || (Out == NULL)) {\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
+  }\r
+\r
+  for (n=0; n < MaxMatch; n++) {\r
+    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
+    }\r
+    if (Match[n].MatchSize < MinLenKey) {\r
+      MinLenKey = Match[n].MatchSize;\r
+    }\r
+  }\r
+\r
+  Key = malloc (MaxLenKey);\r
+  if (Key == NULL) {\r
+    return -5;\r
+  }\r
+\r
+  InFilePos = 0;\r
+  while (InFilePos < (InFileSize - MinLenKey)) {\r
+    fseek (In, InFilePos, SEEK_SET);\r
+    ReadCount = fread (Key, 1, MaxLenKey, In);\r
+    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
+        }\r
+      }\r
+    }\r
+    if (!Found) {\r
+      fputc (Key[0], Out);\r
+    }\r
\r
+    InFilePos++;\r
+  }\r
+\r
\r
+  fclose (In);\r
+  fclose (Out);\r
+  free (Key);\r
+  return 0;\r
+}\r
+\r