25 static int cmp(
const void *pa,
const void *pb);
26 static struct line_cats *Vect__new_cats_struct(
void);
43 if (
NULL == (p = Vect__new_cats_struct()))
57 static struct line_cats *Vect__new_cats_struct(
void)
118 for (n = 0; n < Cats->
n_cats; n++) {
127 _(
"Too many categories (%d), unable to set cat %d (layer %d)"),
170 for (n = 0; n < Cats->
n_cats; n++) {
172 if (
cat && ret == 0) {
205 for (n = 0; n < Cats->
n_cats; n++) {
234 for (n = 0; n < Cats->
n_cats; n++) {
237 Cats->
cat[m] = Cats->
cat[n];
259 register int n, m, found;
272 for (n = 0; n < Cats->
n_cats; n++) {
275 Cats->
cat[m] = Cats->
cat[n];
370 int i, nr,
l,
err = 0;
375 G_debug(3,
"Vect_str_to_cat_list(): str = %s", str);
382 for (i = 0; i <
l; i++)
387 if (
list->alloc_ranges == 0) {
391 else if (nr >
list->alloc_ranges) {
401 e = (
char *)strchr(s,
',');
414 if (sscanf(buf,
"%d-%d", &
min, &
max) == 2) {
416 else if (sscanf(buf,
"%d", &
min) == 1)
420 G_warning(
_(
"Unable to convert category string '%s' (from '%s') to "
450 G_debug(1,
"Vect_array_to_cat_list()");
452 for (i = 0; i < nvals; i++) {
453 if (i == 0 || (vals[i] -
list->max[range]) > 1) {
455 if (range ==
list->alloc_ranges) {
456 list->alloc_ranges += 1000;
458 list->alloc_ranges *
sizeof(int));
460 list->alloc_ranges *
sizeof(int));
462 list->min[range] = vals[i];
463 list->max[range] = vals[i];
466 list->max[range] = vals[i];
470 list->n_ranges = range + 1;
472 return (
list->n_ranges);
491 int i, j, k, n, n_cats, n_ucats, last_cat;
494 G_debug(1,
"Vect_cat_list_to_array()");
498 for (i = 0; i <
list->n_ranges; i++) {
499 n =
list->max[i] -
list->min[i] + 1;
504 cats = (
int *)
G_realloc(cats,
sizeof(
int) * (n_cats + n));
506 for (j = n_cats, k = 0; j < n_cats + n; j++, k++) {
507 cats[j] =
list->min[i] + k;
513 qsort(cats, n_cats,
sizeof(
int), cmp);
516 ucats =
G_malloc(
sizeof(
int) * n_cats);
517 last_cat = ucats[0] = cats[0];
519 for (i = 1; i < n_cats; i++) {
520 if (last_cat == cats[i])
522 last_cat = ucats[n_ucats++] = cats[i];
527 ucats = (
int *)
G_realloc(ucats,
sizeof(
int) * n_ucats);
548 for (i = 0; i <
list->n_ranges; i++)
549 if (cat >=
list->min[i] && cat <= list->
max[i])
567 char *where,
char *catstr)
573 G_warning(
_(
"Layer number must be > 0 for category constraints"));
582 int ncats, *cats =
NULL;
586 G_warning(
_(
"'%s' and '%s' parameters were supplied, cats will be "
609 n_(
"One category loaded",
"%d categories loaded", ncats), ncats);
614 qsort(cats, ncats,
sizeof(
int), cmp);
618 for (i = 1; i < ncats; i++) {
619 if (cats[i] != cats[j - 1]) {
631 G_warning(
_(
"No categories selected with '%s' option"),
"where");
641 G_warning(
_(
"%d errors in '%s' option"), ret,
"cats");
645 if (
list->n_ranges < 1) {
677 G_warning(
_(
"Layer number must be > 0 for category constraints"));
683 for (i = 0; i < Cats->
n_cats; i++) {
684 if (Cats->
field[i] == layer &&
692 for (i = 0; i < Cats->
n_cats; i++) {
693 if (Cats->
field[i] == layer)
714 i = bsearch((
void *)&cat, (
void *)array, (
size_t)ncats,
sizeof(
int), cmp);
722 static int cmp(
const void *pa,
const void *pb)
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int db_select_int(dbDriver *, const char *, const char *, const char *, int **)
Select array of ordered integers from table/column.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void void G_verbose_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_list_append(struct ilist *, int)
Append new item to the end of list if not yet present.
struct field_info * Vect_get_field(struct Map_info *, int)
Get information about link to database (by layer number)
int Vect_reset_list(struct ilist *)
Reset ilist structure.
#define GV_FIELD_MAX
Maximum field.
#define GV_NCATS_MAX
Maximum number of categories for one element.
int dig_alloc_cats(struct line_cats *, int)
Allocate room for 'num' fields and category arrays in struct line_cats.
#define n_(strs, strp, num)
int n_ranges
Number of ranges.
int * min
Array of minimum values.
int * max
Array of maximum values.
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * key
Name of key column (usually 'cat')
int n_values
Number of values in the list.
int * field
Array of layers (fields)
int alloc_cats
Allocated space for categories.
int * cat
Array of categories.
int n_cats
Number of categories attached to element.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
struct cat_list * Vect_new_cat_list(void)
Allocate memory for cat_list structure.
struct cat_list * Vect_cats_set_constraint(struct Map_info *Map, int layer, char *where, char *catstr)
Set category constraints using 'where' or 'cats' option and layer number.
void Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_cat_get(const struct line_cats *Cats, int field, int *cat)
Get first found category of given field.
int Vect_reset_cats(struct line_cats *Cats)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
Delete field/cat from line_cats structure.
int Vect_array_to_cat_list(const int *vals, int nvals, struct cat_list *list)
Convert ordered array of integers to cat_list structure.
int Vect_cat_in_array(int cat, const int *array, int ncats)
Check if category is in ordered array of integers.
int Vect_str_to_cat_list(const char *str, struct cat_list *list)
Converts string of categories and cat ranges separated by commas to cat_list.
int Vect_cat_in_cat_list(int cat, const struct cat_list *list)
Check if category number is in list.
int Vect_cat_list_to_array(const struct cat_list *list, int **vals, int *nvals)
Convert cat_list struct to ordered array of unique integers.
int Vect_field_cat_get(const struct line_cats *Cats, int field, struct ilist *cats)
Get list of categories of given field.
int Vect_cats_in_constraint(struct line_cats *Cats, int layer, struct cat_list *list)
Check if categories match with category constraints.
int Vect_cat_del(struct line_cats *Cats, int field)
Delete all categories of given layer.
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
void Vect_destroy_cat_list(struct cat_list *p)
Frees allocated cat_list memory.
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
Add new field/cat to category structure if doesn't exist yet.