2
* File splitter: splits a text file according to ###### markers in it.
6
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
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.
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.
21
#include <sys/types.h>
27
unsigned long lineNumber(const char *pBegin, const char *pPos);
29
unsigned long lineNumber(const char *pBegin, const char *pPos)
31
const char *p = pBegin;
33
while (*p && p < pPos)
43
int main(int argc, char *argv[])
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");
57
if ( (stat(argv[1], &statIn))
58
|| (!(pFileIn = fopen(argv[1], "r")))
61
fprintf(stderr, "filesplitter: Cannot open file \"%s\" for reading.\n", argv[1]);
67
if ( (stat(argv[2], &statOut))
68
|| ((statOut.st_mode & S_IFMT) != S_IFDIR)
71
fprintf(stderr, "filesplitter: Given argument \"%s\" is not a valid directory.\n", argv[2]);
77
if (!(p = (char*)malloc(statIn.st_size + 1)))
78
fprintf(stderr, "filesplitter: Failed to allocate %ld bytes.\n", (long)statIn.st_size);
82
if (!(read = fread(p, 1, statIn.st_size, pFileIn)))
84
fprintf(stderr, "filesplitter: Failed to read %ld bytes from input file.\n", (long)statIn.st_size);
89
const char *pSearch = p;
93
size_t lengthDirName = strlen(argv[2]);
99
const char *pcszBeginMarker = "\n// ##### BEGINFILE \"";
100
const char *pcszEndMarker = "\n// ##### ENDFILE";
101
size_t lengthBeginMarker = strlen(pcszBeginMarker);
102
const char *pSecondQuote, *pLineAfterBegin;
105
size_t lengthFilename;
108
if (!(pBegin = strstr(pSearch, pcszBeginMarker)))
110
if (!(pLineAfterBegin = strchr(pBegin + lengthBeginMarker, '\n')))
112
fprintf(stderr, "filesplitter: No newline after begin-file marker found.\n");
116
if ( (!(pSecondQuote = strchr(pBegin + lengthBeginMarker, '\"')))
117
|| (pSecondQuote > pLineAfterBegin)
120
fprintf(stderr, "filesplitter: Can't parse filename after begin-file marker (line %lu).\n", lineNumber(p, pcszBeginMarker));
127
if (!(pEnd = strstr(pLineAfterBegin, pcszEndMarker)))
129
fprintf(stderr, "filesplitter: No matching end-line marker for begin-file marker found (line %lu).\n", lineNumber(p, pcszBeginMarker));
134
lengthFilename = pSecondQuote - (pBegin + lengthBeginMarker);
135
if (!(pszFilename = (char*)malloc(lengthDirName + 1 + lengthFilename + 1)))
137
fprintf(stderr, "filesplitter: Can't allocate memory for filename.\n");
141
memcpy(pszFilename, argv[2], lengthDirName);
142
pszFilename[lengthDirName] = '/';
143
memcpy(pszFilename + lengthDirName + 1, pBegin + lengthBeginMarker, lengthFilename);
144
pszFilename[lengthFilename + 1 + lengthDirName] = '\0';
146
if (!(pFileOut = fopen(pszFilename, "w")))
148
fprintf(stderr, "filesplitter: Failed to open file \"%s\" for writing\n", pszFilename);
153
unsigned long cbFile = pEnd - pLineAfterBegin;
154
unsigned long cbWritten = fwrite(pLineAfterBegin,
160
fprintf(stderr, "filesplitter: Failed to write %lu bytes to file \"%s\"\n", cbFile, pszFilename);
169
pSearch = strchr(pEnd, '\n');
180
printf("filesplitter: Created %lu files.\n", c);