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 | * Sun Microsystems, Inc. confidential
|
---|
9 | * All rights reserved
|
---|
10 | */
|
---|
11 |
|
---|
12 | #include <sys/types.h>
|
---|
13 | #include <sys/stat.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <string.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 |
|
---|
18 | unsigned long lineNumber(const char *pBegin, const char *pPos);
|
---|
19 |
|
---|
20 | unsigned long lineNumber(const char *pBegin, const char *pPos)
|
---|
21 | {
|
---|
22 | const char *p = pBegin;
|
---|
23 | unsigned long c = 0;
|
---|
24 | while (*p && p < pPos)
|
---|
25 | {
|
---|
26 | if (*p == '\n')
|
---|
27 | ++c;
|
---|
28 | ++p;
|
---|
29 | }
|
---|
30 |
|
---|
31 | return c;
|
---|
32 | }
|
---|
33 |
|
---|
34 | int main(int argc, char *argv[])
|
---|
35 | {
|
---|
36 | int rc = 0;
|
---|
37 |
|
---|
38 | if (argc != 3)
|
---|
39 | {
|
---|
40 | fprintf(stderr, "filesplitter: Must be started with exactly two arguments,\n"
|
---|
41 | "1) the input file and 2) the directory where to put the output files\n");
|
---|
42 | rc = 2;
|
---|
43 | }
|
---|
44 | else
|
---|
45 | {
|
---|
46 | FILE *pFileIn;
|
---|
47 | struct stat statIn;
|
---|
48 | if ( (stat(argv[1], &statIn))
|
---|
49 | || (!(pFileIn = fopen(argv[1], "r")))
|
---|
50 | )
|
---|
51 | {
|
---|
52 | fprintf(stderr, "filesplitter: Cannot open file \"%s\" for reading.\n", argv[1]);
|
---|
53 | rc = 2;
|
---|
54 | }
|
---|
55 | else
|
---|
56 | {
|
---|
57 | struct stat statOut;
|
---|
58 | if ( (stat(argv[2], &statOut))
|
---|
59 | || ((statOut.st_mode & S_IFMT) != S_IFDIR)
|
---|
60 | )
|
---|
61 | {
|
---|
62 | fprintf(stderr, "filesplitter: Given argument \"%s\" is not a valid directory.\n", argv[2]);
|
---|
63 | rc = 2;
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | char *p;
|
---|
68 | if (!(p = (char*)malloc(statIn.st_size + 1)))
|
---|
69 | fprintf(stderr, "filesplitter: Failed to allocate %ld bytes.\n", (long)statIn.st_size);
|
---|
70 | else
|
---|
71 | {
|
---|
72 | size_t read;
|
---|
73 | if (!(read = fread(p, 1, statIn.st_size, pFileIn)))
|
---|
74 | {
|
---|
75 | fprintf(stderr, "filesplitter: Failed to read %ld bytes from input file.\n", (long)statIn.st_size);
|
---|
76 | rc = 2;
|
---|
77 | }
|
---|
78 | else
|
---|
79 | {
|
---|
80 | const char *pSearch = p;
|
---|
81 | const char *pBegin;
|
---|
82 | unsigned long c = 0;
|
---|
83 |
|
---|
84 | size_t lengthDirName = strlen(argv[2]);
|
---|
85 |
|
---|
86 | p[read] = '\0';
|
---|
87 |
|
---|
88 | do
|
---|
89 | {
|
---|
90 | const char *pcszBeginMarker = "\n// ##### BEGINFILE \"";
|
---|
91 | const char *pcszEndMarker = "\n// ##### ENDFILE";
|
---|
92 | size_t lengthBeginMarker = strlen(pcszBeginMarker);
|
---|
93 | const char *pSecondQuote, *pLineAfterBegin;
|
---|
94 | const char *pEnd;
|
---|
95 | char *pszFilename;
|
---|
96 | size_t lengthFilename;
|
---|
97 | FILE *pFileOut;
|
---|
98 |
|
---|
99 | if (!(pBegin = strstr(pSearch, pcszBeginMarker)))
|
---|
100 | break;
|
---|
101 | if (!(pLineAfterBegin = strchr(pBegin + lengthBeginMarker, '\n')))
|
---|
102 | {
|
---|
103 | fprintf(stderr, "filesplitter: No newline after begin-file marker found.\n");
|
---|
104 | rc = 2;
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | if ( (!(pSecondQuote = strchr(pBegin + lengthBeginMarker, '\"')))
|
---|
108 | || (pSecondQuote > pLineAfterBegin)
|
---|
109 | )
|
---|
110 | {
|
---|
111 | fprintf(stderr, "filesplitter: Can't parse filename after begin-file marker (line %lu).\n", lineNumber(p, pcszBeginMarker));
|
---|
112 | rc = 2;
|
---|
113 | break;
|
---|
114 | }
|
---|
115 |
|
---|
116 | ++pLineAfterBegin;
|
---|
117 |
|
---|
118 | if (!(pEnd = strstr(pLineAfterBegin, pcszEndMarker)))
|
---|
119 | {
|
---|
120 | fprintf(stderr, "filesplitter: No matching end-line marker for begin-file marker found (line %lu).\n", lineNumber(p, pcszBeginMarker));
|
---|
121 | rc = 2;
|
---|
122 | break;
|
---|
123 | }
|
---|
124 |
|
---|
125 | lengthFilename = pSecondQuote - (pBegin + lengthBeginMarker);
|
---|
126 | if (!(pszFilename = (char*)malloc(lengthDirName + 1 + lengthFilename + 1)))
|
---|
127 | {
|
---|
128 | fprintf(stderr, "filesplitter: Can't allocate memory for filename.\n");
|
---|
129 | rc = 2;
|
---|
130 | break;
|
---|
131 | }
|
---|
132 | memcpy(pszFilename, argv[2], lengthDirName);
|
---|
133 | pszFilename[lengthDirName] = '/';
|
---|
134 | memcpy(pszFilename + lengthDirName + 1, pBegin + lengthBeginMarker, lengthFilename);
|
---|
135 | pszFilename[lengthFilename + 1 + lengthDirName] = '\0';
|
---|
136 |
|
---|
137 | if (!(pFileOut = fopen(pszFilename, "w")))
|
---|
138 | {
|
---|
139 | fprintf(stderr, "filesplitter: Failed to open file \"%s\" for writing\n", pszFilename);
|
---|
140 | rc = 2;
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | unsigned long cbFile = pEnd - pLineAfterBegin;
|
---|
145 | unsigned long cbWritten = fwrite(pLineAfterBegin,
|
---|
146 | 1,
|
---|
147 | cbFile,
|
---|
148 | pFileOut);
|
---|
149 | if (!cbWritten)
|
---|
150 | {
|
---|
151 | fprintf(stderr, "filesplitter: Failed to write %lu bytes to file \"%s\"\n", cbFile, pszFilename);
|
---|
152 | rc = 2;
|
---|
153 | }
|
---|
154 |
|
---|
155 | fclose(pFileOut);
|
---|
156 |
|
---|
157 | if (!rc)
|
---|
158 | {
|
---|
159 | ++c;
|
---|
160 | pSearch = strchr(pEnd, '\n');
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | free(pszFilename);
|
---|
165 |
|
---|
166 | if (rc)
|
---|
167 | break;
|
---|
168 |
|
---|
169 | } while (pSearch);
|
---|
170 |
|
---|
171 | printf("filesplitter: Created %lu files.\n", c);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | free(p);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | return rc;
|
---|
181 | }
|
---|