diff options
-rw-r--r-- | src/ImageMetaData.cxx | 61 | ||||
-rw-r--r-- | src/gipfel.cxx | 18 |
2 files changed, 40 insertions, 39 deletions
diff --git a/src/ImageMetaData.cxx b/src/ImageMetaData.cxx index fb1cc8c..07a5c1b 100644 --- a/src/ImageMetaData.cxx +++ b/src/ImageMetaData.cxx @@ -5,7 +5,9 @@ // of the GNU General Public License, incorporated herein by reference. #include <stdlib.h> +#include <unistd.h> #include <stdio.h> +#include <errno.h> #include <math.h> #include <string.h> #include <sys/types.h> @@ -204,29 +206,17 @@ ImageMetaData::load_image_jpgcom(char *name) { int ImageMetaData::save_image_jpgcom(char *in_img, char *out_img) { char * args[32]; - FILE *p, *out; + FILE *p; pid_t pid; - char buf[1024]; - int status; - size_t n; - struct stat in_stat, out_stat; - - if (stat(in_img, &in_stat) != 0) { - perror("stat"); - return 1; - } - - if (stat(out_img, &out_stat) == 0) { - if (in_stat.st_ino == out_stat.st_ino) { - fprintf(stderr, "Input image %s and output image %s are the same file\n", - in_img, out_img); - return 1; - } - } - - out = fopen(out_img, "w"); - if (out == NULL) { - perror("fopen"); + char buf[1024], tmpname[256]; + int status, err = 0; + ssize_t n; + int tmp_fd; + + strncpy(tmpname, "/tmp/gipfelXXXXXX", sizeof(tmpname)); + tmp_fd = mkstemp(tmpname); + if (tmp_fd < 0) { + perror("mkstemp"); return 1; } @@ -253,21 +243,32 @@ ImageMetaData::save_image_jpgcom(char *in_img, char *out_img) { if (p) { while ((n = fread(buf, 1, sizeof(buf), p)) != 0) { - if (fwrite(buf, 1, n, out) != n) { - perror("fwrite"); - fclose(out); - fclose(p); - waitpid(pid, &status, 0); + if (write(tmp_fd, buf, n) != n) { + perror("write"); + err++; + break; } } fclose(p); waitpid(pid, &status, 0); + if (WEXITSTATUS(status) != 0) + err++; if (WEXITSTATUS(status) == 127 || WEXITSTATUS(status) == 126) fprintf(stderr, "%s not found\n", args[0]); - } + } else { + err++; + } - fclose(out); - return 0; + close(tmp_fd); + + if (!err) { + if (rename(tmpname, out_img) != 0) { + perror("rename"); + err++; + } + } + + return err != 0; } void diff --git a/src/gipfel.cxx b/src/gipfel.cxx index 9a48ade..bcf4816 100644 --- a/src/gipfel.cxx +++ b/src/gipfel.cxx @@ -40,7 +40,7 @@ #define DEFAULT_DATAFILE DATADIR "/" PACKAGE_NAME "/alpinkoordinaten.dat" -char *img_file; +char *img_file = NULL; char *data_file = DEFAULT_DATAFILE; GipfelWidget *gipf = NULL; @@ -103,21 +103,22 @@ void open_cb() { view_win->redraw(); control_win->label(file); set_values(); + if (img_file) + free(img_file); + img_file = strdup(file); } } void track_cb() { char *file = fl_file_chooser("Track File?", NULL, NULL); - if (file && gipf->load_track(file) == 0) { + if (file && gipf->load_track(file) == 0) s_track_width->activate(); - } } void save_cb() { - char *file = fl_file_chooser("Save Image As?", NULL, NULL); - if (file) { + char *file = fl_file_chooser("Save Image As?", NULL, img_file); + if (file) gipf->save_image(file); - } } void focal_length_cb(Fl_Slider* o, void*) { @@ -506,9 +507,8 @@ int main(int argc, char** argv) { my_argc = argc - optind; my_argv = argv + optind; - if (my_argc >= 1) { - img_file = my_argv[0]; - } + if (my_argc >= 1) + img_file = strdup(my_argv[0]); if (data_file == NULL || err) { usage(); |