+++ /dev/null
-//\r
-// Quick hack to work around not having sed, or any other reasonable\r
-// way to edit a file from a script on Windows......\r
-//\r
-// Copyright (c) 2010, Apple Inc. All rights reserved.<BR>\r
-//\r
-// SPDX-License-Identifier: BSD-2-Clause-Patent\r
-//\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <limits.h>\r
-\r
-#define TRUE 1\r
-#define FALSE 0\r
-\r
-typedef struct {\r
- char *Match;\r
- int MatchSize;\r
- char *Replace;\r
-} MATCH_PAIR;\r
-\r
-void\r
-Usage (char *Name)\r
-{\r
- printf ("\n%s OldFile NewFile MatchString ReplaceString [MatchString2 ReplaceString2]*\n", Name);\r
- printf (" OldFile - Must be arg[1] File to search for MatchStrings\n");\r
- printf (" NewFile - Must be arg[2] File where MatchString has been replaced with ReplaceString\n");\r
- printf (" MatchString & ReplaceString. Required arguments.\n");\r
- printf (" More MatchString/ReplaceString pairs are supported.\n");\r
-}\r
-\r
-//\r
-// argv[1] - Old File\r
-// argv[2] - New File\r
-// argv[3+n] - Match String\r
-// argv[4+n] - Replace string\r
-int\r
-main (int argc, char **argv)\r
-{\r
- FILE *In, *Out;\r
- char *Key, *Replace;\r
- int c, i, n, Len, MaxLenKey = 0, MinLenKey = INT_MAX;\r
- unsigned long InFileSize, InFilePos;\r
- MATCH_PAIR *Match;\r
- int MaxMatch;\r
- int ReadCount;\r
- int Found;\r
-\r
- if (argc < 5) {\r
- fprintf (stderr, "Need at least two files and one Match/Replacement string pair\n");\r
- Usage (argv[0]);\r
- return -1;\r
- } else if ((argc % 2) == 0) {\r
- fprintf (stderr, "Match and Replace string must come in pairs\n");\r
- return -4;\r
- }\r
-\r
- In = fopen (argv[1], "r");\r
- fseek (In, 0, SEEK_END);\r
- InFileSize = ftell (In);\r
- if (InFileSize == 0) {\r
- fprintf (stderr, "Could not open %s\n", argv[1]);\r
- return -6;\r
- }\r
- fseek (In, 0, SEEK_SET);\r
-\r
-\r
- Out = fopen (argv[2], "w+");\r
- if ((In == NULL) || (Out == NULL)) {\r
- fprintf (stderr, "Could not open %s\n", argv[2]);\r
- return -2;\r
- }\r
-\r
- MaxMatch = (argc - 2)/2;\r
- Match = calloc (MaxMatch, sizeof (MATCH_PAIR));\r
- if (Match == NULL) {\r
- return -7;\r
- }\r
-\r
- for (n=0; n < MaxMatch; n++) {\r
- Match[n].Match = argv[3 + n*2];\r
- Match[n].MatchSize = strlen (argv[3 + n*2]);\r
- Match[n].Replace = argv[3 + n*2 + 1];\r
- if (Match[n].MatchSize > MaxLenKey) {\r
- // Max size of match/replace string pair\r
- MaxLenKey = Match[n].MatchSize;\r
- }\r
- if (Match[n].MatchSize < MinLenKey) {\r
- MinLenKey = Match[n].MatchSize;\r
- }\r
- }\r
-\r
- Key = malloc (MaxLenKey);\r
- if (Key == NULL) {\r
- return -5;\r
- }\r
-\r
- // Search for a match by reading every possition of the file\r
- // into a buffer that is as big as the maximum search key size.\r
- // Then we can search the keys for a match. If no match\r
- // copy the old file character to the new file. If it is a match\r
- // then copy the replacement string into the output file.\r
- // This code assumes the file system is smart and caches the\r
- // file in a buffer. So all the reads don't really hit the disk.\r
- InFilePos = 0;\r
- while (InFilePos < (InFileSize - MinLenKey)) {\r
- fseek (In, InFilePos, SEEK_SET);\r
- ReadCount = fread (Key, 1, MaxLenKey, In);\r
- for (i = 0, Found = FALSE;i < MaxMatch; i++) {\r
- if (ReadCount >= Match[i].MatchSize) {\r
- if (!memcmp (Key, Match[i].Match, Match[i].MatchSize)) {\r
- InFilePos += (Match[i].MatchSize - 1);\r
- fputs (Match[i].Replace, Out);\r
- Found = TRUE;\r
- break;\r
- }\r
- }\r
- }\r
- if (!Found) {\r
- fputc (Key[0], Out);\r
- }\r
-\r
- InFilePos++;\r
- }\r
-\r
- // We stoped searching when we got to the point that we could no longer match.\r
- // So the last few bytes of the file are not copied in the privous loop\r
- fseek (In, InFilePos, SEEK_SET);\r
- while ((c = fgetc (In)) != EOF) {\r
- fputc (c, Out);\r
- }\r
-\r
- fclose (In);\r
- fclose (Out);\r
- free (Key);\r
- free (Match);\r
- return 0;\r
-}\r
-\r