GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71915
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gis/color_rules.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/color_rules.c
3 
4  \brief GIS Library - Color tables management subroutines
5 
6  Taken from r.colors module.
7 
8  (C) 2001-2011 by the GRASS Development Team
9 */
10 
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include <grass/gis.h>
15 #include <grass/glocale.h>
16 
17 static char **scan_rules(int *);
18 static int cmp(const void *, const void *);
19 
20 /*!
21  \brief Get list of color rules for Option->options
22 
23  \return allocated string buffer with options
24 */
26 {
27  char *list, **rules;
28  const char *name;
29  int size, len, nrules;
30  int i, n;
31 
32  list = NULL;
33  size = len = 0;
34 
35  rules = scan_rules(&nrules);
36 
37  for (i = 0; i < nrules; i++) {
38  name = rules[i];
39  n = strlen(name);
40 
41  if (size < len + n + 2) {
42  size = len + n + 200;
43  list = G_realloc(list, size);
44  }
45 
46  if (len > 0)
47  list[len++] = ',';
48 
49  memcpy(&list[len], name, n + 1);
50  len += n;
51  }
52 
53  G_free(rules);
54 
55  return list;
56 }
57 
58 /*!
59  \brief Get color rules description for Option->descriptions
60 
61  \return allocated buffer with descriptions
62 */
64 {
65  char path[GPATH_MAX];
66  struct Key_Value *kv;
67  int result_len, result_max;
68  char *result, **rules;
69  const char *name, *desc;
70  int i, len, nrules;
71 
72  result_len = 0;
73  result_max = 2000;
74  result = G_malloc(result_max);
75 
76  G_snprintf(path, GPATH_MAX, "%s/etc/colors.desc", G_gisbase());
77  kv = G_read_key_value_file(path);
78  if (!kv)
79  return NULL;
80 
81  rules = scan_rules(&nrules);
82 
83  for (i = 0; i < nrules; i++) {
84  name = rules[i];
85  desc = G_find_key_value(name, kv);
86 
87  if (!desc)
88  desc = _("no description");
89 
90  /* desc = _(desc); */
91 
92  len = strlen(name) + strlen(desc) + 2;
93  if (result_len + len >= result_max) {
94  result_max = result_len + len + 1000;
95  result = G_realloc(result, result_max);
96  }
97 
98  sprintf(result + result_len, "%s;%s;", name, desc);
99  result_len += len;
100  }
101 
102  G_free_key_value(kv);
103  G_free(rules);
104 
105  return result;
106 }
107 
108 char **scan_rules(int *nrules)
109 {
110  char **rules;
111  char path[GPATH_MAX];
112 
113  G_snprintf(path, GPATH_MAX, "%s/etc/colors", G_gisbase());
114 
115  rules = G_ls2(path, nrules);
116 
117  rules = G_realloc(rules, (*nrules + 3) * sizeof (const char *));
118 
119  rules[(*nrules)++] = G_store("random");
120  rules[(*nrules)++] = G_store("grey.eq");
121  rules[(*nrules)++] = G_store("grey.log");
122 
123  qsort(rules, *nrules, sizeof (char *), cmp);
124 
125  return rules;
126 }
127 
128 int cmp(const void *aa, const void *bb)
129 {
130  char *const *a = (char *const *) aa;
131  char *const *b = (char *const *) bb;
132 
133  return strcmp(*a, *b);
134 }
135 
136 /*!
137  \brief Print color rules
138 
139  \param out file where to print
140 */
141 void G_list_color_rules(FILE *out)
142 {
143  int i, nrules;
144  char **rules;
145 
146  rules = scan_rules(&nrules);
147 
148  for (i = 0; i < nrules; i++)
149  fprintf(out, "%s\n", rules[i]);
150 }
151 
152 /*!
153  \brief Check if color rule is defined
154 
155  \param name color rule name
156 
157  \return 1 found
158  \return 0 not found
159 */
160 int G_find_color_rule(const char *name)
161 {
162  int i, nrules;
163  char **rules;
164 
165  rules = scan_rules(&nrules);
166 
167  for (i = 0; i < nrules; i++)
168  if (strcmp(name, rules[i]) == 0)
169  return 1;
170 
171  return 0;
172 }
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
int G_find_color_rule(const char *name)
Check if color rule is defined.
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
void G_list_color_rules(FILE *out)
Print color rules.
#define NULL
Definition: ccmath.h:32
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:103
int G_snprintf(char *str, size_t size, const char *fmt,...)
snprintf() clone.
Definition: snprintf.c:43
struct Key_Value * G_read_key_value_file(const char *file)
Read key/values pairs from file.
Definition: key_value3.c:53
char ** G_ls2(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
Definition: ls.c:95
double b
Definition: r_raster.c:39
struct list * list
Definition: read_list.c:24
#define GPATH_MAX
Definition: gis.h:151
Definition: gis.h:479
Definition: path.h:16
#define _(str)
Definition: glocale.h:13
char * G_color_rules_descriptions(void)
Get color rules description for Option-&gt;descriptions.
char * G_color_rules_options(void)
Get list of color rules for Option-&gt;options.
const char * name
Definition: named_colr.c:7
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:41