Advertisement
Guest User

x11 patch

a guest
Jul 1st, 2015
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.52 KB | None | 0 0
  1. --- x11_main.c.orig 2015-07-01 19:37:45.211670210 +0300
  2. +++ x11_main.c  2015-07-01 19:43:21.540775063 +0300
  3. @@ -14,6 +14,11 @@
  4.  #include <unistd.h>
  5.  #include <signal.h>
  6.  
  7. +#include <stdlib.h>
  8. +#include <string.h>
  9. +#include <errno.h>
  10. +#include <pwd.h>
  11. +
  12.  #define mupdf_icon_bitmap_16_width 16
  13.  #define mupdf_icon_bitmap_16_height 16
  14.  static unsigned char mupdf_icon_bitmap_16_bits[] = {
  15. @@ -328,8 +333,139 @@ void wincopyfile(char *source, char *tar
  16.     }
  17.  }
  18.  
  19. +char *get_path_to_marks()
  20. +{
  21. +   char *path = malloc(PATH_MAX);
  22. +   if (path == NULL)
  23. +   {
  24. +       fprintf(stderr, "error: couldn't allocate memory");
  25. +       return NULL;
  26. +   }
  27. +   char *homedir;
  28. +   if ((homedir = getenv("HOME")) == NULL)
  29. +   {
  30. +       homedir = getpwuid(getuid())->pw_dir;
  31. +   }
  32. +   strcpy(path, homedir);
  33. +   strcat(path, "/.mupdf_marks");
  34. +   return path;
  35. +}
  36. +
  37. +void read_marks(pdfapp_t *app, char *filename)
  38. +{
  39. +   FILE *marks_file;
  40. +   char line[LINE_MAX];
  41. +   char *marks_path = get_path_to_marks();
  42. +   if (marks_path == NULL)
  43. +   {
  44. +       return;
  45. +   }
  46. +   if ((marks_file = fopen(marks_path, "r")) != NULL)
  47. +   {
  48. +       while (fgets(line, LINE_MAX, marks_file))
  49. +       {
  50. +           line[strlen(line)-1] = '\0';
  51. +           if (!strcmp(line, realpath(filename, NULL)))
  52. +           {
  53. +               if (!fgets(line, LINE_MAX, marks_file))
  54. +               {
  55. +                   break;
  56. +               }
  57. +               int pageno;
  58. +               char *tail;
  59. +               char *head = line;
  60. +               for (int i = 0; i < nelem(app->marks); i++)
  61. +               {
  62. +                   if (*head == ' ')
  63. +                   {
  64. +                       head++;
  65. +                   }
  66. +                   else if (*head == 0)
  67. +                   {
  68. +                       break;
  69. +                   }
  70. +                   errno = 0;
  71. +                   pageno = strtol(head, &tail, 10);
  72. +                   if (errno)
  73. +                   {
  74. +                       fprintf(stderr, "error: %s\n", strerror(errno));
  75. +                       break;
  76. +                   }
  77. +                   else
  78. +                   {
  79. +                       app->marks[i] = pageno;
  80. +                   }
  81. +                   head = tail;
  82. +               }
  83. +               break;
  84. +           }
  85. +           fgets(line, LINE_MAX, marks_file);
  86. +       }
  87. +   }
  88. +   free(marks_path);
  89. +}
  90. +
  91. +void write_marks(pdfapp_t *app)
  92. +{
  93. +   int not_all_zeros = 0;
  94. +   char *marks_path = get_path_to_marks();
  95. +   if (marks_path == NULL)
  96. +   {
  97. +       return;
  98. +   }
  99. +   for (int i = 0; i < nelem(app->marks); i++)
  100. +   {
  101. +       if (app->marks[i] != 0)
  102. +       {
  103. +           not_all_zeros = 1;
  104. +           break;
  105. +       }
  106. +   }
  107. +   if (not_all_zeros) {
  108. +       FILE *marks_file = fopen(marks_path, "r");
  109. +       char template[] = "./mupdfXXXXXX";
  110. +       int tmpfd = mkstemp(template);
  111. +       if (tmpfd != -1)
  112. +       {
  113. +           FILE *new_marks_file = fdopen(tmpfd, "a+");
  114. +           char line[LINE_MAX];
  115. +           int found = 0;
  116. +           if (marks_file)
  117. +           {
  118. +               while (fgets(line, LINE_MAX, marks_file))
  119. +               {
  120. +                   fprintf(new_marks_file, "%s", line);
  121. +                   line[strlen(line)-1] = '\0';
  122. +                   if (!found && !strcmp(line, realpath(app->docpath, NULL)))
  123. +                   {
  124. +                       for (int i = 0; i < nelem(app->marks); i++)
  125. +                       {
  126. +                           fprintf(new_marks_file, (i>0?" %d":"%d"), app->marks[i]);
  127. +                       }
  128. +                       fprintf(new_marks_file, "\n");
  129. +                       found = 1;
  130. +                       fgets(line, LINE_MAX, marks_file);
  131. +                   }
  132. +               }
  133. +           }
  134. +           if (!found)
  135. +           {
  136. +               fprintf(new_marks_file, "%s\n", realpath(app->docpath, NULL));
  137. +               for (int i = 0; i < nelem(app->marks); i++)
  138. +               {
  139. +                   fprintf(new_marks_file, (i>0?" %d":"%d"), app->marks[i]);
  140. +               }
  141. +               fprintf(new_marks_file, "\n");
  142. +           }
  143. +           rename(template, marks_path);
  144. +       }
  145. +   }
  146. +   free(marks_path);
  147. +}
  148. +
  149.  void cleanup(pdfapp_t *app)
  150.  {
  151. +   write_marks(app);
  152.     fz_context *ctx = app->ctx;
  153.  
  154.     pdfapp_close(app);
  155. @@ -884,6 +1020,7 @@ int main(int argc, char **argv)
  156.     tmo_at.tv_usec = 0;
  157.     timeout = NULL;
  158.  
  159. +   read_marks(&gapp, filename);
  160.     pdfapp_open(&gapp, filename, 0);
  161.  
  162.     FD_ZERO(&fds);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement