23 #include <grass/gis.h>
24 #include <grass/Vect.h>
25 #include <grass/glocale.h>
27 static int cmp(
const void *pa,
const void *pb);
64 p = (
struct line_cats *)G_malloc(
sizeof(
struct line_cats));
123 for (n = 0; n < Cats->n_cats; n++) {
124 if (Cats->field[n] == field && Cats->cat[n] == cat)
130 if (n >= GV_NCATS_MAX) {
131 G_fatal_error(_(
"Too many categories (%d), unable to set cat %d (layer %d)"),
132 Cats->n_cats, cat, field);
135 if (Cats->n_cats == Cats->alloc_cats) {
141 Cats->field[
n] = field;
172 for (n = 0; n < Cats->n_cats; n++) {
173 if (Cats->field[n] == field) {
201 if (field < 1 || field > GV_FIELD_MAX)
205 for (n = 0; n < Cats->n_cats; n++) {
206 if (Cats->field[n] != field)
211 return cats->n_values;
234 for (n = 0; n < Cats->n_cats; n++) {
235 if (Cats->field[n] == field) {
236 for (m = n; m < Cats->n_cats - 1; m++) {
237 Cats->field[m] = Cats->field[m + 1];
238 Cats->cat[m] = Cats->cat[m + 1];
261 register int n, m, found = 0;
270 for (n = 0; n < Cats->n_cats; n++) {
271 if (Cats->field[n] == field && (Cats->cat[n] == cat || cat == -1)) {
272 for (m = n; m < Cats->n_cats - 1; m++) {
273 Cats->field[m] = Cats->field[m + 1];
274 Cats->cat[m] = Cats->cat[m + 1];
312 p = (
struct cat_list *)G_malloc(
sizeof(
struct cat_list));
360 int i, nr,
l,
err = 0;
365 G_debug(3,
"Vect_str_to_cat_list(): str = %s", str);
372 for (i = 0; i <
l; i++)
377 if (list->alloc_ranges == 0) {
378 list->min = (
int *)G_malloc(nr *
sizeof(
int));
379 list->max = (
int *)G_malloc(nr *
sizeof(
int));
381 else if (nr > list->alloc_ranges) {
382 list->min = (
int *)G_realloc((
void *)list->min, nr *
sizeof(
int));
383 list->max = (
int *)G_realloc((
void *)list->max, nr *
sizeof(
int));
391 e = (
char *)strchr(s,
',');
404 if (sscanf(buf,
"%d-%d", &min, &max) == 2) {
406 else if (sscanf(buf,
"%d", &min) == 1)
410 G_warning(_(
"Unable to convert category string '%s' (from '%s') to category range"),
439 G_debug(1,
"Vect_array_to_cat_list()");
441 for (i = 0; i < nvals; i++) {
442 if (i == 0 || (vals[i] - list->max[range]) > 1) {
444 if (range == list->alloc_ranges) {
445 list->alloc_ranges += 1000;
446 list->min = (
int *)G_realloc((
void *)list->min,
450 (
int *)G_realloc((
void *)list->max,
451 list->alloc_ranges *
sizeof(
int));
453 list->min[
range] = vals[i];
454 list->max[
range] = vals[i];
457 list->max[
range] = vals[i];
461 list->n_ranges = range + 1;
463 return (list->n_ranges);
479 for (i = 0; i < list->n_ranges; i++)
480 if (cat >= list->min[i] && cat <= list->
max[i])
500 i = bsearch((
void *)&cat, (
void *)array, (
size_t) ncats,
509 static int cmp(
const void *pa,
const void *pb)
void G_free(void *buf)
Free allocated memory.
int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
Delete field/cat from line_cats structure.
int Vect_destroy_cat_list(struct cat_list *p)
Frees allocated cat_list memory.
int Vect_cat_del(struct line_cats *Cats, int field)
Delete all categories of given layer.
int Vect_reset_cats(struct line_cats *Cats)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_cat_in_cat_list(int cat, struct cat_list *list)
Check if category number is in list.
int Vect_field_cat_get(struct line_cats *Cats, int field, struct ilist *cats)
Get list of categories of given field.
int dig_alloc_cats(struct line_cats *cats, int num)
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
Add new field/cat to category structure if doesn't exist yet.
struct cat_list * Vect_new_cat_list()
Allocate memory for cat_list structure.
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int Vect_array_to_cat_list(int *vals, int nvals, struct cat_list *list)
Convert ordered array of integers to cat_list structure.
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int Vect_cat_in_array(int cat, int *array, int ncats)
Check if category is in ordered array of integers.
int G_debug(int level, const char *msg,...)
Print debugging message.
int Vect_str_to_cat_list(const char *str, struct cat_list *list)
Convert string of categories and cat ranges separated by commas to cat_list.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
struct line_cats * Vect__new_cats_struct(void)
Creates and initializes line_cats structure (lower level fn)
int Vect_cat_get(struct line_cats *Cats, int field, int *cat)
Get first found category of given field.