41 static char z_file_header[] =
42 {0x1f, 0x8b, Z_DEFLATED, 0 , 0,0,0,0 , 0 , OS_CODE};
44 static z_stream z_strm;
45 static unsigned char *df;
46 static unsigned int dfallocated;
59 static const int PAGE_ALIGN = 4095;
61 static size_t gvwrite_no_z (
GVJ_t * job,
const char *
s,
size_t len)
81 return fwrite(s,
sizeof(
char), len, job->
output_file);
85 static void auto_output_filename(
GVJ_t *job)
106 buf = realloc(buf, bufsz *
sizeof(
char));
112 while ((q = strrchr(p,
':'))) {
139 auto_output_filename(job);
143 (job->
common->
errorfn) (
"Could not open \"%s\" for writing : %s\n",
166 z_stream *z = &z_strm;
175 crc = crc32(0L, Z_NULL, 0);
177 if (deflateInit2(z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
178 (job->
common->
errorfn) (
"Error initializing for deflation\n");
181 gvwrite_no_z(job, z_file_header,
sizeof(z_file_header));
199 z_streamp z = &z_strm;
202 #ifdef HAVE_DEFLATEBOUND
203 dflen = deflateBound(z, len);
206 dflen = 2 * len + dfallocated - z->avail_out;
208 if (dfallocated < dflen) {
209 dfallocated = (dflen + 1 + PAGE_ALIGN) & ~PAGE_ALIGN;
210 df = realloc(df, dfallocated);
217 crc = crc32(crc, (
unsigned char*)s, len);
219 z->next_in = (
unsigned char*)s;
221 while (z->avail_in) {
223 z->avail_out = dfallocated;
224 ret=deflate (z, Z_NO_FLUSH);
230 if ((olen = z->next_out - df)) {
231 ret = gvwrite_no_z (job, (
char*)df, olen);
245 ret = gvwrite_no_z (job, s, len);
264 size_t gvfwrite (
const void *ptr,
size_t size,
size_t nmemb, FILE *stream)
266 assert(size =
sizeof(
char));
272 size_t len = strlen(s);
274 if (
gvwrite (job, s, len) != len) {
284 if (
gvwrite (job, &cc, 1) != 1) {
301 static void gvdevice_close(
GVJ_t * job)
326 boolean finalized_p =
FALSE;
330 z_streamp z = &z_strm;
331 unsigned char out[8] =
"";
338 z->avail_out = dfallocated;
339 while ((ret = deflate (z, Z_FINISH)) == Z_OK && (cnt++ <= 100)) {
340 gvwrite_no_z(job, (
char*)df, z->next_out - df);
342 z->avail_out = dfallocated;
344 if (ret != Z_STREAM_END) {
345 (job->
common->
errorfn) (
"deflation finish problem %d cnt=%d\n", ret, cnt);
348 gvwrite_no_z(job, (
char*)df, z->next_out - df);
359 out[4] = z->total_in;
360 out[5] = z->total_in >> 8;
361 out[6] = z->total_in >> 16;
362 out[7] = z->total_in >> 24;
363 gvwrite_no_z(job, (
char*)out,
sizeof(out));
396 va_start(argp, format);
397 #ifdef HAVE_VSNPRINTF
398 len = vsnprintf((
char *)buf, BUFSIZ, format, argp);
400 agerr (
AGERR,
"gvprintf: %s\n", strerror(errno));
403 else if (len >= BUFSIZ) {
409 va_start(argp, format);
410 len = vsprintf(bp, format, argp);
413 len = vsprintf((
char *)buf, format, argp);
428 #define DECPLACES_SCALE 10000
431 #define val_str(n, x) static double n = x; static char n##str[] = #x;
432 val_str(maxnegnum, -999999999999999.99)
438 static char * gvprintnum (
size_t *len,
double number)
440 static char tmpbuf[
sizeof(maxnegnumstr)];
441 char *result = tmpbuf+
sizeof(maxnegnumstr);
443 boolean showzeros, negative;
454 if (number < maxnegnum) {
455 *len =
sizeof(maxnegnumstr)-1;
456 return maxnegnumstr;;
458 if (number > -maxnegnum) {
459 *len =
sizeof(maxnegnumstr)-2;
460 return maxnegnumstr+1;
471 if ((negative = (N < 0)))
473 #ifdef TERMINATED_NUMBER_STRING
481 if (digit || showzeros) {
483 *--result = digit |
'0';
494 #ifdef TERMINATED_NUMBER_STRING
495 *len = tmpbuf+
sizeof(maxnegnumstr)-1 - result;
497 *len = tmpbuf+
sizeof(maxnegnumstr) - result;
503 #ifdef GVPRINTNUM_TEST
504 int main (
int argc,
char *argv[])
510 -maxnegnum*1.1, -maxnegnum*.9,
511 1e8, 10.008, 10, 1, .1, .01,
512 .006, .005, .004, .001, 1e-8,
514 -1e-8, -.001, -.004, -.005, -.006,
515 -.01, -.1, -1, -10, -10.008, -1e8,
516 maxnegnum*.9, maxnegnum*1.1
518 int i =
sizeof(test) /
sizeof(test[0]);
521 buf = gvprintnum(&len, test[i]);
522 fprintf (stdout,
"%g = %s %d\n", test[i], buf, len);
533 static void gv_trim_zeros(
char* buf,
int addSpace)
538 if ((dotp = strchr(buf,
'.'))) {
542 while (*p ==
'0') *p-- =
'\0';
549 p = buf + strlen(buf);
560 if (num > -0.00000001 && num < 0.00000001)
567 snprintf(buf, 50,
"%.02f", num);
568 gv_trim_zeros(buf, 0);
570 gvwrite(job, buf, strlen(buf));
578 buf = gvprintnum(&len, p.
x);
581 buf = gvprintnum(&len, p.
y);
int gvputc(GVJ_t *job, int c)
unsigned int output_data_allocated
#define GVDEVICE_BINARY_FORMAT
void gvprintpointflist(GVJ_t *job, pointf *p, int n)
void * gmalloc(size_t nbytes)
boolean auto_outfile_names
const char * output_filename
int agerr(agerrlevel_t level, const char *fmt,...)
void(* finalize)(GVJ_t *firstjob)
gvplugin_active_device_t device
int gvputs(GVJ_t *job, const char *s)
unsigned int output_data_position
void gvdevice_finalize(GVJ_t *job)
void gvprintpointf(GVJ_t *job, pointf p)
void(* errorfn)(const char *fmt,...)
void gvprintdouble(GVJ_t *job, double num)
int gvferror(FILE *stream)
void gvdevice_format(GVJ_t *job)
void(* initialize)(GVJ_t *firstjob)
size_t gvfwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
#define GVDEVICE_COMPRESSED_FORMAT
void(* format)(GVJ_t *firstjob)
int gvdevice_initialize(GVJ_t *job)
gvdevice_engine_t * engine
int main(int argc, char **argv)
const char * output_langname
size_t gvwrite(GVJ_t *job, const char *s, size_t len)
size_t(* write_fn)(GVJ_t *job, const char *s, size_t len)
void gvprintf(GVJ_t *job, const char *format,...)