ami_stream.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. *
  3. * MODULE: iostream
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. *
  8. * Iostream is a library that implements streams, external memory
  9. * sorting on streams, and an external memory priority queue on
  10. * streams. These are the fundamental components used in external
  11. * memory algorithms.
  12. * Credits: The library was developed by Laura Toma. The kernel of
  13. * class STREAM is based on the similar class existent in the GPL TPIE
  14. * project developed at Duke University. The sorting and priority
  15. * queue have been developed by Laura Toma based on communications
  16. * with Rajiv Wickremesinghe. The library was developed as part of
  17. * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
  18. * Rajiv Wickremesinghe as part of the Terracost project.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * General Public License for more details. *
  29. * **************************************************************************/
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <assert.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. extern "C" {
  39. #include <grass/gis.h>
  40. }
  41. //#include <ami_stream.h>
  42. #include <grass/iostream/ami_stream.h>
  43. const char *ami_str_error[] = {
  44. "AMI_ERROR_NO_ERROR",
  45. "AMI_ERROR_IO_ERROR",
  46. "AMI_ERROR_END_OF_STREAM",
  47. "AMI_ERROR_OUT_OF_RANGE",
  48. "AMI_ERROR_READ_ONLY",
  49. "AMI_ERROR_OS_ERROR",
  50. "AMI_ERROR_MM_ERROR",
  51. "AMI_ERROR_OBJECT_INITIALIZATION",
  52. "AMI_ERROR_PERMISSION_DENIED",
  53. "AMI_ERROR_INSUFFICIENT_MAIN_MEMORY",
  54. "AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS",
  55. "AMI_ERROR_ENV_UNDEFINED",
  56. "AMI_ERROR_NO_MAIN_MEMORY_OPERATION",
  57. };
  58. /**********************************************************************/
  59. /* creates a random file name, opens the file for reading and writing
  60. and and returns a file descriptor */
  61. int
  62. ami_single_temp_name(const std::string& base, char* tmp_path) {
  63. char *base_dir;
  64. int fd;
  65. // get the dir
  66. base_dir = getenv(STREAM_TMPDIR);
  67. if(!base_dir) {
  68. fprintf(stderr, "ami_stream: %s not set\n", STREAM_TMPDIR);
  69. assert(base_dir);
  70. exit(1);
  71. }
  72. sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base.c_str());
  73. fd = G_mkstemp(tmp_path, O_RDWR, 0600);
  74. if (fd == -1) {
  75. cerr << "ami_single_temp_name: ";
  76. perror("G_mkstemp() failed: ");
  77. assert(0);
  78. exit(1);
  79. }
  80. return fd;
  81. }
  82. /**********************************************************************/
  83. /* given fd=fide descriptor, associates with it a stream aopened in
  84. access_mode and returns it */
  85. FILE*
  86. open_stream(int fd, AMI_stream_type st) {
  87. FILE* fp = NULL;
  88. assert(fd > -1);
  89. switch (st) {
  90. case AMI_READ_STREAM:
  91. fp = fdopen(fd, "rb");
  92. break;
  93. case AMI_WRITE_STREAM:
  94. fp = fdopen(fd, "wb");
  95. break;
  96. case AMI_APPEND_WRITE_STREAM:
  97. fp = fdopen(fd, "ab");
  98. break;
  99. case AMI_APPEND_STREAM:
  100. fp = fdopen(fd, "ab+");
  101. break;
  102. case AMI_READ_WRITE_STREAM:
  103. fp = fdopen(fd, "rb+");
  104. if (!fp) {
  105. //if file does not exist, create it
  106. fp = fdopen(fd, "wb+");
  107. }
  108. break;
  109. }
  110. if(!fp) {
  111. perror("fdopen");
  112. }
  113. assert(fp);
  114. return fp;
  115. }
  116. /**********************************************************************/
  117. /* open the file whose name is pathname in access mode */
  118. FILE*
  119. open_stream(char* pathname, AMI_stream_type st) {
  120. FILE* fp = NULL;
  121. assert(pathname);
  122. switch (st) {
  123. case AMI_READ_STREAM:
  124. fp = fopen(pathname, "rb");
  125. break;
  126. case AMI_WRITE_STREAM:
  127. fp = fopen(pathname, "wb");
  128. break;
  129. case AMI_APPEND_WRITE_STREAM:
  130. fp = fopen(pathname, "ab");
  131. break;
  132. case AMI_APPEND_STREAM:
  133. fp = fopen(pathname, "ab+");
  134. assert(fp);
  135. G_fseek (fp, 0, SEEK_END);
  136. break;
  137. case AMI_READ_WRITE_STREAM:
  138. fp = fopen(pathname, "rb+");
  139. if (!fp) {
  140. //if file does not exist, create it
  141. fp = fopen(pathname, "wb+");
  142. }
  143. break;
  144. }
  145. if (!fp) {
  146. perror(pathname);
  147. assert(0);
  148. exit(1);
  149. }
  150. assert(fp);
  151. return fp;
  152. }