2 * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
7 * Program to create an archive file for use with cfile stuff
10 * Revision 1.2 2002/05/07 03:16:43 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:08 root
17 * 2 10/23/98 6:15p Dave
29 unsigned int Total_size=16; // Start with size of header
30 unsigned int Num_files =0;
32 FILE * fp_out_hdr = NULL;
34 typedef struct vp_header {
41 //vp_header Vp_header;
43 char archive_dat[1024];
44 char archive_hdr[1024];
46 #define BLOCK_SIZE (1024*1024)
47 #define VERSION_NUMBER 2;
49 char tmp_data[BLOCK_SIZE]; // 1 MB
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);
61 int write_index(char *hf, char *df)
63 FILE *h = fopen(hf, "rb");
65 FILE *d = fopen(df, "a+b");
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);
76 void pack_file( char *filespec, char *filename, int filesize, time_t time_write )
80 if ( strstr( filename, ".vp" )) {
81 // Don't pack yourself!!
85 if ( strstr( filename, ".hdr" )) {
86 // Don't pack yourself!!
90 if ( filesize == 0 ) {
91 // Don't pack 0 length files, screws up directory structure!
95 memset( path, 0, sizeof(path));
96 strcpy( path, filename );
97 if ( strlen(filename)>31 ) {
98 printf( "Filename '%s' too long\n", filename );
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);
106 Total_size += filesize;
108 printf( "Packing %s\\%s...", filespec, filename );
110 sprintf( path, "%s\\%s", filespec, filename );
112 FILE *fp = fopen( path, "rb" );
115 printf( "Error opening '%s'\n", path );
119 int nbytes, nbytes_read=0;
122 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
124 fwrite( tmp_data, 1, nbytes, fp_out );
125 nbytes_read += nbytes;
128 } while( nbytes > 0 );
132 printf( " %d bytes\n", nbytes_read );
135 // This function adds a directory marker to the header file
136 void add_directory( char * dirname)
139 char *pathptr = path;
142 strcpy(path, dirname);
143 fwrite(&Total_size, 1, 4, fp_out_hdr);
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) {
150 fwrite(pathptr, 1, 32, fp_out_hdr);
151 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
155 void pack_directory( char * filespec)
166 last_slash = strrchr(filespec, '\\');
168 strcpy(dir_name, last_slash+1);
170 strcpy(dir_name, filespec);
173 if ( !stricmp(dir_name, "voice") ) {
178 strcpy( tmp1, filespec );
179 add_directory(filespec);
180 strcat( tmp1, "\\*.*" );
182 printf( "In dir '%s'\n", tmp1 );
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 );
190 strcat( tmp, find.name );
194 pack_file( filespec, find.name, find.size, find.time_write );
197 while( !_findnext( find_handle, &find ) ) {
198 if ( find.attrib & _A_SUBDIR ) {
199 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
200 strcpy( tmp, filespec );
202 strcat( tmp, find.name );
207 pack_file( filespec, find.name, find.size, find.time_write );
216 int main(int argc, char *argv[] )
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" );
230 strcpy( archive, argv[1] );
231 p = strchr( archive, '.' );
232 if (p) *p = 0; // remove extension
234 strcpy( archive_dat, archive );
235 strcat( archive_dat, ".vp" );
237 strcpy( archive_hdr, archive );
238 strcat( archive_hdr, ".hdr" );
240 fp_out = fopen( archive_dat, "wb" );
242 printf( "Couldn't open '%s'!\n", archive_dat );
243 printf( "Press any key to exit...\n" );
248 fp_out_hdr = fopen( archive_hdr, "wb" );
250 printf( "Couldn't open '%s'!\n", archive_hdr );
251 printf( "Press any key to exit...\n" );
258 pack_directory( argv[2] );
265 printf( "Data files written, appending index...\n" );
267 if (!write_index(archive_hdr, archive_dat)) {
268 printf("Error appending index!\n");
269 printf("Press any key to exit...\n");
274 printf( "%d total KB.\n", Total_size/1024 );