hist.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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[2000];
  23. G_debug(3, "Vect_hist_command()");
  24. cmd = G_recreate_command();
  25. Vect_hist_write(Map, "COMMAND: ");
  26. Vect_hist_write(Map, cmd);
  27. Vect_hist_write(Map, "\n");
  28. sprintf(buf, "GISDBASE: %s\n", G_gisdbase()); /* Needed ? */
  29. Vect_hist_write(Map, buf);
  30. sprintf(buf, "LOCATION: %s MAPSET: %s USER: %s DATE: %s\n",
  31. G_location(), G_mapset(), G_whoami(), G_date()); /* Needed ? */
  32. Vect_hist_write(Map, buf);
  33. return 0;
  34. }
  35. /*!
  36. \brief Write string to history file
  37. \param Map pointer to Map_info structure
  38. \param str string to write
  39. \return the number of characters printed
  40. */
  41. int Vect_hist_write(struct Map_info *Map, const char *str)
  42. {
  43. int ret;
  44. G_debug(5, "Vect_hist_write(): %s", str);
  45. ret = 0;
  46. if (Map->hist_fp) {
  47. ret = fprintf(Map->hist_fp, "%s", str);
  48. fflush(Map->hist_fp);
  49. }
  50. return ret;
  51. }
  52. /*!
  53. \brief Reads one line from history file without newline character
  54. \param[out] s buffer, allocated space must be size+1
  55. \param size maximum number of character
  56. \param Map vector map
  57. \return return s on success
  58. \return NULL on error
  59. \return EOF end of file
  60. */
  61. char *Vect_hist_read(char *s, int size, const struct Map_info *Map)
  62. {
  63. int ret;
  64. G_debug(5, "Vect_hist_read()");
  65. if (Map->hist_fp == NULL)
  66. return NULL; /* OK for shapefile etc. */
  67. ret = G_getl2(s, size, Map->hist_fp);
  68. if (ret == 1)
  69. return s;
  70. return NULL;
  71. }
  72. /*!
  73. \brief Rewind history file
  74. \param Map vector map
  75. \return void
  76. */
  77. void Vect_hist_rewind(struct Map_info *Map)
  78. {
  79. G_debug(3, "Vect_hist_rewind()");
  80. if (Map->hist_fp != NULL)
  81. rewind(Map->hist_fp);
  82. }
  83. /*!
  84. \brief Copy history from one map to another
  85. \param In input vector map
  86. \param[out] Out output vector map
  87. \return 0 on success
  88. \return -1 on error
  89. */
  90. int Vect_hist_copy(const struct Map_info *In, struct Map_info *Out)
  91. {
  92. size_t red, ret;
  93. char buf[1000];
  94. G_debug(3, "Vect_hist_copy()");
  95. if (In->hist_fp == NULL)
  96. return 0; /* This is correct (old hist doesn't exist) */
  97. if (Out->hist_fp == NULL)
  98. return -1;
  99. /* skip empty old hist */
  100. G_fseek(In->hist_fp, (long)0, SEEK_END);
  101. if (G_ftell(In->hist_fp) == 0)
  102. return 0;
  103. G_fseek(Out->hist_fp, (long)0, SEEK_END);
  104. rewind(In->hist_fp);
  105. while ((red = fread(buf, sizeof(char), sizeof(char) * 1000, In->hist_fp))) {
  106. if (!(ret = fwrite(buf, sizeof(char), red, Out->hist_fp))) {
  107. return (-1);
  108. }
  109. fflush(Out->hist_fp);
  110. }
  111. /* In ends with \n ? */
  112. G_fseek(In->hist_fp, (long)-1, SEEK_END);
  113. if (fread(buf, sizeof(char), sizeof(char), In->hist_fp) != 1) {
  114. return -1;
  115. }
  116. if (buf[0] != '\n') {
  117. Vect_hist_write(Out, "\n");
  118. }
  119. /* Separator */
  120. Vect_hist_write(Out,
  121. "---------------------------------------------------------------------------------\n");
  122. return (0);
  123. }