hist.c 3.3 KB

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