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