summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Hofmann <johannes.hofmann@gmx.de>2004-10-26 14:12:19 +0000
committerJohannes Hofmann <johannes.hofmann@gmx.de>2004-10-26 14:12:19 +0000
commitd2b86baae9e361de2f1a06cfefa0218f0c66ca8a (patch)
tree821051b910f6e6afc2cac4472c4829415c712877
parenta6c28e22fcfca80231bd1061d906195e5735fb37 (diff)
cleanup input/output file handling
cleanup input/output file handling
-rw-r--r--src/PSEditModel.H6
-rw-r--r--src/PSEditModel.cxx17
-rw-r--r--src/PSEditor.H3
-rw-r--r--src/PSEditor.cxx41
-rw-r--r--src/flpsed.cxx108
5 files changed, 122 insertions, 53 deletions
diff --git a/src/PSEditModel.H b/src/PSEditModel.H
index f69bf1f..eeff69c 100644
--- a/src/PSEditModel.H
+++ b/src/PSEditModel.H
@@ -1,5 +1,5 @@
//
-// "$Id: PSEditModel.H,v 1.6 2004/10/25 20:58:55 hofmann Exp $"
+// "$Id: PSEditModel.H,v 1.7 2004/10/26 16:12:19 hofmann Exp $"
//
// X11 header file for the Fast Light Tool Kit (FLTK).
//
@@ -60,8 +60,8 @@ public:
int ps_to_display_y(int y1);
int ps_x(int x1);
int ps_y(int y1);
- int PSEditModel::load(char *f);
- int save(const char* savefile, int tmp_fd);
+ int PSEditModel::load(FILE *fp);
+ int save(FILE *sfp, int tmp_fd);
};
diff --git a/src/PSEditModel.cxx b/src/PSEditModel.cxx
index b6bcbd2..2b5cb07 100644
--- a/src/PSEditModel.cxx
+++ b/src/PSEditModel.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: PSEditModel.cxx,v 1.6 2004/10/25 20:58:55 hofmann Exp $"
+// "$Id: PSEditModel.cxx,v 1.7 2004/10/26 16:12:19 hofmann Exp $"
//
// PSEditWidget routines.
//
@@ -217,8 +217,7 @@ int PSEditModel::ps_y(int y1) {
return paper_y - (int)((float) y1 * 72.0 / ydpi);
}
-int PSEditModel::load(char *f) {
- FILE *fp;
+int PSEditModel::load(FILE *fp) {
char tmpname[256];
char linebuf[1024];
int ret;
@@ -226,12 +225,6 @@ int PSEditModel::load(char *f) {
PSParser *p2 = new PSParser_2(this);
int tmp_fd;
- fp = fopen(f, "r");
- if (!fp) {
- fprintf(stderr, "Could not open file %s.\n", f);
- return -1;
- }
-
strncpy(tmpname, "/tmp/PSEditorXXXXXX", 256);
tmp_fd = mkstemp(tmpname);
if (tmp_fd < 0) {
@@ -251,7 +244,6 @@ int PSEditModel::load(char *f) {
}
}
- fclose(fp);
lseek(tmp_fd, 0L, SEEK_SET);
delete(p1);
@@ -262,7 +254,7 @@ int PSEditModel::load(char *f) {
-int PSEditModel::save(const char* savefile, int tmp_fd) {
+int PSEditModel::save(FILE *sfp, int tmp_fd) {
off_t pos = lseek(tmp_fd, 0, SEEK_CUR); // save current position
if (pos == -1) {
@@ -273,7 +265,7 @@ int PSEditModel::save(const char* savefile, int tmp_fd) {
FILE *fp = fdopen(tmp_fd, "r");
rewind(fp);
- FILE *sfp = fopen(savefile, "w");
+
PSWriter *pw;
pw = new PSLevel1Writer(this);
@@ -282,7 +274,6 @@ int PSEditModel::save(const char* savefile, int tmp_fd) {
delete(pw);
- fclose(sfp);
lseek(tmp_fd, pos, SEEK_SET); // restore current position
return 0;
diff --git a/src/PSEditor.H b/src/PSEditor.H
index b60696b..10efd39 100644
--- a/src/PSEditor.H
+++ b/src/PSEditor.H
@@ -1,5 +1,5 @@
//
-// "$Id: PSEditor.H,v 1.5 2004/10/23 19:57:14 hofmann Exp $"
+// "$Id: PSEditor.H,v 1.6 2004/10/26 16:12:19 hofmann Exp $"
//
// X11 header file for the Fast Light Tool Kit (FLTK).
//
@@ -35,6 +35,7 @@ class PSEditor : public PSEditWidget {
public:
PSEditor(int X,int Y,int W, int H);
+ int load(FILE *fp);
int load(char *f);
int save(const char* savefile);
int import(char *f);
diff --git a/src/PSEditor.cxx b/src/PSEditor.cxx
index 590a46f..eee0712 100644
--- a/src/PSEditor.cxx
+++ b/src/PSEditor.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: PSEditor.cxx,v 1.16 2004/10/25 20:58:55 hofmann Exp $"
+// "$Id: PSEditor.cxx,v 1.17 2004/10/26 16:12:19 hofmann Exp $"
//
// PSEditor routines.
//
@@ -116,12 +116,12 @@ int PSEditor::handle(int event) {
}
-int PSEditor::load(char *f) {
+int PSEditor::load(FILE *fp) {
if (tmp_fd) {
close(tmp_fd);
}
- tmp_fd = model->load(f);
+ tmp_fd = model->load(fp);
if (tmp_fd < 0) {
return 1;
@@ -132,17 +132,46 @@ int PSEditor::load(char *f) {
}
}
+int PSEditor::load(char *f) {
+ FILE *fp;
+ int ret;
+
+ fp = fopen(f, "r");
+ if (!fp) {
+ perror("fopen");
+ return 1;
+ }
+
+ ret = load(fp);
+ fclose(fp);
+
+ return ret;
+}
+
int PSEditor::save(const char* savefile) {
+ FILE *fp;
+ int ret;
+
if (!file_loaded()) {
return 1;
}
- if (model->save(savefile, tmp_fd) != 0) {
+ fp = fopen(savefile, "w");
+ if (!fp) {
+ perror("fopen");
return 1;
}
- mod = 0;
- return 0;
+
+ ret = model->save(fp, tmp_fd);
+
+ if (ret == 0) {
+ mod = 0;
+ }
+
+ fclose(fp);
+
+ return ret;
}
int PSEditor::import(char *f) {
diff --git a/src/flpsed.cxx b/src/flpsed.cxx
index 333f85c..301a273 100644
--- a/src/flpsed.cxx
+++ b/src/flpsed.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: flpsed.cxx,v 1.19 2004/10/25 20:58:55 hofmann Exp $"
+// "$Id: flpsed.cxx,v 1.20 2004/10/26 16:12:19 hofmann Exp $"
//
// flpsed program.
//
@@ -204,14 +204,15 @@ Fl_Menu_Item menuitems[] = {
#define TV_LEN 256
int main(int argc, char** argv) {
- char c, *sep, *tmp;
+ char c, *sep, *tmp, **my_argv;
int err, batch = 0;
Fl_Window *win;
Fl_Menu_Bar* m;
Fl_Scroll *scroll;
struct {char *tag; char *value;} tv[TV_LEN];
- int tv_idx = 0;
-
+ int tv_idx = 0, my_argc;
+ FILE *in_fp = NULL, *out_fp = NULL;
+
err = 0;
while ((c = getopt(argc, argv, "bt:")) != EOF) {
switch (c) {
@@ -251,47 +252,94 @@ int main(int argc, char** argv) {
exit(1);
}
- // argc -= optind;
- // argv += optind;
+ my_argc = argc - optind;
+ my_argv = argv + optind;
+
+ if (my_argc >= 1) {
+ in_fp = fopen(my_argv[0], "r");
+ if (!in_fp) {
+ perror("fopen");
+ exit(1);
+ }
+ }
+
if (batch) {
+ //
+ // Batch Mode
+ //
+
PSEditModel *m = new PSEditModel(594, 841, 75.0, 75.0);
- int tmp_fd = m->load(argv[argc - 2]);
+ int tmp_fd;
+
+ if (!in_fp) {
+ in_fp = stdin;
+ }
+
+ if (my_argc >= 2) {
+ out_fp = fopen(my_argv[1], "w");
+ if (!in_fp) {
+ perror("fopen");
+ exit(1);
+ }
+ } else {
+ out_fp = stdout;
+ }
+
+ tmp_fd= m->load(in_fp);
+
if (tmp_fd == -1) {
fprintf(stderr, "Could not load %s\n", argv[argc - 2]);
exit(1);
}
+ if (in_fp != stdin) {
+ fclose(in_fp);
+ }
+
for(int i=0; i<tv_idx; i++) {
m->replace_tag(tv[i].tag, tv[i].value);
free(tv[i].tag);
free(tv[i].value);
}
- m->save(argv[argc - 1], tmp_fd);
+ m->save(out_fp, tmp_fd);
- exit(0);
- }
-
-
-
-
- win = new Fl_Window(600,700);
- m = new Fl_Menu_Bar(0, 0, 600, 30);
- m->menu(menuitems);
- scroll = new Fl_Scroll(0, 30, win->w(), win->h()-30);
- gsw_p = new PSEditor(0, 0, 700, 900);
- scroll->end();
-
- fl_open_display();
- Fl::add_handler(xev_handler);
+ if (out_fp != stdout) {
+ fclose(out_fp);
+ }
- win->resizable(scroll);
+ } else {
+ //
+ // Interactive Mode
+ //
+
+ win = new Fl_Window(600,700);
+ m = new Fl_Menu_Bar(0, 0, 600, 30);
+ m->menu(menuitems);
+ scroll = new Fl_Scroll(0, 30, win->w(), win->h()-30);
+ gsw_p = new PSEditor(0, 0, 700, 900);
+ scroll->end();
+
+ fl_open_display();
+ Fl::add_handler(xev_handler);
+
+ win->resizable(scroll);
+
+ win->end();
+ win->callback((Fl_Callback *)quit_cb);
+ win->show(1, argv);
+
+ if (in_fp) {
+ gsw_p->load(in_fp);
+ fclose(in_fp);
+ }
- win->end();
- win->callback((Fl_Callback *)quit_cb);
- win->show(1, argv);
+ for(int i=0; i<tv_idx; i++) {
+ gsw_p->replace_tag(tv[i].tag, tv[i].value);
+ free(tv[i].tag);
+ free(tv[i].value);
+ }
-
- return Fl::run();
+ return Fl::run();
+ }
}
-