]>
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.<BR>
7 // 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 printf ("\n%s OldFile NewFile MatchString ReplaceString [MatchString2 ReplaceString2]*\n", Name
);
33 printf (" OldFile - Must be arg[1] File to search for MatchStrings\n");
34 printf (" NewFile - Must be arg[2] File where MatchString has been replaced with ReplaceString\n");
35 printf (" MatchString & ReplaceString. Required arguments.\n");
36 printf (" More MatchString/ReplaceString pairs are supported.\n");
42 // argv[3+n] - Match String
43 // argv[4+n] - Replace string
45 main (int argc
, char **argv
)
49 int c
, i
, n
, Len
, MaxLenKey
= 0, MinLenKey
= INT_MAX
;
50 unsigned long InFileSize
, InFilePos
;
57 fprintf (stderr
, "Need at least two files and one Match/Replacement string pair\n");
60 } else if ((argc
% 2) == 0) {
61 fprintf (stderr
, "Match and Replace string must come in pairs\n");
65 In
= fopen (argv
[1], "r");
66 fseek (In
, 0, SEEK_END
);
67 InFileSize
= ftell (In
);
68 if (InFileSize
== 0) {
69 fprintf (stderr
, "Could not open %s\n", argv
[1]);
72 fseek (In
, 0, SEEK_SET
);
75 Out
= fopen (argv
[2], "w+");
76 if ((In
== NULL
) || (Out
== NULL
)) {
77 fprintf (stderr
, "Could not open %s\n", argv
[2]);
81 MaxMatch
= (argc
- 2)/2;
82 Match
= calloc (MaxMatch
, sizeof (MATCH_PAIR
));
87 for (n
=0; n
< MaxMatch
; n
++) {
88 Match
[n
].Match
= argv
[3 + n
*2];
89 Match
[n
].MatchSize
= strlen (argv
[3 + n
*2]);
90 Match
[n
].Replace
= argv
[3 + n
*2 + 1];
91 if (Match
[n
].MatchSize
> MaxLenKey
) {
92 // Max size of match/replace string pair
93 MaxLenKey
= Match
[n
].MatchSize
;
95 if (Match
[n
].MatchSize
< MinLenKey
) {
96 MinLenKey
= Match
[n
].MatchSize
;
100 Key
= malloc (MaxLenKey
);
105 // Search for a match by reading every possition of the file
106 // into a buffer that is as big as the maximum search key size.
107 // Then we can search the keys for a match. If no match
108 // copy the old file character to the new file. If it is a match
109 // then copy the replacement string into the output file.
110 // This code assumes the file system is smart and caches the
111 // file in a buffer. So all the reads don't really hit the disk.
113 while (InFilePos
< (InFileSize
- MinLenKey
)) {
114 fseek (In
, InFilePos
, SEEK_SET
);
115 ReadCount
= fread (Key
, 1, MaxLenKey
, In
);
116 for (i
= 0, Found
= FALSE
;i
< MaxMatch
; i
++) {
117 if (ReadCount
>= Match
[i
].MatchSize
) {
118 if (!memcmp (Key
, Match
[i
].Match
, Match
[i
].MatchSize
)) {
119 InFilePos
+= (Match
[i
].MatchSize
- 1);
120 fputs (Match
[i
].Replace
, Out
);
133 // We stoped searching when we got to the point that we could no longer match.
134 // So the last few bytes of the file are not copied in the privous loop
135 fseek (In
, InFilePos
, SEEK_SET
);
136 while ((c
= fgetc (In
)) != EOF
) {