2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
15 * Program to create an archive file for use with cfile stuff
18 * Revision 1.3 2002/06/09 04:41:15 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:43 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:08 root
28 * 2 10/23/98 6:15p Dave
40 unsigned int Total_size=16; // Start with size of header
41 unsigned int Num_files =0;
43 FILE * fp_out_hdr = NULL;
45 typedef struct vp_header {
52 //vp_header Vp_header;
54 char archive_dat[1024];
55 char archive_hdr[1024];
57 #define BLOCK_SIZE (1024*1024)
58 #define VERSION_NUMBER 2;
60 char tmp_data[BLOCK_SIZE]; // 1 MB
64 fseek(fp_out, 0, SEEK_SET);
65 fwrite("VPVP", 1, 4, fp_out);
66 int ver = VERSION_NUMBER;
67 fwrite(&ver, 1, 4, fp_out);
68 fwrite(&Total_size, 1, 4, fp_out);
69 fwrite(&Num_files, 1, 4, fp_out);
72 int write_index(char *hf, char *df)
74 FILE *h = fopen(hf, "rb");
76 FILE *d = fopen(df, "a+b");
78 for (unsigned int i=0;i<Num_files;i++) {
79 fread(tmp_data, 32+4+4+4, 1, h);
80 fwrite(tmp_data, 32+4+4+4, 1, d);
87 void pack_file( char *filespec, char *filename, int filesize, time_t time_write )
91 if ( strstr( filename, ".vp" )) {
92 // Don't pack yourself!!
96 if ( strstr( filename, ".hdr" )) {
97 // Don't pack yourself!!
101 if ( filesize == 0 ) {
102 // Don't pack 0 length files, screws up directory structure!
106 memset( path, 0, sizeof(path));
107 strcpy( path, filename );
108 if ( strlen(filename)>31 ) {
109 printf( "Filename '%s' too long\n", filename );
112 fwrite( &Total_size, 1, 4, fp_out_hdr );
113 fwrite( &filesize, 1, 4, fp_out_hdr );
114 fwrite( &path, 1, 32, fp_out_hdr );
115 fwrite( &time_write, 1, sizeof(time_t), fp_out_hdr);
117 Total_size += filesize;
119 printf( "Packing %s\\%s...", filespec, filename );
121 sprintf( path, "%s\\%s", filespec, filename );
123 FILE *fp = fopen( path, "rb" );
126 printf( "Error opening '%s'\n", path );
130 int nbytes, nbytes_read=0;
133 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
135 fwrite( tmp_data, 1, nbytes, fp_out );
136 nbytes_read += nbytes;
139 } while( nbytes > 0 );
143 printf( " %d bytes\n", nbytes_read );
146 // This function adds a directory marker to the header file
147 void add_directory( char * dirname)
150 char *pathptr = path;
153 strcpy(path, dirname);
154 fwrite(&Total_size, 1, 4, fp_out_hdr);
156 fwrite(&i, 1, 4, fp_out_hdr);
157 // strip out any directories that this dir is a subdir of
158 while ((tmpptr = strchr(pathptr, '\\')) != NULL) {
161 fwrite(pathptr, 1, 32, fp_out_hdr);
162 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
166 void pack_directory( char * filespec)
177 last_slash = strrchr(filespec, '\\');
179 strcpy(dir_name, last_slash+1);
181 strcpy(dir_name, filespec);
184 if ( !stricmp(dir_name, "voice") ) {
189 strcpy( tmp1, filespec );
190 add_directory(filespec);
191 strcat( tmp1, "\\*.*" );
193 printf( "In dir '%s'\n", tmp1 );
195 find_handle = _findfirst( tmp1, &find );
196 if( find_handle != -1 ) {
197 if ( find.attrib & _A_SUBDIR ) {
198 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
199 strcpy( tmp, filespec );
201 strcat( tmp, find.name );
205 pack_file( filespec, find.name, find.size, find.time_write );
208 while( !_findnext( find_handle, &find ) ) {
209 if ( find.attrib & _A_SUBDIR ) {
210 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
211 strcpy( tmp, filespec );
213 strcat( tmp, find.name );
218 pack_file( filespec, find.name, find.size, find.time_write );
227 int main(int argc, char *argv[] )
233 printf( "Usage: %s archive_name src_dir\n", argv[0] );
234 printf( "Example: %s freespace c:\\freespace\\data\n", argv[0] );
235 printf( "Creates an archive named freespace out of the\nfreespace data tree\n" );
236 printf( "Press any key to exit...\n" );
241 strcpy( archive, argv[1] );
242 p = strchr( archive, '.' );
243 if (p) *p = 0; // remove extension
245 strcpy( archive_dat, archive );
246 strcat( archive_dat, ".vp" );
248 strcpy( archive_hdr, archive );
249 strcat( archive_hdr, ".hdr" );
251 fp_out = fopen( archive_dat, "wb" );
253 printf( "Couldn't open '%s'!\n", archive_dat );
254 printf( "Press any key to exit...\n" );
259 fp_out_hdr = fopen( archive_hdr, "wb" );
261 printf( "Couldn't open '%s'!\n", archive_hdr );
262 printf( "Press any key to exit...\n" );
269 pack_directory( argv[2] );
276 printf( "Data files written, appending index...\n" );
278 if (!write_index(archive_hdr, archive_dat)) {
279 printf("Error appending index!\n");
280 printf("Press any key to exit...\n");
285 printf( "%d total KB.\n", Total_size/1024 );