scr_dump.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. scr_dump
  5. --------
  6. ### Synopsis
  7. int putwin(WINDOW *win, FILE *filep);
  8. WINDOW *getwin(FILE *filep);
  9. int scr_dump(const char *filename);
  10. int scr_init(const char *filename);
  11. int scr_restore(const char *filename);
  12. int scr_set(const char *filename);
  13. ### Description
  14. getwin() reads window-related data previously stored in a file by
  15. putwin(). It then creates and initialises a new window using that
  16. data.
  17. putwin() writes all data associated with a window into a file, using
  18. an unspecified format. This information can be retrieved later using
  19. getwin().
  20. scr_dump() writes the current contents of the virtual screen to the
  21. file named by filename in an unspecified format.
  22. scr_restore() function sets the virtual screen to the contents of the
  23. file named by filename, which must have been written using
  24. scr_dump(). The next refresh operation restores the screen to the way
  25. it looked in the dump file.
  26. In PDCurses, scr_init() does nothing, and scr_set() is a synonym for
  27. scr_restore(). Also, scr_dump() and scr_restore() save and load from
  28. curscr. This differs from some other implementations, where
  29. scr_init() works with curscr, and scr_restore() works with newscr;
  30. but the effect should be the same. (PDCurses has no newscr.)
  31. ### Return Value
  32. On successful completion, getwin() returns a pointer to the window it
  33. created. Otherwise, it returns a null pointer. Other functions return
  34. OK or ERR.
  35. ### Portability
  36. X/Open ncurses NetBSD
  37. putwin Y Y Y
  38. getwin Y Y Y
  39. scr_dump Y Y -
  40. scr_init Y Y -
  41. scr_restore Y Y -
  42. scr_set Y Y -
  43. **man-end****************************************************************/
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #define DUMPVER 1 /* Should be updated whenever the WINDOW struct is
  47. changed */
  48. int putwin(WINDOW *win, FILE *filep)
  49. {
  50. static const char *marker = "PDC";
  51. static const unsigned char version = DUMPVER;
  52. PDC_LOG(("putwin() - called\n"));
  53. /* write the marker and the WINDOW struct */
  54. if (filep && fwrite(marker, strlen(marker), 1, filep)
  55. && fwrite(&version, 1, 1, filep)
  56. && fwrite(win, sizeof(WINDOW), 1, filep))
  57. {
  58. int i;
  59. /* write each line */
  60. for (i = 0; i < win->_maxy && win->_y[i]; i++)
  61. if (!fwrite(win->_y[i], win->_maxx * sizeof(chtype), 1, filep))
  62. return ERR;
  63. return OK;
  64. }
  65. return ERR;
  66. }
  67. WINDOW *getwin(FILE *filep)
  68. {
  69. WINDOW *win;
  70. char marker[4];
  71. int i, nlines, ncols;
  72. PDC_LOG(("getwin() - called\n"));
  73. win = malloc(sizeof(WINDOW));
  74. if (!win)
  75. return (WINDOW *)NULL;
  76. /* check for the marker, and load the WINDOW struct */
  77. if (!filep || !fread(marker, 4, 1, filep) || strncmp(marker, "PDC", 3)
  78. || marker[3] != DUMPVER || !fread(win, sizeof(WINDOW), 1, filep))
  79. {
  80. free(win);
  81. return (WINDOW *)NULL;
  82. }
  83. nlines = win->_maxy;
  84. ncols = win->_maxx;
  85. /* allocate the line pointer array */
  86. win->_y = malloc(nlines * sizeof(chtype *));
  87. if (!win->_y)
  88. {
  89. free(win);
  90. return (WINDOW *)NULL;
  91. }
  92. /* allocate the minchng and maxchng arrays */
  93. win->_firstch = malloc(nlines * sizeof(int));
  94. if (!win->_firstch)
  95. {
  96. free(win->_y);
  97. free(win);
  98. return (WINDOW *)NULL;
  99. }
  100. win->_lastch = malloc(nlines * sizeof(int));
  101. if (!win->_lastch)
  102. {
  103. free(win->_firstch);
  104. free(win->_y);
  105. free(win);
  106. return (WINDOW *)NULL;
  107. }
  108. /* allocate the lines */
  109. win = PDC_makelines(win);
  110. if (!win)
  111. return (WINDOW *)NULL;
  112. /* read them */
  113. for (i = 0; i < nlines; i++)
  114. {
  115. if (!fread(win->_y[i], ncols * sizeof(chtype), 1, filep))
  116. {
  117. delwin(win);
  118. return (WINDOW *)NULL;
  119. }
  120. }
  121. touchwin(win);
  122. return win;
  123. }
  124. int scr_dump(const char *filename)
  125. {
  126. FILE *filep;
  127. PDC_LOG(("scr_dump() - called: filename %s\n", filename));
  128. if (filename && (filep = fopen(filename, "wb")) != NULL)
  129. {
  130. int result = putwin(curscr, filep);
  131. fclose(filep);
  132. return result;
  133. }
  134. return ERR;
  135. }
  136. int scr_init(const char *filename)
  137. {
  138. PDC_LOG(("scr_init() - called: filename %s\n", filename));
  139. return OK;
  140. }
  141. int scr_restore(const char *filename)
  142. {
  143. FILE *filep;
  144. PDC_LOG(("scr_restore() - called: filename %s\n", filename));
  145. if (filename && (filep = fopen(filename, "rb")) != NULL)
  146. {
  147. WINDOW *replacement = getwin(filep);
  148. fclose(filep);
  149. if (replacement)
  150. {
  151. int result = overwrite(replacement, curscr);
  152. delwin(replacement);
  153. return result;
  154. }
  155. }
  156. return ERR;
  157. }
  158. int scr_set(const char *filename)
  159. {
  160. PDC_LOG(("scr_set() - called: filename %s\n", filename));
  161. return scr_restore(filename);
  162. }