ami_stream.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //#include <ami_stream.h>
  39. #include <grass/iostream/ami_stream.h>
  40. char *ami_str_error[] = {
  41. "AMI_ERROR_NO_ERROR",
  42. "AMI_ERROR_IO_ERROR",
  43. "AMI_ERROR_END_OF_STREAM",
  44. "AMI_ERROR_OUT_OF_RANGE",
  45. "AMI_ERROR_READ_ONLY",
  46. "AMI_ERROR_OS_ERROR",
  47. "AMI_ERROR_MM_ERROR",
  48. "AMI_ERROR_OBJECT_INITIALIZATION",
  49. "AMI_ERROR_PERMISSION_DENIED",
  50. "AMI_ERROR_INSUFFICIENT_MAIN_MEMORY",
  51. "AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS",
  52. "AMI_ERROR_ENV_UNDEFINED",
  53. "AMI_ERROR_NO_MAIN_MEMORY_OPERATION",
  54. };
  55. /**********************************************************************/
  56. /* creates a random file name, opens the file for reading and writing
  57. and and returns a file descriptor */
  58. int
  59. ami_single_temp_name(const std::string& base, char* tmp_path) {
  60. char *base_dir;
  61. int fd;
  62. // get the dir
  63. base_dir = getenv(STREAM_TMPDIR);
  64. if(!base_dir) {
  65. fprintf(stderr, "ami_stream: %s not set\n", STREAM_TMPDIR);
  66. assert(base_dir);
  67. exit(1);
  68. }
  69. sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base.c_str());
  70. #ifdef __MINGW32__
  71. fd = mktemp(tmp_path) ? open(tmp_path, O_CREAT|O_EXCL|O_RDWR, 0600) : -1;
  72. #else
  73. fd = mkstemp(tmp_path);
  74. #endif
  75. if (fd == -1) {
  76. cerr << "ami_single_temp_name: ";
  77. #ifdef __MINGW32__
  78. perror("mktemp failed: ");
  79. #else
  80. perror("mkstemp failed: ");
  81. #endif
  82. assert(0);
  83. exit(1);
  84. }
  85. return fd;
  86. }
  87. /**********************************************************************/
  88. /* given fd=fide descriptor, associates with it a stream aopened in
  89. access_mode and returns it */
  90. FILE*
  91. open_stream(int fd, AMI_stream_type st) {
  92. FILE* fp = NULL;
  93. assert(fd > -1);
  94. switch (st) {
  95. case AMI_READ_STREAM:
  96. fp = fdopen(fd, "rb");
  97. break;
  98. case AMI_WRITE_STREAM:
  99. fp = fdopen(fd, "wb");
  100. break;
  101. case AMI_APPEND_WRITE_STREAM:
  102. fp = fdopen(fd, "ab");
  103. break;
  104. case AMI_APPEND_STREAM:
  105. fp = fdopen(fd, "ab+");
  106. break;
  107. case AMI_READ_WRITE_STREAM:
  108. fp = fdopen(fd, "rb+");
  109. if (!fp) {
  110. //if file does not exist, create it
  111. fp = fdopen(fd, "wb+");
  112. }
  113. break;
  114. }
  115. if(!fp) {
  116. perror("fdopen");
  117. }
  118. assert(fp);
  119. return fp;
  120. }
  121. /**********************************************************************/
  122. /* open the file whose name is pathname in access mode */
  123. FILE*
  124. open_stream(char* pathname, AMI_stream_type st) {
  125. FILE* fp = NULL;
  126. assert(pathname);
  127. switch (st) {
  128. case AMI_READ_STREAM:
  129. fp = fopen(pathname, "rb");
  130. break;
  131. case AMI_WRITE_STREAM:
  132. fp = fopen(pathname, "wb");
  133. break;
  134. case AMI_APPEND_WRITE_STREAM:
  135. fp = fopen(pathname, "ab");
  136. break;
  137. case AMI_APPEND_STREAM:
  138. fp = fopen(pathname, "ab+");
  139. assert(fp);
  140. G_fseek (fp, 0, SEEK_END);
  141. break;
  142. case AMI_READ_WRITE_STREAM:
  143. fp = fopen(pathname, "rb+");
  144. if (!fp) {
  145. //if file does not exist, create it
  146. fp = fopen(pathname, "wb+");
  147. }
  148. break;
  149. }
  150. if (!fp) {
  151. perror(pathname);
  152. assert(0);
  153. exit(1);
  154. }
  155. assert(fp);
  156. return fp;
  157. }