File: object-list-interface.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (287 lines) | stat: -rw-r--r-- 8,040 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (c) 2024 Attila Szakacs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "filterx/object-list-interface.h"
#include "filterx/object-primitive.h"
#include "filterx/object-json.h"

FilterXObject *
filterx_list_get_subscript(FilterXObject *s, gint64 index)
{
  FilterXObject *index_obj = filterx_integer_new(index);
  FilterXObject *result = filterx_object_get_subscript(s, index_obj);

  filterx_object_unref(index_obj);
  return result;
}

gboolean
filterx_list_set_subscript(FilterXObject *s, gint64 index, FilterXObject **new_value)
{
  FilterXObject *index_obj = filterx_integer_new(index);
  gboolean result = filterx_object_set_subscript(s, index_obj, new_value);

  filterx_object_unref(index_obj);
  return result;
}

gboolean
filterx_list_append(FilterXObject *s, FilterXObject **new_value)
{
  return filterx_object_set_subscript(s, NULL, new_value);
}

gboolean
filterx_list_unset_index(FilterXObject *s, gint64 index)
{
  FilterXObject *index_obj = filterx_integer_new(index);
  gboolean result = filterx_object_unset_key(s, index_obj);

  filterx_object_unref(index_obj);
  return result;
}

static gboolean
_len(FilterXObject *s, guint64 *len)
{
  FilterXList *self = (FilterXList *) s;
  *len = self->len(self);
  return TRUE;
}

static gboolean
_normalize_index(FilterXList *self, gint64 index, guint64 *normalized_index, const gchar **error)
{
  guint64 len = self->len(self);

  if (index >= 0)
    {
      if (index >= len)
        {
          *error = "Index out of range";
          return FALSE;
        }

      *normalized_index = (guint64) index;
      return TRUE;
    }

  if (len > G_MAXINT64)
    {
      *error = "Index exceeds maximal supported value";
      return FALSE;
    }

  *normalized_index = len + index;
  if (*normalized_index < 0)
    {
      *error = "Index out of range";
      return FALSE;
    }

  return TRUE;
}

static FilterXObject *
_get_subscript(FilterXObject *s, FilterXObject *key)
{
  FilterXList *self = (FilterXList *) s;

  gint64 index;
  if (!filterx_integer_unwrap(key, &index))
    {
      msg_error("FilterX: Failed to get element from list",
                evt_tag_str("error", "Index must be integer"),
                evt_tag_str("index_type", key->type->name));
      return NULL;
    }

  guint64 normalized_index;
  const gchar *error;
  if (!_normalize_index(self, index, &normalized_index, &error))
    {
      msg_error("FilterX: Failed to get element from list",
                evt_tag_str("error", error),
                evt_tag_printf("index", "%" G_GINT64_FORMAT, index),
                evt_tag_printf("len", "%" G_GUINT64_FORMAT, self->len(self)));
      return NULL;
    }

  return self->get_subscript(self, normalized_index);
}

static gboolean
_set_subscript(FilterXObject *s, FilterXObject *key, FilterXObject **new_value)
{
  FilterXList *self = (FilterXList *) s;

  if (!key)
    return self->append(self, new_value);

  gint64 index;
  if (!filterx_integer_unwrap(key, &index))
    {
      msg_error("FilterX: Failed to set element of list",
                evt_tag_str("error", "Index must be integer"),
                evt_tag_str("index_type", key->type->name));
      return FALSE;
    }

  guint64 normalized_index;
  const gchar *error;
  if (!_normalize_index(self, index, &normalized_index, &error))
    {
      msg_error("FilterX: Failed to set element of list",
                evt_tag_str("error", error),
                evt_tag_printf("index", "%" G_GINT64_FORMAT, index),
                evt_tag_printf("len", "%" G_GUINT64_FORMAT, self->len(self)));
      return FALSE;
    }

  return self->set_subscript(self, normalized_index, new_value);
}

static gboolean
_is_key_set(FilterXObject *s, FilterXObject *key)
{
  FilterXList *self = (FilterXList *) s;

  if (!key)
    {
      msg_error("FilterX: Failed to check index of list",
                evt_tag_str("error", "Index must be set"));
      return FALSE;
    }

  gint64 index;
  if (!filterx_integer_unwrap(key, &index))
    {
      msg_error("FilterX: Failed to check index of list",
                evt_tag_str("error", "Index must be integer"),
                evt_tag_str("index_type", key->type->name));
      return FALSE;
    }

  guint64 normalized_index;
  const gchar *error;
  return _normalize_index(self, index, &normalized_index, &error);
}

static gboolean
_unset_key(FilterXObject *s, FilterXObject *key)
{
  FilterXList *self = (FilterXList *) s;

  if (!key)
    {
      msg_error("FilterX: Failed to unset element of list",
                evt_tag_str("error", "Index must be set"));
      return FALSE;
    }

  gint64 index;
  if (!filterx_integer_unwrap(key, &index))
    {
      msg_error("FilterX: Failed to unset element of list",
                evt_tag_str("error", "Index must be integer"),
                evt_tag_str("index_type", key->type->name));
      return FALSE;
    }

  guint64 normalized_index;
  const gchar *error;
  if (!_normalize_index(self, index, &normalized_index, &error))
    {
      msg_error("FilterX: Failed to unset element of list",
                evt_tag_str("error", error),
                evt_tag_printf("index", "%" G_GINT64_FORMAT, index),
                evt_tag_printf("len", "%" G_GUINT64_FORMAT, self->len(self)));
      return FALSE;
    }

  return self->unset_index(self, normalized_index);
}

static gboolean
_map_to_json(FilterXObject *s, struct json_object **object, FilterXObject **assoc_object)
{
  *object = json_object_new_array();

  guint64 len;
  g_assert(filterx_object_len(s, &len));

  for (guint64 i = 0; i < len; i++)
    {
      FilterXObject *value_obj = filterx_list_get_subscript(s, (gint64) MIN(i, G_MAXINT64));

      struct json_object *value;
      FilterXObject *elem_assoc_object = NULL;
      if (!filterx_object_map_to_json(value_obj, &value, &elem_assoc_object))
        {
          filterx_object_unref(value_obj);
          goto error;
        }

      filterx_json_associate_cached_object(*object, elem_assoc_object);

      filterx_object_unref(elem_assoc_object);
      filterx_object_unref(value_obj);

      if (json_object_array_add(*object, value) != 0)
        {
          json_object_put(value);
          goto error;
        }
    }

  *assoc_object = filterx_json_new_from_object(json_object_get(*object));
  return TRUE;

error:
  json_object_put(*object);
  *object = NULL;
  return FALSE;
}

void
filterx_list_init_instance(FilterXList *self, FilterXType *type)
{
  g_assert(type->is_mutable);
  g_assert(type->len == _len);
  g_assert(type->get_subscript == _get_subscript);
  g_assert(type->set_subscript == _set_subscript);
  g_assert(type->is_key_set == _is_key_set);
  g_assert(type->unset_key == _unset_key);

  filterx_object_init_instance(&self->super, type);
}

FILTERX_DEFINE_TYPE(list, FILTERX_TYPE_NAME(object),
                    .is_mutable = TRUE,
                    .len = _len,
                    .get_subscript = _get_subscript,
                    .set_subscript = _set_subscript,
                    .is_key_set = _is_key_set,
                    .unset_key = _unset_key,
                    .map_to_json = _map_to_json,
                   );