hist.c 3.4 KB

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