changeset 12097:b9544a96bc8d octave-forge

support viewing large objects with viewers needing a temporary file * inst/pq_lo_view.m: New function file. * src/__pq_internal_exit__.cc: New file. * src/Makefile.in: Include __pq_internal_exit__.cc. * NEWS, INDEX, DESCRIPTION: Respective changes.
author i7tiol
date Tue, 08 Oct 2013 13:54:46 +0000
parents 3b98291666c7
children b2e12aa2b4d6
files main/database/DESCRIPTION main/database/INDEX main/database/NEWS main/database/inst/pq_lo_view.m main/database/src/Makefile.in main/database/src/__pq_internal_exit__.cc
diffstat 6 files changed, 137 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/main/database/DESCRIPTION	Wed Oct 02 21:50:55 2013 +0000
+++ b/main/database/DESCRIPTION	Tue Oct 08 13:54:46 2013 +0000
@@ -1,6 +1,6 @@
 Name: database
 Version: 2.2.0
-Date: 2013-05-20
+Date: 2013-10-08
 Author: Olaf Till <i7tiol@t-online.de>
 Maintainer: Olaf Till <i7tiol@t-online.de>
 Title: Database.
--- a/main/database/INDEX	Wed Oct 02 21:50:55 2013 +0000
+++ b/main/database/INDEX	Tue Oct 08 13:54:46 2013 +0000
@@ -10,6 +10,7 @@
  pq_lo_import
  pq_lo_export
  pq_lo_unlink
+ pq_lo_view
 Option setting
  setdbopts
  getdbopts
--- a/main/database/NEWS	Wed Oct 02 21:50:55 2013 +0000
+++ b/main/database/NEWS	Tue Oct 08 13:54:46 2013 +0000
@@ -1,3 +1,8 @@
+database 2.2.1
+--------------
+
+ ** New function pq_lo_view.
+
 database 2.2.0
 --------------
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/database/inst/pq_lo_view.m	Tue Oct 08 13:54:46 2013 +0000
@@ -0,0 +1,91 @@
+## Copyright (C) 2013 Olaf Till <i7tiol@t-online.de>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} pq_lo_view (@var{connection}, @var{oid}, @var{viewer})
+##
+## Exports the large object of Oid @var{oid} in the database associated
+## with @var{connection} to a temporary file and starts the program
+## @var{viewer} in the background with the name of the temporary file as
+## argument.
+##
+## The temporary file will be removed after termination of the viewer.
+##
+## @end deftypefn
+
+function pq_lo_view (conn, oid, viewer)
+
+  if (nargin () != 3 || ! ischar (viewer) || rows (viewer) != 1)
+    print_usage ();
+  endif
+
+  if (([opid, omsg] = fork ()) == 0)
+    ## outer child
+
+    unwind_protect
+
+      if (([vpid, vmsg] = fork ()) == 0)
+        ## child for viewer
+
+        unwind_protect
+
+          ## We don't rely on Octave to delete the tempfile since we
+          ## perform a "shortcut" exit from Octave in the child.
+          if (([~, tname, msg] = mkstemp ...
+               (fullfile (P_tmpdir (), "octave-pq_lo_view-XXXXXX"))) == -1)
+            error ("could not make temporary file: %s", msg);
+          endif
+
+          unwind_protect
+
+            ## throws an error for errors
+            pq_lo_export (conn, oid, tname);
+
+            if (system (sprintf ("%s %s", viewer, tname)) != 0)
+              error ("error in execution of viewer program or viewer terminated by a signal");
+            endif
+
+          unwind_protect_cleanup
+
+            unlink (tname);
+
+          end_unwind_protect
+
+        unwind_protect_cleanup
+
+          __pq_internal_exit__ ();
+
+        end_unwind_protect
+
+      elseif (vpid < 0)
+        ## fork for viewer went wrong
+        error ("error in fork for viewer process: %s", vmsg);
+      endif
+
+    unwind_protect_cleanup
+
+      __pq_internal_exit__ ();
+
+    end_unwind_protect
+
+  elseif (opid < 0)
+    ## fork for outer child went wrong
+    error ("error in fork for outer child: %s", omsg);
+  endif
+
+  ## parent  
+  waitpid (opid);
+
+endfunction
--- a/main/database/src/Makefile.in	Wed Oct 02 21:50:55 2013 +0000
+++ b/main/database/src/Makefile.in	Tue Oct 08 13:54:46 2013 +0000
@@ -9,7 +9,7 @@
 
 OBJECTS := __pq_connect__.o pq_close.o pq_exec.o converters.o \
            converters_arr_comp.o pq_connection.o command.o \
-           pq_update_types.o pq_lo.o pq_conninfo.o
+           pq_update_types.o pq_lo.o pq_conninfo.o __pq_internal_exit__.o
 
 pq_interface.oct: $(OBJECTS)
 	@MKOCTFILE@ -o pq_interface.oct -L`@PG_CONFIG@ --libdir` -lpq $(OBJECTS)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/database/src/__pq_internal_exit__.cc	Tue Oct 08 13:54:46 2013 +0000
@@ -0,0 +1,38 @@
+// Copyright (C) 2008, 2013 Olaf Till <olaf.till@uni-jena.de>
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+
+
+#include <octave/oct.h>
+
+#include <unistd.h>
+#include <signal.h>
+
+// PKG_ADD: autoload ("__pq_internal_exit__", "pq_interface.oct");
+
+// This function duplicates __exit__.cc to avoid the dependency on the
+// package main/general.
+
+DEFUN_DLD (__pq_internal_exit__, args, , 
+  "-*- texinfo -*-\n\
+@deftypefn {Loadable Function} __pq_internal_exit__ (status)\n\
+This is a wrapper over the POSIX _exit() system call. Calling this function\n\
+will terminate the running process immediatelly, bypassing normal Octave\n\
+terminating sequence. It is suitable to terminate a forked process. It\n\
+should be considered expert-only and not to be used in normal code.\n\
+@end deftypefn") 
+{
+  _exit (args.length () > 0 ? args(0).int_value () : 0);
+}