hist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. \file lib/vector/Vlib/hist.c
  3. \brief Vector library - history manipulation
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Radim Blazek
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <grass/vector.h>
  14. /*!
  15. \brief Write command info to history file
  16. \param Map pointer to Map_info structure
  17. \return 0 on success
  18. \return -1 error
  19. */
  20. int Vect_hist_command(struct Map_info *Map)
  21. {
  22. char *cmd, buf[GPATH_MAX];
  23. G_debug(3, "Vect_hist_command()");
  24. cmd = G_recreate_command();
  25. if (0 > Vect_hist_write(Map, "COMMAND: "))
  26. return -1;
  27. if (0 > Vect_hist_write(Map, cmd))
  28. return -1;
  29. if (0 > Vect_hist_write(Map, "\n"))
  30. return -1;
  31. sprintf(buf, "GISDBASE: %s\n", G_gisdbase()); /* Needed ? */
  32. if (0 > Vect_hist_write(Map, buf))
  33. return -1;
  34. sprintf(buf, "LOCATION: %s MAPSET: %s USER: %s DATE: %s\n",
  35. G_location(), G_mapset(), G_whoami(), G_date()); /* Needed ? */
  36. if (0 > Vect_hist_write(Map, buf))
  37. return -1;
  38. return 0;
  39. }
  40. /*!
  41. \brief Write string to history file
  42. \param Map pointer to Map_info structure
  43. \param str string to write
  44. \return the number of characters printed
  45. \return -1 on error
  46. */
  47. int Vect_hist_write(struct Map_info *Map, const char *str)
  48. {
  49. int ret;
  50. G_debug(5, "Vect_hist_write(): %s", str);
  51. ret = 0;
  52. if (Map->hist_fp) {
  53. ret = fprintf(Map->hist_fp, "%s", str);
  54. fflush(Map->hist_fp);
  55. }
  56. return ret;
  57. }
  58. /*!
  59. \brief Reads one line from history file without newline character
  60. \param[out] s buffer, allocated space must be size+1
  61. \param size maximum number of character
  62. \param Map vector map
  63. \return return s on success
  64. \return NULL on error
  65. \return EOF end of file
  66. */
  67. char *Vect_hist_read(char *s, int size, const struct Map_info *Map)
  68. {
  69. int ret;
  70. G_debug(5, "Vect_hist_read()");
  71. if (Map->hist_fp == NULL)
  72. return NULL; /* OK for shapefile etc. */
  73. ret = G_getl2(s, size, Map->hist_fp);
  74. if (ret == 1)
  75. return s;
  76. return NULL;
  77. }
  78. /*!
  79. \brief Rewind history file
  80. \param Map vector map
  81. \return void
  82. */
  83. void Vect_hist_rewind(struct Map_info *Map)
  84. {
  85. G_debug(3, "Vect_hist_rewind()");
  86. if (Map->hist_fp != NULL)
  87. rewind(Map->hist_fp);
  88. }
  89. /*!
  90. \brief Copy history from one map to another
  91. \param In input vector map
  92. \param[out] Out output vector map
  93. \return 0 on success
  94. \return -1 on error
  95. */
  96. int Vect_hist_copy(const struct Map_info *In, struct Map_info *Out)
  97. {
  98. size_t red, ret;
  99. char buf[1000];
  100. G_debug(3, "Vect_hist_copy()");
  101. if (In->hist_fp == NULL)
  102. return 0; /* This is correct (old hist doesn't exist) */
  103. if (Out->hist_fp == NULL)
  104. return -1;
  105. /* skip empty old hist */
  106. G_fseek(In->hist_fp, (long)0, SEEK_END);
  107. if (G_ftell(In->hist_fp) == 0)
  108. return 0;
  109. G_fseek(Out->hist_fp, (long)0, SEEK_END);
  110. rewind(In->hist_fp);
  111. while ((red = fread(buf, sizeof(char), sizeof(char) * 1000, In->hist_fp))) {
  112. if (!(ret = fwrite(buf, sizeof(char), red, Out->hist_fp))) {
  113. return (-1);
  114. }
  115. fflush(Out->hist_fp);
  116. }
  117. /* In ends with \n ? */
  118. G_fseek(In->hist_fp, (long)-1, SEEK_END);
  119. if (fread(buf, sizeof(char), sizeof(char), In->hist_fp) != 1) {
  120. return -1;
  121. }
  122. if (buf[0] != '\n') {
  123. Vect_hist_write(Out, "\n");
  124. }
  125. /* Separator */
  126. Vect_hist_write(Out,
  127. "---------------------------------------------------------------------------------\n");
  128. return (0);
  129. }