File: cats.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (472 lines) | stat: -rw-r--r-- 10,890 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
****************************************************************************
*
* MODULE:       Vector library 
*   	    	
* AUTHOR(S):    Original author CERL, probably Dave Gerdes or Mike Higgins.
*               Update to GRASS 5.7 Radim Blazek and David D. Gray.
*
* PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT:    (C) 2001 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 <string.h>
#include "gis.h"
#include "Vect.h"

static int cmp();
struct line_cats *Vect__new_cats_struct (void);

/*!
 \fn struct line_cats *Vect_new_cats_struct ()
 \brief creates and initializes a struct line_cats. This structure is
   used for reading and writing vector cats. The library routines handle
   all memory allocation.
 \return struct line_cats *  or NULL on error
 \param -
*/
struct line_cats *
Vect_new_cats_struct ()
{
  struct line_cats *p;

  if (NULL == (p = Vect__new_cats_struct ()))
    G_fatal_error ("New_line: Out of memory");

  return p;
}

struct line_cats *
Vect__new_cats_struct ()
{
  struct line_cats *p;

  p = (struct line_cats *) malloc (sizeof (struct line_cats));

  /* n_cats MUST be initialized to zero */
  if (p)
    p->n_cats = 0;

  if (p)
    p->alloc_cats = 0;
  
  return p;
}

/*!
 \fn int Vect_destroy_cats_struct (struct line_cats *p)
 \brief frees all memory associated with a struct line_cats, including the struct itself
 \return 0
 \param line_cats structure
*/
int 
Vect_destroy_cats_struct (struct line_cats *p)
{
  if (p)			/* probably a moot test */
    {
      if (p->n_cats)
	{
	  free ((char *) p->field);
	  free ((char *) p->cat);
	}
      free ((char *) p);
    }

  return 0;
}

/*!
 \fn int Vect_cat_set (struct line_cats *Cats, int field, int cat)
 \brief add new field/cat to category structure if doesn't exist yet.
 \return new number of categories
               0 if no space for new category in structure, n_cats would be > GV_NCATS_MAX
              -1 on out of memory
              -2 if field out of range: 1 - GV_FIELD_MAX or cat out of range:  1 - GV_CAT_MAX
 \param Cats
 \param field
 \param cat
*/
int 
Vect_cat_set (struct line_cats *Cats, int field, int cat)
{
  register int n;

  /* check input values */
  /* compiler may warn: 
   * comparison is always 0 due to limited range of data type
   * but remember that limit is set to portable data type length
   * and machine native size may be longer */
  /*
  if (field < 1 || field > GV_FIELD_MAX || cat < 0 || cat > GV_CAT_MAX)
    return (-2);
   */
    
  /* go through old cats and find if field/category exists */
  for (n = 0; n < Cats->n_cats; n++) {
      if (Cats->field[n] == field && Cats->cat[n] == cat )
	  return (1);
  }

  /* field was not found so we shall append new cat */
  /* test if space exist */
  if (n >= GV_NCATS_MAX) {
      G_fatal_error ( "Too many categories (%d), cannot set cat %d (field %d).", Cats->n_cats, cat, field);
  }

  if ( Cats->n_cats == Cats->alloc_cats ) {
      if (0 > dig_alloc_cats (Cats, Cats->n_cats + 100))
	return (-1);
  }

  n = Cats->n_cats;
  Cats->field[n] = field;
  Cats->cat[n] = cat;
  Cats->n_cats++;
  return (1);
}

/*!
 \fn int Vect_cat_get (struct line_cats *Cats, int field, int *cat)
 \brief get first found category of given field
       'cat' is set to first category found or -1 if field was not found
 \return 1 found, 0 field does not exist
 \param Cats
 \param field
 \param cat pointer to variable where cat will be written
*/
int 
Vect_cat_get (struct line_cats *Cats, int field, int *cat)
{
  register int n;

  /* check input value */
  /*
  if (field < 1 || field > GV_FIELD_MAX)
    return (0);
  */

  *cat = -1;
    
  /* go through cats and find if field exist */
  for (n = 0; n < Cats->n_cats; n++)
    {
      if (Cats->field[n] == field)
	{
	  *cat = Cats->cat[n];
	  return (1);
	}
    }

  /* field was not found */
  return (0);
}

/*!
 \fn int Vect_cat_del (struct line_cats *Cats, int field)
 \brief delete all categories of given field
 \return 1 deleted, 0 category number does not exist
 \param Cats
 \param field
*/
int 
Vect_cat_del (struct line_cats *Cats, int field)
{
  int n, m, found = 0;

  /* check input value */
  /*
  if (field < 1 || field > GV_FIELD_MAX)
    return (0);
   */
    
  /* go through cats and find if field exist */
  for (n = 0; n < Cats->n_cats; n++) {
      if (Cats->field[n] == field) {
	  for (m = n; m < Cats->n_cats - 1; m++) {
	      Cats->field[m] = Cats->field[m + 1];
	      Cats->cat[m] = Cats->cat[m + 1];
	  }
	  Cats->n_cats--;
	  found = 1;
	  n--; /* check again this position */
      }
  }

  return (found);
}

/*!
 \fn int Vect_field_cat_del (struct line_cats *Cats, int field, int cat)
 \brief delete field/cat from line_cats structure
 \return 1 deleted, 0 field/category number does not exist
 \param Cats
 \param field
 \param cat category to be deleted or -1 to delete all cats of given field
*/
int 
Vect_field_cat_del (struct line_cats *Cats, int field, int cat)
{
  register int n, m, found = 0;

  /* check input value */
  /*
  if (field < 1 || field > GV_FIELD_MAX)
    return (0);
   */
    
  /* go through cats and find if field exist */
  for (n = 0; n < Cats->n_cats; n++) {
      if (Cats->field[n] == field && ( Cats->cat[n] == cat || cat == -1) ) {
	  for (m = n; m < Cats->n_cats - 1; m++) {
	      Cats->field[m] = Cats->field[m + 1];
	      Cats->cat[m] = Cats->cat[m + 1];
	  }
	  Cats->n_cats--;
	  found = 1;
      }
  }

  return (found);
}

/*!
 \fn int Vect_reset_cats (struct line_cats *Cats)
 \brief reset category structure to make sure cats structure is clean
   to be re-used. I.e. it has no cats associated with it. Cats must have
   previously been created with Vect_new_cats_struct()
 \return 0
 \param line_cats structure
*/
int 
Vect_reset_cats (struct line_cats *Cats)
{
  Cats->n_cats = 0;

  return 0;
}

/*!
 \fn struct cat_list *Vect_new_cat_list ()
 \brief ADD TEXT
 \return ADD
 \param -
*/
struct cat_list *
Vect_new_cat_list ()
{
  struct cat_list *p;

  p = (struct cat_list *) malloc (sizeof (struct cat_list));

  /* n_ranges MUST be initialized to zero */
  if (p) {
    p->n_ranges = 0;
    p->alloc_ranges = 0;
    p->field = 0;
    p->min = NULL;
    p->max = NULL;
  }
  
  return p;
}


/*!
 \fn int Vect_destroy_cat_list (struct cat_list *p)
 \brief ADD TEXT
 \return ADD
 \param line_cats structure
*/
int 
Vect_destroy_cat_list (struct cat_list *p)
{
  if (p)			/* probably a moot test */
    {
      if (p->n_ranges)
	{
	  free ((char *) p->min);
	  free ((char *) p->max);
	}
      free ((char *) p);
    }

  return 0;
}


/*!
 \fn int Vect_str_to_cat_list (char *str, struct cat_list *list)
 \brief Convert string of categories and cat ranges separated by
   commas to cat_list. Examples of string: 2,3,5-9,20. str - input
   string
 \return number of errors in ranges
 \param string, line_cats structure
*/
int 
Vect_str_to_cat_list (char *str, struct cat_list *list)
{
  int i, nr, l, err = 0;
  char *s, *e, buf[100];
  int min, max;
  
  G_debug (3, "Vect_str_to_cat_list(): str = %s", str);
  
  list->n_ranges = 0;
  l = strlen (str); 
  
  /* find number of ranges */
  nr = 1; /* one range */
  for ( i=0; i < l; i++)  
      if (str[i] == ',')
	   nr++;
	  
  /* allocate space */
  if ( list->alloc_ranges == 0 )
    {	    
      list->min = (int *) G_malloc (nr * sizeof(int));
      list->max = (int *) G_malloc (nr * sizeof(int));
    }
  else if (nr > list->alloc_ranges)
    {
      list->min = (int *) G_realloc ((void *)list->min, 
	                                nr * sizeof(int));
      list->max = (int *) G_realloc ((void *)list->max, 
	                                nr * sizeof(int));
    }
    
  /* go through string and read ranges */
  i = 0;  
  s = str;  
  
  while (s)
    {
      e = (char *) strchr (s, ','); /* first comma */
      if( e )
        {
          l = e - s;
          strncpy (buf, s, l);
	  buf[l] = '\0';
	  s = e + 1;
	}
      else
        {
          strcpy (buf, s);
	  s = NULL;
	}
      
      G_debug (3, "  buf = %s", buf);
      if ( sscanf (buf, "%d-%d", &min, &max) == 2 ) {}
      else if ( sscanf (buf, "%d", &min) == 1 )
          max = min;
      else  /* error */ 
        {
	  G_warning ("Cannot convert category string '%s' (from '%s') to category range", buf, str);
	  err++;	
	  continue;
        }
  
      list->min[i] = min;  
      list->max[i] = max;
      i++;
    }  
      
  list->n_ranges = i;
  
  return (err);
}

/*!
 \fn int Vect_array_to_cat_list (int *vals, int nvals, struct cat_list *list)
 \brief Convert ordered array of integers to cat_list
 \return number of ranges
 \param array of integers ?, number of values ?, line_cats structure
*/
int
Vect_array_to_cat_list (int *vals, int nvals, struct cat_list *list)
{
    int i, range;

    G_debug (1, "Vect_array_to_cat_list()");
    range = -1;
    for (i = 0; i < nvals; i++)
      {
	if ( i == 0 || (vals[i] - list->max[range]) > 1 )
	  {
            range++;
            if ( range == list->alloc_ranges)
              {
	        list->alloc_ranges += 1000;	  
                list->min = (int *) G_realloc ((void *)list->min, 
	                                list->alloc_ranges * sizeof(int));
                list->max = (int *) G_realloc ((void *)list->max, 
	                                list->alloc_ranges * sizeof(int));
              }
	    list->min[range] = vals[i];
	    list->max[range] = vals[i];
	  }
	else
	  {
	    list->max[range] = vals[i];
	  }
      }
    
    list->n_ranges = range+1;

    return (list->n_ranges);
}

/*!
 \fn int Vect_cat_in_cat_list (int cat, struct cat_list *list)
 \brief check if category is in list
 \return TRUE  if cat is in list, FALSE if it is not
 \param category number, line_cats structure
*/
int 
Vect_cat_in_cat_list (int cat, struct cat_list *list)
{
  int i;
  
  for ( i=0; i < list->n_ranges; i++)  
      if ( cat >= list->min[i] && cat <= list->max[i] )
	   return (TRUE);
	  
  return (FALSE);
}

/*!
 \fn int Vect_cat_in_array (int cat, int *array, int ncats)
 \brief check if category is in ordered array of integers
 \return TRUE  if cat is in list, FALSE if it is not
 \param category number, ordered array of integers, line_cats structure
*/
int 
Vect_cat_in_array (int cat, int *array, int ncats)
{
  int *i;
  
  i = bsearch ( (void *) &cat, (void *) array, ncats,
		sizeof (int), cmp);

  if ( i != NULL ) return (TRUE);
  
  return (FALSE);
}


int cmp ( const void *pa, const void *pb)
{
    int *p1 = (int *) pa;
    int *p2 = (int *) pb;
	 
    if( *p1 < *p2 )
       return -1;
    if( *p1 > *p2 )
       return 1;
    return 0;
}