Advertisement
Guest User

w32 patch

a guest
Jul 1st, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.03 KB | None | 0 0
  1. --- win_main.c.orig 2015-07-01 19:59:06.227617801 +0300
  2. +++ win_main.c  2015-07-01 21:40:15.831861595 +0300
  3. @@ -11,6 +11,10 @@
  4.  #include <commdlg.h>
  5.  #include <shellapi.h>
  6.  
  7. +#include <stdio.h>
  8. +#include <stdlib.h>
  9. +#include <string.h>
  10. +
  11.  #ifndef WM_MOUSEWHEEL
  12.  #define WM_MOUSEWHEEL 0x020A
  13.  #endif
  14. @@ -654,9 +658,165 @@ void winopen()
  15.     SetCursor(arrowcurs);
  16.  }
  17.  
  18. +char *get_path_to_marks()
  19. +{
  20. +   char *path = malloc(PATH_MAX);
  21. +   if (path == NULL)
  22. +   {
  23. +       fprintf(stderr, "error: couldn't allocate memory\n");
  24. +       return NULL;
  25. +   }
  26. +   if (!GetEnvironmentVariable("HOMEPATH", path, PATH_MAX))
  27. +   {
  28. +       fprintf(stderr, "error: couldn't get current user's home directory\n");
  29. +   }
  30. +   strcat(path, "\\.mupdf_marks");
  31. +   return path;
  32. +}
  33. +
  34. +char *realpath(char *path) {
  35. +   char *absolute = malloc(_MAX_PATH);
  36. +   if (absolute == NULL)
  37. +   {
  38. +       fprintf(stderr, "error: couldn't allocate memory\n");
  39. +       return NULL;
  40. +   }
  41. +   if (!GetFullPathName(path, absolute, _MAX_PATH, NULL))
  42. +   {
  43. +       fprintf(stderr, "error: GetFullPathName failed\n");
  44. +       return NULL;
  45. +   }
  46. +   return absolute;
  47. +}
  48. +
  49. +void read_marks(pdfapp_t *app, char *filename)
  50. +{
  51. +   FILE *marks_file;
  52. +   char line[LINE_MAX];
  53. +   char *marks_path = get_path_to_marks();
  54. +   if (marks_path == NULL)
  55. +   {
  56. +       return;
  57. +   }
  58. +   if ((marks_file = fopen(marks_path, "r")) != NULL)
  59. +   {
  60. +       while (fgets(line, LINE_MAX, marks_file))
  61. +       {
  62. +           line[strlen(line)-1] = '\0';
  63. +           char *real = realpath(filename);
  64. +           if (real == NULL) {
  65. +               return;
  66. +           }
  67. +           if (!strcmp(line, real))
  68. +           {
  69. +               if (!fgets(line, LINE_MAX, marks_file))
  70. +               {
  71. +                   break;
  72. +               }
  73. +               int pageno;
  74. +               char *tail;
  75. +               char *head = line;
  76. +               for (int i = 0; i < nelem(app->marks); i++)
  77. +               {
  78. +                   if (*head == ' ')
  79. +                   {
  80. +                       head++;
  81. +                   }
  82. +                   else if (*head == 0)
  83. +                   {
  84. +                       break;
  85. +                   }
  86. +                   errno = 0;
  87. +                   pageno = strtol(head, &tail, 10);
  88. +                   if (errno)
  89. +                   {
  90. +                       fprintf(stderr, "error: %s\n", strerror(errno));
  91. +                       break;
  92. +                   }
  93. +                   else
  94. +                   {
  95. +                       app->marks[i] = pageno;
  96. +                   }
  97. +                   head = tail;
  98. +               }
  99. +               break;
  100. +           }
  101. +           free(real);
  102. +           fgets(line, LINE_MAX, marks_file);
  103. +       }
  104. +   }
  105. +   free(marks_path);
  106. +}
  107. +
  108. +void write_marks(pdfapp_t *app)
  109. +{
  110. +   int not_all_zeros = 0;
  111. +   char *marks_path = get_path_to_marks();
  112. +   if (marks_path == NULL)
  113. +   {
  114. +       return;
  115. +   }
  116. +   for (int i = 0; i < nelem(app->marks); i++)
  117. +   {
  118. +       if (app->marks[i] != 0)
  119. +       {
  120. +           not_all_zeros = 1;
  121. +           break;
  122. +       }
  123. +   }
  124. +   if (not_all_zeros)
  125. +   {
  126. +       FILE *marks_file = fopen(marks_path, "r");
  127. +       char temp_name[L_tmpnam_s];
  128. +       if (!tmpnam_s(temp_name, L_tmpnam_s))
  129. +       {
  130. +           FILE *new_marks_file = fopen(temp_name, "a+");
  131. +           char line[LINE_MAX];
  132. +           int found = 0;
  133. +           char *realdocpath = realpath(app->docpath);
  134. +           if (marks_file)
  135. +           {
  136. +               while (fgets(line, LINE_MAX, marks_file))
  137. +               {
  138. +                   fprintf(new_marks_file, "%s", line);
  139. +                   line[strlen(line)-1] = '\0';
  140. +                   if (!found && !strcmp(line, realdocpath))
  141. +                   {
  142. +                       for (int i = 0; i < nelem(app->marks); i++)
  143. +                       {
  144. +                           fprintf(new_marks_file, (i>0?" %d":"%d"), app->marks[i]);
  145. +                       }
  146. +                       fprintf(new_marks_file, "\n");
  147. +                       found = 1;
  148. +                       fgets(line, LINE_MAX, marks_file);
  149. +                   }
  150. +               }
  151. +           }
  152. +           if (!found)
  153. +           {
  154. +               fprintf(new_marks_file, "%s\n", realdocpath);
  155. +               for (int i = 0; i < nelem(app->marks); i++)
  156. +               {
  157. +                   fprintf(new_marks_file, (i>0?" %d":"%d"), app->marks[i]);
  158. +               }
  159. +               fprintf(new_marks_file, "\n");
  160. +           }
  161. +           rename(template, marks_path);
  162. +           free(realdocpath);
  163. +       }
  164. +       else
  165. +       {
  166. +           fprintf(stderr, "error: couldn't create a temporary file\n");
  167. +           return;
  168. +       }
  169. +   }
  170. +   free(marks_path);
  171. +}
  172. +
  173.  static void
  174.  do_close(pdfapp_t *app)
  175.  {
  176. +   write_marks(app);
  177.     fz_context *ctx = app->ctx;
  178.     pdfapp_close(app);
  179.     free(dibinf);
  180. @@ -1280,6 +1440,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE h
  181.         gapp.layout_css = layout_css_buf;
  182.     }
  183.  
  184. +   read_marks(&gapp, filename);
  185.     if (bps)
  186.         pdfapp_open_progressive(&gapp, filename, 0, bps);
  187.     else
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement