Re: [AD] dat

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


sorry just notice a small error on my part.. its fixed now.. promise! :)

--- dat.c	Sun Nov 09 19:43:12 2003
+++ datnew.c	Mon Feb 23 20:23:32 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;
@@ -111,6 +112,7 @@
    printf("\t'-v' selects verbose mode\n");
    printf("\t'-w' always updates the entire contents of the datafile\n");
    printf("\t'-007 password' sets the file encryption key\n");
+   printf("\t'-+' recursively add folders as if they are datafiles\n");
    printf("\t'PROP=value' sets object properties\n");
 }
 
@@ -391,8 +393,57 @@
       }
    }
 }
+typedef struct {DATAFILE *parent; DATAFILE *dat;} dataarg_t;
 
+dataarg_t* make_folder(dataarg_t *param,char *prop_value_string)
+{
+   DATAFILE *v=NULL;
+   long size = 0;
+   DATAFILE *parent=param->parent;
+   DATAFILE *dat=param->dat;
+   DATAFILE *d;
+   int replace=0;
+   int c;
+   dataarg_t *tmp=malloc(sizeof(dataarg_t));
+   
+   for (c=0; dat[c].type != DAT_END; c++) { // check it already exists and it is a datafile
+      if (dat[c].type==DAT_FILE&&stricmp(prop_value_string, get_datafile_property(dat+c, DAT_NAME)) == 0) {
+         tmp->parent=tmp->dat;
+         tmp->dat= (DATAFILE *)dat[c].dat;
+         return tmp;
+      }
+   }
+
+   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, prop_value_string, DAT_FILE, v, size);
+   tmp->parent=d;
+   param->dat=d;
+
+   changed=1;
+
+   if(dat==datafile) {
+      datafile = d;  
+   }
+   else {
+     for (c=0; parent[c].type != DAT_END; c++) {
+       if(parent[c].type==DAT_FILE&&((DATAFILE *)parent[c].dat)==dat) { 
+         parent[c].dat=d;
+         return tmp;
+       }      
+     }
+     return NULL;
+   } 
+
+   return tmp; 
+}
 
 /* adds a file to the archive */
 static int do_add_file(AL_CONST char *filename, int attrib, void *param)
@@ -400,13 +451,20 @@
    char fname[256];
    char name[256];
    int c;
+   DATAFILE *parent=((dataarg_t*)param)->parent;
+   DATAFILE *dat=((dataarg_t*)param)->dat;
    DATAFILE *d;
-   DATEDIT_GRAB_PARAMETERS params;
+   DATEDIT_GRAB_PARAMETERS params; 
+   dataarg_t *da;
 
+  
    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 +473,30 @@
 	    name[c] = '_';
    }
 
+   if(opt_recurse&&(attrib&FA_DIREC)) { // hunt with in it... 
+
+     printf("Inserting %s -> %s\n", fname, name);
+
+     da=make_folder(param,name);
+
+     if(!da) {
+        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, 0, do_add_file, da) <= 0) {
+	fprintf(stderr, "Error: %s not found\n", fname);
+        free(da);
+	err=1;
+	return 1;
+     }
+     free(da);
+     return 0;
+   }
+
    params.datafile = canonical_datafilename;
    params.filename = fname;
    params.name = name;
@@ -426,10 +508,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 +523,29 @@
    }
 
    printf("Inserting %s -> %s\n", fname, name);
-   d = datedit_grabnew(datafile, &params);
+   d = datedit_grabnew(dat, &params);
+   ((dataarg_t*)param)->dat=d;
+   
+   
    if (!d) {
       err = 1;
       return -1;
    }
    else {
-      datafile = d;
+      if(dat==datafile) {
+        datafile = d;
+
+      }
+      else {
+        for (c=0; parent[c].type != DAT_END; c++) {
+          if(((DATAFILE *)parent[c].dat)==dat) {
+            parent[c].dat=d;
+            return 0; 
+          }
+        }
+        return 1;
+      }
+
       changed = TRUE;
       return 0;
    }
@@ -693,6 +791,10 @@
 
 	 switch (utolower(argv[c][1])) {
 
+            case '+':
+               opt_recurse = TRUE;
+               break;
+
 	    case 'd':
 	       if (stricmp(argv[c]+2, "ither") == 0) {
 		  colorconv_mode |= COLORCONV_DITHER;
@@ -922,14 +1024,19 @@
 		  err = 1;
 	       }
 	       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) {
+		     for (c=0; c<opt_numnames; c++) {
+		     dataarg_t *tmp=malloc(sizeof(dataarg_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;


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