hist.c 3.5 KB

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