GRASS Programmer's Manual  6.4.2(2012)
segment/try.c
Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003  *
00004  * MODULE:       segment
00005  * AUTHOR(S):    CERL (original contributors)
00006  *               Markus Neteler <neteler itc.it>, 
00007  *               Bernhard Reiter <bernhard intevation.de>, 
00008  *               Brad Douglas <rez touchofmadness.com>, 
00009  *               Glynn Clements <glynn gclements.plus.com>
00010  * PURPOSE:      Segment test routines
00011  * COPYRIGHT:    (C) 1999-2006 by the GRASS Development Team
00012  *
00013  *               This program is free software under the GNU General Public
00014  *               License (>=v2). Read the file COPYING that comes with GRASS
00015  *               for details.
00016  *
00017  *****************************************************************************/
00018 
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <grass/segment.h>
00022 
00023 
00024 #define NCOLS 100
00025 #define NROWS 100
00026 #define SROWS 8
00027 #define SCOLS 8
00028 #define LEN   2
00029 #define NSEGS 4
00030 
00031 
00041 int main(int argc, char **argv)
00042 {
00043     SEGMENT seg;
00044     char data[NROWS * NCOLS * LEN];
00045     int row, col;
00046     int i;
00047     int fd;
00048     char junk[3];
00049 
00050     fprintf(stdout, "creating seg.file\n");
00051     fd = creat("seg.file", 0666);
00052     if (fd < 0) {
00053         perror("seg.file");
00054         exit(EXIT_FAILURE);
00055     }
00056     segment_format(fd, NROWS, NCOLS, SROWS, SCOLS, LEN);
00057     close(fd);
00058 
00059     fprintf(stdout, "opening seg.file\n");
00060     fd = open("seg.file", 2);
00061     if (fd < 0) {
00062         perror("seg.file");
00063         exit(EXIT_FAILURE);
00064     }
00065     segment_init(&seg, fd, NSEGS);
00066 
00067     fprintf(stdout, "rows %d, cols %d (len %d)\n", seg.nrows, seg.ncols,
00068             seg.len);
00069     if (seg.nrows != NROWS || seg.ncols != NCOLS || seg.len != LEN) {
00070         fprintf(stdout, "OOPS - wrong segment file\n");
00071         exit(EXIT_FAILURE);
00072     }
00073 
00074     fprintf(stdout, "writing seg.file\n");
00075     for (row = 0; row < NROWS; row++) {
00076         for (col = 0; col < NCOLS; col++) {
00077             data[col * 2] = row;
00078             data[col * 2 + 1] = col;
00079         }
00080         segment_put_row(&seg, data, row);
00081     }
00082 
00083     while (1) {
00084         for (i = 0; i < seg.nseg; i++)
00085             if (seg.scb[i].n >= 0) {
00086                 fprintf(stdout, "segment %d age %d",
00087                         seg.scb[i].n, seg.scb[i].age);
00088                 if (i == seg.cur)
00089                     fprintf(stdout, " current");
00090                 fprintf(stdout, "\n");
00091             }
00092         fprintf(stdout, "\nenter row col: ");
00093         if (!fgets(data, 20, stdin))
00094             break;
00095         if (sscanf(data, "%1s", junk) != 1)
00096             continue;
00097         if (sscanf(data, "%d%d", &row, &col) != 2)
00098             fprintf(stdout, "??\n");
00099         else if (row < 0 || row >= NROWS || col < 0 || col >= NCOLS)
00100             fprintf(stdout, "bad row/col value(s)\n");
00101         else {
00102             segment_get(&seg, data, row, col);
00103             fprintf(stdout, "data = %d %d\n", data[0], data[1]);
00104         }
00105     }
00106 
00107     segment_release(&seg);
00108     close(fd);
00109 
00110     return 0;
00111 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines