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.

There are '-f', '-i','-j' and '-q' too but this doesn't matter since...

> '-R' wouldn't work either as the options to dat are case insensitive.

... '-r' is clearly the best choice so I've renamed the former '-r' into '-f' 
and used '-r' for your option.

> Attached is the patch against the original dat.c with all the alterations
> you suggested.

Thanks.  It turns out that we can simplify it a bit so as not to use the new 
structure by adding a counterpart to 'open_folder'.  However, we need first 
to get rid of the global variable 'datafile' to properly do it.

So:
- dat.diff removes the global variable,
- dat-2.diff documents the '-x' alias (thanks for spotting this!)
- dat_recurse.diff implements the new -'r' option.

dat.diff has been applied to mainline, dat-2.diff to mainline and 4.0 branch.  
I'm waiting a bit before applying dat_recurse.diff because of the naming 
issue.

-- 
Eric Botcazou
--- /home/eric/cvs/allegro/tools/dat.c	Tue May 27 18:50:21 2003
+++ allegro/tools/dat.c	Fri Feb 27 08:33:43 2004
@@ -32,8 +32,6 @@
 
 
 
-static DATAFILE *datafile = NULL;
-
 static int err = 0;
 static int changed = 0;
 
@@ -397,11 +395,12 @@ static void do_delete(DATAFILE **dat, ch
 /* adds a file to the archive */
 static int do_add_file(AL_CONST char *filename, int attrib, void *param)
 {
+   DATEDIT_GRAB_PARAMETERS params;
+   DATAFILE **dat = param;
+   DATAFILE *d;
    char fname[256];
    char name[256];
    int c;
-   DATAFILE *d;
-   DATEDIT_GRAB_PARAMETERS params;
 
    canonicalize_filename(fname, filename, sizeof(fname));   
 
@@ -426,10 +425,10 @@ static int do_add_file(AL_CONST char *fi
    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 +440,13 @@ static int do_add_file(AL_CONST char *fi
    }
 
    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;
+      *dat = d;
       changed = TRUE;
       return 0;
    }
@@ -680,6 +679,7 @@ int main(int argc, char *argv[])
 {
    int c, colorconv_mode = 0;
    int *opt_fixed_prop = NULL;
+   DATAFILE *datafile = NULL;
 
    if (install_allegro(SYSTEM_NONE, &errno, atexit) != 0)
       return 1;
@@ -923,7 +923,7 @@ int main(int argc, char *argv[])
 	       }
 	       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) {
+		     if (for_each_file_ex(opt_namelist[c], 0, ~(FA_ARCH | FA_RDONLY), do_add_file, (void *)&datafile) <= 0) {
 			fprintf(stderr, "Error: %s not found\n", opt_namelist[c]);
 			err = 1;
 			break;
diff -u /home/eric/cvs/allegro/tools/dat.c allegro/tools/dat.c
--- /home/eric/cvs/allegro/tools/dat.c	Fri Feb 27 09:38:10 2004
+++ allegro/tools/dat.c	Fri Feb 27 09:44:16 2004
@@ -108,6 +108,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' alias for -e\n");
    printf("\t'-007 password' sets the file encryption key\n");
    printf("\t'PROP=value' sets object properties\n");
 }
diff -u /home/eric/cvs/allegro/tools/grabber.txt allegro/tools/grabber.txt
--- /home/eric/cvs/allegro/tools/grabber.txt	Sat Feb 14 12:03:39 2004
+++ allegro/tools/grabber.txt	Fri Feb 27 09:45:20 2004
@@ -392,6 +392,9 @@
    '-w'
       Always updates the entire contents of the datafile.
 
+   '-x <objects>'
+      Alias for '-e <objects>'.
+
    '-007 password'
       Sets the file encryption key.
 
--- /home/eric/cvs/allegro/tools/dat.c	Fri Feb 27 09:47:34 2004
+++ allegro/tools/dat.c	Fri Feb 27 13:23:50 2004
@@ -12,6 +12,8 @@
  *
  *      By Shawn Hargreaves.
  *
+ *      Nathan Smith added the recursive handling of directories.
+ *
  *      See readme.txt for copyright information.
  */
 
@@ -56,6 +58,8 @@ static char *opt_prefixstring = NULL;
 static char *opt_password = NULL;
 static char *opt_palette = NULL;
 
+static int attrib_ok = FA_ARCH|FA_RDONLY;
+
 static char canonical_datafilename[1024];
 
 #define MAX_FILES    256
@@ -88,6 +92,7 @@ static void usage(void)
    printf("\t'-d' deletes the named objects from the datafile\n");
    printf("\t'-dither' dithers when reducing color depths\n");
    printf("\t'-e' extracts the named objects from the datafile\n");
+   printf("\t'-f' store references to original files as relative filenames\n");
    printf("\t'-g x y w h' grabs bitmap data from a specific grid location\n");
    printf("\t'-h outputfile.h' sets the output header file\n");
    printf("\t'-k' keeps the original filenames when grabbing objects\n");
@@ -98,7 +103,7 @@ static void usage(void)
    printf("\t'-o output' sets the output file or directory when extracting data\n");
    printf("\t'-p prefixstring' sets the prefix for the output header file\n");
    printf("\t'-pal objectname' specifies which palette to use\n");
-   printf("\t'-r' store references to original files as relative filenames\n");
+   printf("\t'-r' recursively adds directories as nested datafiles\n");
    printf("\t'-s0' no strip: save everything\n");
    printf("\t'-s1' strip grabber specific information from the file\n");
    printf("\t'-s2' strip all object properties and names from the file\n");
@@ -393,6 +398,51 @@ static void do_delete(DATAFILE **dat, ch
 
 
 
+/* open a sub-datafile in the parent datafile */
+static DATAFILE *open_sub_datafile(DATAFILE *parent, AL_CONST char *name)
+{
+   DATAFILE *dat;
+   int c;
+
+   /* Return the datafile if it already exists. */
+   for (c=0; parent[c].type != DAT_END; c++) {
+      if ((parent[c].type == DAT_FILE) && (stricmp(name, get_datafile_property(parent+c, DAT_NAME)) == 0))
+	 return parent[c].dat;
+   }
+
+   /* Otherwise create a new datafile. */
+   printf("Creating sub-datafile %s\n", name);
+
+   dat = malloc(sizeof(DATAFILE));
+   dat->dat = NULL;
+   dat->type = DAT_END;
+   dat->size = 0;
+   dat->prop = NULL;
+
+   return dat;
+}
+
+
+
+/* close a sub-datafile from the parent datafile */
+static DATAFILE *close_sub_datafile(DATAFILE *parent, AL_CONST char *name, DATAFILE *dat)
+{
+   int c;
+
+   /* Adjust the datafile if it already exists. */
+   for (c=0; parent[c].type != DAT_END; c++) {
+      if ((parent[c].type == DAT_FILE) && (stricmp(name, get_datafile_property(parent+c, DAT_NAME)) == 0)) {
+	 parent[c].dat = dat;
+	 return parent;
+      }
+   }
+
+   /* Otherwise insert the datafile. */
+   return datedit_insert(parent, NULL, name, DAT_FILE, dat, 0);
+}
+
+
+
 /* adds a file to the archive */
 static int do_add_file(AL_CONST char *filename, int attrib, void *param)
 {
@@ -407,6 +457,9 @@ static int do_add_file(AL_CONST char *fi
 
    strcpy(name, get_filename(fname));
 
+   if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0))
+      return 0;
+
    if (!opt_keepnames) {
       strupr(name);
 
@@ -415,6 +468,28 @@ static int do_add_file(AL_CONST char *fi
 	    name[c] = '_';
    }
 
+   if (attrib & FA_DIREC) {
+      DATAFILE *sub_dat = open_sub_datafile(*dat, name);
+
+      if (!sub_dat) {
+	 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, ~attrib_ok, do_add_file, &sub_dat) <= 0) {
+	 fprintf(stderr, "Error: failed to add objects to %s\n", name);
+	 err = 1;
+	 return -1;
+      }
+
+      *dat = close_sub_datafile(*dat, name, sub_dat);
+      changed = TRUE;
+      return 0;
+   }
+
    params.datafile = canonical_datafilename;
    params.filename = fname;
    params.name = name;
@@ -737,6 +812,10 @@ int main(int argc, char *argv[])
 	       opt_compression = argv[c][2] - '0'; 
 	       break;
 
+	    case 'f':
+	       opt_relf = TRUE;
+	       break;
+
 	    case 'g':
 	       if ((opt_gridx > 0) || (c >= argc-4)) {
 		  usage();
@@ -810,8 +889,8 @@ int main(int argc, char *argv[])
 	       break;
                
 	    case 'r':
-               opt_relf = TRUE;
-               break;
+	       attrib_ok |= FA_DIREC;
+	       break;
 
 	    case 's':
 	       if (argv[c][2] == '-') {
@@ -924,7 +1003,7 @@ int main(int argc, char *argv[])
 	       }
 	       else {
 		  for (c=0; c<opt_numnames; c++) {
-		     if (for_each_file_ex(opt_namelist[c], 0, ~(FA_ARCH | FA_RDONLY), do_add_file, (void *)&datafile) <= 0) {
+		     if (for_each_file_ex(opt_namelist[c], 0, ~attrib_ok, do_add_file, &datafile) <= 0) {
 			fprintf(stderr, "Error: %s not found\n", opt_namelist[c]);
 			err = 1;
 			break;
--- /home/eric/cvs/allegro/tools/grabber.txt	Fri Feb 27 09:47:34 2004
+++ allegro/tools/grabber.txt	Fri Feb 27 13:18:35 2004
@@ -322,6 +322,11 @@ line dat utility. This accepts the follo
 
 	 dat myfile.dat -e * -o c:\output\
 
+   '-f'
+      Stores the references to original files as relative filenames instead of
+      absolute filenames. This greatly increases the portability of complete
+      source trees containing the source data.
+
    '-g x y w h'
       Grabs bitmap data from a specific grid location.
 
@@ -364,9 +369,8 @@ line dat utility. This accepts the follo
       Specifies which palette to use.
 
    '-r'
-      Stores the references to original files as relative filenames instead of
-      absolute filenames. This greatly increases the portability of complete
-      source trees containing the source data.
+      Recursively adds directories as nested datafiles. This makes it possible
+      to maintain the data hierarchy within the datafile.
 
    '-s0' - no strip: save everything
    '-s1' - strip grabber specific information from the file


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