ARB
ad_colorset.cxx
Go to the documentation of this file.
1 // ============================================================ //
2 // //
3 // File : ad_colorset.cxx //
4 // Purpose : item-colors and colorsets //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in July 2015 //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================ //
10 
11 #include "ad_colorset.h"
12 #include "arbdbt.h"
13 
14 #include <arb_strbuf.h>
15 
16 int GBT_get_color_group(GBDATA *gb_item) {
21  GBDATA *gb_cgroup = GB_entry(gb_item, GB_COLORGROUP_ENTRY);
22  if (gb_cgroup) {
23  int color_group = GB_read_int(gb_cgroup);
24  if (color_group<1 || color_group>GB_COLOR_GROUPS) {
25  color_group = 0;
26  }
27  return color_group;
28  }
29  return 0;
30 }
31 GB_ERROR GBT_set_color_group(GBDATA *gb_item, int color_group) {
40  if (color_group) {
41  if (color_group<1 || color_group>GB_COLOR_GROUPS) {
42  return GBS_global_string("invalid color_group %i", color_group);
43  }
44  return GBT_write_int(gb_item, GB_COLORGROUP_ENTRY, color_group);
45  }
46 
47  // do not store 0
48  GBDATA *gb_cgroup = GB_entry(gb_item, GB_COLORGROUP_ENTRY);
49  return gb_cgroup ? GB_delete(gb_cgroup) : NULp;
50 }
51 
52 GBDATA *GBT_colorset_root(GBDATA *gb_main, const char *itemsname) {
57  GBDATA *gb_colorsets = GB_search(gb_main, "colorsets", GB_CREATE_CONTAINER);
58  GBDATA *gb_item_root = gb_colorsets ? GB_search(gb_colorsets, itemsname, GB_CREATE_CONTAINER) : NULp;
59  return gb_item_root;
60 }
61 
62 void GBT_get_colorset_names(ConstStrArray& colorsetNames, GBDATA *gb_colorset_root) {
67  for (GBDATA *gb_colorset = GB_entry(gb_colorset_root, "colorset");
68  gb_colorset;
69  gb_colorset = GB_nextEntry(gb_colorset))
70  {
71  const char *name = GBT_get_name(gb_colorset);
72  if (name) colorsetNames.put(name);
73  }
74 }
75 
76 GBDATA *GBT_find_colorset(GBDATA *gb_colorset_root, const char *name) {
82  return GBT_find_item_rel_item_data(gb_colorset_root, "name", name);
83 }
84 
85 GBDATA *GBT_find_or_create_colorset(GBDATA *gb_colorset_root, const char *name) {
91  return GBT_find_or_create_item_rel_item_data(gb_colorset_root, "colorset", "name", name, false);
92 }
93 
94 GB_ERROR GBT_load_colorset(GBDATA *gb_colorset, ConstStrArray& colorsetDefs) {
101  GB_ERROR error = NULp;
102  char *colorset = GBT_read_string(gb_colorset, "color_set");
103  if (!colorset) {
104  error = GB_have_error() ? GB_await_error() : "Missing 'color_set' entry";
105  error = GBS_global_string("Failed to read colorset (Reason: %s)", error);
106  }
107  else {
108  GBT_splitNdestroy_string(colorsetDefs, colorset, ';');
109  }
110  return error;
111 }
112 
113 GB_ERROR GBT_save_colorset(GBDATA *gb_colorset, CharPtrArray& colorsetDefs) {
119  GBS_strstruct buffer(colorsetDefs.size()*(8+1+2+1));
120  for (size_t d = 0; d<colorsetDefs.size(); ++d) {
121  buffer.cat(colorsetDefs[d]);
122  buffer.put(';');
123  }
124  buffer.cut_tail(1); // remove trailing ';'
125 
126  return GBT_write_string(gb_colorset, "color_set", buffer.get_data());
127 }
128 
129 
const char * GB_ERROR
Definition: arb_core.h:25
void cut_tail(size_t byte_count)
Definition: arb_strbuf.h:134
void put(const char *elem)
Definition: arb_strarray.h:199
size_t size() const
Definition: arb_strarray.h:85
GBDATA * GBT_colorset_root(GBDATA *gb_main, const char *itemsname)
Definition: ad_colorset.cxx:52
#define GB_COLOR_GROUPS
Definition: ad_colorset.h:22
long GB_read_int(GBDATA *gbd)
Definition: arbdb.cxx:699
GBDATA * GB_nextEntry(GBDATA *entry)
Definition: adquery.cxx:339
GBDATA * GBT_find_or_create_colorset(GBDATA *gb_colorset_root, const char *name)
Definition: ad_colorset.cxx:85
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
bool GB_have_error()
Definition: arb_msg.cxx:349
void cat(const char *from)
Definition: arb_strbuf.h:156
char buffer[MESSAGE_BUFFERSIZE]
Definition: seq_search.cxx:34
GB_ERROR GB_delete(GBDATA *&source)
Definition: arbdb.cxx:1880
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
char * GBT_read_string(GBDATA *gb_container, const char *fieldpath)
Definition: adtools.cxx:267
int GBT_get_color_group(GBDATA *gb_item)
Definition: ad_colorset.cxx:16
#define GB_COLORGROUP_ENTRY
Definition: ad_colorset.h:21
static void error(const char *msg)
Definition: mkptypes.cxx:96
void GBT_splitNdestroy_string(ConstStrArray &names, char *&namelist, const char *separator, bool dropEmptyTokens)
GB_ERROR GBT_save_colorset(GBDATA *gb_colorset, CharPtrArray &colorsetDefs)
GB_ERROR GBT_load_colorset(GBDATA *gb_colorset, ConstStrArray &colorsetDefs)
Definition: ad_colorset.cxx:94
GB_ERROR GBT_write_string(GBDATA *gb_container, const char *fieldpath, const char *content)
Definition: adtools.cxx:451
GBDATA * GBT_find_colorset(GBDATA *gb_colorset_root, const char *name)
Definition: ad_colorset.cxx:76
#define NULp
Definition: cxxforward.h:97
const char * get_data() const
Definition: arb_strbuf.h:70
const char * GBT_get_name(GBDATA *gb_item)
Definition: aditem.cxx:446
void GBT_get_colorset_names(ConstStrArray &colorsetNames, GBDATA *gb_colorset_root)
Definition: ad_colorset.cxx:62
GBDATA * GBT_find_item_rel_item_data(GBDATA *gb_item_data, const char *id_field, const char *id_value)
Definition: aditem.cxx:74
GB_ERROR GBT_write_int(GBDATA *gb_container, const char *fieldpath, long content)
Definition: adtools.cxx:471
GBDATA * gb_main
Definition: adname.cxx:33
GB_ERROR GBT_set_color_group(GBDATA *gb_item, int color_group)
Definition: ad_colorset.cxx:31
GBDATA * GB_search(GBDATA *gbd, const char *fieldpath, GB_TYPES create)
Definition: adquery.cxx:531
GBDATA * GBT_find_or_create_item_rel_item_data(GBDATA *gb_item_data, const char *itemname, const char *id_field, const char *id, bool markCreated)
Definition: aditem.cxx:18
GBDATA * GB_entry(GBDATA *father, const char *key)
Definition: adquery.cxx:334
void put(char c)
Definition: arb_strbuf.h:138