source: branches/stable/ARBDB/ad_colorset.cxx

Last change on this file was 18159, checked in by westram, 5 years ago
  • full update from child 'fix' into 'trunk'
    • fix item name accessors (GBT_get_name + GBT_get_name_or_description)
    • add null2empty
  • adds: log:branches/fix@18140:18158
File size: 4.8 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
16int 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    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}
31GB_ERROR GBT_set_color_group(GBDATA *gb_item, int color_group) {
32    /*! set color group of item
33     * @param gb_item the item
34     * @param color_group the color group [1 .. GB_COLOR_GROUPS] or zero to delete it.
35     * @return error if sth went wrong
36     *
37     * Please note that this function may be bypassed by user; e.g. via 'write to fields of listed'.
38     */
39
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
52GBDATA *GBT_colorset_root(GBDATA *gb_main, const char *itemsname) {
53    /*! return root container for colorsets
54     * @param gb_main database
55     * @param itemsname name of items ( == ItemSelector::items_name)
56     */
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
62void GBT_get_colorset_names(ConstStrArray& colorsetNames, GBDATA *gb_colorset_root) {
63    /*! retrieve names of existing colorsets from DB
64     * @param colorsetNames result-param: will be filled with colorset names
65     * @param gb_colorset_root result of GBT_colorset_root()
66     */
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
76GBDATA *GBT_find_colorset(GBDATA *gb_colorset_root, const char *name) {
77    /*! lookup (existing) colorset
78     * @param gb_colorset_root result of GBT_colorset_root()
79     * @param name colorset name
80     * @return colorset DB entry (or NULp, in which case an error MAY be exported)
81     */
82    return GBT_find_item_rel_item_data(gb_colorset_root, "name", name);
83}
84
85GBDATA *GBT_find_or_create_colorset(GBDATA *gb_colorset_root, const char *name) {
86    /*! create a new colorset
87     * @param gb_colorset_root result of GBT_colorset_root()
88     * @param name colorset name
89     * @return colorset DB entry (or NULp, in which case an error IS exported)
90     */
91    return GBT_find_or_create_item_rel_item_data(gb_colorset_root, "colorset", "name", name, false);
92}
93
94GB_ERROR GBT_load_colorset(GBDATA *gb_colorset, ConstStrArray& colorsetDefs) {
95    /*! load a colorset
96     * @param gb_colorset result of GBT_find_colorset()
97     * @param colorsetDefs result-param: will be filled with colorset-entries ("itemname=color")
98     * @return error if sth went wrong
99     */
100
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
113GB_ERROR GBT_save_colorset(GBDATA *gb_colorset, CharPtrArray& colorsetDefs) {
114    /*! saves a colorset
115     * @param gb_colorset result of GBT_find_colorset() or GBT_create_colorset()
116     * @param colorsetDefs contains colorset-entries ("itemname=color")
117     * @return error if sth went wrong
118     */
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
Note: See TracBrowser for help on using the repository browser.