VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/split-soapC.cpp@ 65314

Last change on this file since 65314 was 63392, checked in by vboxsync, 8 years ago

split-soapC.cpp: format string fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
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-2016 Oracle Corporation
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
18#include <iprt/types.h>
19#include <sys/types.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <limits.h>
24
25
26char *readfileIntoBuffer(const char *pszFile, size_t *pcbFile)
27{
28 FILE *pFileIn = fopen(pszFile, "rb");
29 if (pFileIn)
30 {
31 int rc2 = fseek(pFileIn, 0, SEEK_END);
32 long cbFileIn = ftell(pFileIn);
33 int rc3 = fseek(pFileIn, 0, SEEK_SET);
34 if (rc3 != -1 && rc2 != -1 && cbFileIn >= 0)
35 {
36 char *pBuffer = (char *)malloc(cbFileIn + 1);
37 if (pBuffer)
38 {
39 size_t cbRead = fread(pBuffer, 1, cbFileIn, pFileIn);
40 if (cbRead == (size_t)cbFileIn)
41 {
42 pBuffer[cbFileIn] = '\0';
43 fclose(pFileIn);
44 *pcbFile = (size_t)cbFileIn;
45 return pBuffer;
46 }
47
48 fprintf(stderr, "split-soapC: Failed to read %ld bytes from input file.\n", cbFileIn);
49 free(pBuffer);
50 }
51 else
52 fprintf(stderr, "split-soapC: Failed to allocate %ld bytes.\n", cbFileIn);
53 }
54 else
55 fprintf(stderr, "split-soapC: Seek failure.\n");
56 fclose(pFileIn);
57 }
58 else
59 fprintf(stderr, "split-soapC: Cannot open file \"%s\" for reading.\n", pszFile);
60 return NULL;
61}
62
63
64int main(int argc, char *argv[])
65{
66 /*
67 * Check argument count.
68 */
69 if (argc != 4)
70 {
71 fprintf(stderr, "split-soapC: Must be started with exactly three arguments,\n"
72 "1) the input file, 2) the directory where to put the output files and\n"
73 "3) the number chunks to create.\n");
74 return RTEXITCODE_SYNTAX;
75 }
76
77 /*
78 * Number of chunks (argv[3]).
79 */
80 char *pszEnd = NULL;
81 unsigned long cChunks = strtoul(argv[3], &pszEnd, 0);
82 if (cChunks == ULONG_MAX || cChunks == 0 || !argv[3] || *pszEnd)
83 {
84 fprintf(stderr, "split-soapC: Given argument \"%s\" is not a valid chunk count.\n", argv[3]);
85 return RTEXITCODE_SYNTAX;
86 }
87
88 /*
89 * Read the input file into a zero terminated memory buffer.
90 */
91 size_t cbFileIn;
92 char *pszBuffer = readfileIntoBuffer(argv[1], &cbFileIn);
93 if (!pszBuffer)
94 return RTEXITCODE_FAILURE;
95
96 /*
97 * Split the file.
98 */
99 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
100 FILE *pFileOut = NULL;
101 const char *pszLine = pszBuffer;
102 size_t cbChunk = cbFileIn / cChunks;
103 unsigned long cFiles = 0;
104 size_t cbLimit = 0;
105 size_t cbWritten = 0;
106 unsigned long cIfNesting = 0;
107 unsigned long cBraceNesting = 0;
108 unsigned long cLinesSinceStaticMap = ~0UL / 2;
109 bool fJustZero = false;
110
111 do
112 {
113 if (!pFileOut)
114 {
115 /* construct output filename */
116 char szFilename[1024];
117 sprintf(szFilename, "%s/soapC-%lu.cpp", argv[2], ++cFiles);
118 szFilename[sizeof(szFilename)-1] = '\0';
119 printf("info: soapC-%lu.cpp\n", cFiles);
120
121 /* create output file */
122 pFileOut = fopen(szFilename, "wb");
123 if (!pFileOut)
124 {
125 fprintf(stderr, "split-soapC: Failed to open file \"%s\" for writing\n", szFilename);
126 rcExit = RTEXITCODE_FAILURE;
127 break;
128 }
129 if (cFiles > 1)
130 fprintf(pFileOut, "#include \"soapH.h\"%s\n",
131#ifdef RT_OS_WINDOWS
132 "\r"
133#else
134 ""
135#endif
136 );
137 cbLimit += cbChunk;
138 cLinesSinceStaticMap = ~0UL / 2;
139 }
140
141 /* find begin of next line and print current line */
142 const char *pszNextLine = strchr(pszLine, '\n');
143 size_t cbLine;
144 if (pszNextLine)
145 {
146 pszNextLine++;
147 cbLine = pszNextLine - pszLine;
148 }
149 else
150 cbLine = strlen(pszLine);
151 if (fwrite(pszLine, 1, cbLine, pFileOut) != cbLine)
152 {
153 fprintf(stderr, "split-soapC: Failed to write to output file\n");
154 rcExit = RTEXITCODE_FAILURE;
155 break;
156 }
157 cbWritten += cbLine;
158
159 /* process nesting depth information */
160 if (!strncmp(pszLine, "#if", 3))
161 cIfNesting++;
162 else if (!strncmp(pszLine, "#endif", 6))
163 {
164 cIfNesting--;
165 if (!cBraceNesting && !cIfNesting)
166 fJustZero = true;
167 }
168 else
169 {
170 for (const char *p = pszLine; p < pszLine + cbLine; p++)
171 {
172 if (*p == '{')
173 cBraceNesting++;
174 else if (*p == '}')
175 {
176 cBraceNesting--;
177 if (!cBraceNesting && !cIfNesting)
178 fJustZero = true;
179 }
180 }
181 }
182
183 /* look for static variables used for enum conversion. */
184 if (!strncmp(pszLine, "static const struct soap_code_map", sizeof("static const struct soap_code_map") - 1))
185 cLinesSinceStaticMap = 0;
186 else
187 cLinesSinceStaticMap++;
188
189 /* start a new output file if necessary and possible */
190 if ( cbWritten >= cbLimit
191 && cIfNesting == 0
192 && fJustZero
193 && cFiles < cChunks
194 && cLinesSinceStaticMap > 150 /*hack!*/)
195 {
196 fclose(pFileOut);
197 pFileOut = NULL;
198 }
199
200 fJustZero = false;
201 pszLine = pszNextLine;
202 } while (pszLine);
203
204 printf("split-soapC: Created %lu files.\n", (unsigned long)cFiles);
205
206 free(pszBuffer);
207 if (pFileOut)
208 fclose(pFileOut);
209
210 return rcExit;
211}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette