]> git.proxmox.com Git - mirror_edk2.git/blame - BeagleBoardPkg/Tools/replace.c
MdePkg/UefiBaseType.h: treat EBC as a non-native machine type
[mirror_edk2.git] / BeagleBoardPkg / Tools / replace.c
CommitLineData
f3198cba 1//\r
3402aac7 2// Quick hack to work around not having sed, or any other reasonable\r
f3198cba 3// way to edit a file from a script on Windows......\r
4//\r
1ebd6c11 5// Copyright (c) 2010, Apple Inc. All rights reserved.<BR>\r
3402aac7 6//\r
a1594be9 7// SPDX-License-Identifier: BSD-2-Clause-Patent\r
f3198cba 8//\r
9\r
10#include <stdio.h>\r
11#include <stdlib.h>\r
12#include <limits.h>\r
13\r
14#define TRUE 1\r
15#define FALSE 0\r
16\r
17typedef struct {\r
18 char *Match;\r
19 int MatchSize;\r
20 char *Replace;\r
21} MATCH_PAIR;\r
22\r
3aa4215f 23void\r
24Usage (char *Name)\r
25{\r
26 printf ("\n%s OldFile NewFile MatchString ReplaceString [MatchString2 ReplaceString2]*\n", Name);\r
27 printf (" OldFile - Must be arg[1] File to search for MatchStrings\n");\r
28 printf (" NewFile - Must be arg[2] File where MatchString has been replaced with ReplaceString\n");\r
29 printf (" MatchString & ReplaceString. Required arguments.\n");\r
30 printf (" More MatchString/ReplaceString pairs are supported.\n");\r
31}\r
32\r
f3198cba 33//\r
34// argv[1] - Old File\r
35// argv[2] - New File\r
36// argv[3+n] - Match String\r
3402aac7 37// argv[4+n] - Replace string\r
f3198cba 38int\r
39main (int argc, char **argv)\r
40{\r
41 FILE *In, *Out;\r
42 char *Key, *Replace;\r
43 int c, i, n, Len, MaxLenKey = 0, MinLenKey = INT_MAX;\r
44 unsigned long InFileSize, InFilePos;\r
45 MATCH_PAIR *Match;\r
46 int MaxMatch;\r
47 int ReadCount;\r
48 int Found;\r
49\r
50 if (argc < 5) {\r
3aa4215f 51 fprintf (stderr, "Need at least two files and one Match/Replacement string pair\n");\r
52 Usage (argv[0]);\r
f3198cba 53 return -1;\r
54 } else if ((argc % 2) == 0) {\r
3aa4215f 55 fprintf (stderr, "Match and Replace string must come in pairs\n");\r
f3198cba 56 return -4;\r
57 }\r
58\r
59 In = fopen (argv[1], "r");\r
60 fseek (In, 0, SEEK_END);\r
61 InFileSize = ftell (In);\r
62 if (InFileSize == 0) {\r
3aa4215f 63 fprintf (stderr, "Could not open %s\n", argv[1]);\r
f3198cba 64 return -6;\r
65 }\r
66 fseek (In, 0, SEEK_SET);\r
67\r
68\r
69 Out = fopen (argv[2], "w+");\r
70 if ((In == NULL) || (Out == NULL)) {\r
3aa4215f 71 fprintf (stderr, "Could not open %s\n", argv[2]);\r
f3198cba 72 return -2;\r
73 }\r
74\r
75 MaxMatch = (argc - 2)/2;\r
f3198cba 76 Match = calloc (MaxMatch, sizeof (MATCH_PAIR));\r
77 if (Match == NULL) {\r
78 return -7;\r
79 }\r
80\r
81 for (n=0; n < MaxMatch; n++) {\r
82 Match[n].Match = argv[3 + n*2];\r
83 Match[n].MatchSize = strlen (argv[3 + n*2]);\r
84 Match[n].Replace = argv[3 + n*2 + 1];\r
f3198cba 85 if (Match[n].MatchSize > MaxLenKey) {\r
86 // Max size of match/replace string pair\r
87 MaxLenKey = Match[n].MatchSize;\r
88 }\r
89 if (Match[n].MatchSize < MinLenKey) {\r
90 MinLenKey = Match[n].MatchSize;\r
91 }\r
92 }\r
93\r
94 Key = malloc (MaxLenKey);\r
95 if (Key == NULL) {\r
96 return -5;\r
97 }\r
98\r
3aa4215f 99 // Search for a match by reading every possition of the file\r
100 // into a buffer that is as big as the maximum search key size.\r
101 // Then we can search the keys for a match. If no match\r
102 // copy the old file character to the new file. If it is a match\r
3402aac7
RC
103 // then copy the replacement string into the output file.\r
104 // This code assumes the file system is smart and caches the\r
105 // file in a buffer. So all the reads don't really hit the disk.\r
f3198cba 106 InFilePos = 0;\r
107 while (InFilePos < (InFileSize - MinLenKey)) {\r
108 fseek (In, InFilePos, SEEK_SET);\r
109 ReadCount = fread (Key, 1, MaxLenKey, In);\r
110 for (i = 0, Found = FALSE;i < MaxMatch; i++) {\r
111 if (ReadCount >= Match[i].MatchSize) {\r
112 if (!memcmp (Key, Match[i].Match, Match[i].MatchSize)) {\r
f3198cba 113 InFilePos += (Match[i].MatchSize - 1);\r
f3198cba 114 fputs (Match[i].Replace, Out);\r
115 Found = TRUE;\r
116 break;\r
117 }\r
118 }\r
119 }\r
120 if (!Found) {\r
121 fputc (Key[0], Out);\r
122 }\r
3402aac7 123\r
f3198cba 124 InFilePos++;\r
125 }\r
126\r
3aa4215f 127 // We stoped searching when we got to the point that we could no longer match.\r
128 // So the last few bytes of the file are not copied in the privous loop\r
129 fseek (In, InFilePos, SEEK_SET);\r
130 while ((c = fgetc (In)) != EOF) {\r
131 fputc (c, Out);\r
132 }\r
3402aac7 133\r
f3198cba 134 fclose (In);\r
135 fclose (Out);\r
136 free (Key);\r
3aa4215f 137 free (Match);\r
f3198cba 138 return 0;\r
139}\r
140\r