On 2005-04-03, 11:07, Simon Kagstrom wrote:
> Hello Dia-developers!
> 
> I implemented a patch for Dia yesterday against the Debian/Sarge dia package
> source (attached in the mail). This patch implements a command-line option to
> specify which layers to show on exports.

I updated the patch for the CVS version of Dia and attached it here. It
implements --show-layers-range and works as outlined in the last mail.

One thing which I didn't like is that with one more command-line argument,
do_convert and a few other functions are starting to get may arguments. Maybe
it's better to define a struct with all arguments and pass that to the
functions that need the arguments instead? In the patch, I just added another
argument.

Anyway: hope you like it :-)

// Simon
Index: app/app_procs.c
===================================================================
RCS file: /cvs/gnome/dia/app/app_procs.c,v
retrieving revision 1.159
diff -u -r1.159 app_procs.c
--- app/app_procs.c     20 Mar 2005 22:35:59 -0000      1.159
+++ app/app_procs.c     3 Apr 2005 13:39:16 -0000
@@ -101,7 +101,8 @@
                       const char *export_file_name,
                       const char *export_file_format,
                       const char *size,
-                      char *show_layers);
+                      char *show_layers,
+                      char *show_layers_range);
 
 static void create_user_dirs(void);
 static PluginInitResult internal_plugin_init(PluginInfo *info);
@@ -113,9 +114,11 @@
 #endif
                         GSList **files, char **export_file_name,
                         char **export_file_format, char **size,
-                        char **show_layers, gboolean *nosplash);
+                        char **show_layers, char **show_layers_range,
+                        gboolean *nosplash);
 static gboolean handle_all_diagrams(GSList *files, char *export_file_name,
-                                   char *export_file_format, char *size, char 
*show_layers);
+                                   char *export_file_format, char *size, char 
*show_layers,
+                                   char *show_layers_range);
 static void print_credits(gboolean credits);
 
 static gboolean dia_is_interactive = TRUE;
@@ -203,6 +206,121 @@
   return tmp;
 }
 
+
+/* Handle the string between commas. We have either of:
+ *
+ * 1. XX, the number XX
+ * 2. -XX, every number until XX
+ * 3. XX-, every number from XX until n_layers
+ * 4. XX-YY, every number between XX-YY
+ */
+static void
+layer_range_handle_substr(gboolean *visible_layers, gint n_layers, const char 
*str)
+{
+  char *p;
+  unsigned long int low = 0;
+  unsigned long int high = n_layers;
+  unsigned long int i;
+
+  if (str == NULL)
+    return;
+
+  /* Case 2, starts with '-' */
+  if (*str == '-') {
+    str++;
+    low = 0;
+    high = strtoul(str, &p, 10)+1;
+    if (p == str) {
+      g_error(_("error: Cannot convert %s into a number\n"), str );
+      exit(1);
+    }
+  }
+  else {
+    /* Case 1, 3 or 4 */
+    low = strtoul(str, &p, 10);
+    high = low+1; /* Assume case 1 */
+    if (p == str) {
+      g_error(_("error: Cannot convert %s into a number\n"), str );
+      exit(1);
+    }
+    if (*p == '-')
+      {
+       /* Case 3 or 4 */
+       str = p + 1;
+       if (*str == '\0') /* Case 3 */
+         high = n_layers;
+       else
+         {
+           high = strtoul(str, &p, 10)+1;
+           if (p == str) {
+             g_error(_("error: Cannot convert %s into a number\n"), str );
+             exit(1);
+           }
+         }
+      }
+  }
+
+  if ( high <= low ) {
+    g_error(_("error: invalid layer range %lu - %lu\n"), low, high-1 );
+    exit(1);
+  }
+  if (high > n_layers)
+    high = n_layers;
+
+  /* Set the visible layers */
+  for ( i = low; i < high; i++ )
+    visible_layers[i] = TRUE;
+}
+
+static void
+layer_range_parse_string(gboolean *visible_layers, gint n_layers, const char 
*str)
+{
+  char *p, *dummy;
+  char *cpy = strdup(str);
+
+  if (!cpy) {
+    g_error(_("error: strdup failed\n") );
+    exit(1);
+  }
+
+  for (p = strtok_r(cpy, ",", &dummy);
+       p != NULL;
+       p = strtok_r(NULL, ",", &dummy)) {
+    layer_range_handle_substr(visible_layers, n_layers, p);
+  }
+
+  free(cpy);
+}
+
+
+static void
+handle_layer_range(DiagramData *diagdata, const char *show_layers_range)
+{
+  gboolean *visible_layers;
+  Layer *layer;
+  int i;
+
+  visible_layers = g_malloc(diagdata->layers->len * sizeof(gboolean));
+  /* Assume all layers are non-visible */
+  for (i=0;i<diagdata->layers->len;i++)
+    visible_layers[i] = FALSE;
+
+  /* Split the layer-range by commas */
+  layer_range_parse_string(visible_layers, diagdata->layers->len,
+                          show_layers_range);
+
+  for (i=0;i<diagdata->layers->len;i++) {
+    layer = g_ptr_array_index(diagdata->layers, i);
+
+    if (visible_layers[i] == TRUE)
+      layer->visible = TRUE;
+    else
+      layer->visible = FALSE;
+  }
+  g_free(visible_layers);
+}
+
+
 const char *argv0 = NULL;
 
 /** Convert infname to outfname, using input filter inf and export filter
@@ -213,7 +331,8 @@
 do_convert(const char *infname, 
           const char *outfname, DiaExportFilter *ef,
           const char *size,
-          char *show_layers)
+          char *show_layers,
+          char *show_layers_range)
 {
   DiaImportFilter *inf;
   DiagramData *diagdata = NULL;
@@ -248,8 +367,13 @@
     exit(1);
   }
 
+  /* Apply --show-layers-range. Should be before --show-layers since it
+   * assumes all layers are invisible */
+  if (show_layers_range)
+    handle_layer_range(diagdata, show_layers_range);
+
   /* Apply --show-layers=LAYER,LAYER,... switch. 13.3.2004 [EMAIL PROTECTED] */
-  
+
   if (show_layers && *show_layers) {
     GPtrArray *layers = diagdata->layers;
     if (layers) {
@@ -413,7 +537,8 @@
                       const char *out_file_name,
                       const char *export_file_format,
                       const char *size,
-                      char* show_layers) {
+                      char* show_layers,
+                      char* show_layers_range) {
   DDisplay *ddisp = NULL;
   Diagram *diagram = NULL;
   gboolean made_conversions = FALSE;
@@ -443,7 +568,8 @@
     }
     made_conversions |= do_convert(in_file_name,
       (out_file_name != NULL?out_file_name:export_file_name),
-                                  ef, size, show_layers);
+                                  ef, size, show_layers,
+                                  show_layers_range);
     g_free(export_file_name);
   } else if (out_file_name) {
     DiaExportFilter *ef = NULL;
@@ -453,7 +579,7 @@
       ef = filter_get_by_name ("png-libart");
     
     made_conversions |= do_convert(in_file_name, out_file_name, ef,
-                                  size, show_layers);
+                                  size, show_layers, show_layers_range);
   } else {
     if (g_file_test(in_file_name, G_FILE_TEST_EXISTS)) {
       diagram = diagram_load (in_file_name, NULL);
@@ -516,6 +642,7 @@
   static char *export_file_format = NULL;
   static char *size = NULL;
   static char *show_layers = NULL;
+  static char *show_layers_range = NULL;
   gboolean made_conversions = FALSE;
   GSList *files = NULL;
 
@@ -547,6 +674,8 @@
      N_("Export graphics size"), N_("WxH")},
     {"show-layers", 'L', 0, G_OPTION_ARG_STRING, NULL,
      N_("Show only specified layers (e.g. when exporting)"), 
N_("LAYER,LAYER,...")},
+    {"show-layers-range", 'R', 0, G_OPTION_ARG_STRING, NULL,
+     N_("Show only a range of layers (e.g. when exporting), from 0 to the 
number of layers in the image"), N_("X-Y,Z,...")},
     {"nosplash", 'n', 0, G_OPTION_ARG_NONE, &nosplash,
      N_("Don't show the splash screen"), NULL },
     {"log-to-stderr", 'l', 0, G_OPTION_ARG_NONE, &log_to_stderr,
@@ -572,6 +701,8 @@
      N_("Export graphics size"), N_("WxH")},
     {"show-layers", 'L', POPT_ARG_STRING, NULL, 0,  /* 13.3.2004 [EMAIL 
PROTECTED] */
      N_("Show only specified layers (e.g. when exporting)"), 
N_("LAYER,LAYER,...")},
+    {"show-layers-range", 'R', POPT_ARG_STRING, NULL, 0,  /* 3.4.2005 [EMAIL 
PROTECTED] */
+     N_("Show only a range of layers (e.g. when exporting), from 0 to the 
number of layers in the image"), N_("X-Y,Z,...")},
     {"nosplash", 'n', POPT_ARG_NONE, &nosplash, 0,
      N_("Don't show the splash screen"), NULL },
     {"log-to-stderr", 'l', POPT_ARG_NONE, &log_to_stderr, 0,
@@ -593,11 +724,13 @@
   options[1].description = export_format_string;
   options[2].arg_data = &size;
   options[3].arg_data = &show_layers;
+  options[4].arg_data = &show_layers_range;
 #elif defined HAVE_POPT
   options[0].arg = &export_file_name;
   options[1].arg = &export_file_format;
   options[2].arg = &size;
   options[3].arg = &show_layers;
+  options[4].arg = &show_layers_range;
 #endif
 
   argv0 = (argc > 0) ? argv[0] : "(none)";
@@ -613,7 +746,7 @@
                context, options, 
 #endif
                &files,
-            &export_file_name, &export_file_format, &size, &show_layers, 
&nosplash);
+            &export_file_name, &export_file_format, &size, &show_layers, 
&show_layers_range, &nosplash);
 
 #if defined ENABLE_NLS && defined HAVE_BIND_TEXTDOMAIN_CODESET
   bind_textdomain_codeset(GETTEXT_PACKAGE,"UTF-8");  
@@ -767,7 +900,8 @@
   }
 
   made_conversions = handle_all_diagrams(files, export_file_name,
-                                        export_file_format, size, show_layers);
+                                        export_file_format, size, show_layers,
+                                        show_layers_range);
   if (dia_is_interactive && files == NULL) {
     gchar *filename = g_filename_from_utf8(_("Diagram1.dia"), -1, NULL, NULL, 
NULL);
     Diagram *diagram = new_diagram (filename);
@@ -965,7 +1099,8 @@
 #endif
             GSList **files, char **export_file_name,
             char **export_file_format, char **size,
-            char **show_layers, gboolean* nosplash)
+            char **show_layers, char **show_layers_range,
+            gboolean* nosplash)
 {
 #if defined HAVE_POPT && !GLIB_CHECK_VERSION(2,5,5)
   int rc = 0;
@@ -1047,6 +1182,12 @@
                   *show_layers = argv[i];
                   continue;
               }
+          } else if (0 == strcmp(argv[i],"-R")) {
+              if (i < (argc-1)) {
+                  i++;
+                  *show_layers_range = argv[i];
+                  continue;
+              }
           } else if (0 == strcmp(argv[i],"-n")) {
              *nosplash = 1;
              continue;
@@ -1061,7 +1202,8 @@
 
 static gboolean
 handle_all_diagrams(GSList *files, char *export_file_name,
-                   char *export_file_format, char *size, char *show_layers)
+                   char *export_file_format, char *size, char *show_layers,
+                   char *show_layers_range)
 {
   GSList *node = NULL;
   gboolean made_conversions = FALSE;
@@ -1069,7 +1211,8 @@
   for (node = files; node; node = node->next) {
     made_conversions |=
       handle_initial_diagram(node->data, export_file_name,
-                            export_file_format, size, show_layers);
+                            export_file_format, size, show_layers,
+                            show_layers_range);
   }
   return made_conversions;
 }
_______________________________________________
Dia-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/dia-list
FAQ at http://www.gnome.org/projects/dia/faq.html
Main page at http://www.gnome.org/projects/dia

Reply via email to