move eet to HEAD
[profile/ivi/eet.git] / src / bin / eet_main.c
1 #include "Eet.h"
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <dirent.h>
10 #include <limits.h>
11 #include <fnmatch.h>
12
13 /* just some sample code on how to use encoder/decoders */
14 #if 0
15 #include <Evas.h>
16
17 typedef struct _blah2
18 {
19    char *string;
20 }
21 Blah2;
22
23 typedef struct _blah3
24 {
25    char *string;
26 }
27 Blah3;
28
29 typedef struct _blah
30 {
31    char character;
32    short sixteen;
33    int integer;
34    long long lots;
35    float floating;
36    double floating_lots;
37    char *string;
38    Blah2 *blah2;
39    Evas_List *blah3;
40 }
41 Blah;
42
43 void
44 encdectest(void)
45 {
46    Blah blah;
47    Blah2 blah2;
48    Blah3 blah3;
49    Eet_Data_Descriptor *edd, *edd2, *edd3;
50    void *data;
51    int size;
52    FILE *f;
53    Blah *blah_in;
54    
55    edd3 = eet_data_descriptor_new("blah3", sizeof(Blah3),
56                                   evas_list_next, 
57                                   evas_list_append, 
58                                   evas_list_data, 
59                                   evas_hash_foreach, 
60                                   evas_hash_add);
61    EET_DATA_DESCRIPTOR_ADD_BASIC(edd3, Blah3, "string3", string, EET_T_STRING);
62    
63    edd2 = eet_data_descriptor_new("blah2", sizeof(Blah2),
64                                   evas_list_next, 
65                                   evas_list_append, 
66                                   evas_list_data, 
67                                   evas_hash_foreach, 
68                                   evas_hash_add);
69    EET_DATA_DESCRIPTOR_ADD_BASIC(edd2, Blah2, "string2", string, EET_T_STRING);
70    
71    edd = eet_data_descriptor_new("blah", sizeof(Blah),
72                                   evas_list_next, 
73                                   evas_list_append, 
74                                   evas_list_data, 
75                                   evas_hash_foreach, 
76                                   evas_hash_add);
77    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "character", character, EET_T_CHAR);
78    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "sixteen", sixteen, EET_T_SHORT);
79    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "integer", integer, EET_T_INT);
80    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "lots", lots, EET_T_LONG_LONG);
81    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "floating", floating, EET_T_FLOAT);
82    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "floating_lots", floating_lots, EET_T_DOUBLE);
83    EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Blah, "string", string, EET_T_STRING);
84    EET_DATA_DESCRIPTOR_ADD_SUB  (edd, Blah, "blah2", blah2, edd2);
85    EET_DATA_DESCRIPTOR_ADD_LIST (edd, Blah, "blah3", blah3, edd3);
86
87    blah3.string="PANTS";
88    
89    blah2.string="subtype string here!";
90    
91    blah.character='7';
92    blah.sixteen=0x7777;
93    blah.integer=0xc0def00d;
94    blah.lots=0xdeadbeef31337777;
95    blah.floating=3.141592654;
96    blah.floating_lots=0.777777777777777;
97    blah.string="bite me like a turnip";
98    blah.blah2 = &blah2;
99    blah.blah3 = evas_list_append(NULL, &blah3);
100    blah.blah3 = evas_list_append(blah.blah3, &blah3);
101    blah.blah3 = evas_list_append(blah.blah3, &blah3);
102    blah.blah3 = evas_list_append(blah.blah3, &blah3);
103    blah.blah3 = evas_list_append(blah.blah3, &blah3);
104    blah.blah3 = evas_list_append(blah.blah3, &blah3);
105    blah.blah3 = evas_list_append(blah.blah3, &blah3);
106    
107    data = eet_data_descriptor_encode(edd, &blah, &size);
108    f = fopen("out", "w");
109    if (f)
110      {
111         fwrite(data, size, 1, f);
112         fclose(f);
113      }      
114    printf("-----DECODING\n");
115    blah_in = eet_data_descriptor_decode(edd, data, size);
116    printf("-----DECODED!\n");
117    printf("%c\n", blah_in->character);
118    printf("%x\n", (int)blah_in->sixteen);
119    printf("%x\n", blah_in->integer);
120    printf("%lx\n", blah_in->lots);
121    printf("%f\n", (double)blah_in->floating);
122    printf("%f\n", (double)blah_in->floating_lots);
123    printf("%s\n", blah_in->string);
124    printf("%p\n", blah_in->blah2);
125    printf("  %s\n", blah_in->blah2->string);
126      {
127         Evas_List *l;
128         
129         for (l = blah_in->blah3; l; l = l->next)
130           {
131              Blah3 *blah3_in;
132              
133              blah3_in = l->data;
134              printf("%p\n", blah3_in);
135              printf("  %s\n", blah3_in->string);
136           }
137      }
138    eet_data_descriptor_free(edd);
139    eet_data_descriptor_free(edd2);
140    eet_data_descriptor_free(edd3);
141    
142    exit(0);
143 }
144 #endif
145
146 int eet_mkdir(char *dir);
147 void eet_mkdirs(char *s);
148
149 void depak_file(Eet_File *ef, char *file);
150 void depack(char *pak_file);
151
152 void list(char *pak_file);
153
154 void pak_file(Eet_File *ef, char *file, char **noz, int noz_num);
155 void pak_dir(Eet_File *ef, char *dir, char **noz, int noz_num);
156 void pack(char *pak_file, char **files, int count, char **noz, int noz_num);
157
158 static mode_t default_mode =
159 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
160    
161 int
162 eet_mkdir(char *dir)
163 {
164    if (mkdir(dir, default_mode) < 0) return 0;
165    return 1;
166 }
167
168 void
169 eet_mkdirs(char *s)
170 {
171    char ss[PATH_MAX];
172    int  i, ii;
173    
174    i = 0;
175    ii = 0;
176    while (s[i])
177      {
178         ss[ii++] = s[i];
179         ss[ii] = 0;
180         if (s[i] == '/') eet_mkdir(ss);
181         i++;
182      }
183 }
184
185 void
186 depak_file(Eet_File *ef, char *file)
187 {
188    void *data;
189    int size;
190    char *last;
191    
192    data = eet_read(ef, file, &size);
193    if (data)
194      {
195         FILE *f;
196         char buf[PATH_MAX];
197         
198         strcpy(buf, file);
199         last = strrchr(buf, '/');
200         if (last) 
201           {
202              last[1] = 0;
203              eet_mkdirs(buf);
204           }
205           
206         f = fopen(file, "w");
207         if (f)
208           {
209              fwrite(data, 1, size, f);
210              fclose(f);
211              printf("exported: %s\n", file);
212           }
213         else
214           printf("error exporting: %s\n", file);
215         free(data);
216      }
217    else
218      {
219         printf("error reading: %s\n", file);
220      }
221 }
222
223 void
224 depack(char *pak_file)
225 {
226    int i, num;
227    char **list;
228    Eet_File *ef;
229    
230    ef = eet_open(pak_file, EET_FILE_MODE_READ);   
231    if (!ef)
232      {
233         printf("cannot open for reading: %s\n", pak_file);
234         return;
235      }
236    list = eet_list(ef, "*", &num);
237    if (list)
238      {
239         for (i = 0; i < num; i++)
240           depak_file(ef, list[i]);
241         free(list);
242      }
243    eet_close(ef);   
244 }
245
246 void
247 list(char *pak_file)
248 {
249    int i, num;
250    char **list;
251    Eet_File *ef;
252    
253    ef = eet_open(pak_file, EET_FILE_MODE_READ);   
254    if (!ef)
255      {
256         printf("cannot open for reading: %s\n", pak_file);
257         return;
258      }
259    list = eet_list(ef, "*", &num);
260    if (list)
261      {
262         for (i = 0; i < num; i++)
263           printf("%s\n",list[i]);
264         free(list);
265      }
266    eet_close(ef);   
267 }
268
269 void
270 pak_file(Eet_File *ef, char *file, char **noz, int noz_num)
271 {
272    struct stat st;
273    
274    if (stat(file, &st) >= 0)
275      {
276         void *buf;
277         
278         buf = malloc(st.st_size);
279         if (buf)
280           {
281              FILE *f;
282              
283              f = fopen(file, "r");
284              if (f)
285                {
286                   int compress = 1;
287                   int i;
288                   
289                   for (i = 0; i < noz_num; i++)
290                     {
291                        if (!fnmatch(noz[i], file, 0))
292                          {
293                             compress = 0;
294                             break;
295                          }
296                     }
297                   fread(buf, 1, st.st_size, f);
298                   if (!eet_write(ef, file, buf, st.st_size, compress))
299                     printf("error importing: %s\n", file);
300                   else
301                     {
302                        if (compress)
303                          printf("compress: %s\n", file);
304                        else
305                          printf("imported: %s\n", file);
306                     }
307                   fclose(f);
308                }
309              free(buf);
310           }
311      }
312 }
313
314 void
315 pak_dir(Eet_File *ef, char *dir, char **noz, int noz_num)
316 {
317    DIR *dirp;
318    struct dirent *dp;
319
320    dirp = opendir(dir);
321    if (!dirp)
322      pak_file(ef, dir, noz, noz_num);
323    else
324      {
325         while ((dp = readdir(dirp)))
326           {
327              char buf[PATH_MAX];
328              
329              if ((!strcmp(".", dp->d_name)) || (!strcmp("..", dp->d_name)))
330                {
331                }
332              else
333                {
334                   snprintf(buf, sizeof(buf), "%s/%s", dir, dp->d_name);
335                   pak_dir(ef, buf, noz, noz_num);
336                }
337           }
338      }
339 }
340
341 void
342 pack(char *pak_file, char **files, int count, char **noz, int noz_num)
343 {
344    Eet_File *ef;
345    int i;
346    
347    ef = eet_open(pak_file, EET_FILE_MODE_WRITE);
348    if (!ef)
349      {
350         printf("cannot open for writing: %s\n", pak_file);
351         return;
352      }
353    for (i = 0; i < count; i++) pak_dir(ef, files[i], noz, noz_num);
354    printf("done.\n");
355    eet_close(ef);
356 }
357
358 int
359 main(int argc, char **argv)
360 {   
361    if (argc == 3)
362      {
363         if (!strcmp(argv[1], "-d"))
364           {
365              depack(argv[2]);
366              return 0;
367           }
368         else if (!strcmp(argv[1], "-l"))
369           {
370              list(argv[2]);
371              return 0;
372           }
373      }
374    else if (argc > 3)
375      {
376         char **noz     = NULL;
377         int    noz_num = 0;
378         
379         if (!strcmp(argv[1], "-c"))
380           {
381              int i;
382              
383              for (i = 3; i < argc; i++)
384                {
385                   if (!strcmp(argv[i], "-nz"))
386                     {
387                        if (i < (argc - 1))
388                          {
389                             i++;
390                             noz_num++;
391                             noz = realloc(noz, noz_num * sizeof(char *));
392                             noz[noz_num - 1] = argv[i];
393                          }
394                     }
395                   else
396                     break;
397                }
398              pack(argv[2], &(argv[i]), argc - i, noz, noz_num);
399              return 0;
400           }
401      }
402    printf("usage:\n"
403           "  %s -l in_file\n"
404           "  %s -d in_file\n"
405           "  %s -c out_file [-nz glob [-nz glob ...]] dir_file1 [dir_file2 ...]\n"
406           "\n"
407           "where:\n"
408           "  -l in_file     list contents of eet file\n"
409           "  -d in_file     unpack eet file\n"
410           "  -c out_file    pack up eet file\n"
411           "  -nz match      don't compress files matching match glob\n"
412           "\n"
413           "example:\n"
414           "  %s -c out.eet -nz \"*.jpg\" things/\n"
415           "  %s -l out.eet\n"
416           "  %s -d out.eet\n",
417           argv[0], argv[0], argv[0],
418           argv[0], argv[0], argv[0]);
419    return -1;
420 }