GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
vector/Vlib/color_read.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/color_read.c
3 
4  \brief Vector Library - read color table of vector map
5 
6  (C) 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Martin Landa <landa.martin gmail.com>
12  */
13 
14 #include <string.h>
15 
16 #include <grass/gis.h>
17 #include <grass/raster.h>
18 #include <grass/vector.h>
19 #include <grass/glocale.h>
20 
21 /*!
22  \brief Read color table of vector map
23 
24  The color table for the vector map <i>name</i> in the specified
25  <i>mapset</i> is read into the <i>colors</i> structure.
26 
27  Note: If a secondary color file for map name <i>name</i> exists in
28  the current mapset, that color file is read. This allows the user to
29  define their own color lookup tables for vector maps found in other
30  mapsets.
31 
32  Warning message is printed if the color file is missing or invalid.
33 
34  \param name vector map name
35  \param mapset mapset name ("" for search path)
36  \param[out] colors pointer to Colors structure (can be NULL)
37 
38  \return -1 on error
39  \return 0 if color table missing
40  \return 1 on success (color table found)
41  */
42 int Vect_read_colors(const char *name, const char *mapset,
43  struct Colors *colors)
44 {
45  int ret;
46  char buf[GPATH_MAX];
47  char xname[GNAME_MAX];
48 
49  if (colors)
50  Rast_init_colors(colors);
51 
52  if (G_strlcpy(xname, name, sizeof(xname)) >= sizeof(xname)) {
53  G_warning(_("Vector map name <%s> is too long"), name);
54  return -1;
55  }
56  mapset = G_find_vector(xname, mapset);
57  if (!mapset)
58  return -1;
59 
60  name = xname;
61 
62  if (strcmp(mapset, G_mapset()) == 0) {
63  /* look for the regular color table */
64  (void)snprintf(buf, sizeof(buf), "%s/%s", GV_DIRECTORY, name);
65  ret = Rast__read_colors(buf, GV_COLR_ELEMENT, mapset, colors);
66  }
67  else {
68  /* look for secondary color table in current mapset */
69  (void)snprintf(buf, sizeof(buf), "%s/%s", GV_COLR2_DIRECTORY, mapset);
70  ret = Rast__read_colors(buf, name, G_mapset(), colors);
71  }
72  if (ret == -2)
73  return 0;
74 
75  return ret;
76 }
void G_warning(const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
const char * G_find_vector(char *, const char *)
Finds a vector map.
Definition: find_vect.c:41
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition: strlcpy.c:52
int Rast__read_colors(const char *, const char *, const char *, struct Colors *)
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition: color_init.c:25
#define GV_COLR2_DIRECTORY
Name of directory for alternative color tables.
Definition: dig_defines.h:30
#define GV_DIRECTORY
Name of vector directory.
Definition: dig_defines.h:8
#define GV_COLR_ELEMENT
Color table.
Definition: dig_defines.h:28
#define GPATH_MAX
Definition: gis.h:194
#define GNAME_MAX
Definition: gis.h:191
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:6
Definition: gis.h:686
int Vect_read_colors(const char *name, const char *mapset, struct Colors *colors)
Read color table of vector map.