File: try.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (111 lines) | stat: -rw-r--r-- 2,804 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

/****************************************************************************
 *
 * MODULE:       segment
 * AUTHOR(S):    CERL (original contributors)
 *               Markus Neteler <neteler itc.it>, 
 *               Bernhard Reiter <bernhard intevation.de>, 
 *               Brad Douglas <rez touchofmadness.com>, 
 *               Glynn Clements <glynn gclements.plus.com>
 * PURPOSE:      Segment test routines
 * COPYRIGHT:    (C) 1999-2006 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the file COPYING that comes with GRASS
 *               for details.
 *
 *****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <grass/segment.h>


#define NCOLS 100
#define NROWS 100
#define SROWS 8
#define SCOLS 8
#define LEN   2
#define NSEGS 4


/**
 * \fn int main (int argc, char **argv)
 *
 * \brief Segment library test program.
 *
 * \return 0 on success
 * \return calls <i>exit()</i> on error
 */

int main(int argc, char **argv)
{
    SEGMENT seg;
    char data[NROWS * NCOLS * LEN];
    int row, col;
    int i;
    int fd;
    char junk[3];

    fprintf(stdout, "creating seg.file\n");
    fd = creat("seg.file", 0666);
    if (fd < 0) {
	perror("seg.file");
	exit(EXIT_FAILURE);
    }
    segment_format(fd, NROWS, NCOLS, SROWS, SCOLS, LEN);
    close(fd);

    fprintf(stdout, "opening seg.file\n");
    fd = open("seg.file", 2);
    if (fd < 0) {
	perror("seg.file");
	exit(EXIT_FAILURE);
    }
    segment_init(&seg, fd, NSEGS);

    fprintf(stdout, "rows %d, cols %d (len %d)\n", seg.nrows, seg.ncols,
	    seg.len);
    if (seg.nrows != NROWS || seg.ncols != NCOLS || seg.len != LEN) {
	fprintf(stdout, "OOPS - wrong segment file\n");
	exit(EXIT_FAILURE);
    }

    fprintf(stdout, "writing seg.file\n");
    for (row = 0; row < NROWS; row++) {
	for (col = 0; col < NCOLS; col++) {
	    data[col * 2] = row;
	    data[col * 2 + 1] = col;
	}
	segment_put_row(&seg, data, row);
    }

    while (1) {
	for (i = 0; i < seg.nseg; i++)
	    if (seg.scb[i].n >= 0) {
		fprintf(stdout, "segment %d age %d",
			seg.scb[i].n, seg.scb[i].age);
		if (i == seg.cur)
		    fprintf(stdout, " current");
		fprintf(stdout, "\n");
	    }
	fprintf(stdout, "\nenter row col: ");
	if (!fgets(data, 20, stdin))
	    break;
	if (sscanf(data, "%1s", junk) != 1)
	    continue;
	if (sscanf(data, "%d%d", &row, &col) != 2)
	    fprintf(stdout, "??\n");
	else if (row < 0 || row >= NROWS || col < 0 || col >= NCOLS)
	    fprintf(stdout, "bad row/col value(s)\n");
	else {
	    segment_get(&seg, data, row, col);
	    fprintf(stdout, "data = %d %d\n", data[0], data[1]);
	}
    }

    segment_release(&seg);
    close(fd);

    return 0;
}