2 * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
7 * Program to create an archive file for use with cfile stuff
10 * Revision 1.1 2002/05/03 03:28:08 root
14 * 2 10/23/98 6:15p Dave
26 unsigned int Total_size=16; // Start with size of header
27 unsigned int Num_files =0;
29 FILE * fp_out_hdr = NULL;
31 typedef struct vp_header {
38 //vp_header Vp_header;
40 char archive_dat[1024];
41 char archive_hdr[1024];
43 #define BLOCK_SIZE (1024*1024)
44 #define VERSION_NUMBER 2;
46 char tmp_data[BLOCK_SIZE]; // 1 MB
50 fseek(fp_out, 0, SEEK_SET);
51 fwrite("VPVP", 1, 4, fp_out);
52 int ver = VERSION_NUMBER;
53 fwrite(&ver, 1, 4, fp_out);
54 fwrite(&Total_size, 1, 4, fp_out);
55 fwrite(&Num_files, 1, 4, fp_out);
58 int write_index(char *hf, char *df)
60 FILE *h = fopen(hf, "rb");
62 FILE *d = fopen(df, "a+b");
64 for (unsigned int i=0;i<Num_files;i++) {
65 fread(tmp_data, 32+4+4+4, 1, h);
66 fwrite(tmp_data, 32+4+4+4, 1, d);
73 void pack_file( char *filespec, char *filename, int filesize, time_t time_write )
77 if ( strstr( filename, ".vp" )) {
78 // Don't pack yourself!!
82 if ( strstr( filename, ".hdr" )) {
83 // Don't pack yourself!!
87 if ( filesize == 0 ) {
88 // Don't pack 0 length files, screws up directory structure!
92 memset( path, 0, sizeof(path));
93 strcpy( path, filename );
94 if ( strlen(filename)>31 ) {
95 printf( "Filename '%s' too long\n", filename );
98 fwrite( &Total_size, 1, 4, fp_out_hdr );
99 fwrite( &filesize, 1, 4, fp_out_hdr );
100 fwrite( &path, 1, 32, fp_out_hdr );
101 fwrite( &time_write, 1, sizeof(time_t), fp_out_hdr);
103 Total_size += filesize;
105 printf( "Packing %s\\%s...", filespec, filename );
107 sprintf( path, "%s\\%s", filespec, filename );
109 FILE *fp = fopen( path, "rb" );
112 printf( "Error opening '%s'\n", path );
116 int nbytes, nbytes_read=0;
119 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
121 fwrite( tmp_data, 1, nbytes, fp_out );
122 nbytes_read += nbytes;
125 } while( nbytes > 0 );
129 printf( " %d bytes\n", nbytes_read );
132 // This function adds a directory marker to the header file
133 void add_directory( char * dirname)
136 char *pathptr = path;
139 strcpy(path, dirname);
140 fwrite(&Total_size, 1, 4, fp_out_hdr);
142 fwrite(&i, 1, 4, fp_out_hdr);
143 // strip out any directories that this dir is a subdir of
144 while ((tmpptr = strchr(pathptr, '\\')) != NULL) {
147 fwrite(pathptr, 1, 32, fp_out_hdr);
148 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
152 void pack_directory( char * filespec)
163 last_slash = strrchr(filespec, '\\');
165 strcpy(dir_name, last_slash+1);
167 strcpy(dir_name, filespec);
170 if ( !stricmp(dir_name, "voice") ) {
175 strcpy( tmp1, filespec );
176 add_directory(filespec);
177 strcat( tmp1, "\\*.*" );
179 printf( "In dir '%s'\n", tmp1 );
181 find_handle = _findfirst( tmp1, &find );
182 if( find_handle != -1 ) {
183 if ( find.attrib & _A_SUBDIR ) {
184 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
185 strcpy( tmp, filespec );
187 strcat( tmp, find.name );
191 pack_file( filespec, find.name, find.size, find.time_write );
194 while( !_findnext( find_handle, &find ) ) {
195 if ( find.attrib & _A_SUBDIR ) {
196 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
197 strcpy( tmp, filespec );
199 strcat( tmp, find.name );
204 pack_file( filespec, find.name, find.size, find.time_write );
213 int main(int argc, char *argv[] )
219 printf( "Usage: %s archive_name src_dir\n", argv[0] );
220 printf( "Example: %s freespace c:\\freespace\\data\n", argv[0] );
221 printf( "Creates an archive named freespace out of the\nfreespace data tree\n" );
222 printf( "Press any key to exit...\n" );
227 strcpy( archive, argv[1] );
228 p = strchr( archive, '.' );
229 if (p) *p = 0; // remove extension
231 strcpy( archive_dat, archive );
232 strcat( archive_dat, ".vp" );
234 strcpy( archive_hdr, archive );
235 strcat( archive_hdr, ".hdr" );
237 fp_out = fopen( archive_dat, "wb" );
239 printf( "Couldn't open '%s'!\n", archive_dat );
240 printf( "Press any key to exit...\n" );
245 fp_out_hdr = fopen( archive_hdr, "wb" );
247 printf( "Couldn't open '%s'!\n", archive_hdr );
248 printf( "Press any key to exit...\n" );
255 pack_directory( argv[2] );
262 printf( "Data files written, appending index...\n" );
264 if (!write_index(archive_hdr, archive_dat)) {
265 printf("Error appending index!\n");
266 printf("Press any key to exit...\n");
271 printf( "%d total KB.\n", Total_size/1024 );