cats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*!
  2. * \file lib/vector/Vlib/cats.c
  3. *
  4. * \brief Vector library - Category management
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Original author CERL, probably Dave Gerdes or Mike
  14. * Higgins
  15. * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  16. * \author Various updates by Martin Landa <landa.martin gmail.com>
  17. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/vector.h>
  21. #include <grass/glocale.h>
  22. static int cmp(const void *pa, const void *pb);
  23. struct line_cats *Vect__new_cats_struct(void);
  24. /*!
  25. \brief Creates and initializes line_cats structure.
  26. This structure is used for reading and writing vector cats. The
  27. library routines handle all memory allocation.
  28. To free allocated memory call Vect_destroy_cats_struct().
  29. \return struct line_cats *
  30. \return NULL on error
  31. */
  32. struct line_cats *Vect_new_cats_struct()
  33. {
  34. struct line_cats *p;
  35. if (NULL == (p = Vect__new_cats_struct()))
  36. G_fatal_error(_("Vect_new_cats_struct(): Out of memory"));
  37. return p;
  38. }
  39. /*!
  40. \brief Creates and initializes line_cats structure (lower level fn)
  41. This structure is used for reading and writing vector cats. The
  42. library routines handle all memory allocation.
  43. \return struct line_cats *
  44. */
  45. struct line_cats *Vect__new_cats_struct()
  46. {
  47. struct line_cats *p;
  48. p = (struct line_cats *)G_malloc(sizeof(struct line_cats));
  49. /* n_cats MUST be initialized to zero */
  50. if (p)
  51. p->n_cats = 0;
  52. if (p)
  53. p->alloc_cats = 0;
  54. return p;
  55. }
  56. /*!
  57. \brief Frees all memory associated with line_cats structure,
  58. including the struct itself.
  59. \param p line_cats structure
  60. */
  61. void Vect_destroy_cats_struct(struct line_cats *p)
  62. {
  63. if (p) { /* probably a moot test */
  64. if (p->n_cats) {
  65. G_free((void *)p->field);
  66. G_free((void *)p->cat);
  67. }
  68. G_free((void *)p);
  69. }
  70. }
  71. /*!
  72. \brief Add new field/cat to category structure if doesn't exist
  73. yet.
  74. \param[in] Cats line_cats structure
  75. \param[in] field layer number
  76. \param[in] cat category number
  77. \return number of categories
  78. \return 0 if no space for new category in structure, n_cats would be > GV_NCATS_MAX
  79. \return -1 on out of memory
  80. \return -2 if field out of range: 1 - GV_FIELD_MAX or cat out of range: 1 - GV_CAT_MAX
  81. */
  82. int Vect_cat_set(struct line_cats *Cats, int field, int cat)
  83. {
  84. register int n;
  85. /* check input values */
  86. /* compiler may warn:
  87. * comparison is always 0 due to limited range of data type
  88. * but remember that limit is set to portable data type length
  89. * and machine native size may be longer */
  90. /*
  91. if (field < 1 || field > GV_FIELD_MAX || cat < 0 || cat > GV_CAT_MAX)
  92. return (-2);
  93. */
  94. /* go through old cats and find if field/category exists */
  95. for (n = 0; n < Cats->n_cats; n++) {
  96. if (Cats->field[n] == field && Cats->cat[n] == cat)
  97. return (1);
  98. }
  99. /* field was not found so we shall append new cat */
  100. /* test if space exist */
  101. if (n >= GV_NCATS_MAX) {
  102. G_fatal_error(_("Too many categories (%d), unable to set cat %d (layer %d)"),
  103. Cats->n_cats, cat, field);
  104. }
  105. if (Cats->n_cats == Cats->alloc_cats) {
  106. if (0 > dig_alloc_cats(Cats, Cats->n_cats + 100))
  107. return (-1);
  108. }
  109. n = Cats->n_cats;
  110. Cats->field[n] = field;
  111. Cats->cat[n] = cat;
  112. Cats->n_cats++;
  113. return (1);
  114. }
  115. /*!
  116. \brief Get first found category of given field.
  117. <em>cat</em> is set to first category found or -1 if field was not
  118. found
  119. \param Cats pointer line_cats structure
  120. \param field layer number
  121. \param[out] cat pointer to variable where cat will be written (can be NULL)
  122. \return number of found cats for given field (first reported)
  123. \return 0 layer does not exist
  124. */
  125. int Vect_cat_get(const struct line_cats *Cats, int field, int *cat)
  126. {
  127. int n, ret;
  128. /* check input value */
  129. /*
  130. if (field < 1 || field > GV_FIELD_MAX)
  131. return (0);
  132. */
  133. /* field was not found */
  134. ret = 0;
  135. if (cat)
  136. *cat = -1;
  137. /* go through cats and find if field exist */
  138. for (n = 0; n < Cats->n_cats; n++) {
  139. if (Cats->field[n] == field) {
  140. if (cat && ret == 0) {
  141. *cat = Cats->cat[n];
  142. }
  143. ret++;
  144. }
  145. }
  146. return ret;
  147. }
  148. /*!
  149. \brief Get list of categories of given field.
  150. \param Cats line_cats structure
  151. \param field layer number
  152. \param[out] cats pointer to list where cats will be written
  153. \return number of found categories
  154. \return -1 on invalid field
  155. */
  156. int Vect_field_cat_get(const struct line_cats *Cats, int field, struct ilist *cats)
  157. {
  158. int n;
  159. /* reset list of categories */
  160. Vect_reset_list(cats);
  161. /* check input value */
  162. if (field < 1 || field > GV_FIELD_MAX)
  163. return -1;
  164. /* go through cats and find if field exist */
  165. for (n = 0; n < Cats->n_cats; n++) {
  166. if (Cats->field[n] != field)
  167. continue;
  168. Vect_list_append(cats, Cats->cat[n]);
  169. }
  170. return cats->n_values;
  171. }
  172. /*!
  173. \brief Delete all categories of given layer
  174. \param[in,out] Cats line_cats structure
  175. \param field layer number
  176. \return 1 deleted
  177. \return 0 layer does not exist
  178. */
  179. int Vect_cat_del(struct line_cats *Cats, int field)
  180. {
  181. int n, m, found = 0;
  182. /* check input value */
  183. /*
  184. if (field < 1 || field > GV_FIELD_MAX)
  185. return (0);
  186. */
  187. /* go through cats and find if field exist */
  188. for (n = 0; n < Cats->n_cats; n++) {
  189. if (Cats->field[n] == field) {
  190. for (m = n; m < Cats->n_cats - 1; m++) {
  191. Cats->field[m] = Cats->field[m + 1];
  192. Cats->cat[m] = Cats->cat[m + 1];
  193. }
  194. Cats->n_cats--;
  195. found = 1;
  196. n--; /* check again this position */
  197. }
  198. }
  199. return (found);
  200. }
  201. /*!
  202. \brief Delete field/cat from line_cats structure
  203. \param[in,out] Cats line_cats structure
  204. \param field layer number
  205. \param cat category to be deleted or -1 to delete all cats of given field
  206. \return 1 deleted
  207. \return 0 field/category number does not exist
  208. */
  209. int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
  210. {
  211. register int n, m, found = 0;
  212. /* check input value */
  213. /*
  214. if (field < 1 || field > GV_FIELD_MAX)
  215. return (0);
  216. */
  217. /* go through cats and find if field exist */
  218. for (n = 0; n < Cats->n_cats; n++) {
  219. if (Cats->field[n] == field && (Cats->cat[n] == cat || cat == -1)) {
  220. for (m = n; m < Cats->n_cats - 1; m++) {
  221. Cats->field[m] = Cats->field[m + 1];
  222. Cats->cat[m] = Cats->cat[m + 1];
  223. }
  224. Cats->n_cats--;
  225. found = 1;
  226. }
  227. }
  228. return (found);
  229. }
  230. /*!
  231. \brief Reset category structure to make sure cats structure is clean to be re-used.
  232. I.e. it has no cats associated with it. Cats must have
  233. previously been created with Vect_new_cats_struct()
  234. \param[out] Cats line_cats structure
  235. \return 0
  236. */
  237. int Vect_reset_cats(struct line_cats *Cats)
  238. {
  239. Cats->n_cats = 0;
  240. return 0;
  241. }
  242. /*!
  243. \brief Allocate memory for cat_list structure.
  244. \return poiter to allocated structure
  245. \return NULL on out of memory
  246. */
  247. struct cat_list *Vect_new_cat_list()
  248. {
  249. struct cat_list *p;
  250. p = (struct cat_list *)G_malloc(sizeof(struct cat_list));
  251. /* n_ranges MUST be initialized to zero */
  252. if (p) {
  253. p->n_ranges = 0;
  254. p->alloc_ranges = 0;
  255. p->field = 0;
  256. p->min = NULL;
  257. p->max = NULL;
  258. }
  259. return p;
  260. }
  261. /*!
  262. \brief Frees allocated cat_list memory.
  263. \param p pointer to line_cats structure
  264. */
  265. void Vect_destroy_cat_list(struct cat_list *p)
  266. {
  267. if (p) { /* probably a moot test */
  268. if (p->n_ranges) {
  269. G_free((void *)p->min);
  270. G_free((void *)p->max);
  271. }
  272. G_free((void *)p);
  273. }
  274. }
  275. /*!
  276. \brief Converts string of categories and cat ranges separated by commas to cat_list.
  277. \par Examples of string:
  278. \verbatim
  279. 5,6,7
  280. 3-9
  281. 2,3,5-9,20\endverbatim
  282. \par Example:
  283. \code
  284. ...
  285. str = "2,3,5-9,20"
  286. catList = Vect_new_cat_list()
  287. Vect_str_to_cat_list(str, catList)
  288. \endcode
  289. \verbatim
  290. catList.field = 0
  291. catList.n_ranges = 4
  292. catList.min = {2, 3, 5, 20}
  293. catList.max = {2, 3, 9, 20}\endverbatim
  294. \param[in] str category list as a string
  295. \param[out] list result cat_list structure
  296. \return number of errors in ranges
  297. */
  298. int Vect_str_to_cat_list(const char *str, struct cat_list *list)
  299. {
  300. int i, nr, l, err = 0;
  301. const char *s, *e;
  302. char buf[100];
  303. int min, max;
  304. G_debug(3, "Vect_str_to_cat_list(): str = %s", str);
  305. list->n_ranges = 0;
  306. l = strlen(str);
  307. /* find number of ranges */
  308. nr = 1; /* one range */
  309. for (i = 0; i < l; i++)
  310. if (str[i] == ',')
  311. nr++;
  312. /* allocate space */
  313. if (list->alloc_ranges == 0) {
  314. list->min = (int *)G_malloc(nr * sizeof(int));
  315. list->max = (int *)G_malloc(nr * sizeof(int));
  316. }
  317. else if (nr > list->alloc_ranges) {
  318. list->min = (int *)G_realloc((void *)list->min, nr * sizeof(int));
  319. list->max = (int *)G_realloc((void *)list->max, nr * sizeof(int));
  320. }
  321. /* go through string and read ranges */
  322. i = 0;
  323. s = str;
  324. while (s) {
  325. e = (char *)strchr(s, ','); /* first comma */
  326. if (e) {
  327. l = e - s;
  328. strncpy(buf, s, l);
  329. buf[l] = '\0';
  330. s = e + 1;
  331. }
  332. else {
  333. strcpy(buf, s);
  334. s = NULL;
  335. }
  336. G_debug(3, " buf = %s", buf);
  337. if (sscanf(buf, "%d-%d", &min, &max) == 2) {
  338. }
  339. else if (sscanf(buf, "%d", &min) == 1)
  340. max = min;
  341. else { /* error */
  342. G_warning(_("Unable to convert category string '%s' (from '%s') to category range"),
  343. buf, str);
  344. err++;
  345. continue;
  346. }
  347. list->min[i] = min;
  348. list->max[i] = max;
  349. i++;
  350. }
  351. list->n_ranges = i;
  352. return (err);
  353. }
  354. /*!
  355. \brief Convert ordered array of integers to cat_list structure.
  356. \param vals array of integers
  357. \param nvals number of values
  358. \param[out] list result cat_list structure
  359. \return number of ranges
  360. */
  361. int Vect_array_to_cat_list(const int *vals, int nvals, struct cat_list *list)
  362. {
  363. int i, range;
  364. G_debug(1, "Vect_array_to_cat_list()");
  365. range = -1;
  366. for (i = 0; i < nvals; i++) {
  367. if (i == 0 || (vals[i] - list->max[range]) > 1) {
  368. range++;
  369. if (range == list->alloc_ranges) {
  370. list->alloc_ranges += 1000;
  371. list->min = (int *)G_realloc((void *)list->min,
  372. list->alloc_ranges *
  373. sizeof(int));
  374. list->max =
  375. (int *)G_realloc((void *)list->max,
  376. list->alloc_ranges * sizeof(int));
  377. }
  378. list->min[range] = vals[i];
  379. list->max[range] = vals[i];
  380. }
  381. else {
  382. list->max[range] = vals[i];
  383. }
  384. }
  385. list->n_ranges = range + 1;
  386. return (list->n_ranges);
  387. }
  388. /*!
  389. \brief Check if category number is in list.
  390. \param cat category number
  391. \param list cat_list structure
  392. \return TRUE if cat is in list
  393. \return FALSE if it is not
  394. */
  395. int Vect_cat_in_cat_list(int cat, const struct cat_list *list)
  396. {
  397. int i;
  398. for (i = 0; i < list->n_ranges; i++)
  399. if (cat >= list->min[i] && cat <= list->max[i])
  400. return (TRUE);
  401. return (FALSE);
  402. }
  403. /*!
  404. \brief Check if category is in ordered array of integers.
  405. \param cat category number
  406. \param array ordered array of integers
  407. \param ncats number of categories in array
  408. \return TRUE if cat is in list
  409. \return FALSE if it is not
  410. */
  411. int Vect_cat_in_array(int cat, const int *array, int ncats)
  412. {
  413. int *i;
  414. i = bsearch((void *)&cat, (void *)array, (size_t) ncats,
  415. sizeof(int), cmp);
  416. if (i != NULL)
  417. return (TRUE);
  418. return (FALSE);
  419. }
  420. static int cmp(const void *pa, const void *pb)
  421. {
  422. int *p1 = (int *)pa;
  423. int *p2 = (int *)pb;
  424. if (*p1 < *p2)
  425. return -1;
  426. if (*p1 > *p2)
  427. return 1;
  428. return 0;
  429. }