1 | /* $Id: split-soapC.cpp 98297 2023-01-25 01:59:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Splits soapC.cpp and soapH-noinline.cpp into more manageable portions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <iprt/types.h>
|
---|
33 | #include <iprt/path.h>
|
---|
34 | #include <sys/types.h>
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <string.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <limits.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | static char *readfileIntoBuffer(const char *pszFile, size_t *pcbFile)
|
---|
42 | {
|
---|
43 | FILE *pFileIn = fopen(pszFile, "rb");
|
---|
44 | if (pFileIn)
|
---|
45 | {
|
---|
46 | int iRc2 = fseek(pFileIn, 0, SEEK_END);
|
---|
47 | long cbFileIn = ftell(pFileIn);
|
---|
48 | int iRc3 = fseek(pFileIn, 0, SEEK_SET);
|
---|
49 | if (iRc3 != -1 && iRc2 != -1 && cbFileIn >= 0)
|
---|
50 | {
|
---|
51 | char *pBuffer = (char *)malloc(cbFileIn + 1);
|
---|
52 | if (pBuffer)
|
---|
53 | {
|
---|
54 | size_t cbRead = fread(pBuffer, 1, cbFileIn, pFileIn);
|
---|
55 | if (cbRead == (size_t)cbFileIn)
|
---|
56 | {
|
---|
57 | pBuffer[cbFileIn] = '\0';
|
---|
58 | fclose(pFileIn);
|
---|
59 | *pcbFile = (size_t)cbFileIn;
|
---|
60 | return pBuffer;
|
---|
61 | }
|
---|
62 |
|
---|
63 | fprintf(stderr, "split-soapC: Failed to read %ld bytes from input file.\n", cbFileIn);
|
---|
64 | free(pBuffer);
|
---|
65 | }
|
---|
66 | else
|
---|
67 | fprintf(stderr, "split-soapC: Failed to allocate %ld bytes.\n", cbFileIn);
|
---|
68 | }
|
---|
69 | else
|
---|
70 | fprintf(stderr, "split-soapC: Seek failure.\n");
|
---|
71 | fclose(pFileIn);
|
---|
72 | }
|
---|
73 | else
|
---|
74 | fprintf(stderr, "split-soapC: Cannot open file \"%s\" for reading.\n", pszFile);
|
---|
75 | return NULL;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | int main(int argc, char *argv[])
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Check argument count.
|
---|
83 | */
|
---|
84 | if (argc != 4)
|
---|
85 | {
|
---|
86 | fprintf(stderr, "split-soapC: Must be started with exactly four arguments,\n"
|
---|
87 | "1) the input file, 2) the output filename prefix and\n"
|
---|
88 | "3) the number chunks to create.");
|
---|
89 | return RTEXITCODE_SYNTAX;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Number of chunks (argv[3]).
|
---|
94 | */
|
---|
95 | char *pszEnd = NULL;
|
---|
96 | unsigned long cChunks = strtoul(argv[3], &pszEnd, 0);
|
---|
97 | if (cChunks == ULONG_MAX || cChunks == 0 || !argv[3] || *pszEnd)
|
---|
98 | {
|
---|
99 | fprintf(stderr, "split-soapC: Given argument \"%s\" is not a valid chunk count.\n", argv[3]);
|
---|
100 | return RTEXITCODE_SYNTAX;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Read the input file into a zero terminated memory buffer.
|
---|
105 | */
|
---|
106 | size_t cbFileIn;
|
---|
107 | char *pszBuffer = readfileIntoBuffer(argv[1], &cbFileIn);
|
---|
108 | if (!pszBuffer)
|
---|
109 | return RTEXITCODE_FAILURE;
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Split the file.
|
---|
113 | */
|
---|
114 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
115 | FILE *pFileOut = NULL;
|
---|
116 | const char *pszLine = pszBuffer;
|
---|
117 | size_t cbChunk = cbFileIn / cChunks;
|
---|
118 | unsigned long cFiles = 0;
|
---|
119 | size_t cbLimit = 0;
|
---|
120 | size_t cbWritten = 0;
|
---|
121 | unsigned long cIfNesting = 0;
|
---|
122 | unsigned long cWarningNesting = 0;
|
---|
123 | unsigned long cBraceNesting = 0;
|
---|
124 | unsigned long cLinesSinceStaticMap = ~0UL / 2;
|
---|
125 | bool fJustZero = false;
|
---|
126 |
|
---|
127 | do
|
---|
128 | {
|
---|
129 | if (!pFileOut)
|
---|
130 | {
|
---|
131 | /* construct output filename */
|
---|
132 | char szFilename[1024];
|
---|
133 | sprintf(szFilename, "%s%lu.cpp", argv[2], ++cFiles);
|
---|
134 | szFilename[sizeof(szFilename)-1] = '\0';
|
---|
135 |
|
---|
136 | size_t offName = strlen(szFilename);
|
---|
137 | while (offName > 0 && !RTPATH_IS_SEP(szFilename[offName - 1]))
|
---|
138 | offName -= 1;
|
---|
139 | printf("info: %s\n", &szFilename[offName]);
|
---|
140 |
|
---|
141 | /* create output file */
|
---|
142 | pFileOut = fopen(szFilename, "wb");
|
---|
143 | if (!pFileOut)
|
---|
144 | {
|
---|
145 | fprintf(stderr, "split-soapC: Failed to open file \"%s\" for writing\n", szFilename);
|
---|
146 | rcExit = RTEXITCODE_FAILURE;
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | if (cFiles > 1)
|
---|
150 | fprintf(pFileOut, "#include \"soapH.h\"%s\n",
|
---|
151 | #ifdef RT_OS_WINDOWS
|
---|
152 | "\r"
|
---|
153 | #else
|
---|
154 | ""
|
---|
155 | #endif
|
---|
156 | );
|
---|
157 | cbLimit += cbChunk;
|
---|
158 | cLinesSinceStaticMap = ~0UL / 2;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* find begin of next line and print current line */
|
---|
162 | const char *pszNextLine = strchr(pszLine, '\n');
|
---|
163 | size_t cbLine;
|
---|
164 | if (pszNextLine)
|
---|
165 | {
|
---|
166 | pszNextLine++;
|
---|
167 | cbLine = pszNextLine - pszLine;
|
---|
168 | }
|
---|
169 | else
|
---|
170 | cbLine = strlen(pszLine);
|
---|
171 | if (fwrite(pszLine, 1, cbLine, pFileOut) != cbLine)
|
---|
172 | {
|
---|
173 | fprintf(stderr, "split-soapC: Failed to write to output file\n");
|
---|
174 | rcExit = RTEXITCODE_FAILURE;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | cbWritten += cbLine;
|
---|
178 |
|
---|
179 | /* process nesting depth information */
|
---|
180 | if (!strncmp(pszLine, "#if", 3))
|
---|
181 | cIfNesting++;
|
---|
182 | else if (!strncmp(pszLine, "#endif", 6))
|
---|
183 | {
|
---|
184 | cIfNesting--;
|
---|
185 | if (!cBraceNesting && !cIfNesting)
|
---|
186 | fJustZero = true;
|
---|
187 | }
|
---|
188 | else if (!strncmp(pszLine, RT_STR_TUPLE("#pragma warning(push)")))
|
---|
189 | cWarningNesting++;
|
---|
190 | else if (!strncmp(pszLine, RT_STR_TUPLE("#pragma warning(pop)")))
|
---|
191 | cWarningNesting--;
|
---|
192 | else
|
---|
193 | {
|
---|
194 | for (const char *p = pszLine; p < pszLine + cbLine; p++)
|
---|
195 | {
|
---|
196 | if (*p == '{')
|
---|
197 | cBraceNesting++;
|
---|
198 | else if (*p == '}')
|
---|
199 | {
|
---|
200 | cBraceNesting--;
|
---|
201 | if (!cBraceNesting && !cIfNesting)
|
---|
202 | fJustZero = true;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* look for static variables used for enum conversion. */
|
---|
208 | if (!strncmp(pszLine, "static const struct soap_code_map", sizeof("static const struct soap_code_map") - 1))
|
---|
209 | cLinesSinceStaticMap = 0;
|
---|
210 | else
|
---|
211 | cLinesSinceStaticMap++;
|
---|
212 |
|
---|
213 | /* start a new output file if necessary and possible */
|
---|
214 | if ( cbWritten >= cbLimit
|
---|
215 | && cIfNesting == 0
|
---|
216 | && cWarningNesting == 0
|
---|
217 | && fJustZero
|
---|
218 | && cFiles < cChunks
|
---|
219 | && cLinesSinceStaticMap > 150 /*hack!*/)
|
---|
220 | {
|
---|
221 | fclose(pFileOut);
|
---|
222 | pFileOut = NULL;
|
---|
223 | }
|
---|
224 |
|
---|
225 | fJustZero = false;
|
---|
226 | pszLine = pszNextLine;
|
---|
227 | } while (pszLine);
|
---|
228 |
|
---|
229 | printf("split-soapC: Created %lu files.\n", (unsigned long)cFiles);
|
---|
230 |
|
---|
231 | free(pszBuffer);
|
---|
232 | if (pFileOut)
|
---|
233 | fclose(pFileOut);
|
---|
234 |
|
---|
235 | return rcExit;
|
---|
236 | }
|
---|