]> granicus.if.org Git - graphviz/commitdiff
redirect cout to gvdevice_fputs()
authorellson <devnull@localhost>
Fri, 5 Sep 2008 11:18:23 +0000 (11:18 +0000)
committerellson <devnull@localhost>
Fri, 5 Sep 2008 11:18:23 +0000 (11:18 +0000)
lib/gvc/gvplugin_render.h
plugin/lasi/gvrender_lasi.cpp

index 03348d4537986002404dd5b5bc5f5f77e804e25e..86e6b1d793a8f52f082d4a0e7f9a88b21856f46b 100644 (file)
 #include "gvplugin.h"
 #include "gvcjob.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 extern void gvdevice_fputs(GVJ_t * job, const char *s);
 extern void gvdevice_printf(GVJ_t * job, const char *format, ...);
 extern void gvdevice_printnum(GVJ_t * job, double num);
 extern void gvdevice_printpointf(GVJ_t * job, pointf p);
 extern void gvdevice_printpointflist(GVJ_t * job, pointf *p, int n);
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
     struct gvrender_engine_s {
        void (*begin_job) (GVJ_t * job);
        void (*end_job) (GVJ_t * job);
index 4e9c9299a64d16e7f19392d2eceaf4eceb33d55d..fe757271502f2cc3e414bf61c8c875020cd496a6 100644 (file)
@@ -44,8 +44,6 @@ using namespace std;
 
 extern "C" {
        extern void epsf_define(FILE * of);
-//     extern char *ps_string(char *ins, int latin);
-       extern size_t gvdevice_write(GVJ_t *job, const char *s, unsigned int n);
 }
 
 typedef enum { FORMAT_PS, FORMAT_PS2, FORMAT_EPS } format_type;
@@ -122,24 +120,30 @@ static void lasi_begin_job(GVJ_t * job)
     doc.osHeader() << "%%For: " << job->common->user << endl;
 }
 
-// ostream wrapper for gvdevice_write
-// Based on idea in: http://www.oneunified.net/blog/OpenSource/Programming/streamoverload.article
-class Gvout : public streambuf {
-private:
-  GVJ_t *thisjob;
-  // write a string s of length n to the current gvdevice
-  int xsputn (char_type* s, streamsize n) {
-    return gvdevice_write(thisjob, s, n);
-  }
-public:
-  Gvout (GVJ_t *job) {
-    thisjob=job;
-  }
-};
-
 static void lasi_end_job(GVJ_t * job)
 {
-    ostream gvout(new Gvout(job));
+    // create the new stream to "redirect" cout's output to
+    ostringstream output;
+    string str;
+
+    // smart class that will swap streambufs and replace them
+    // when object goes out of scope.
+    class StreamBuf_Swapper
+    {
+    public:
+       StreamBuf_Swapper(ostream & orig, ostream & replacement)
+                     : buf_(orig.rdbuf()), str_(orig)
+       {
+           orig.rdbuf(replacement.rdbuf());
+       }
+       ~StreamBuf_Swapper()
+       {
+           str_.rdbuf(buf_);
+       }
+    private:
+       std::streambuf * buf_;
+       std::ostream & str_;
+    } swapper(cout, output);
 
 //    gvdevice_fputs(job, "%%Trailer\n");
     if (job->render.id != FORMAT_EPS)
@@ -159,8 +163,9 @@ static void lasi_end_job(GVJ_t * job)
     doc.osFooter() << "restore" << endl;
 //    gvdevice_fputs(job, "%%EOF\n");
 
-//    doc.write(cout);
-    doc.write(gvout);
+    doc.write(cout);
+    str =  output.str();
+    gvdevice_fputs(job, str.c_str());
 }
 
 static void lasi_begin_graph(GVJ_t * job)