Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- x11_main.c.orig 2015-07-01 19:37:45.211670210 +0300
- +++ x11_main.c 2015-07-01 19:43:21.540775063 +0300
- @@ -14,6 +14,11 @@
- #include <unistd.h>
- #include <signal.h>
- +#include <stdlib.h>
- +#include <string.h>
- +#include <errno.h>
- +#include <pwd.h>
- +
- #define mupdf_icon_bitmap_16_width 16
- #define mupdf_icon_bitmap_16_height 16
- static unsigned char mupdf_icon_bitmap_16_bits[] = {
- @@ -328,8 +333,139 @@ void wincopyfile(char *source, char *tar
- }
- }
- +char *get_path_to_marks()
- +{
- + char *path = malloc(PATH_MAX);
- + if (path == NULL)
- + {
- + fprintf(stderr, "error: couldn't allocate memory");
- + return NULL;
- + }
- + char *homedir;
- + if ((homedir = getenv("HOME")) == NULL)
- + {
- + homedir = getpwuid(getuid())->pw_dir;
- + }
- + strcpy(path, homedir);
- + strcat(path, "/.mupdf_marks");
- + return path;
- +}
- +
- +void read_marks(pdfapp_t *app, char *filename)
- +{
- + FILE *marks_file;
- + char line[LINE_MAX];
- + char *marks_path = get_path_to_marks();
- + if (marks_path == NULL)
- + {
- + return;
- + }
- + if ((marks_file = fopen(marks_path, "r")) != NULL)
- + {
- + while (fgets(line, LINE_MAX, marks_file))
- + {
- + line[strlen(line)-1] = '\0';
- + if (!strcmp(line, realpath(filename, NULL)))
- + {
- + if (!fgets(line, LINE_MAX, marks_file))
- + {
- + break;
- + }
- + int pageno;
- + char *tail;
- + char *head = line;
- + for (int i = 0; i < nelem(app->marks); i++)
- + {
- + if (*head == ' ')
- + {
- + head++;
- + }
- + else if (*head == 0)
- + {
- + break;
- + }
- + errno = 0;
- + pageno = strtol(head, &tail, 10);
- + if (errno)
- + {
- + fprintf(stderr, "error: %s\n", strerror(errno));
- + break;
- + }
- + else
- + {
- + app->marks[i] = pageno;
- + }
- + head = tail;
- + }
- + break;
- + }
- + fgets(line, LINE_MAX, marks_file);
- + }
- + }
- + free(marks_path);
- +}
- +
- +void write_marks(pdfapp_t *app)
- +{
- + int not_all_zeros = 0;
- + char *marks_path = get_path_to_marks();
- + if (marks_path == NULL)
- + {
- + return;
- + }
- + for (int i = 0; i < nelem(app->marks); i++)
- + {
- + if (app->marks[i] != 0)
- + {
- + not_all_zeros = 1;
- + break;
- + }
- + }
- + if (not_all_zeros) {
- + FILE *marks_file = fopen(marks_path, "r");
- + char template[] = "./mupdfXXXXXX";
- + int tmpfd = mkstemp(template);
- + if (tmpfd != -1)
- + {
- + FILE *new_marks_file = fdopen(tmpfd, "a+");
- + char line[LINE_MAX];
- + int found = 0;
- + if (marks_file)
- + {
- + while (fgets(line, LINE_MAX, marks_file))
- + {
- + fprintf(new_marks_file, "%s", line);
- + line[strlen(line)-1] = '\0';
- + if (!found && !strcmp(line, realpath(app->docpath, NULL)))
- + {
- + for (int i = 0; i < nelem(app->marks); i++)
- + {
- + fprintf(new_marks_file, (i>0?" %d":"%d"), app->marks[i]);
- + }
- + fprintf(new_marks_file, "\n");
- + found = 1;
- + fgets(line, LINE_MAX, marks_file);
- + }
- + }
- + }
- + if (!found)
- + {
- + fprintf(new_marks_file, "%s\n", realpath(app->docpath, NULL));
- + for (int i = 0; i < nelem(app->marks); i++)
- + {
- + fprintf(new_marks_file, (i>0?" %d":"%d"), app->marks[i]);
- + }
- + fprintf(new_marks_file, "\n");
- + }
- + rename(template, marks_path);
- + }
- + }
- + free(marks_path);
- +}
- +
- void cleanup(pdfapp_t *app)
- {
- + write_marks(app);
- fz_context *ctx = app->ctx;
- pdfapp_close(app);
- @@ -884,6 +1020,7 @@ int main(int argc, char **argv)
- tmo_at.tv_usec = 0;
- timeout = NULL;
- + read_marks(&gapp, filename);
- pdfapp_open(&gapp, filename, 0);
- FD_ZERO(&fds);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement