@@ -19,7 +19,8 @@ int pack(const char *path)
1919 }
2020
2121 char binPath [MAX_PATH_LENGTH ];
22- snprintf (binPath , sizeof (binPath ), "%s.asset" , path );
22+ char * slash = strrchr (path , '\\' );
23+ snprintf (binPath , sizeof (binPath ), "%s%s.asset" , path , slash );
2324
2425 FILE * bin = fopen (binPath , "wb" );
2526
@@ -34,29 +35,33 @@ int pack(const char *path)
3435 if (strstr (files -> d_name , ".png" ) != NULL )
3536 {
3637 char filePath [MAX_PATH_LENGTH ];
37- snprintf (filePath , sizeof (filePath ), "%s/ %s" , path , files -> d_name );
38+ snprintf (filePath , sizeof (filePath ), "%s\\ %s" , path , files -> d_name );
3839
39- FILE * photo = fopen (filePath , "rb" );
40+ FILE * file = fopen (filePath , "rb" );
4041
41- if (photo == NULL )
42+ if (file == NULL )
4243 {
43- perror ("Error opening a photo file" );
44+ perror ("Error opening a file" );
4445 return -1 ;
4546 }
4647
47- fseek (photo , 0 , SEEK_END );
48- long size = ftell (photo );
49- fseek (photo , 0 , SEEK_SET );
48+ char skip = '\O' ;
49+ fwrite (& skip , sizeof (skip ), 1 , bin );
5050
51+ char name [255 ];
52+ strncpy (name , files -> d_name , sizeof (name ));
53+ fwrite (& name , sizeof (name ), 1 , bin );
54+
55+ fseek (file , 0 , SEEK_END );
56+ long size = ftell (file );
57+ fseek (file , 0 , SEEK_SET );
5158 fwrite (& size , sizeof (size ), 1 , bin );
5259
5360 char data [size ];
54- fread (data , 1 , size , photo );
55-
56- fwrite (files -> d_name , sizeof (files -> d_name ), 1 , bin );
57- fwrite (data , 1 , size , bin );
61+ fread (& data , 1 , sizeof (data ), file );
62+ fwrite (& data , 1 , sizeof (data ), bin );
5863
59- fclose (photo );
64+ fclose (file );
6065 }
6166 }
6267
@@ -67,48 +72,65 @@ int pack(const char *path)
6772
6873int unpack (const char * path )
6974{
70- char binPath [MAX_PATH_LENGTH ];
71- snprintf (binPath , sizeof (binPath ), "%s.asset" , path );
72-
73- FILE * bin = fopen (binPath , "rb" );
75+ DIR * dir ;
76+ struct dirent * bins ;
77+ dir = opendir (path );
7478
75- if (bin == NULL )
79+ if (dir == NULL )
7680 {
77- perror ("Error opening binary file " );
81+ perror ("Error opening directory " );
7882 return -1 ;
7983 }
8084
81- struct stat st = {0 };
85+ while ((bins = readdir (dir )) != NULL )
86+ {
87+ if (strstr (bins -> d_name , ".asset" ) != NULL )
88+ {
89+ char binPath [MAX_PATH_LENGTH ];
90+ snprintf (binPath , sizeof (binPath ), "%s\\%s" , path , bins -> d_name );
8291
83- if ( stat ( path , & st ) == -1 ) mkdir ( path );
92+ FILE * bin = fopen ( binPath , "rb" );
8493
85- while (!feof (bin ))
86- {
87- long size ;
88- fread (& size , sizeof (size ), 1 , bin );
94+ if (bin == NULL )
95+ {
96+ perror ("Error opening binary file" );
97+ return -1 ;
98+ }
8999
90- char name [256 ];
91- fread (name , sizeof (name ), 1 , bin );
100+ while (1 )
101+ {
102+ char skip ;
103+ if ((fread (& skip , sizeof (skip ), 1 , bin )) != 1 ) break ;
92104
93- char filePath [ MAX_PATH_LENGTH ];
94- snprintf ( filePath , sizeof (filePath ), "%s/%s" , path , name ) ;
105+ char name [ 255 ];
106+ if (( fread ( & name , sizeof (name ), 1 , bin )) != 1 ) break ;
95107
96- FILE * photo = fopen (filePath , "wb" );
108+ char filePath [MAX_PATH_LENGTH ];
109+ snprintf (filePath , sizeof (filePath ), "%s\\%s" , path , name );
97110
98- if (photo == NULL )
99- {
100- perror ("Error creating photo file" );
101- return -1 ;
102- }
111+ FILE * file = fopen (filePath , "wb" );
112+
113+ if (file == NULL )
114+ {
115+ perror ("Error creating a file" );
116+ return -1 ;
117+ }
118+
119+ long size ;
120+ fread (& size , sizeof (size ), 1 , bin );
103121
104- char data [size ];
105- fread (data , 1 , size , bin );
122+ char data [size ];
123+ fread (& data , 1 , sizeof ( data ) , bin );
106124
107- fwrite (data , 1 , size , photo );
108- fclose (photo );
125+ fwrite (& data , 1 , sizeof (data ), file );
126+ fclose (file );
127+ }
128+
129+ fclose (bin );
130+ }
109131 }
110132
111- fclose ( bin );
133+
112134 return 0 ;
113135}
114136
@@ -157,4 +179,6 @@ int main(int argc, char *argv[])
157179 fprintf (stderr , "Unknown command: %s\n" , argv [1 ]);
158180 return -1 ;
159181 }
182+
183+ system ("pause" );
160184}
0 commit comments