summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/GsWidget.H2
-rw-r--r--src/GsWidget.cxx8
-rw-r--r--src/PostscriptDSC.cxx59
3 files changed, 51 insertions, 18 deletions
diff --git a/src/GsWidget.H b/src/GsWidget.H
index cc4e9fd..9fb6372 100644
--- a/src/GsWidget.H
+++ b/src/GsWidget.H
@@ -37,6 +37,7 @@
#include "PostscriptDSC.H"
class GsWidget : public Fl_Widget {
+private:
PostscriptDSC *dsc;
int gs_win;
Atom atoms[5];
@@ -45,6 +46,7 @@ class GsWidget : public Fl_Widget {
int in_fd, reload_needed;
int zoom_percent;
int initial_width, initial_height;
+ int feeding;
protected:
int page;
diff --git a/src/GsWidget.cxx b/src/GsWidget.cxx
index 3d3a081..27a8cb7 100644
--- a/src/GsWidget.cxx
+++ b/src/GsWidget.cxx
@@ -115,6 +115,7 @@ GsWidget::GsWidget(int X,int Y,int W, int H) : Fl_Widget(X, Y, W, H) {
in_fd = -1;
reload_needed = 0;
dsc = NULL;
+ feeding = 0;
}
GsWidget::~GsWidget() {
@@ -204,7 +205,7 @@ GsWidget::load_page(int p) {
pid_t pid;
int pdes[2];
- if (in_fd < 0) {
+ if (feeding || in_fd < 0) {
return 1;
}
@@ -225,6 +226,8 @@ GsWidget::load_page(int p) {
perror("pipe");
return 1;
}
+
+ feeding = 1;
lseek(in_fd, 0L, SEEK_SET);
setProps();
@@ -247,6 +250,7 @@ GsWidget::load_page(int p) {
len = dsc->get_setup_len();
if (fd_copy(pdes[1], in_fd, len) != 0) {
close(pdes[1]);
+ feeding = 0;
return 1;
}
@@ -254,12 +258,14 @@ GsWidget::load_page(int p) {
len = dsc->get_page_len(p);
if (fd_copy(pdes[1], in_fd, len) != 0) {
close(pdes[1]);
+ feeding = 0;
return 1;
}
close(pdes[1]);
}
+ feeding = 0;
return 0;
}
diff --git a/src/PostscriptDSC.cxx b/src/PostscriptDSC.cxx
index 40b39c9..dfc9775 100644
--- a/src/PostscriptDSC.cxx
+++ b/src/PostscriptDSC.cxx
@@ -36,6 +36,12 @@ PostscriptDSC::PostscriptDSC() {
}
PostscriptDSC::~PostscriptDSC() {
+ if (page_off) {
+ free(page_off);
+ }
+ if (page_len) {
+ free(page_len);
+ }
}
int
@@ -52,8 +58,15 @@ PostscriptDSC::parse(int fd) {
bb_h = 841; //
setup_len = 0;
pages = 0;
- page_off = NULL;
- page_len = NULL;
+
+ if (page_off) {
+ free(page_off);
+ page_off = NULL;
+ }
+ if(page_len) {
+ free(page_len);
+ page_len = NULL;
+ }
fp = fdopen(fd, "r");
if (!fp) {
@@ -72,38 +85,50 @@ PostscriptDSC::parse(int fd) {
} else if (sscanf(linebuf, "%%%%Pages: %d", &ps) == 1) {
if (pages != 0) {
fprintf(stderr, "Multiple Pages tags found\n");
- free(page_off);
- free(page_len);
return 1;
}
pages = ps;
- page_off = (size_t*) malloc(sizeof(size_t) * (pages + 1));
- memset(page_off, 0, sizeof(size_t) * (pages + 1));
- page_len = (size_t*) malloc(sizeof(size_t) * (pages + 1));
- memset(page_len, 0, sizeof(size_t) * (pages + 1));
+ page_off = (size_t*) malloc(sizeof(size_t) * pages);
+ memset(page_off, 0, sizeof(size_t) * pages);
+ page_len = (size_t*) malloc(sizeof(size_t) * pages);
+ memset(page_len, 0, sizeof(size_t) * pages);
+
} else if (sscanf(linebuf, "%%%%Page: %d %d", &p1, &p2) == 2) {
+
+ if (!page_off || !page_len) {
+ fprintf(stderr, "Number of pages not defined\n");
+ return 1;
+ }
+
if (p1 > pages || p1 < 1) {
- fprintf(stderr, "Page %d out of range (0 - %d)\n", p1, pages);
+ fprintf(stderr, "Page %d out of range (1 - %d)\n", p1, pages);
return 1;
}
- if (page_off[p1] != NULL) {
+ if (page_off[p1 - 1] != 0) {
fprintf(stderr, "Page %d already defined\n", p1);
return 1;
}
- if (p1 > 1 && page_off[p1 - 1] == NULL) {
+ if (p1 > 1 && page_off[p1 - 2] == NULL) {
fprintf(stderr, "Page %d not yet defined\n", p1 - 1);
return 1;
}
- page_off[p1] = ftello(fp);
+ page_off[p1 - 1] = ftello(fp);
if (p1 > 1) {
- page_len[p1 - 1] = page_off[p1] - page_off[p1 - 1];
+ page_len[p1 - 2] = page_off[p1 - 1] - page_off[p1 - 2];
}
}
}
- page_len[p1] = ftello(fp) - page_off[p1];
+ page_len[p1 - 1] = ftello(fp) - page_off[p1 - 1];
+
+ for (int i=0; i<pages; i++) {
+ if (page_off[i] == 0 || page_len[i] == 0) {
+ fprintf(stderr, "Page %d not defined\n", i);
+ return 1;
+ }
+ }
return 0;
}
@@ -128,12 +153,12 @@ PostscriptDSC::get_setup_len() {
size_t
PostscriptDSC::get_page_off(int p) {
- return page_off[p+1];
+ return page_off[p];
}
size_t
PostscriptDSC::get_page_len(int p) {
- return page_len[p+1];
+ return page_len[p];
}
void
@@ -142,7 +167,7 @@ PostscriptDSC::print() {
printf("x %d, y %d, w %d, h %d\n", bb_x, bb_y, bb_w, bb_h);
printf("setup_len %d\n", setup_len);
- for (i=1; i<=pages; i++) {
+ for (i=0; i<pages; i++) {
printf("p %d, off %d, len %d\n", i, page_off[i], page_len[i]);
}
}