history.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*!
  2. * \file gis/history.c
  3. *
  4. * \brief GIS Library - History management
  5. *
  6. * (C) 2001-2009 GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <string.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. static void print_history_error(const char *, const char *, FILE *);
  17. /*!
  18. * \brief Read raster history file
  19. *
  20. * This routine reads the history file for the raster map <i>name</i>
  21. * in <i>mapset</i> into the <i>hist</i> structure.
  22. *
  23. * A diagnostic message is printed and -1 is returned if there is an
  24. * error reading the history file. Otherwise, 0 is returned.
  25. *
  26. * \param name map name
  27. * \param mapset mapset name
  28. * \param hist pointer to History structure which holds history info
  29. *
  30. * \return -1 on error
  31. * \return 0 on success
  32. */
  33. int G_read_history(const char *name, const char *mapset, struct History *hist)
  34. {
  35. FILE *fd;
  36. G_zero(hist, sizeof(struct History));
  37. fd = G_fopen_old("hist", name, mapset);
  38. if (!fd) {
  39. print_history_error(name, mapset, fd);
  40. return -1;
  41. }
  42. if (!G_getl(hist->mapid, sizeof(hist->mapid), fd)) {
  43. print_history_error(name, mapset, fd);
  44. return -1;
  45. }
  46. G_ascii_check(hist->mapid);
  47. if (!G_getl(hist->title, sizeof(hist->title), fd)) {
  48. print_history_error(name, mapset, fd);
  49. return -1;
  50. }
  51. G_ascii_check(hist->title);
  52. if (!G_getl(hist->mapset, sizeof(hist->mapset), fd)) {
  53. print_history_error(name, mapset, fd);
  54. return -1;
  55. }
  56. G_ascii_check(hist->mapset);
  57. if (!G_getl(hist->creator, sizeof(hist->creator), fd)) {
  58. print_history_error(name, mapset, fd);
  59. return -1;
  60. }
  61. G_ascii_check(hist->creator);
  62. if (!G_getl(hist->maptype, sizeof(hist->maptype), fd)) {
  63. print_history_error(name, mapset, fd);
  64. return -1;
  65. }
  66. G_ascii_check(hist->maptype);
  67. if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd)) {
  68. print_history_error(name, mapset, fd);
  69. return -1;
  70. }
  71. G_ascii_check(hist->datsrc_1);
  72. if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd)) {
  73. print_history_error(name, mapset, fd);
  74. return -1;
  75. }
  76. G_ascii_check(hist->datsrc_2);
  77. if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd)) {
  78. print_history_error(name, mapset, fd);
  79. return -1;
  80. }
  81. G_ascii_check(hist->keywrd);
  82. hist->edlinecnt = 0;
  83. while ((hist->edlinecnt < MAXEDLINES) &&
  84. (G_getl
  85. (hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) {
  86. G_ascii_check(hist->edhist[hist->edlinecnt]);
  87. hist->edlinecnt++;
  88. }
  89. fclose(fd);
  90. return 0;
  91. }
  92. void print_history_error(const char *name, const char *mapset, FILE *fd)
  93. {
  94. if (fd != NULL)
  95. fclose(fd);
  96. if (mapset) {
  97. G_warning(_("Unable to get history information for <%s@%s>"),
  98. name, mapset);
  99. }
  100. else { /* write */
  101. G_warning(_("Unable to write history information for <%s>"), name);
  102. }
  103. }
  104. /*!
  105. * \brief Write raster history file
  106. *
  107. * This routine writes the history file for the raster map
  108. * <i>name</i> in the current mapset from the <i>hist</i> structure.
  109. *
  110. * A diagnostic message is printed and -1 is returned if there is an
  111. * error writing the history file. Otherwise, 0 is returned.
  112. *
  113. * <b>Note:</b> The <i>hist</i> structure should first be initialized
  114. * using G_short_history().
  115. *
  116. * \param name map name
  117. * \param[out] hist pointer to History structure which holds history info
  118. *
  119. * \return -1 on error
  120. * \return 0 on success
  121. */
  122. int G_write_history(const char *name, struct History *hist)
  123. {
  124. FILE *fd;
  125. int i;
  126. fd = G_fopen_new("hist", name);
  127. if (!fd) {
  128. print_history_error(name, NULL, fd);
  129. return -1;
  130. }
  131. fprintf(fd, "%s\n", hist->mapid);
  132. fprintf(fd, "%s\n", hist->title);
  133. fprintf(fd, "%s\n", hist->mapset);
  134. fprintf(fd, "%s\n", hist->creator);
  135. fprintf(fd, "%s\n", hist->maptype);
  136. fprintf(fd, "%s\n", hist->datsrc_1);
  137. fprintf(fd, "%s\n", hist->datsrc_2);
  138. fprintf(fd, "%s\n", hist->keywrd);
  139. for (i = 0; i < hist->edlinecnt; i++)
  140. fprintf(fd, "%s\n", hist->edhist[i]);
  141. fclose(fd);
  142. return 0;
  143. }
  144. /*!
  145. * \brief Initialize history structure
  146. *
  147. * This routine initializes the <i>hist</i> structure, recording the
  148. * date, user, module name and the raster map <i>name</i>
  149. * structure. The <i>type</i> is an anachronism from earlier versions
  150. * of GRASS and should be specified as "raster".
  151. *
  152. * <b>Note:</b> This routine only initializes the data structure. It
  153. * does not write the history file.
  154. *
  155. * \param name map name
  156. * \param type map type
  157. * \param hist pointer to History structure which holds history info
  158. */
  159. void G_short_history(const char *name, const char *type, struct History *hist)
  160. {
  161. strncpy(hist->mapid, G_date(), RECORD_LEN);
  162. strncpy(hist->title, name, RECORD_LEN);
  163. strncpy(hist->mapset, G_mapset(), RECORD_LEN);
  164. strncpy(hist->creator, G_whoami(), RECORD_LEN);
  165. strncpy(hist->maptype, type, RECORD_LEN);
  166. sprintf(hist->keywrd, _("generated by %s"), G_program_name());
  167. strcpy(hist->datsrc_1, "");
  168. strcpy(hist->datsrc_2, "");
  169. hist->edlinecnt = 0;
  170. }
  171. /*!
  172. * \brief Save command line to raster history structure
  173. *
  174. * This routine takes an existing (run G_short_history first() history
  175. * structure and adds the command line to the end of the comments
  176. * array, as cleaned & expanded by the parser.
  177. *
  178. * History file is limited to [80]x[50], as defined in include/gis.h
  179. *
  180. * - First version had for loops of [i][j] character assignments and ending
  181. * nulls, but using the string libraries is cleaner and less bug prone.
  182. * - Second version had white space detection, intelligent wrapping, and
  183. * indentation of continued lines, but this proved a pain in the neck for
  184. * things like r.patch which can have long strings without any
  185. * parser-acceptable breaks.
  186. * - This is MK-III, simplified, but that's good: it's cut & paste-able.
  187. *
  188. * Note: use G_write_history() to write the structure.
  189. *
  190. * Sample Usage:
  191. * \code
  192. * struct History history;
  193. * G_short_history(rasterfile, "raster", &history);
  194. * G_command_history(&history);
  195. * G_write_history(rasterfile, &history);
  196. * \endcode
  197. *
  198. * \param hist pointer to History structure which holds history info
  199. *
  200. * \return 0 on success
  201. * \return 1 on failure (history file full, no change)
  202. * \return 2 on failure (history file full, added as much as we could)
  203. */
  204. int G_command_history(struct History *hist)
  205. {
  206. int j, cmdlen;
  207. char *cmdlin;
  208. cmdlin = G_recreate_command();
  209. cmdlen = strlen(cmdlin);
  210. if (hist->edlinecnt > MAXEDLINES - 2) {
  211. G_warning(_("Not enough room in history file to record command line"));
  212. return 1;
  213. }
  214. if (hist->edlinecnt > 0) { /* add a blank line if preceding history exists */
  215. strcpy(hist->edhist[hist->edlinecnt], "");
  216. hist->edlinecnt++;
  217. }
  218. if (cmdlen < 70) { /* ie if it will fit on a single line */
  219. sprintf(hist->edhist[hist->edlinecnt], G_recreate_command());
  220. hist->edlinecnt++;
  221. }
  222. else { /* multi-line required */
  223. j = 0; /* j is the current position in the command line string */
  224. while ((cmdlen - j) > 70) {
  225. strncpy(hist->edhist[hist->edlinecnt], &cmdlin[j], 68);
  226. hist->edhist[hist->edlinecnt][68] = '\0';
  227. strcat(hist->edhist[hist->edlinecnt], "\\");
  228. j += 68;
  229. hist->edlinecnt++;
  230. if (hist->edlinecnt > MAXEDLINES - 2) {
  231. G_warning(_("Not enough room in history file for command line (truncated)"));
  232. return 2;
  233. }
  234. }
  235. if ((cmdlen - j) > 0) { /* ie anything left */
  236. strcpy(hist->edhist[hist->edlinecnt], &cmdlin[j]);
  237. hist->edlinecnt++;
  238. }
  239. }
  240. return 0;
  241. }