summaryrefslogtreecommitdiff
path: root/src/VBox/Main/webservice/split-soapC.cpp
blob: d491235b19967c3e37bae70df66dc0064295c7ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/** @file
 * File splitter: splits soapC.cpp into manageable pieces. It is somewhat
 * intelligent and avoids splitting inside functions or similar places.
 */

/*
 * Copyright (C) 2009-2010 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>


int main(int argc, char *argv[])
{
    int rc = 0;
    FILE *pFileIn = NULL;
    FILE *pFileOut = NULL;
    char *pBuffer = NULL;

    do
    {
        if (argc != 4)
        {
            fprintf(stderr, "split-soapC: Must be started with exactly three arguments,\n"
                            "1) the input file, 2) the directory where to put the output files and\n"
                            "3) the number chunks to create.\n");
            rc = 2;
            break;
        }

        char *pEnd = NULL;
        unsigned long cChunk = strtoul(argv[3], &pEnd, 0);
        if (cChunk == ULONG_MAX || cChunk == 0 || !argv[3] || *pEnd)
        {
            fprintf(stderr, "split-soapC: Given argument \"%s\" is not a valid chunk count.\n", argv[3]);
            rc = 2;
            break;
        }

        pFileIn = fopen(argv[1], "rb");
        if (!pFileIn)
        {
            fprintf(stderr, "split-soapC: Cannot open file \"%s\" for reading.\n", argv[1]);
            rc = 2;
            break;
        }
        int rc2 = fseek(pFileIn, 0, SEEK_END);
        long cbFileIn = ftell(pFileIn);
        int rc3 = fseek(pFileIn, 0, SEEK_SET);
        if (rc3 == -1 || rc2 == -1 || cbFileIn < 0)
        {
            fprintf(stderr, "split-soapC: Seek failure.\n");
            rc = 2;
            break;
        }

        if (!(pBuffer = (char*)malloc(cbFileIn + 1)))
        {
            fprintf(stderr, "split-soapC: Failed to allocate %ld bytes.\n", cbFileIn);
            rc = 2;
            break;
        }

        if (fread(pBuffer, 1, cbFileIn, pFileIn) != (size_t)cbFileIn)
        {
            fprintf(stderr, "split-soapC: Failed to read %ld bytes from input file.\n", cbFileIn);
            rc = 2;
            break;
        }
        pBuffer[cbFileIn] = '\0';

        const char *pLine = pBuffer;
        unsigned long cbChunk = cbFileIn / cChunk;
        unsigned long cFiles = 0;
        unsigned long uLimit = 0;
        unsigned long cbWritten = 0;
        unsigned long cIfNesting = 0;
        unsigned long cBraceNesting = 0;
        unsigned long cLinesSinceStaticMap = ~0UL / 2;
        bool fJustZero = false;

        do
        {
            if (!pFileOut)
            {
                /* construct output filename */
                char szFilename[1024];
                sprintf(szFilename, "%s/soapC-%lu.cpp", argv[2], ++cFiles);
                szFilename[sizeof(szFilename)-1] = '\0';
                printf("info: soapC-%lu.cpp\n", cFiles);

                /* create output file */
                if (!(pFileOut = fopen(szFilename, "wb")))
                {
                    fprintf(stderr, "split-soapC: Failed to open file \"%s\" for writing\n", szFilename);
                    rc = 2;
                    break;
                }
                if (cFiles > 1)
                    fprintf(pFileOut, "#include \"soapH.h\"%s\n",
#ifdef RT_OS_WINDOWS
                                      "\r"
#else /* !RT_OS_WINDOWS */
                                      ""
#endif /* !RT_OS_WINDOWS */
                           );
                uLimit += cbChunk;
                cLinesSinceStaticMap = ~0UL / 2;
            }

            /* find begin of next line and print current line */
            const char *pNextLine = strchr(pLine, '\n');
            size_t cbLine;
            if (pNextLine)
            {
                pNextLine++;
                cbLine = pNextLine - pLine;
            }
            else
                cbLine = strlen(pLine);
            if (fwrite(pLine, 1, cbLine, pFileOut) != cbLine)
            {
                fprintf(stderr, "split-soapC: Failed to write to output file\n");
                rc = 2;
                break;
            }
            cbWritten += cbLine;

            /* process nesting depth information */
            if (!strncmp(pLine, "#if", 3))
                cIfNesting++;
            else if (!strncmp(pLine, "#endif", 6))
            {
                cIfNesting--;
                if (!cBraceNesting && !cIfNesting)
                    fJustZero = true;
            }
            else
            {
                for (const char *p = pLine; p < pLine + cbLine; p++)
                {
                    if (*p == '{')
                        cBraceNesting++;
                    else if (*p == '}')
                    {
                        cBraceNesting--;
                        if (!cBraceNesting && !cIfNesting)
                            fJustZero = true;
                    }
                }
            }

            /* look for static variables used for enum conversion. */
            if (!strncmp(pLine, "static const struct soap_code_map", sizeof("static const struct soap_code_map") - 1))
                cLinesSinceStaticMap = 0;
            else
                cLinesSinceStaticMap++;

            /* start a new output file if necessary and possible */
            if (   cbWritten >= uLimit
                && cIfNesting == 0
                && fJustZero
                && cFiles < cChunk
                && cLinesSinceStaticMap > 150 /*hack!*/)
            {
                fclose(pFileOut);
                pFileOut = NULL;
            }

            if (rc)
                break;

            fJustZero = false;
            pLine = pNextLine;
        } while (pLine);

        printf("split-soapC: Created %lu files.\n", cFiles);
    } while (0);

    if (pBuffer)
        free(pBuffer);
    if (pFileIn)
        fclose(pFileIn);
    if (pFileOut)
        fclose(pFileOut);

    return rc;
}