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