try.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /****************************************************************************
  2. *
  3. * MODULE: segment
  4. * AUTHOR(S): CERL (original contributors)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Brad Douglas <rez touchofmadness.com>,
  8. * Glynn Clements <glynn gclements.plus.com>
  9. * PURPOSE: Segment test routines
  10. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <grass/segment.h>
  20. #define NCOLS 100
  21. #define NROWS 100
  22. #define SROWS 8
  23. #define SCOLS 8
  24. #define LEN 2
  25. #define NSEGS 4
  26. /**
  27. * \fn int main (int argc, char **argv)
  28. *
  29. * \brief Segment library test program.
  30. *
  31. * \return 0 on success
  32. * \return calls <i>exit()</i> on error
  33. */
  34. int main(int argc, char **argv)
  35. {
  36. SEGMENT seg;
  37. char data[NROWS*NCOLS*LEN];
  38. int row, col;
  39. int i;
  40. int fd;
  41. char junk[3];
  42. fprintf (stdout,"creating seg.file\n");
  43. fd = creat ("seg.file", 0666);
  44. if (fd < 0)
  45. {
  46. perror ("seg.file");
  47. exit(EXIT_FAILURE);
  48. }
  49. segment_format (fd, NROWS,NCOLS,SROWS,SCOLS,LEN);
  50. close (fd);
  51. fprintf (stdout,"opening seg.file\n");
  52. fd = open ("seg.file", 2);
  53. if (fd < 0)
  54. {
  55. perror ("seg.file");
  56. exit(EXIT_FAILURE);
  57. }
  58. segment_init (&seg, fd, NSEGS);
  59. fprintf (stdout,"rows %d, cols %d (len %d)\n", seg.nrows, seg.ncols, seg.len);
  60. if (seg.nrows != NROWS || seg.ncols != NCOLS || seg.len != LEN)
  61. {
  62. fprintf (stdout,"OOPS - wrong segment file\n");
  63. exit(EXIT_FAILURE);
  64. }
  65. fprintf (stdout,"writing seg.file\n");
  66. for (row = 0 ; row < NROWS; row++)
  67. {
  68. for (col = 0; col < NCOLS; col++)
  69. {
  70. data[col*2] = row;
  71. data[col*2 + 1] = col;
  72. }
  73. segment_put_row (&seg, data, row);
  74. }
  75. while(1)
  76. {
  77. for (i = 0; i < seg.nseg; i++)
  78. if (seg.scb[i].n >= 0)
  79. {
  80. fprintf (stdout,"segment %d age %d",
  81. seg.scb[i].n, seg.scb[i].age);
  82. if (i == seg.cur)
  83. fprintf (stdout," current");
  84. fprintf (stdout,"\n");
  85. }
  86. fprintf (stdout,"\nenter row col: ");
  87. if (!fgets(data,20,stdin)) break;
  88. if (sscanf (data, "%1s", junk) != 1) continue;
  89. if (sscanf (data, "%d%d", &row, &col) != 2)
  90. fprintf (stdout,"??\n");
  91. else if(row < 0 || row >= NROWS || col < 0 || col >= NCOLS)
  92. fprintf (stdout,"bad row/col value(s)\n");
  93. else
  94. {
  95. segment_get (&seg, data, row, col);
  96. fprintf (stdout,"data = %d %d\n", data[0], data[1]);
  97. }
  98. }
  99. segment_release (&seg) ;
  100. close (fd);
  101. return 0;
  102. }