Re: [AD] dat

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Evert Glebbeek wrote:

Also, I'm not sure that dat wouldn't interpret -af as -a -f, which I would
expect it to do personally. Looking at the man pages, I see that zip and ls
already use -R to specify recursive directories, so I would instead recommend
that dat be made case-sensitive.
okay, it now uses '-R'.

--- datold.c	Sun Nov 09 19:43:12 2003
+++ dat.c	Fri Feb 27 07:29:43 2004
@@ -44,6 +44,7 @@
 static int opt_relf = FALSE;
 static int opt_verbose = FALSE;
 static int opt_keepnames = FALSE;
+static int opt_recurse = FALSE;
 static int opt_colordepth = -1;
 static int opt_gridx = -1;
 static int opt_gridy = -1;
@@ -110,8 +111,11 @@
    printf("\t'-u' updates the contents of the datafile\n");
    printf("\t'-v' selects verbose mode\n");
    printf("\t'-w' always updates the entire contents of the datafile\n");
+   printf("\t'-x' extracts the named objects from the datafile\n");
    printf("\t'-007 password' sets the file encryption key\n");
+   printf("\t'-R' recursively adds directories as if they are datafiles\n");
    printf("\t'PROP=value' sets object properties\n");
+   
 }
 
 
@@ -256,7 +260,8 @@
 
 
 
-/* checks if a string is one of the names specified on the command line */
+/* checks if a string is one of the names specified on the command line 
+*/
 static int is_name(AL_CONST char *name)
 {
    char str1[256], str2[256];
@@ -392,7 +397,60 @@
    }
 }
 
+typedef struct foldertree_t {struct foldertree_t *parent; DATAFILE *dat;} foldertree_t;
 
+foldertree_t* open_folder(foldertree_t *param,char *fname,char *name)
+{
+   DATAFILE *v=NULL;
+   long size = 0;
+   DATAFILE *dat=param->dat;
+   DATAFILE *d;
+   int replace=0;
+   int c;
+   foldertree_t *tmp=malloc(sizeof(foldertree_t));
+
+   tmp->parent=param;
+   
+   for (c=0; dat[c].type != DAT_END; c++) { /* if it already exists and it is a datafile then return that */
+      if (dat[c].type==DAT_FILE&&stricmp(name, get_datafile_property(dat+c, DAT_NAME)) == 0) {
+         tmp->dat= (DATAFILE *)dat[c].dat;
+         return tmp;
+      }
+   }
+
+   /* otherwise we create a new folder, and open that */
+
+   printf("Inserting %s -> %s\n", fname, name);
+
+   v = malloc(sizeof(DATAFILE));
+
+   v->dat = NULL;
+   v->type = DAT_END;
+   v->size = 0;
+   v->prop = NULL;
+
+   tmp->dat= v;
+
+   d=datedit_insert(dat, NULL, name, DAT_FILE, v, size);
+   param->dat=d;
+
+   changed=TRUE;
+
+   if(dat==datafile) {
+      datafile = d;  
+      return tmp; 
+   }
+   else {
+     for (c=0; param->parent->dat[c].type != DAT_END; c++) {
+       if(param->parent->dat[c].type==DAT_FILE&&((DATAFILE *)param->parent->dat[c].dat)==dat) { 
+         param->parent->dat[c].dat=d;
+         return tmp;
+       }      
+     }
+   } 
+
+   return NULL;
+}
 
 /* adds a file to the archive */
 static int do_add_file(AL_CONST char *filename, int attrib, void *param)
@@ -400,13 +458,20 @@
    char fname[256];
    char name[256];
    int c;
+   foldertree_t *parent=((foldertree_t*)param)->parent;
+   DATAFILE *dat=((foldertree_t*)param)->dat;
    DATAFILE *d;
-   DATEDIT_GRAB_PARAMETERS params;
+   DATEDIT_GRAB_PARAMETERS params; 
+   foldertree_t *ft;
 
+  
    canonicalize_filename(fname, filename, sizeof(fname));   
-
    strcpy(name, get_filename(fname));
 
+
+   if(strcmp(name,".")==0||strcmp(name,"..")==0)
+       return 0;
+
    if (!opt_keepnames) {
       strupr(name);
 
@@ -415,6 +480,28 @@
 	    name[c] = '_';
    }
 
+   if(opt_recurse&&(attrib&FA_DIREC)) { /* hunt with in it... ;) */
+
+     ft=open_folder(param,fname,name);
+
+     if(!ft) {
+        fprintf(stderr, "Error: failed to create sub datafile %s.\n",name);
+        err=1;
+	return 1;
+     }
+
+     append_filename(fname, fname, "*", sizeof(fname));
+
+     if (for_each_file_ex(fname, 0, ~(FA_ARCH|FA_DIREC|FA_RDONLY), do_add_file, ft) <= 0) {
+	fprintf(stderr, "Error: %s not found\n", fname);
+        free(ft);
+	err=1;
+	return 1;
+     }
+     free(ft);
+     return 0;
+   }
+
    params.datafile = canonical_datafilename;
    params.filename = fname;
    params.name = name;
@@ -426,10 +513,10 @@
    params.colordepth = opt_colordepth;
    params.relative = opt_relf;
 
-   for (c=0; datafile[c].type != DAT_END; c++) {
-      if (stricmp(name, get_datafile_property(datafile+c, DAT_NAME)) == 0) {
+   for (c=0; dat[c].type != DAT_END; c++) {
+      if (stricmp(name, get_datafile_property(dat+c, DAT_NAME)) == 0) {
 	 printf("Replacing %s -> %s\n", fname, name);
-	 if (!datedit_grabreplace(datafile+c, &params)) {
+	 if (!datedit_grabreplace(dat+c, &params)) {
 	    err = 1;
 	    return -1;
 	 }
@@ -441,13 +528,27 @@
    }
 
    printf("Inserting %s -> %s\n", fname, name);
-   d = datedit_grabnew(datafile, &params);
+   d = datedit_grabnew(dat, &params);
+   ((foldertree_t*)param)->dat=d;
+
    if (!d) {
       err = 1;
       return -1;
    }
    else {
-      datafile = d;
+      if(dat==datafile) {
+        datafile = d;
+      }
+      else {
+        for (c=0; parent->dat[c].type != DAT_END; c++) {
+          if(((DATAFILE *)parent->dat[c].dat)==dat) {
+            parent->dat[c].dat=d;
+            return 0; 
+          }
+        }
+        return 1;
+      }
+
       changed = TRUE;
       return 0;
    }
@@ -691,10 +792,10 @@
    for (c=1; c<argc; c++) {
       if (argv[c][0] == '-') {
 
-	 switch (utolower(argv[c][1])) {
+	 switch (argv[c][1]) {
 
 	    case 'd':
-	       if (stricmp(argv[c]+2, "ither") == 0) {
+	       if (strcmp(argv[c]+2, "ither") == 0) {
 		  colorconv_mode |= COLORCONV_DITHER;
 		  break;
 	       }
@@ -710,7 +811,7 @@
 		  usage();
 		  return 1;
 	       }
-	       opt_command = utolower(argv[c][1]);
+	       opt_command = argv[c][1];
 	       break;
 
 	    case 'b':
@@ -808,9 +909,12 @@
 	       }
 	       break;
                
-	    case 'r':
+        case 'r':
                opt_relf = TRUE;
                break;
+        case 'R':
+               opt_recurse = TRUE;
+               break;
 
 	    case 's':
 	       if (argv[c][2] == '-') {
@@ -899,7 +1003,8 @@
       return 1;
    }
 
-   canonicalize_filename(canonical_datafilename, opt_datafilename, sizeof(canonical_datafilename));
+   canonicalize_filename(canonical_datafilename, opt_datafilename, 
+sizeof(canonical_datafilename));
 
    if (colorconv_mode)
       set_color_conversion(colorconv_mode);
@@ -923,13 +1028,18 @@
 	       }
 	       else {
 		  for (c=0; c<opt_numnames; c++) {
-		     if (for_each_file_ex(opt_namelist[c], 0, ~(FA_ARCH | FA_RDONLY), do_add_file, NULL) <= 0) {
+		     foldertree_t *tmp=malloc(sizeof(foldertree_t));
+                     tmp->parent=NULL;
+                     tmp->dat=datafile;
+                     if (for_each_file_ex(opt_namelist[c], 0, (opt_recurse)?(~(FA_ARCH|FA_DIREC|FA_RDONLY)) :(~(FA_ARCH|FA_RDONLY)) , do_add_file, (void *)tmp) <= 0) {
 			fprintf(stderr, "Error: %s not found\n", opt_namelist[c]);
 			err = 1;
+                        free(tmp);
 			break;
 		     }
 		     else
 			opt_usedname[c] = TRUE;
+                     free(tmp);
 		  }
 	       }
 	       break;
@@ -1045,3 +1155,5 @@
 }
 
 END_OF_MAIN();
+
+ 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/