session.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include "local_proto.h"
  4. /* TODO: when done,
  5. * remove session file or save it to a user-specified location */
  6. static char cur[2000];
  7. static char *sessionfile = NULL;
  8. static FILE *fd;
  9. int add_to_session(int indent, char *buf)
  10. {
  11. accept();
  12. sprintf(cur, "%s%s", indent ? " " : "", buf);
  13. return 0;
  14. }
  15. int accept(void)
  16. {
  17. if (sessionfile == NULL) {
  18. *cur = 0;
  19. sessionfile = G_tempfile();
  20. fd = fopen(sessionfile, "w");
  21. if (fd == NULL) {
  22. error("session file", "", "can't open");
  23. return 1;
  24. }
  25. }
  26. if (fd != NULL && *cur) {
  27. fprintf(fd, "%s\n", cur);
  28. fflush(fd);
  29. *cur = 0;
  30. }
  31. return 0;
  32. }
  33. int reject(void)
  34. {
  35. *cur = 0;
  36. return 0;
  37. }
  38. int print_session(FILE * out)
  39. {
  40. FILE *in;
  41. char buf[1024];
  42. if (sessionfile == NULL)
  43. return 1;
  44. if (fd != NULL)
  45. fflush(fd);
  46. if ((in = fopen(sessionfile, "r")) == NULL) {
  47. error("session file", "", "can't open");
  48. return 1;
  49. }
  50. while (fgets(buf, sizeof buf, in))
  51. fprintf(out, "%s", buf);
  52. fclose(in);
  53. return 0;
  54. }