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 |
23 | typedef struct {\r |
24 | char *Match;\r |
25 | int MatchSize;\r |
26 | char *Replace;\r |
27 | } MATCH_PAIR;\r |
28 | \r |
3aa4215f |
29 | void\r |
30 | Usage (char *Name)\r |
31 | {\r |
32 | printf ("\n%s OldFile NewFile MatchString ReplaceString [MatchString2 ReplaceString2]*\n", Name);\r |
33 | printf (" OldFile - Must be arg[1] File to search for MatchStrings\n");\r |
34 | printf (" NewFile - Must be arg[2] File where MatchString has been replaced with ReplaceString\n");\r |
35 | printf (" MatchString & ReplaceString. Required arguments.\n");\r |
36 | printf (" More MatchString/ReplaceString pairs are supported.\n");\r |
37 | }\r |
38 | \r |
f3198cba |
39 | //\r |
40 | // argv[1] - Old File\r |
41 | // argv[2] - New File\r |
42 | // argv[3+n] - Match String\r |
43 | // argv[4+n] - Replace string \r |
44 | int\r |
45 | main (int argc, char **argv)\r |
46 | {\r |
47 | FILE *In, *Out;\r |
48 | char *Key, *Replace;\r |
49 | int c, i, n, Len, MaxLenKey = 0, MinLenKey = INT_MAX;\r |
50 | unsigned long InFileSize, InFilePos;\r |
51 | MATCH_PAIR *Match;\r |
52 | int MaxMatch;\r |
53 | int ReadCount;\r |
54 | int Found;\r |
55 | \r |
56 | if (argc < 5) {\r |
3aa4215f |
57 | fprintf (stderr, "Need at least two files and one Match/Replacement string pair\n");\r |
58 | Usage (argv[0]);\r |
f3198cba |
59 | return -1;\r |
60 | } else if ((argc % 2) == 0) {\r |
3aa4215f |
61 | fprintf (stderr, "Match and Replace string must come in pairs\n");\r |
f3198cba |
62 | return -4;\r |
63 | }\r |
64 | \r |
65 | In = fopen (argv[1], "r");\r |
66 | fseek (In, 0, SEEK_END);\r |
67 | InFileSize = ftell (In);\r |
68 | if (InFileSize == 0) {\r |
3aa4215f |
69 | fprintf (stderr, "Could not open %s\n", argv[1]);\r |
f3198cba |
70 | return -6;\r |
71 | }\r |
72 | fseek (In, 0, SEEK_SET);\r |
73 | \r |
74 | \r |
75 | Out = fopen (argv[2], "w+");\r |
76 | if ((In == NULL) || (Out == NULL)) {\r |
3aa4215f |
77 | fprintf (stderr, "Could not open %s\n", argv[2]);\r |
f3198cba |
78 | return -2;\r |
79 | }\r |
80 | \r |
81 | MaxMatch = (argc - 2)/2;\r |
f3198cba |
82 | Match = calloc (MaxMatch, sizeof (MATCH_PAIR));\r |
83 | if (Match == NULL) {\r |
84 | return -7;\r |
85 | }\r |
86 | \r |
87 | for (n=0; n < MaxMatch; n++) {\r |
88 | Match[n].Match = argv[3 + n*2];\r |
89 | Match[n].MatchSize = strlen (argv[3 + n*2]);\r |
90 | Match[n].Replace = argv[3 + n*2 + 1];\r |
f3198cba |
91 | if (Match[n].MatchSize > MaxLenKey) {\r |
92 | // Max size of match/replace string pair\r |
93 | MaxLenKey = Match[n].MatchSize;\r |
94 | }\r |
95 | if (Match[n].MatchSize < MinLenKey) {\r |
96 | MinLenKey = Match[n].MatchSize;\r |
97 | }\r |
98 | }\r |
99 | \r |
100 | Key = malloc (MaxLenKey);\r |
101 | if (Key == NULL) {\r |
102 | return -5;\r |
103 | }\r |
104 | \r |
3aa4215f |
105 | // Search for a match by reading every possition of the file\r |
106 | // into a buffer that is as big as the maximum search key size.\r |
107 | // Then we can search the keys for a match. If no match\r |
108 | // copy the old file character to the new file. If it is a match\r |
109 | // then copy the replacement string into the output file. \r |
110 | // This code assumes the file system is smart and caches the \r |
111 | // file in a buffer. So all the reads don't really hit the disk. \r |
f3198cba |
112 | InFilePos = 0;\r |
113 | while (InFilePos < (InFileSize - MinLenKey)) {\r |
114 | fseek (In, InFilePos, SEEK_SET);\r |
115 | ReadCount = fread (Key, 1, MaxLenKey, In);\r |
116 | for (i = 0, Found = FALSE;i < MaxMatch; i++) {\r |
117 | if (ReadCount >= Match[i].MatchSize) {\r |
118 | if (!memcmp (Key, Match[i].Match, Match[i].MatchSize)) {\r |
f3198cba |
119 | InFilePos += (Match[i].MatchSize - 1);\r |
f3198cba |
120 | fputs (Match[i].Replace, Out);\r |
121 | Found = TRUE;\r |
122 | break;\r |
123 | }\r |
124 | }\r |
125 | }\r |
126 | if (!Found) {\r |
127 | fputc (Key[0], Out);\r |
128 | }\r |
129 | \r |
130 | InFilePos++;\r |
131 | }\r |
132 | \r |
3aa4215f |
133 | // We stoped searching when we got to the point that we could no longer match.\r |
134 | // So the last few bytes of the file are not copied in the privous loop\r |
135 | fseek (In, InFilePos, SEEK_SET);\r |
136 | while ((c = fgetc (In)) != EOF) {\r |
137 | fputc (c, Out);\r |
138 | }\r |
f3198cba |
139 | \r |
140 | fclose (In);\r |
141 | fclose (Out);\r |
142 | free (Key);\r |
3aa4215f |
143 | free (Match);\r |
f3198cba |
144 | return 0;\r |
145 | }\r |
146 | \r |