#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);
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;
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)
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)