debug.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * \file debug.c
  3. *
  4. * \brief Segment debug routines.
  5. *
  6. * This file has debug versions of <i>segment_get()</i> and
  7. * <i>segment_put()</i> which check the row,col and print error messages
  8. * to <em>stderr</em> upon violations.
  9. *
  10. * <b>Build Note:</b> Load the debug.o file before the SEGMENTLIB and
  11. * the debug versions will supercede the original.
  12. *
  13. * This program is free software under the GNU General Public License
  14. * (>=v2). Read the file COPYING that comes with GRASS for details.
  15. *
  16. * \author GRASS GIS Development Team
  17. *
  18. * \date 2005-2006
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <grass/segment.h>
  23. static int check(const SEGMENT *, int, int, char *);
  24. /**
  25. * \fn int segment_get (SEGMENT *SEG, void *buf, int row, int col)
  26. *
  27. * \brief Get value from segment file.
  28. *
  29. * Provides random read access to the segmented data. It gets
  30. * <i>len</i> bytes of data into <b>value</b> from the segment file
  31. * <b>seg</b> for the corresponding <b>row</b> and <b>col</b> in the
  32. * original data matrix.
  33. *
  34. * \param[in] seg
  35. * \param[in,out] buf
  36. * \param[in] row
  37. * \param[in] col
  38. * \return 1 on success
  39. * \return -1 if unable to seek or read segment file
  40. */
  41. int segment_get(SEGMENT * SEG, void *buf, int row, int col)
  42. {
  43. int index, n;
  44. if (!check(SEG, row, col, "segment_get"))
  45. return -1;
  46. segment_address(SEG, row, col, &n, &index);
  47. if ((i = segment_pagein(SEG, n)) < 0)
  48. return -1;
  49. memcpy(buf, &SEG->scb[i].buf[index], SEG->len);
  50. return 1;
  51. }
  52. /**
  53. * \fn int segment_put (SEGMENT *SEG, void *buf, int row, int col)
  54. *
  55. * \brief Put value to segment file.
  56. *
  57. * Provides random write access to the segmented data. It
  58. * copies <i>len</i> bytes of data from <b>value</b> into the segment
  59. * structure <b>seg</b> for the corresponding <b>row</b> and <b>col</b> in
  60. * the original data matrix.
  61. *
  62. * The data is not written to disk immediately. It is stored in a memory segment
  63. * until the segment routines decide to page the segment to disk.
  64. *
  65. * \param[in] seg
  66. * \param[in,out] buf
  67. * \param[in] row
  68. * \param[in] col
  69. * \return 1 on success
  70. * \return -1 if unable to seek or write segment file
  71. */
  72. int segment_put(SEGMENT * SEG, const void *buf, int row, int col)
  73. {
  74. int index, n;
  75. if (!check(SEG, row, col, "segment_put"))
  76. return -1;
  77. segment_address(SEG, row, col, &n, &index);
  78. if ((i = segment_pagein(SEG, n)) < 0)
  79. return -1;
  80. SEG->scb[i].dirty = 1;
  81. memcpy(&SEG->scb[i].buf[index], buf, SEG->len);
  82. return 1;
  83. }
  84. static int check(const SEGMENT * SEG, int row, int col, char *me)
  85. {
  86. int r = row >= 0 && row < SEG->nrows;
  87. int c = col >= 0 && col < SEG->ncols;
  88. if (r && c)
  89. return 1;
  90. fprintf(stderr, "%s(SEG=%lx,fd=%d,row=%d,col=%d) ",
  91. me, (unsigned long int)SEG, SEG->fd, row, col);
  92. if (!r) {
  93. fprintf(stderr, "bad row ");
  94. if (row >= SEG->nrows)
  95. fprintf(stderr, "(max %d) ", SEG->nrows - 1);
  96. }
  97. if (!c) {
  98. fprintf(stderr, "bad col ");
  99. if (col >= SEG->ncols)
  100. fprintf(stderr, "(max %d) ", SEG->ncols - 1);
  101. }
  102. fprintf(stderr, "\n");
  103. return 0;
  104. }