From a82b383687cf380806879465d56492072f2236a5 Mon Sep 17 00:00:00 2001 From: ellson Date: Fri, 5 Sep 2008 11:18:23 +0000 Subject: [PATCH] redirect cout to gvdevice_fputs() --- lib/gvc/gvplugin_render.h | 8 +++---- plugin/lasi/gvrender_lasi.cpp | 45 +++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/gvc/gvplugin_render.h b/lib/gvc/gvplugin_render.h index 03348d453..86e6b1d79 100644 --- a/lib/gvc/gvplugin_render.h +++ b/lib/gvc/gvplugin_render.h @@ -21,16 +21,16 @@ #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); diff --git a/plugin/lasi/gvrender_lasi.cpp b/plugin/lasi/gvrender_lasi.cpp index 4e9c9299a..fe7572715 100644 --- a/plugin/lasi/gvrender_lasi.cpp +++ b/plugin/lasi/gvrender_lasi.cpp @@ -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) -- 2.40.0