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-2006
  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,int len)
  54. {
  55. return _segment_format (fd, nrows, ncols, srows, scols, len, 1);
  56. }
  57. /**
  58. * \fn int segment_format_nofill (int fd, int nrows, int ncols, int srows, int scols, int len)
  59. *
  60. * \brief Format a segment file.
  61. *
  62. * The segmentation routines require a disk file to be used for paging
  63. * segments in and out of memory. This routine formats the file open for
  64. * write on file descriptor <b>fd</b> for use as a segment file.
  65. *
  66. * A segment file must be formatted before it can be processed by other
  67. * segment routines. The configuration parameters <b>nrows</b>,
  68. * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
  69. * to the beginning of the segment file which is then filled with zeros.
  70. *
  71. * The corresponding nonsegmented data matrix, which is to be
  72. * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
  73. * segment file is to be formed of segments which are <b>srows</b> by
  74. * <b>scols</b>. The data items have length <b>len</b> bytes. For
  75. * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
  76. *
  77. * <b>Note:</b> This version of the function does <b>not</b> fill in the
  78. * initialized data structures with zeros.
  79. *
  80. * \param[in] fd file descriptor
  81. * \param[in] nrows number of non-segmented rows
  82. * \param[in] ncols number of non-segmented columns
  83. * \param[in] srows segment rows
  84. * \param[in] scols segment columns
  85. * \param[in] len length of data type
  86. * \return 1 of successful
  87. * \return -1 if unable to seek or write <b>fd</b>
  88. * \return -3 if illegal parameters are passed
  89. */
  90. int segment_format_nofill (int fd, int nrows, int ncols, int srows, int scols, int len)
  91. {
  92. return _segment_format (fd, nrows, ncols, srows, scols, len, 0);
  93. }
  94. static int _segment_format(
  95. int fd,
  96. int nrows,int ncols,
  97. int srows,int scols,
  98. 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. {
  104. G_warning ("segment_format(fd,%d,%d,%d,%d,%d): illegal value(s)",
  105. nrows, ncols, srows, scols, len);
  106. return -3;
  107. }
  108. if (lseek (fd, 0L, SEEK_SET) == (off_t)-1)
  109. {
  110. G_warning ("Segment_format: %s",strerror(errno));
  111. return -1;
  112. }
  113. if (!write_int (fd, nrows) || !write_int (fd, ncols)
  114. || !write_int (fd, srows) || !write_int (fd, scols)
  115. || !write_int (fd, len)) return -1;
  116. if (!fill) 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. {
  156. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes ;
  157. if(write (fd, buf, n) != n) {
  158. G_warning("%s",strerror(errno));
  159. return -1;
  160. }
  161. nbytes -= n;
  162. }
  163. return 1;
  164. #else
  165. /* Using lseek (faster upon initialization).
  166. NOTE: This version doesn't allocate disk storage for the file; storage will
  167. be allocated dynamically as blocks are actually written. This could
  168. result in zero_fill() succeeding but a subsequent call to write() failing
  169. with ENOSPC ("No space left on device").
  170. */
  171. static const char buf[10];
  172. G_debug(3,"Using new segmentation code...");
  173. if ( lseek(fd,nbytes-1,SEEK_CUR) < 0 ) {
  174. G_warning("%s",strerror(errno));
  175. return -1;
  176. }
  177. if(write (fd,buf, 1) != 1) {
  178. G_warning("%s",strerror(errno));
  179. return -1;
  180. }
  181. return 1;
  182. #endif
  183. }