[monkey-bubble: 15/753] A solid beginning, I think, for file conversion routines. It seems to work. TODO:



commit 968c56c949ddc0b766e703047ab1dd000e6ad300
Author: Elliot Lee <sopwith src gnome org>
Date:   Tue Dec 23 02:47:56 1997 +0000

    A solid beginning, I think, for file conversion routines. It seems to
    work. TODO:
    
    . Perhaps finding a way to use the gnome_config routines, or at least
    deciding on a standard config file location, for storing the filters
    information.
    
    . Allow listing quality ratings for each filter, to be used to find
    the best path in combination with item #3.
    
    . Allowing conversion paths to go through multiple filters (basically
    just stick your algorithm into gfc_get_path()). I have an idea of how
    to do this using recursion, but someone who has studied graph theory
    or whatever might be able to do this justice.
    
    -- Elliot
    
    P.S.
    While I'm at it, the trigger support seems broken - has anyone
    changed anything?

 libgnome/gnome-fileconvert.c |   99 ++++++++++++++++++++++++++++++++++++++++++
 libgnome/gnome-fileconvert.h |   15 ++++++
 2 files changed, 114 insertions(+), 0 deletions(-)
---
diff --git a/libgnome/gnome-fileconvert.c b/libgnome/gnome-fileconvert.c
new file mode 100644
index 0000000..eaefc4e
--- /dev/null
+++ b/libgnome/gnome-fileconvert.c
@@ -0,0 +1,99 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include "gnome-defs.h"
+#include "gnome-string.h"
+#include "gnome-fileconvert.h"
+
+#define TYPES_CVT_FILENAME "types.cvt"
+
+static GList *
+gfc_get_path(gchar *fromtype, gchar *totype);
+static gint
+gfc_run_pipe(gchar *acmd, gint infd);
+
+gint
+gnome_file_convert(gchar *filename, gchar *fromtype, gchar *totype)
+{
+  gint fd = open(filename, O_RDONLY);
+  if(fd >= 0)
+    return gnome_file_convert_fd(fd, fromtype, totype);
+  else
+    return -1;
+}
+
+gint
+gnome_file_convert_fd(gint fd, gchar *fromtype, gchar *totype)
+{
+  GList *convlist, *l;
+  gint infd, outfd;
+  gchar *ccmd;
+
+  convlist = gfc_get_path(fromtype, totype);
+
+  for(l = convlist, infd = fd; l; l = l->next) {
+    ccmd = l->data;
+    outfd = gfc_run_pipe(ccmd, infd);
+    infd = outfd;
+  }
+  return infd;
+}
+
+static GList *
+gfc_get_path(gchar *fromtype, gchar *totype)
+{
+  GList *retval = NULL;
+  gchar aline[512];
+  gchar **parts, *cmd;
+  FILE *conffile;
+
+  conffile = fopen(TYPES_CVT_FILENAME, "r");
+
+  if(conffile) {
+    while(fgets(aline, sizeof(aline), conffile)) {
+      gnome_chomp_string(aline, TRUE);
+      if(aline[0] == '#'
+	 || aline[0] == '\0')
+	continue;
+      parts = gnome_split_string(aline, " ", 3);
+
+      if(!strcmp(fromtype, parts[0])
+	 && !strcmp(totype, parts[1])) {
+	cmd = parts[2];
+	g_free(parts[0]); g_free(parts[1]); g_free(parts);
+	return g_list_append(retval, cmd);
+      }
+    }
+  }
+  return retval;
+}
+
+static gint
+gfc_run_pipe(gchar *acmd, gint infd)
+{
+  gchar **parts;
+  gint childpid;
+  gint fds[2];
+  
+  if(pipe(fds))
+    return -1;
+
+  childpid = fork();
+  if(childpid < 0)
+    return -1;
+
+  if(childpid)
+    return fds[0];
+
+  /* else */
+  parts = gnome_split_string(acmd, " ", -1);
+  dup2(infd, 0);
+  dup2(fds[1], 1);
+  execv(parts[0], parts);
+
+  /* ERROR IF REACHED */
+  close(0); close(1);
+  exit(69);
+}
diff --git a/libgnome/gnome-fileconvert.h b/libgnome/gnome-fileconvert.h
new file mode 100644
index 0000000..7b96129
--- /dev/null
+++ b/libgnome/gnome-fileconvert.h
@@ -0,0 +1,15 @@
+#ifndef __GNOME_FILECONVERT_H__
+#define __GNOME_FILECONVERT_H__ 1
+
+BEGIN_GNOME_DECLS
+
+/* Returns -1 if no conversion is possible */
+gint
+gnome_file_convert_fd(gint fd, gchar *fromtype, gchar *totype);
+/* Convenience wrapper for the above function */
+gint
+gnome_file_convert(gchar *filename, gchar *fromtype, gchar *totype);
+
+END_GNOME_DECLS
+
+#endif /* __GNOME_FILECONVERT_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]