25 static int cmp(
const void *pa,
const void *pb);
26 static struct line_cats *Vect__new_cats_struct(
void);
44 if (
NULL == (p = Vect__new_cats_struct()))
58 static struct line_cats *Vect__new_cats_struct()
119 for (n = 0; n < Cats->
n_cats; n++) {
120 if (Cats->
field[n] == field && Cats->
cat[n] == cat)
127 G_fatal_error(
_(
"Too many categories (%d), unable to set cat %d (layer %d)"),
128 Cats->
n_cats, cat, field);
170 for (n = 0; n < Cats->
n_cats; n++) {
171 if (Cats->
field[n] == field) {
172 if (cat && ret == 0) {
204 for (n = 0; n < Cats->
n_cats; n++) {
205 if (Cats->
field[n] == field)
233 for (n = 0; n < Cats->
n_cats; n++) {
234 if (Cats->
field[n] != field) {
236 Cats->
cat[m] = Cats->
cat[n];
258 register int n, m, found;
271 for (n = 0; n < Cats->
n_cats; n++) {
272 if (Cats->
field[n] != field || Cats->
cat[n] != cat) {
274 Cats->
cat[m] = Cats->
cat[n];
369 int i, nr,
l,
err = 0;
374 G_debug(3,
"Vect_str_to_cat_list(): str = %s", str);
381 for (i = 0; i <
l; i++)
400 e = (
char *)strchr(s,
',');
413 if (sscanf(buf,
"%d-%d", &min, &max) == 2) {
415 else if (sscanf(buf,
"%d", &min) == 1)
419 G_warning(
_(
"Unable to convert category string '%s' (from '%s') to category range"),
448 G_debug(1,
"Vect_array_to_cat_list()");
450 for (i = 0; i < nvals; i++) {
451 if (i == 0 || (vals[i] - list->
max[range]) > 1) {
462 list->
min[range] = vals[i];
463 list->
max[range] = vals[i];
466 list->
max[range] = vals[i];
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])
566 char *where,
char *catstr)
572 G_warning(
_(
"Layer number must be > 0 for category constraints"));
581 int ncats, *cats =
NULL;
585 G_warning(
_(
"'%s' and '%s' parameters were supplied, cats will be ignored"),
"where",
"cats");
610 qsort(cats, ncats,
sizeof(
int), cmp);
614 for (i = 1; i < ncats; i++) {
615 if (cats[i] != cats[j - 1]) {
627 G_warning(
_(
"No categories selected with '%s' option"),
"where");
637 G_warning(
_(
"%d errors in '%s' option"), ret,
"cats");
673 G_warning(
_(
"Layer number must be > 0 for category constraints"));
679 for (i = 0; i < Cats->
n_cats; i++) {
680 if (Cats->
field[i] == layer &&
688 for (i = 0; i < Cats->
n_cats; i++) {
689 if (Cats->
field[i] == layer)
711 i = bsearch((
void *)&cat, (
void *)array, (
size_t) ncats,
720 static int cmp(
const void *pa,
const void *pb)
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Vect_field_cat_get(const struct line_cats *Cats, int field, struct ilist *cats)
Get list of categories of given field.
int alloc_ranges
Allocated space for ranges.
int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
Delete field/cat from line_cats structure.
int Vect_reset_list(struct ilist *)
Reset ilist structure.
void 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.
#define n_(strs, strp, num)
int alloc_cats
Allocated space for categories.
char * table
Name of DB table.
int n_values
Number of values in the list.
void G_free(void *)
Free allocated memory.
int * min
Array of minimum values.
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
void Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
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_list_to_array(const struct cat_list *list, int **vals, int *nvals)
Convert cat_list struct to ordered array of unique integers.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
struct field_info * Vect_get_field(const struct Map_info *, int)
Get information about link to database (by layer number)
int Vect_cat_in_cat_list(int cat, const struct cat_list *list)
Check if category number is in list.
int n_ranges
Number of ranges.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
Add new field/cat to category structure if doesn't exist yet.
int dig_alloc_cats(struct line_cats *, int)
Allocate room for 'num' fields and category arrays in struct line_cats.
Layer (old: field) information.
struct cat_list * Vect_new_cat_list()
Allocate memory for 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_array_to_cat_list(const int *vals, int nvals, struct cat_list *list)
Convert ordered array of integers to cat_list structure.
#define GV_NCATS_MAX
Maximum number of categories for one element.
int Vect_cat_get(const struct line_cats *Cats, int field, int *cat)
Get first found category of given field.
int field
Category layer (field)
int n_cats
Number of categories attached to element.
int * cat
Array of categories.
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.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
int Vect_cats_in_constraint(struct line_cats *Cats, int layer, struct cat_list *list)
Check if categories match with category constraints.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
int Vect_list_append(struct ilist *, int)
Append new item to the end of list if not yet present.
void G_warning(const char *,...) __attribute__((format(printf
#define GV_FIELD_MAX
Maximum field.
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 * field
Array of layers (fields)
void void G_verbose_message(const char *,...) __attribute__((format(printf
int db_select_int(dbDriver *, const char *, const char *, const char *, int **)
Select array of ordered integers from table/column.
int G_debug(int, const char *,...) __attribute__((format(printf
int * max
Array of maximum values.
char * key
Name of key column (usually 'cat')