1#include "SkStream.h"
2#include "SkString.h"
3#include "SkTDArray.h"
4#include <stdio.h>
5
6static bool replace(const char* fun, const char* dir, const char* filename, const char* marker,
7        const char* marker2, const char* replace, size_t replaceLen) {
8    SkString outFileStr(dir);
9    outFileStr.append(filename);
10    SkFILEStream opStreamIn(outFileStr.c_str());
11    if (!opStreamIn.isValid()) {
12        SkDebugf("%s couldn't find %s\n", fun, outFileStr.c_str());
13        return false;
14    }
15    SkTDArray<char> opData;
16    opData.setCount(opStreamIn.getLength());
17    size_t opLen = opData.count();
18    opStreamIn.read(opData.begin(), opLen);
19    opStreamIn.setPath(NULL);
20    SkFILEWStream opStreamOut(outFileStr.c_str());
21    if (!opStreamOut.isValid()) {
22        SkDebugf("%s couldn't open for writing %s\n", fun, outFileStr.c_str());
23        return false;
24    }
25
26    char* opInsert = strstr(opData.begin(), marker);
27    if (!opInsert) {
28        SkDebugf("%s missing marker in %s\n", fun, outFileStr.c_str());
29        opStreamOut.write(opData.begin(), opLen);
30        opStreamOut.flush();
31        return false;
32    }
33    const char* opInsertEnd = opInsert + strlen(marker);
34    if (marker2) {
35        char* opInsert2 = strstr(opInsert, marker2);
36        if (!opInsert2) {
37            SkDebugf("%s missing marker second half in %s\n", fun, outFileStr.c_str());
38            opStreamOut.write(opData.begin(), opLen);
39            opStreamOut.flush();
40            return false;
41        }
42        opInsertEnd = opInsert2 + strlen(marker2);
43    }
44    opStreamOut.write(opData.begin(), opInsert - opData.begin());
45    opStreamOut.write(replace, replaceLen);
46    opStreamOut.write(opInsertEnd, opLen - (opInsertEnd - opData.begin()));
47    opStreamOut.flush();
48    return true;
49}
50
51int main (int argc, char * const argv[]) {
52    if (argc != 2) {
53        SkDebugf("%s expected filename\n", argv[0]);
54        return 0;
55    }
56    const char* dir = "../../experimental/Intersection/";
57    SkString inFileStr;
58    if (argv[1][0] != '/') {
59        inFileStr.append(dir);
60    }
61    inFileStr.append(argv[1]);
62    SkFILEStream inFile(inFileStr.c_str());
63    if (!inFile.isValid()) {
64        SkDebugf("%s couldn't find %s\n", argv[0], argv[1]);
65        return 0;
66    }
67    SkTDArray<char> inData;
68    inData.setCount(inFile.getLength());
69    size_t inLen = inData.count();
70    inFile.read(inData.begin(), inLen);
71    inFile.setPath(NULL);
72    char* insert = strstr(inData.begin(), "\n\n\n");
73    if (!insert) {
74        SkDebugf("%s missing two blank line delimiter in %s\n", argv[0], argv[1]);
75        return 0;
76    }
77    insert += 1; // include first blank line
78    const char opMarker[] =
79            "</div>" "\n"
80            "\n"
81            "<script type=\"text/javascript\">" "\n"
82            "\n"
83            "var testDivs = ["  "\n"
84            ;
85    if (!replace(argv[0], dir, "op.htm", opMarker, NULL, inData.begin(),
86            insert - inData.begin())) {
87        return 0;
88    }
89    const char newMarker[] =
90            "static void (*firstTest)() = "
91            ;
92    const char newMarker2[] =
93            ";"  "\n"
94            "\n"
95            "static struct {"  "\n"
96            "    void (*fun)();"  "\n"
97            "    const char* str;"  "\n"
98            "} tests[] = {"  "\n"
99            ;
100    if (!replace(argv[0], dir, "SimplifyNew_Test.cpp", newMarker, newMarker2, insert + 2,
101            inLen - (insert - inData.begin()) - 2)) {
102        return 0;
103    }
104    const char forceReleaseMarker[] =
105            "#define FORCE_RELEASE 1  // set force release to 1 for multiple thread -- no debugging"
106            ;
107    const char forceReleaseReplace[] =
108            "#define FORCE_RELEASE 0  // set force release to 1 for multiple thread -- no debugging"
109            ;
110    if (!replace(argv[0], dir, "DataTypes.h", forceReleaseMarker, NULL, forceReleaseReplace,
111            sizeof(forceReleaseReplace) - 1)) {
112        return 0;
113    }
114    return 0;
115}
116