@@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2020#include <stdint.h>
2121#include <string.h>
2222#include <stdlib.h>
23+ #include <ctype.h>
2324
2425static FILE * out ;
2526
@@ -57,10 +58,11 @@ static void write_header (int32_t directory_offset, int32_t directory_size)
5758
5859int main (int argc , char * argv [])
5960{
61+ FILE * toc_file = NULL ;
6062
6163 if (argc < 4 )
6264 {
63- fprintf (stderr , "Usage: mkpak [output.pak] [root dir for files] [files... ]\n" );
65+ fprintf (stderr , "Usage: mkpak [output.pak] [root dir for files] [toc file ]\n" );
6466 return 1 ;
6567 }
6668
@@ -70,9 +72,28 @@ int main (int argc, char *argv[])
7072 fprintf (stderr , "Could not open output file '%s'" , argv [1 ]);
7173 return 1 ;
7274 }
75+ // read as text file
76+ toc_file = fopen (argv [3 ], "r" );
77+ if (toc_file == NULL )
78+ {
79+ fprintf (stderr , "Could not open toc_file file '%s'" , argv [3 ]);
80+ return 1 ;
81+ }
82+
83+ char entry_path [1024 ] = {0 };
84+
85+ int32_t num_in_files = 0 ;
86+
87+ // count the number of lines = num_in_files
88+ while (fgets (entry_path , sizeof (entry_path ), toc_file ))
89+ {
90+ num_in_files ++ ;
91+ }
92+ // rewind
93+ rewind (toc_file );
7394
7495 int32_t directory_offset = 12 ;
75- int32_t num_in_files = argc - 3 ;
96+
7697 int32_t directory_size = num_in_files * sizeof (dpackfile_t );
7798 int32_t file_offset = directory_offset + directory_size ;
7899
@@ -83,13 +104,30 @@ int main (int argc, char *argv[])
83104
84105 char input_file_path [1024 ] = {0 };
85106
86- for (int i = 3 ; i < argc ; ++ i )
107+ int file_index = -1 ;
108+ // read each line
109+ while (fgets (entry_path , sizeof (entry_path ), toc_file ))
87110 {
88- int file_index = i - 3 ;
111+ // trim leading and trailing whaitespaces:
112+ char * entry_path_start = entry_path ;
113+
114+ while (isspace ((unsigned char )* entry_path_start ))
115+ entry_path_start ++ ;
116+
117+ for (int char_index = 0 ; char_index < strlen (entry_path_start ); char_index ++ )
118+ {
119+ if (isspace (entry_path_start [char_index ]))
120+ {
121+ entry_path_start [char_index ] = '\0' ;
122+ break ;
123+ }
124+ }
125+
126+ file_index ++ ;
89127 // prepend the root dir
90- snprintf (input_file_path , sizeof (input_file_path ), "%s/%s" , argv [2 ], argv [ i ] );
128+ snprintf (input_file_path , sizeof (input_file_path ), "%s/%s" , argv [2 ], entry_path_start );
91129
92- // printf (" argv[%d]= %s\n", i , input_file_path);
130+ // printf (" pak entry = %s, filename = %s\n", entry_path_start , input_file_path);
93131
94132 in = fopen (input_file_path , "rb" );
95133
@@ -107,7 +145,7 @@ int main (int argc, char *argv[])
107145
108146 dpackfile_t pack_entry ;
109147 memset (& pack_entry , 0 , sizeof (pack_entry ));
110- strncpy (pack_entry .name , argv [ i ] , sizeof (pack_entry .name ) - 1 );
148+ strncpy (pack_entry .name , entry_path_start , sizeof (pack_entry .name ) - 1 );
111149 pack_entry .filelen = (int )in_size ;
112150 pack_entry .filepos = file_offset ;
113151
@@ -119,7 +157,11 @@ int main (int argc, char *argv[])
119157
120158 file_offset += (int32_t )in_size ;
121159 fclose (in );
122- }
160+
161+ } // end while
162+
163+ fclose (out );
164+ fclose (toc_file );
123165 free (in_buffer );
124166
125167 return 0 ;
0 commit comments