Re: [AD] dat

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


yes, when I discovered '-r' was taken I was mildly annoyed. Every single letter has been taken except for '-y' and '-z' and niether of those said 'recursively add new folders' to me. '-R' wouldn't work either as the options to dat are case insensitive. Since '-+' didn't take your fancy, I have choosen '-af' as my next choice. (since it is 'add folders') I hope that this will be to your graces liking. Attached is the patch against the original dat.c with all the alterations you suggested.

--- dat.c	Sun Nov 09 19:43:12 2003
+++ datnew.c	Thu Feb 26 17:08:52 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;
@@ -83,6 +84,7 @@
    printf("Usage: dat [options] filename.dat [names]\n\n");
    printf("Options:\n");
    printf("\t'-a' adds the named files to the datafile\n");
+   printf("\t'-af' adds the named folders recursively as if they are datafiles\n");
    printf("\t'-bpp colordepth' grabs bitmaps in the specified format\n");
    printf("\t'-c0' no compression\n");
    printf("\t'-c1' compress objects individually\n");
@@ -110,6 +112,7 @@
    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'PROP=value' sets object properties\n");
 }
@@ -256,7 +259,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 +396,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 +457,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 +479,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 +512,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 +527,26 @@
    }
 
    printf("Inserting %s -> %s\n", fname, name);
-   d = datedit_grabnew(datafile, &params);
+   d = datedit_grabnew(dat, &params);
+
    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;
    }
@@ -701,6 +800,8 @@
 	       /* fall through */
 
 	    case 'a':
+               if(argv[c][2] == 'f') 
+                 opt_recurse = TRUE;
 	    case 'e':
 	    case 'l':
 	    case 'u':
@@ -808,7 +909,7 @@
 	       }
 	       break;
                
-	    case 'r':
+            case 'r':
                opt_relf = TRUE;
                break;
 
@@ -899,7 +1000,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 +1025,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 +1152,5 @@
 }
 
 END_OF_MAIN();
+
+ 


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