]> icculus.org git repositories - taylor/freespace2.git/blob - src/cfilearchiver/cfilearchiver.cpp
The Great Newline Fix
[taylor/freespace2.git] / src / cfilearchiver / cfilearchiver.cpp
1 /*
2  * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Program to create an archive file for use with cfile stuff
8  *
9  * $Log$
10  * Revision 1.2  2002/05/07 03:16:43  theoddone33
11  * The Great Newline Fix
12  *
13  * Revision 1.1.1.1  2002/05/03 03:28:08  root
14  * Initial import.
15  *
16  * 
17  * 2     10/23/98 6:15p Dave
18  *
19  * $NoKeywords: $
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <direct.h>
25 #include <io.h>
26 #include <string.h>
27 #include <conio.h>
28
29 unsigned int Total_size=16; // Start with size of header
30 unsigned int Num_files =0;
31 FILE * fp_out = NULL;
32 FILE * fp_out_hdr = NULL;
33
34 typedef struct vp_header {
35         char id[4];
36         int version;
37         int index_offset;
38         int num_files;
39 } vp_header;
40
41 //vp_header Vp_header;
42
43 char archive_dat[1024];
44 char archive_hdr[1024];
45
46 #define BLOCK_SIZE (1024*1024)
47 #define VERSION_NUMBER 2;
48
49 char tmp_data[BLOCK_SIZE];              // 1 MB
50
51 void write_header()
52 {
53         fseek(fp_out, 0, SEEK_SET);
54         fwrite("VPVP", 1, 4, fp_out);
55         int ver = VERSION_NUMBER;
56         fwrite(&ver, 1, 4, fp_out);
57         fwrite(&Total_size, 1, 4, fp_out);
58         fwrite(&Num_files, 1, 4, fp_out);
59 }
60
61 int write_index(char *hf, char *df)
62 {
63         FILE *h = fopen(hf, "rb");
64         if (!h) return 0;
65         FILE *d = fopen(df, "a+b");
66         if (!d) return 0;
67         for (unsigned int i=0;i<Num_files;i++) {
68                 fread(tmp_data, 32+4+4+4, 1, h);
69                 fwrite(tmp_data, 32+4+4+4, 1, d);
70         }
71         fclose(h);
72         fclose(d);
73         return 1;
74 }
75
76 void pack_file( char *filespec, char *filename, int filesize, time_t time_write )
77 {
78         char path[1024];
79
80         if ( strstr( filename, ".vp" )) {
81                 // Don't pack yourself!!
82                 return;
83         }
84
85         if ( strstr( filename, ".hdr" ))        {
86                 // Don't pack yourself!!
87                 return;
88         }
89
90         if ( filesize == 0 ) {
91                 // Don't pack 0 length files, screws up directory structure!
92                 return;
93         }
94
95         memset( path, 0, sizeof(path));
96         strcpy( path, filename );
97         if ( strlen(filename)>31 )      {
98                 printf( "Filename '%s' too long\n", filename );
99                 exit(1);
100         }
101         fwrite( &Total_size, 1, 4, fp_out_hdr );
102         fwrite( &filesize, 1, 4, fp_out_hdr );
103         fwrite( &path, 1, 32, fp_out_hdr );
104         fwrite( &time_write, 1, sizeof(time_t), fp_out_hdr);
105
106         Total_size += filesize;
107         Num_files++;
108         printf( "Packing %s\\%s...", filespec, filename );
109
110         sprintf( path, "%s\\%s", filespec, filename );
111
112         FILE *fp = fopen( path, "rb" );
113         
114         if ( fp == NULL )       {
115                 printf( "Error opening '%s'\n", path );
116                 exit(1);
117         }
118
119         int nbytes, nbytes_read=0;
120
121         do      {
122                 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
123                 if ( nbytes > 0 )       {
124                         fwrite( tmp_data, 1, nbytes, fp_out );
125                         nbytes_read += nbytes;
126
127                 }
128         } while( nbytes > 0 );
129
130         fclose(fp);
131
132         printf( " %d bytes\n", nbytes_read );
133 }
134
135 // This function adds a directory marker to the header file
136 void add_directory( char * dirname)
137 {
138         char path[256];
139         char *pathptr = path;
140         char *tmpptr;
141
142         strcpy(path, dirname);
143         fwrite(&Total_size, 1, 4, fp_out_hdr);
144         int i = 0;
145         fwrite(&i, 1, 4, fp_out_hdr);
146         // strip out any directories that this dir is a subdir of
147         while ((tmpptr = strchr(pathptr, '\\')) != NULL) {
148                 pathptr = tmpptr+1;
149         }
150         fwrite(pathptr, 1, 32, fp_out_hdr);
151         fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
152         Num_files++;
153 }
154
155 void pack_directory( char * filespec)
156 {
157         int find_handle;
158         _finddata_t find;
159         char tmp[512];
160         char tmp1[512];
161
162 /*
163         char dir_name[512];
164         char *last_slash;
165
166         last_slash = strrchr(filespec, '\\');
167         if ( last_slash ) {
168                 strcpy(dir_name, last_slash+1);
169         } else {
170                 strcpy(dir_name, filespec);
171         }
172
173         if ( !stricmp(dir_name, "voice") ) {
174                 return;
175         }
176 */
177
178         strcpy( tmp1, filespec );
179         add_directory(filespec);
180         strcat( tmp1, "\\*.*" );
181         
182         printf( "In dir '%s'\n", tmp1 );
183
184         find_handle = _findfirst( tmp1, &find );
185         if( find_handle != -1 ) {
186                 if ( find.attrib & _A_SUBDIR )  {
187                         if (strcmp( "..", find.name) && strcmp( ".", find.name))        {
188                                 strcpy( tmp, filespec );
189                                 strcat( tmp, "\\" );
190                                 strcat( tmp, find.name );
191                                 pack_directory(tmp);
192                         }
193                 } else {
194                         pack_file( filespec, find.name, find.size, find.time_write );
195                 }
196
197                 while( !_findnext( find_handle, &find ) )       {
198                         if ( find.attrib & _A_SUBDIR )  {
199                                 if (strcmp( "..", find.name) && strcmp( ".", find.name))        {
200                                         strcpy( tmp, filespec );
201                                         strcat( tmp, "\\" );
202                                         strcat( tmp, find.name );
203                                         pack_directory(tmp);
204
205                                 }
206                         } else {
207                                 pack_file( filespec, find.name, find.size, find.time_write );
208                         }
209                 }
210         }
211         add_directory("..");
212 }
213
214
215
216 int main(int argc, char *argv[] )
217 {
218         char archive[1024];
219         char *p;
220
221         if ( argc < 3 ) {
222                 printf( "Usage: %s archive_name src_dir\n", argv[0] );
223                 printf( "Example: %s freespace c:\\freespace\\data\n", argv[0] );
224                 printf( "Creates an archive named freespace out of the\nfreespace data tree\n" );
225                 printf( "Press any key to exit...\n" );
226                 getch();
227                 return 1;
228         }
229
230         strcpy( archive, argv[1] );
231         p = strchr( archive, '.' );
232         if (p) *p = 0;          // remove extension     
233
234         strcpy( archive_dat, archive );
235         strcat( archive_dat, ".vp" );
236
237         strcpy( archive_hdr, archive );
238         strcat( archive_hdr, ".hdr" );
239
240         fp_out = fopen( archive_dat, "wb" );
241         if ( !fp_out )  {
242                 printf( "Couldn't open '%s'!\n", archive_dat );
243                 printf( "Press any key to exit...\n" );
244                 getch();
245                 return 1;
246         }
247
248         fp_out_hdr = fopen( archive_hdr, "wb" );
249         if ( !fp_out_hdr )      {
250                 printf( "Couldn't open '%s'!\n", archive_hdr );
251                 printf( "Press any key to exit...\n" );
252                 getch();
253                 return 1;
254         }
255
256         write_header();
257
258         pack_directory( argv[2] );
259
260         write_header();
261
262         fclose(fp_out);
263         fclose(fp_out_hdr);
264
265         printf( "Data files written, appending index...\n" );
266
267         if (!write_index(archive_hdr, archive_dat)) {
268                 printf("Error appending index!\n");
269                 printf("Press any key to exit...\n");
270                 getch();
271                 return 1;
272         }
273         
274         printf( "%d total KB.\n", Total_size/1024 );
275         return 0;
276 }
277