source: tags/ms_r18q1/ARBDB/ad_colorset.cxx

Last change on this file was 16763, checked in by westram, 6 years ago
File size: 4.4 KB
Line 
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
16long GBT_get_color_group(GBDATA *gb_item) {
17    /*! return color group of item (special, gene, ...)
18     * @param gb_item the item
19     * @return color group if defined (0 if none)
20     */
21    GBDATA *gb_cgroup = GB_entry(gb_item, GB_COLORGROUP_ENTRY);
22    return gb_cgroup ? GB_read_int(gb_cgroup) : 0;
23}
24GB_ERROR GBT_set_color_group(GBDATA *gb_item, long color_group) {
25    /*! set color group of item
26     * @param gb_item the item
27     * @param color_group the color group [1...]
28     * @return error if sth went wrong
29     */
30
31    if (color_group) return GBT_write_int(gb_item, GB_COLORGROUP_ENTRY, color_group);
32
33    // do not store 0
34    GBDATA *gb_cgroup = GB_entry(gb_item, GB_COLORGROUP_ENTRY);
35    return gb_cgroup ? GB_delete(gb_cgroup) : NULp;
36}
37
38GBDATA *GBT_colorset_root(GBDATA *gb_main, const char *itemsname) {
39    /*! return root container for colorsets
40     * @param gb_main database
41     * @param itemsname name of items ( == ItemSelector::items_name)
42     */
43    GBDATA *gb_colorsets = GB_search(gb_main, "colorsets", GB_CREATE_CONTAINER);
44    GBDATA *gb_item_root = gb_colorsets ? GB_search(gb_colorsets, itemsname, GB_CREATE_CONTAINER) : NULp;
45    return gb_item_root;
46}
47
48void GBT_get_colorset_names(ConstStrArray& colorsetNames, GBDATA *gb_colorset_root) {
49    /*! retrieve names of existing colorsets from DB
50     * @param colorsetNames result-param: will be filled with colorset names
51     * @param gb_colorset_root result of GBT_colorset_root()
52     */
53    for (GBDATA *gb_colorset = GB_entry(gb_colorset_root, "colorset");
54         gb_colorset;
55         gb_colorset = GB_nextEntry(gb_colorset))
56    {
57        const char *name = GBT_read_name(gb_colorset);
58        colorsetNames.put(name);
59    }
60}
61
62GBDATA *GBT_find_colorset(GBDATA *gb_colorset_root, const char *name) {
63    /*! lookup (existing) colorset
64     * @param gb_colorset_root result of GBT_colorset_root()
65     * @param name colorset name
66     * @return colorset DB entry (or NULp, in which case an error MAY be exported)
67     */
68    return GBT_find_item_rel_item_data(gb_colorset_root, "name", name);
69}
70
71GBDATA *GBT_find_or_create_colorset(GBDATA *gb_colorset_root, const char *name) {
72    /*! create a new colorset
73     * @param gb_colorset_root result of GBT_colorset_root()
74     * @param name colorset name
75     * @return colorset DB entry (or NULp, in which case an error IS exported)
76     */
77    return GBT_find_or_create_item_rel_item_data(gb_colorset_root, "colorset", "name", name, false);
78}
79
80GB_ERROR GBT_load_colorset(GBDATA *gb_colorset, ConstStrArray& colorsetDefs) {
81    /*! load a colorset
82     * @param gb_colorset result of GBT_find_colorset()
83     * @param colorsetDefs result-param: will be filled with colorset-entries ("itemname=color")
84     * @return error if sth went wrong
85     */
86
87    GB_ERROR  error    = NULp;
88    char     *colorset = GBT_read_string(gb_colorset, "color_set");
89    if (!colorset) {
90        error = GB_have_error() ? GB_await_error() : "Missing 'color_set' entry";
91        error = GBS_global_string("Failed to read colorset (Reason: %s)", error);
92    }
93    else {
94        GBT_splitNdestroy_string(colorsetDefs, colorset, ';');
95    }
96    return error;
97}
98
99GB_ERROR GBT_save_colorset(GBDATA *gb_colorset, CharPtrArray& colorsetDefs) {
100    /*! saves a colorset
101     * @param gb_colorset result of GBT_find_colorset() or GBT_create_colorset()
102     * @param colorsetDefs contains colorset-entries ("itemname=color")
103     * @return error if sth went wrong
104     */
105    GBS_strstruct buffer(colorsetDefs.size()*(8+1+2+1));
106    for (size_t d = 0; d<colorsetDefs.size(); ++d) {
107        buffer.cat(colorsetDefs[d]);
108        buffer.put(';');
109    }
110    buffer.cut_tail(1); // remove trailing ';'
111
112    return GBT_write_string(gb_colorset, "color_set", buffer.get_data());
113}
114
115
Note: See TracBrowser for help on using the repository browser.