]>
git.proxmox.com Git - mirror_edk2.git/blob - BeagleBoardPkg/Tools/replace.c
2 // Quick hack to work around not having sed, or any other reasonable
3 // way to edit a file from a script on Windows......
5 // Copyright (c) 2010, Apple Inc. All rights reserved.
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
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.
32 // argv[3+n] - Match String
33 // argv[4+n] - Replace string
35 main (int argc
, char **argv
)
39 int c
, i
, n
, Len
, MaxLenKey
= 0, MinLenKey
= INT_MAX
;
40 unsigned long InFileSize
, InFilePos
;
47 // Need at least two files and two strings
49 } else if ((argc
% 2) == 0) {
50 // Match and Replace string must come in pairs
54 In
= fopen (argv
[1], "r");
55 fseek (In
, 0, SEEK_END
);
56 InFileSize
= ftell (In
);
57 if (InFileSize
== 0) {
60 fseek (In
, 0, SEEK_SET
);
63 Out
= fopen (argv
[2], "w+");
64 if ((In
== NULL
) || (Out
== NULL
)) {
68 MaxMatch
= (argc
- 2)/2;
69 printf ("\nMaxMatch = %d:%d\n", MaxMatch
, argc
);
70 Match
= calloc (MaxMatch
, sizeof (MATCH_PAIR
));
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
;
84 if (Match
[n
].MatchSize
< MinLenKey
) {
85 MinLenKey
= Match
[n
].MatchSize
;
89 Key
= malloc (MaxLenKey
);
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
);