1 #include <stdio.h>
2 #include <string.h>
3
4 #ifdef __MINGW32__
5 #include <process.h>
6 #else
7 #include <sys/wait.h>
8 #endif
9
10 #include <grass/gis.h>
11 #include <grass/raster.h>
12 #include <grass/dbmi.h>
13 #include <grass/vector.h>
14 #include <grass/glocale.h>
15 #include "global.h"
16
17 /* function prototypes */
18 static int blank_line(void *buf);
19
20
21 /* move - move to next point in line */
22
move(struct COOR * point)23 struct COOR *move(struct COOR *point)
24 {
25 if (direction == FORWARD) {
26 if (point->fptr == NULL) /* at open end of line */
27 return (NULL);
28 if (point->fptr->fptr == point) /* direction change coming up */
29 direction = BACKWARD;
30 return (point->fptr);
31 }
32 else {
33 if (point->bptr == NULL)
34 return (NULL);
35 if (point->bptr->bptr == point)
36 direction = FORWARD;
37 return (point->bptr);
38 }
39 }
40
41 /* find_end - search for end of line, starting at a given point and */
42 /* moving in a given direction */
43
find_end(struct COOR * seed,int dir,int * result,int * n)44 struct COOR *find_end(struct COOR *seed, int dir, int *result, int *n)
45 {
46 struct COOR *start;
47
48 start = seed;
49 direction = dir;
50 *result = *n = 0;
51 while (!*result) {
52 seed = move(seed);
53 (*n)++;
54 if (seed == start)
55 *result = LOOP;
56 else {
57 if (seed == NULL)
58 *result = OPEN;
59 else {
60 if (at_end(seed))
61 *result = END;
62 }
63 }
64 }
65 return (seed);
66 }
67
68 /* at_end - test whether a point is at the end of a line; if so, give */
69 /* the direction in which to move to go away from that end */
70
at_end(struct COOR * ptr)71 int at_end(struct COOR *ptr)
72 {
73 if (ptr->fptr == ptr)
74 return (BACKWARD);
75 if (ptr->bptr == ptr)
76 return (FORWARD);
77 return (0);
78 }
79
read_row(void * buf)80 int read_row(void *buf)
81 {
82 void *p;
83
84 if (last_read)
85 return (0);
86 if (first_read) {
87 blank_line(buf);
88 first_read = 0;
89 }
90 else {
91 if (row_count >= n_rows) {
92 last_read = 1;
93 blank_line(buf);
94 }
95 else {
96 /* The buf variable is a void pointer and thus */
97 /* points to anything. Therefore, it's size is */
98 /* unknown and thus, it cannot be used for pointer */
99 /* arithmetic (some compilers treat this as an error */
100 /* - SGI MIPSPro compiler for one). Make the */
101 /* assumption that data_size is the proper number of */
102 /* bytes and cast the buf variable to char * before */
103 /* incrementing */
104 p = ((char *)buf) + data_size;
105 Rast_get_row(input_fd, p, row_count++, data_type);
106 p = buf;
107 Rast_set_null_value(p, 1, data_type);
108
109 /* Again we need to cast p to char * under the */
110 /* assumption that the increment is the proper */
111 /* number of bytes. */
112 p = ((char *)p) + (row_length + 1) * data_size;
113 Rast_set_null_value(p, 1, data_type);
114 }
115 }
116 return (row_length + 2);
117 }
118
blank_line(void * buf)119 static int blank_line(void *buf)
120 {
121 Rast_set_null_value(buf, row_length + 2, data_type);
122
123 return 0;
124 }
125
126 /* insert values into the table (cat, val | dval) */
insert_value(int cat,int val,double dval)127 void insert_value(int cat, int val, double dval)
128 {
129 char buf[1000];
130
131 sprintf(buf, "insert into %s values (%d", Fi->table, cat);
132 db_set_string(&sql, buf);
133
134 if (data_type == CELL_TYPE)
135 sprintf(buf, ", %d", val);
136 else
137 sprintf(buf, ", %f", dval);
138
139 db_append_string(&sql, buf);
140
141 if (has_cats) {
142 char *lab;
143
144 lab = Rast_get_c_cat(&val, &RastCats); /*cats are loaded only for CELL type */
145
146 db_set_string(&label, lab);
147 db_double_quote_string(&label);
148 sprintf(buf, ", '%s'", db_get_string(&label));
149 db_append_string(&sql, buf);
150 }
151
152 db_append_string(&sql, ")");
153
154 G_debug(3, "%s", db_get_string(&sql));
155
156 if (db_execute_immediate(driver, &sql) != DB_OK)
157 G_fatal_error(_("Cannot insert new row: %s"), db_get_string(&sql));
158
159 }
160
free_ptr(struct COOR * ptr)161 int free_ptr(struct COOR *ptr)
162 {
163 G_free(ptr);
164
165 return (--n_alloced_ptrs);
166 }
167