summaryrefslogtreecommitdiff
path: root/src/cstore/cstore-c.cpp
blob: fd818a5f960dc8040999450e32ddb91028136bf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2010 Vyatta, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <cstring>
#include <vector>
#include <string>

#include <cstore/cstore-c.h>
#include <cstore/unionfs/cstore-unionfs.hpp>

static void
_get_str_vec(vector<string>& vec, const char *strs[], int num_strs)
{
  for (int i = 0; i < num_strs; i++) {
    vec.push_back(strs[i]);
  }
}

void *
cstore_init(void)
{
  Cstore *handle = new UnionfsCstore();
  return (void *) handle;
}

void
cstore_free(void *handle)
{
  UnionfsCstore *h = (UnionfsCstore *) handle;
  delete h;
}

int
cstore_validate_tmpl_path(void *handle, const char *path_comps[],
                          int num_comps, int validate_tags)
{
  if (handle) {
    vector<string> vs;
    _get_str_vec(vs, path_comps, num_comps);
    Cstore *cs = (Cstore *) handle;
    return (cs->validateTmplPath(vs, validate_tags) ? 1 : 0);
  }
  return 0;
}

int
cstore_validate_tmpl_path_d(void *handle, const char *path_comps[],
                            int num_comps, int validate_tags, vtw_def *def)
{
  if (handle) {
    vector<string> vs;
    _get_str_vec(vs, path_comps, num_comps);
    Cstore *cs = (Cstore *) handle;
    return (cs->validateTmplPath(vs, validate_tags, *def) ? 1 : 0);
  }
  return 0;
}

int
cstore_cfg_path_exists(void *handle, const char *path_comps[], int num_comps)
{
  if (handle) {
    vector<string> vs;
    _get_str_vec(vs, path_comps, num_comps);
    Cstore *cs = (Cstore *) handle;
    return (cs->cfgPathExists(vs) ? 1 : 0);
  }
  return 0;
}

int
cstore_get_var_ref(void *handle, const char *ref_str, vtw_type_e *type,
                   char **val, int from_active)
{
  if (handle) {
    Cstore *cs = (Cstore *) handle;
    *val = cs->getVarRef(ref_str, *type, from_active);
    return (*val ? 1 : 0);
  }
  return 0;
}

int
cstore_set_var_ref(void *handle, const char *ref_str, const char *value,
                   int to_active)
{
  if (handle) {
    Cstore *cs = (Cstore *) handle;
    return (cs->setVarRef(ref_str, value, to_active) ? 1 : 0);
  }
  return 0;
}

int
cstore_cfg_path_deactivated(void *handle, const char *path_comps[],
                            int num_comps, int in_active)
{
  if (handle) {
    vector<string> vs;
    _get_str_vec(vs, path_comps, num_comps);
    Cstore *cs = (Cstore *) handle;
    return (cs->cfgPathDeactivated(vs, in_active) ? 1 : 0);
  }
  return 0;
}

char *
cstore_cfg_path_get_effective_value(void *handle, const char *path_comps[],
                                    int num_comps)
{
  if (handle) {
    vector<string> vs;
    _get_str_vec(vs, path_comps, num_comps);
    Cstore *cs = (Cstore *) handle;
    string val;
    if (!cs->cfgPathGetEffectiveValue(vs, val)) {
      return NULL;
    }

    int vsize = val.length();
    char *buf = (char *) malloc(vsize + 1);
    if (!buf) {
      return NULL;
    }
    strncpy(buf, val.c_str(), vsize);
    buf[vsize] = 0;

    return buf;
  }
  return NULL;
}

char **
cstore_path_string_to_path_comps(const char *path_str, int *num_comps)
{
  char *pstr = strdup(path_str);
  size_t len = strlen(pstr);
  vector<string> vec;
  char *start = NULL;
  for (size_t i = 0; i < len; i++) {
    if (pstr[i] == '/') {
      if (start) {
        pstr[i] = 0;
        vec.push_back(start);
        pstr[i] = '/';
        start = NULL;
      }
      continue;
    } else if (!start) {
      start = &(pstr[i]);
    }
  }
  if (start) {
    vec.push_back(start);
  }
  char **ret = (char **) malloc(sizeof(char *) * vec.size());
  if (ret) {
    for (size_t i = 0; i < vec.size(); i++) {
      ret[i] = strdup(vec[i].c_str());
    }
    *num_comps = vec.size();
  }
  free(pstr);
  return ret;
}

void
cstore_free_path_comps(char **path_comps, int num_comps)
{
  for (int i = 0; i < num_comps; i++) {
    free(path_comps[i]);
  }
  free(path_comps);
}