Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- win_main.c.orig 2015-07-01 19:59:06.227617801 +0300
- +++ win_main.c 2015-07-01 21:40:15.831861595 +0300
- @@ -11,6 +11,10 @@
- #include <commdlg.h>
- #include <shellapi.h>
- +#include <stdio.h>
- +#include <stdlib.h>
- +#include <string.h>
- +
- #ifndef WM_MOUSEWHEEL
- #define WM_MOUSEWHEEL 0x020A
- #endif
- @@ -654,9 +658,165 @@ void winopen()
- SetCursor(arrowcurs);
- }
- +char *get_path_to_marks()
- +{
- + char *path = malloc(PATH_MAX);
- + if (path == NULL)
- + {
- + fprintf(stderr, "error: couldn't allocate memory\n");
- + return NULL;
- + }
- + if (!GetEnvironmentVariable("HOMEPATH", path, PATH_MAX))
- + {
- + fprintf(stderr, "error: couldn't get current user's home directory\n");
- + }
- + strcat(path, "\\.mupdf_marks");
- + return path;
- +}
- +
- +char *realpath(char *path) {
- + char *absolute = malloc(_MAX_PATH);
- + if (absolute == NULL)
- + {
- + fprintf(stderr, "error: couldn't allocate memory\n");
- + return NULL;
- + }
- + if (!GetFullPathName(path, absolute, _MAX_PATH, NULL))
- + {
- + fprintf(stderr, "error: GetFullPathName failed\n");
- + return NULL;
- + }
- + return absolute;
- +}
- +
- +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';
- + char *real = realpath(filename);
- + if (real == NULL) {
- + return;
- + }
- + if (!strcmp(line, real))
- + {
- + 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;
- + }
- + free(real);
- + 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 temp_name[L_tmpnam_s];
- + if (!tmpnam_s(temp_name, L_tmpnam_s))
- + {
- + FILE *new_marks_file = fopen(temp_name, "a+");
- + char line[LINE_MAX];
- + int found = 0;
- + char *realdocpath = realpath(app->docpath);
- + 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, realdocpath))
- + {
- + 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", realdocpath);
- + 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(realdocpath);
- + }
- + else
- + {
- + fprintf(stderr, "error: couldn't create a temporary file\n");
- + return;
- + }
- + }
- + free(marks_path);
- +}
- +
- static void
- do_close(pdfapp_t *app)
- {
- + write_marks(app);
- fz_context *ctx = app->ctx;
- pdfapp_close(app);
- free(dibinf);
- @@ -1280,6 +1440,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE h
- gapp.layout_css = layout_css_buf;
- }
- + read_marks(&gapp, filename);
- if (bps)
- pdfapp_open_progressive(&gapp, filename, 0, bps);
- else
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement