format.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * \file format.c
  3. *
  4. * \brief Segment formatting routines.
  5. *
  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. *
  9. * \author GRASS GIS Development Team
  10. *
  11. * \date 2005-2009
  12. */
  13. #include <grass/config.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <grass/segment.h>
  19. static int _segment_format(int, int, int, int, int, int, int);
  20. static int write_int(int, int);
  21. static int zero_fill(int, off_t);
  22. /* fd must be open for write */
  23. /**
  24. * \fn int segment_format (int fd, int nrows, int ncols, int srows, int scols, int len)
  25. *
  26. * \brief Format a segment file.
  27. *
  28. * The segmentation routines require a disk file to be used for paging
  29. * segments in and out of memory. This routine formats the file open for
  30. * write on file descriptor <b>fd</b> for use as a segment file.
  31. *
  32. * A segment file must be formatted before it can be processed by other
  33. * segment routines. The configuration parameters <b>nrows</b>,
  34. * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
  35. * to the beginning of the segment file which is then filled with zeros.
  36. *
  37. * The corresponding nonsegmented data matrix, which is to be
  38. * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
  39. * segment file is to be formed of segments which are <b>srows</b> by
  40. * <b>scols</b>. The data items have length <b>len</b> bytes. For
  41. * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
  42. *
  43. * \param[in] fd file descriptor
  44. * \param[in] nrows number of non-segmented rows
  45. * \param[in] ncols number of non-segmented columns
  46. * \param[in] srows segment rows
  47. * \param[in] scols segment columns
  48. * \param[in] len length of data type
  49. * \return 1 of successful
  50. * \return -1 if unable to seek or write <b>fd</b>
  51. * \return -3 if illegal parameters are passed
  52. */
  53. int segment_format(int fd, int nrows, int ncols, int srows, int scols,
  54. int len)
  55. {
  56. return _segment_format(fd, nrows, ncols, srows, scols, len, 1);
  57. }
  58. /**
  59. * \fn int segment_format_nofill (int fd, int nrows, int ncols, int srows, int scols, int len)
  60. *
  61. * \brief Format a segment file.
  62. *
  63. * The segmentation routines require a disk file to be used for paging
  64. * segments in and out of memory. This routine formats the file open for
  65. * write on file descriptor <b>fd</b> for use as a segment file.
  66. *
  67. * A segment file must be formatted before it can be processed by other
  68. * segment routines. The configuration parameters <b>nrows</b>,
  69. * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
  70. * to the beginning of the segment file which is then filled with zeros.
  71. *
  72. * The corresponding nonsegmented data matrix, which is to be
  73. * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
  74. * segment file is to be formed of segments which are <b>srows</b> by
  75. * <b>scols</b>. The data items have length <b>len</b> bytes. For
  76. * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
  77. *
  78. * <b>Note:</b> This version of the function does <b>not</b> fill in the
  79. * initialized data structures with zeros.
  80. *
  81. * \param[in] fd file descriptor
  82. * \param[in] nrows number of non-segmented rows
  83. * \param[in] ncols number of non-segmented columns
  84. * \param[in] srows segment rows
  85. * \param[in] scols segment columns
  86. * \param[in] len length of data type
  87. * \return 1 of successful
  88. * \return -1 if unable to seek or write <b>fd</b>
  89. * \return -3 if illegal parameters are passed
  90. */
  91. int segment_format_nofill(int fd, int nrows, int ncols, int srows, int scols,
  92. int len)
  93. {
  94. return _segment_format(fd, nrows, ncols, srows, scols, len, 0);
  95. }
  96. static int _segment_format(int fd,
  97. int nrows, int ncols,
  98. int srows, int scols, int len, int fill)
  99. {
  100. off_t nbytes;
  101. int spr, size;
  102. if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
  103. G_warning("segment_format(fd,%d,%d,%d,%d,%d): illegal value(s)",
  104. nrows, ncols, srows, scols, len);
  105. return -3;
  106. }
  107. if (lseek(fd, 0L, SEEK_SET) == (off_t) - 1) {
  108. G_warning("Segment_format: %s", strerror(errno));
  109. return -1;
  110. }
  111. if (!write_int(fd, nrows) || !write_int(fd, ncols)
  112. || !write_int(fd, srows) || !write_int(fd, scols)
  113. || !write_int(fd, len))
  114. return -1;
  115. if (!fill)
  116. return 1;
  117. spr = ncols / scols;
  118. if (ncols % scols)
  119. spr++;
  120. size = srows * scols * len;
  121. /* calculate total number of segments */
  122. nbytes = spr * ((nrows + srows - 1) / srows);
  123. nbytes *= size;
  124. /* fill segment file with zeros */
  125. /* NOTE: this could be done faster using lseek() by seeking
  126. * ahead nbytes and then writing a single byte of 0,
  127. * provided lseek() on all version of UNIX will create a file
  128. * with holes that read as zeros.
  129. */
  130. if (zero_fill(fd, nbytes) < 0)
  131. return -1;
  132. return 1;
  133. }
  134. static int write_int(int fd, int n)
  135. {
  136. int x;
  137. int bytes_wrote;
  138. x = n;
  139. if ((bytes_wrote = write(fd, &x, sizeof(int)) == sizeof(int)) < 0)
  140. G_warning("%s", strerror(errno));
  141. return bytes_wrote;
  142. }
  143. static int zero_fill(int fd, off_t nbytes)
  144. {
  145. #ifndef USE_LSEEK
  146. char buf[10240];
  147. register char *b;
  148. register int n;
  149. /* zero buf */
  150. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  151. b = buf;
  152. while (n-- > 0)
  153. *b++ = 0;
  154. while (nbytes > 0) {
  155. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  156. if (write(fd, buf, n) != n) {
  157. G_warning("%s", strerror(errno));
  158. return -1;
  159. }
  160. nbytes -= n;
  161. }
  162. return 1;
  163. #else
  164. /* Using lseek (faster upon initialization).
  165. NOTE: This version doesn't allocate disk storage for the file; storage will
  166. be allocated dynamically as blocks are actually written. This could
  167. result in zero_fill() succeeding but a subsequent call to write() failing
  168. with ENOSPC ("No space left on device").
  169. */
  170. static const char buf[10];
  171. G_debug(3, "Using new segmentation code...");
  172. if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
  173. G_warning("%s", strerror(errno));
  174. return -1;
  175. }
  176. if (write(fd, buf, 1) != 1) {
  177. G_warning("%s", strerror(errno));
  178. return -1;
  179. }
  180. return 1;
  181. #endif
  182. }