VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/filesplitter.c@ 22762

Last change on this file since 22762 was 16122, checked in by vboxsync, 16 years ago

fixed webservice copyright

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1/** @file
2 * File splitter: splits a text file according to ###### markers in it.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26
27unsigned long lineNumber(const char *pBegin, const char *pPos);
28
29unsigned long lineNumber(const char *pBegin, const char *pPos)
30{
31 const char *p = pBegin;
32 unsigned long c = 0;
33 while (*p && p < pPos)
34 {
35 if (*p == '\n')
36 ++c;
37 ++p;
38 }
39
40 return c;
41}
42
43int main(int argc, char *argv[])
44{
45 int rc = 0;
46
47 if (argc != 3)
48 {
49 fprintf(stderr, "filesplitter: Must be started with exactly two arguments,\n"
50 "1) the input file and 2) the directory where to put the output files\n");
51 rc = 2;
52 }
53 else
54 {
55 FILE *pFileIn;
56 struct stat statIn;
57 if ( (stat(argv[1], &statIn))
58 || (!(pFileIn = fopen(argv[1], "r")))
59 )
60 {
61 fprintf(stderr, "filesplitter: Cannot open file \"%s\" for reading.\n", argv[1]);
62 rc = 2;
63 }
64 else
65 {
66 struct stat statOut;
67 if ( (stat(argv[2], &statOut))
68 || ((statOut.st_mode & S_IFMT) != S_IFDIR)
69 )
70 {
71 fprintf(stderr, "filesplitter: Given argument \"%s\" is not a valid directory.\n", argv[2]);
72 rc = 2;
73 }
74 else
75 {
76 char *p;
77 if (!(p = (char*)malloc(statIn.st_size + 1)))
78 fprintf(stderr, "filesplitter: Failed to allocate %ld bytes.\n", (long)statIn.st_size);
79 else
80 {
81 size_t read;
82 if (!(read = fread(p, 1, statIn.st_size, pFileIn)))
83 {
84 fprintf(stderr, "filesplitter: Failed to read %ld bytes from input file.\n", (long)statIn.st_size);
85 rc = 2;
86 }
87 else
88 {
89 const char *pSearch = p;
90 const char *pBegin;
91 unsigned long c = 0;
92
93 size_t lengthDirName = strlen(argv[2]);
94
95 p[read] = '\0';
96
97 do
98 {
99 const char *pcszBeginMarker = "\n// ##### BEGINFILE \"";
100 const char *pcszEndMarker = "\n// ##### ENDFILE";
101 size_t lengthBeginMarker = strlen(pcszBeginMarker);
102 const char *pSecondQuote, *pLineAfterBegin;
103 const char *pEnd;
104 char *pszFilename;
105 size_t lengthFilename;
106 FILE *pFileOut;
107
108 if (!(pBegin = strstr(pSearch, pcszBeginMarker)))
109 break;
110 if (!(pLineAfterBegin = strchr(pBegin + lengthBeginMarker, '\n')))
111 {
112 fprintf(stderr, "filesplitter: No newline after begin-file marker found.\n");
113 rc = 2;
114 break;
115 }
116 if ( (!(pSecondQuote = strchr(pBegin + lengthBeginMarker, '\"')))
117 || (pSecondQuote > pLineAfterBegin)
118 )
119 {
120 fprintf(stderr, "filesplitter: Can't parse filename after begin-file marker (line %lu).\n", lineNumber(p, pcszBeginMarker));
121 rc = 2;
122 break;
123 }
124
125 ++pLineAfterBegin;
126
127 if (!(pEnd = strstr(pLineAfterBegin, pcszEndMarker)))
128 {
129 fprintf(stderr, "filesplitter: No matching end-line marker for begin-file marker found (line %lu).\n", lineNumber(p, pcszBeginMarker));
130 rc = 2;
131 break;
132 }
133
134 lengthFilename = pSecondQuote - (pBegin + lengthBeginMarker);
135 if (!(pszFilename = (char*)malloc(lengthDirName + 1 + lengthFilename + 1)))
136 {
137 fprintf(stderr, "filesplitter: Can't allocate memory for filename.\n");
138 rc = 2;
139 break;
140 }
141 memcpy(pszFilename, argv[2], lengthDirName);
142 pszFilename[lengthDirName] = '/';
143 memcpy(pszFilename + lengthDirName + 1, pBegin + lengthBeginMarker, lengthFilename);
144 pszFilename[lengthFilename + 1 + lengthDirName] = '\0';
145
146 if (!(pFileOut = fopen(pszFilename, "w")))
147 {
148 fprintf(stderr, "filesplitter: Failed to open file \"%s\" for writing\n", pszFilename);
149 rc = 2;
150 }
151 else
152 {
153 unsigned long cbFile = pEnd - pLineAfterBegin;
154 unsigned long cbWritten = fwrite(pLineAfterBegin,
155 1,
156 cbFile,
157 pFileOut);
158 if (!cbWritten)
159 {
160 fprintf(stderr, "filesplitter: Failed to write %lu bytes to file \"%s\"\n", cbFile, pszFilename);
161 rc = 2;
162 }
163
164 fclose(pFileOut);
165
166 if (!rc)
167 {
168 ++c;
169 pSearch = strchr(pEnd, '\n');
170 }
171 }
172
173 free(pszFilename);
174
175 if (rc)
176 break;
177
178 } while (pSearch);
179
180 printf("filesplitter: Created %lu files.\n", c);
181 }
182 }
183
184 free(p);
185 }
186 }
187 }
188
189 return rc;
190}
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