1 | /** @file
|
---|
2 | * File splitter: splits soapC.cpp into manageable pieces. It is somewhat
|
---|
3 | * intelligent and avoids splitting inside functions or similar places.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <sys/types.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <string.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <limits.h>
|
---|
27 |
|
---|
28 |
|
---|
29 | int main(int argc, char *argv[])
|
---|
30 | {
|
---|
31 | int rc = 0;
|
---|
32 | FILE *pFileIn = NULL;
|
---|
33 | FILE *pFileOut = NULL;
|
---|
34 | char *pBuffer = NULL;
|
---|
35 |
|
---|
36 | do
|
---|
37 | {
|
---|
38 | if (argc != 4)
|
---|
39 | {
|
---|
40 | fprintf(stderr, "split-soapC: Must be started with exactly three arguments,\n"
|
---|
41 | "1) the input file, 2) the directory where to put the output files and\n"
|
---|
42 | "3) the number chunks to create.\n");
|
---|
43 | rc = 2;
|
---|
44 | break;
|
---|
45 | }
|
---|
46 |
|
---|
47 | char *pEnd = NULL;
|
---|
48 | unsigned long cChunk = strtoul(argv[3], &pEnd, 0);
|
---|
49 | if (cChunk == ULONG_MAX || cChunk == 0 || !argv[3] || *pEnd)
|
---|
50 | {
|
---|
51 | fprintf(stderr, "split-soapC: Given argument \"%s\" is not a valid chunk count.\n", argv[3]);
|
---|
52 | rc = 2;
|
---|
53 | break;
|
---|
54 | }
|
---|
55 |
|
---|
56 | pFileIn = fopen(argv[1], "rb");
|
---|
57 | if (!pFileIn)
|
---|
58 | {
|
---|
59 | fprintf(stderr, "split-soapC: Cannot open file \"%s\" for reading.\n", argv[1]);
|
---|
60 | rc = 2;
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | fseek(pFileIn, 0, SEEK_END);
|
---|
64 | long cbFileIn = ftell(pFileIn);
|
---|
65 | rewind(pFileIn);
|
---|
66 |
|
---|
67 | if (!(pBuffer = (char*)malloc(cbFileIn + 1)))
|
---|
68 | {
|
---|
69 | fprintf(stderr, "split-soapC: Failed to allocate %ld bytes.\n", cbFileIn);
|
---|
70 | rc = 2;
|
---|
71 | break;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (fread(pBuffer, 1, cbFileIn, pFileIn) != cbFileIn)
|
---|
75 | {
|
---|
76 | fprintf(stderr, "split-soapC: Failed to read %ld bytes from input file.\n", cbFileIn);
|
---|
77 | rc = 2;
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | pBuffer[cbFileIn] = '\0';
|
---|
81 |
|
---|
82 | const char *pLine = pBuffer;
|
---|
83 | unsigned long cbChunk = cbFileIn / cChunk;
|
---|
84 | unsigned long cFiles = 0;
|
---|
85 | unsigned long uLimit = 0;
|
---|
86 | unsigned long cbWritten = 0;
|
---|
87 | unsigned long cIfNesting = 0;
|
---|
88 | unsigned long cBraceNesting = 0;
|
---|
89 | bool fJustZero = false;
|
---|
90 |
|
---|
91 | do
|
---|
92 | {
|
---|
93 | if (!pFileOut)
|
---|
94 | {
|
---|
95 | /* construct output filename */
|
---|
96 | char szFilename[1024];
|
---|
97 | sprintf(szFilename, "%s/soapC-%u.cpp", argv[2], ++cFiles);
|
---|
98 | szFilename[sizeof(szFilename)-1] = '\0';
|
---|
99 | printf("info: soapC-%u.cpp\n", cFiles);
|
---|
100 |
|
---|
101 | /* create output file */
|
---|
102 | if (!(pFileOut = fopen(szFilename, "wb")))
|
---|
103 | {
|
---|
104 | fprintf(stderr, "split-soapC: Failed to open file \"%s\" for writing\n", szFilename);
|
---|
105 | rc = 2;
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | if (cFiles > 1)
|
---|
109 | fprintf(pFileOut, "#include \"soapH.h\"%s\n",
|
---|
110 | #ifdef RT_OS_WINDOWS
|
---|
111 | "\r"
|
---|
112 | #else /* !RT_OS_WINDOWS */
|
---|
113 | ""
|
---|
114 | #endif /* !RT_OS_WINDOWS */
|
---|
115 | );
|
---|
116 | uLimit += cbChunk;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* find begin of next line and print current line */
|
---|
120 | const char *pNextLine = strchr(pLine, '\n');
|
---|
121 | size_t cbLine;
|
---|
122 | if (pNextLine)
|
---|
123 | {
|
---|
124 | pNextLine++;
|
---|
125 | cbLine = pNextLine - pLine;
|
---|
126 | }
|
---|
127 | else
|
---|
128 | cbLine = strlen(pLine);
|
---|
129 | if (fwrite(pLine, 1, cbLine, pFileOut) != cbLine)
|
---|
130 | {
|
---|
131 | fprintf(stderr, "split-soapC: Failed to write to output file\n");
|
---|
132 | rc = 2;
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | cbWritten += cbLine;
|
---|
136 |
|
---|
137 | /* process nesting depth information */
|
---|
138 | if (!strncmp(pLine, "#if", 3))
|
---|
139 | cIfNesting++;
|
---|
140 | else if (!strncmp(pLine, "#endif", 6))
|
---|
141 | {
|
---|
142 | cIfNesting--;
|
---|
143 | if (!cBraceNesting && !cIfNesting)
|
---|
144 | fJustZero = true;
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | for (const char *p = pLine; p < pLine + cbLine; p++)
|
---|
149 | {
|
---|
150 | if (*p == '{')
|
---|
151 | cBraceNesting++;
|
---|
152 | else if (*p == '}')
|
---|
153 | {
|
---|
154 | cBraceNesting--;
|
---|
155 | if (!cBraceNesting && !cIfNesting)
|
---|
156 | fJustZero = true;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* start a new output file if necessary and possible */
|
---|
162 | if (cbWritten >= uLimit && cIfNesting == 0 && fJustZero && cFiles < cChunk)
|
---|
163 | {
|
---|
164 | fclose(pFileOut);
|
---|
165 | pFileOut = NULL;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (rc)
|
---|
169 | break;
|
---|
170 |
|
---|
171 | fJustZero = false;
|
---|
172 | pLine = pNextLine;
|
---|
173 | } while (pLine);
|
---|
174 |
|
---|
175 | printf("split-soapC: Created %lu files.\n", cFiles);
|
---|
176 | } while (0);
|
---|
177 |
|
---|
178 | if (pBuffer)
|
---|
179 | free(pBuffer);
|
---|
180 | if (pFileIn)
|
---|
181 | fclose(pFileIn);
|
---|
182 | if (pFileOut)
|
---|
183 | fclose(pFileOut);
|
---|
184 |
|
---|
185 | return rc;
|
---|
186 | }
|
---|