]> git.proxmox.com Git - mirror_edk2.git/blame - BeagleBoardPkg/Tools/replace.c
Update input of disasmembler to support IfThen construct. Add prototype dos script...
[mirror_edk2.git] / BeagleBoardPkg / Tools / replace.c
CommitLineData
f3198cba 1//\r
2// Quick hack to work around not having sed, or any other reasonable \r
3// way to edit a file from a script on Windows......\r
4//\r
5// Copyright (c) 2010, Apple Inc. All rights reserved.\r
6// \r
7// All rights reserved. This program and the accompanying materials\r
8// are licensed and made available under the terms and conditions of the BSD License\r
9// which accompanies this distribution. The full text of the license may be found at\r
10// http://opensource.org/licenses/bsd-license.php\r
11//\r
12// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14//\r
15\r
16#include <stdio.h>\r
17#include <stdlib.h>\r
18#include <limits.h>\r
19\r
20#define TRUE 1\r
21#define FALSE 0\r
22\r
23typedef struct {\r
24 char *Match;\r
25 int MatchSize;\r
26 char *Replace;\r
27} MATCH_PAIR;\r
28\r
29//\r
30// argv[1] - Old File\r
31// argv[2] - New File\r
32// argv[3+n] - Match String\r
33// argv[4+n] - Replace string \r
34int\r
35main (int argc, char **argv)\r
36{\r
37 FILE *In, *Out;\r
38 char *Key, *Replace;\r
39 int c, i, n, Len, MaxLenKey = 0, MinLenKey = INT_MAX;\r
40 unsigned long InFileSize, InFilePos;\r
41 MATCH_PAIR *Match;\r
42 int MaxMatch;\r
43 int ReadCount;\r
44 int Found;\r
45\r
46 if (argc < 5) {\r
47 // Need at least two files and two strings\r
48 return -1;\r
49 } else if ((argc % 2) == 0) {\r
50 // Match and Replace string must come in pairs\r
51 return -4;\r
52 }\r
53\r
54 In = fopen (argv[1], "r");\r
55 fseek (In, 0, SEEK_END);\r
56 InFileSize = ftell (In);\r
57 if (InFileSize == 0) {\r
58 return -6;\r
59 }\r
60 fseek (In, 0, SEEK_SET);\r
61\r
62\r
63 Out = fopen (argv[2], "w+");\r
64 if ((In == NULL) || (Out == NULL)) {\r
65 return -2;\r
66 }\r
67\r
68 MaxMatch = (argc - 2)/2;\r
69 printf ("\nMaxMatch = %d:%d\n", MaxMatch, argc);\r
70 Match = calloc (MaxMatch, sizeof (MATCH_PAIR));\r
71 if (Match == NULL) {\r
72 return -7;\r
73 }\r
74\r
75 for (n=0; n < MaxMatch; n++) {\r
76 Match[n].Match = argv[3 + n*2];\r
77 Match[n].MatchSize = strlen (argv[3 + n*2]);\r
78 Match[n].Replace = argv[3 + n*2 + 1];\r
79printf ("%s > %s\n", Match[n].Match, Match[n].Replace);\r
80 if (Match[n].MatchSize > MaxLenKey) {\r
81 // Max size of match/replace string pair\r
82 MaxLenKey = Match[n].MatchSize;\r
83 }\r
84 if (Match[n].MatchSize < MinLenKey) {\r
85 MinLenKey = Match[n].MatchSize;\r
86 }\r
87 }\r
88\r
89 Key = malloc (MaxLenKey);\r
90 if (Key == NULL) {\r
91 return -5;\r
92 }\r
93\r
94 InFilePos = 0;\r
95 while (InFilePos < (InFileSize - MinLenKey)) {\r
96 fseek (In, InFilePos, SEEK_SET);\r
97 ReadCount = fread (Key, 1, MaxLenKey, In);\r
98 for (i = 0, Found = FALSE;i < MaxMatch; i++) {\r
99 if (ReadCount >= Match[i].MatchSize) {\r
100 if (!memcmp (Key, Match[i].Match, Match[i].MatchSize)) {\r
101 printf ("Found [%s] @ %u\n", Match[i].Match, InFilePos);\r
102 InFilePos += (Match[i].MatchSize - 1);\r
103 printf ("InFilePos = %u", InFilePos);\r
104 fputs (Match[i].Replace, Out);\r
105 Found = TRUE;\r
106 break;\r
107 }\r
108 }\r
109 }\r
110 if (!Found) {\r
111 fputc (Key[0], Out);\r
112 }\r
113 \r
114 InFilePos++;\r
115 }\r
116\r
117 \r
118 fclose (In);\r
119 fclose (Out);\r
120 free (Key);\r
121 return 0;\r
122}\r
123\r