GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
segment/try.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: segment
5  * AUTHOR(S): CERL (original contributors)
6  * Markus Neteler <neteler itc.it>,
7  * Bernhard Reiter <bernhard intevation.de>,
8  * Brad Douglas <rez touchofmadness.com>,
9  * Glynn Clements <glynn gclements.plus.com>
10  * PURPOSE: Segment test routines
11  * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <grass/segment.h>
22 
23 
24 #define NCOLS 100
25 #define NROWS 100
26 #define SROWS 8
27 #define SCOLS 8
28 #define LEN 2
29 #define NSEGS 4
30 
31 
41 int main(int argc, char **argv)
42 {
43  SEGMENT seg;
44  char data[NROWS * NCOLS * LEN];
45  int row, col;
46  int i;
47  int fd;
48  char junk[3];
49 
50  fprintf(stdout, "creating seg.file\n");
51  fd = creat("seg.file", 0666);
52  if (fd < 0) {
53  perror("seg.file");
54  exit(EXIT_FAILURE);
55  }
57  close(fd);
58 
59  fprintf(stdout, "opening seg.file\n");
60  fd = open("seg.file", 2);
61  if (fd < 0) {
62  perror("seg.file");
63  exit(EXIT_FAILURE);
64  }
65  segment_init(&seg, fd, NSEGS);
66 
67  fprintf(stdout, "rows %d, cols %d (len %d)\n", seg.nrows, seg.ncols,
68  seg.len);
69  if (seg.nrows != NROWS || seg.ncols != NCOLS || seg.len != LEN) {
70  fprintf(stdout, "OOPS - wrong segment file\n");
71  exit(EXIT_FAILURE);
72  }
73 
74  fprintf(stdout, "writing seg.file\n");
75  for (row = 0; row < NROWS; row++) {
76  for (col = 0; col < NCOLS; col++) {
77  data[col * 2] = row;
78  data[col * 2 + 1] = col;
79  }
80  segment_put_row(&seg, data, row);
81  }
82 
83  while (1) {
84  for (i = 0; i < seg.nseg; i++)
85  if (seg.scb[i].n >= 0) {
86  fprintf(stdout, "segment %d age %d",
87  seg.scb[i].n, seg.scb[i].age);
88  if (i == seg.cur)
89  fprintf(stdout, " current");
90  fprintf(stdout, "\n");
91  }
92  fprintf(stdout, "\nenter row col: ");
93  if (!fgets(data, 20, stdin))
94  break;
95  if (sscanf(data, "%1s", junk) != 1)
96  continue;
97  if (sscanf(data, "%d%d", &row, &col) != 2)
98  fprintf(stdout, "??\n");
99  else if (row < 0 || row >= NROWS || col < 0 || col >= NCOLS)
100  fprintf(stdout, "bad row/col value(s)\n");
101  else {
102  segment_get(&seg, data, row, col);
103  fprintf(stdout, "data = %d %d\n", data[0], data[1]);
104  }
105  }
106 
107  segment_release(&seg);
108  close(fd);
109 
110  return 0;
111 }
#define NCOLS
Definition: segment/try.c:24
FILE * fd
Definition: g3dcolor.c:368
tuple data
int segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:56
int segment_format(int fd, int nrows, int ncols, int srows, int scols, int len)
Format a segment file.
int main(int argc, char *argv[])
Definition: gem/main.c:302
int segment_put_row(const SEGMENT *SEG, const void *buf, int row)
#define LEN
Definition: segment/try.c:28
int segment_release(SEGMENT *SEG)
Free memory allocated to segment.
#define SCOLS
Definition: segment/try.c:27
#define SROWS
Definition: segment/try.c:26
#define NSEGS
Definition: segment/try.c:29
#define NROWS
Definition: segment/try.c:25
int segment_get(SEGMENT *SEG, void *buf, int row, int col)
Get value from segment file.
Definition: segment/debug.c:48