File: list-scanner.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 (223 lines) | stat: -rw-r--r-- 5,251 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2016 Balabit
 *
 * 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 "scanner/list-scanner/list-scanner.h"
#include "str-repr/encode.h"
#include "str-repr/decode.h"
#include "str-utils.h"

#include <string.h>
#include <stdarg.h>

static void
_free_input(ListScanner *self)
{
  if (self->argv)
    {
      if (self->free_argv_payload)
        g_ptr_array_foreach(self->argv_buffer, (GFunc) g_free, NULL);
    }
  g_ptr_array_set_size(self->argv_buffer, 0);
}

static void
_store_input(ListScanner *self, gint argc, gchar **argv, gboolean free_argv)
{
  self->argc = argc;
  self->argv = (gchar **) self->argv_buffer->pdata;
  self->free_argv_payload = free_argv;
  self->current_arg_ndx = 0;
  self->current_arg = self->argv[self->current_arg_ndx];
}

void
list_scanner_input_va(ListScanner *self, const gchar *arg1, ...)
{
  va_list va;
  gint i;
  const char *arg;

  _free_input(self);

  va_start(va, arg1);
  for (i = 0, arg = arg1; arg; i++, arg = va_arg(va, const gchar *))
    {
      g_ptr_array_add(self->argv_buffer, g_strdup(arg));
    }
  g_ptr_array_add(self->argv_buffer, NULL);
  va_end(va);
  _store_input(self, i, (gchar **) self->argv_buffer->pdata, TRUE);
}

void
list_scanner_input_gstring_array(ListScanner *self, gint argc, GString *const *argv)
{
  gint i;

  for (i = 0; i < argc; i++)
    {
      g_ptr_array_add(self->argv_buffer, argv[i]->str);
    }
  g_ptr_array_add(self->argv_buffer, NULL);
  _store_input(self, argc, (gchar **) self->argv_buffer->pdata, FALSE);
}

void
list_scanner_input_string(ListScanner *self, const gchar *value, gssize value_len)
{
  _free_input(self);
  if (value_len < 0)
    value_len = strlen(value);
  g_ptr_array_add(self->argv_buffer, g_strndup(value, value_len));
  g_ptr_array_add(self->argv_buffer, NULL);
  _store_input(self, 1, (gchar **) self->argv_buffer->pdata, TRUE);
}

static gboolean
_is_finished_with_args(ListScanner *self)
{
  return self->current_arg_ndx >= self->argc;
}

static gboolean
_parse_value_from_current_position(ListScanner *self)
{
  StrReprDecodeOptions options =
  {
    0,
    .delimiter_chars = ",",
  };
  const gchar *end;

  /* NOTE: we always try to parse elements, even if the string is malformed,
   * otherwise we would be losing data.  Prefer to have data in an
   * incorrectly formatted way, than no data at all. */
  if (!str_repr_decode_with_options(self->value, self->current_arg, &end, &options))
    {
      g_string_truncate(self->value, 0);
      g_string_append_len(self->value, self->current_arg, end - self->current_arg);
    }
  self->current_arg = end;

  return TRUE;
}

static gboolean
_skip_if_current_arg_is_empty(ListScanner *self)
{
  if (self->current_arg[0] == 0)
    {
      self->current_arg_ndx++;
      self->current_arg = self->argv[self->current_arg_ndx];
      return TRUE;
    }
  return FALSE;
}

static gboolean
_skip_unquoted_empty_value(ListScanner *self)
{
  if (*self->current_arg == ',')
    {
      self->current_arg = self->current_arg + 1;
      return TRUE;
    }
  return FALSE;
}

static void
_skip_empty_values(ListScanner *self)
{
  while (!_is_finished_with_args(self))
    {
      if (!_skip_if_current_arg_is_empty(self) &&
          !_skip_unquoted_empty_value(self))
        break;
    }
  return;
}

gboolean
list_scanner_scan_next(ListScanner *self)
{
  g_string_truncate(self->value, 0);

  _skip_empty_values(self);

  if (!_is_finished_with_args(self) &&
      _parse_value_from_current_position(self))
    return TRUE;

  return FALSE;
}

void
list_scanner_skip_n(ListScanner *self, gint n)
{
  gint count = 0;
  while (++count <= n && list_scanner_scan_next(self))
    ;
}

const gchar *
list_scanner_get_current_value(ListScanner *self)
{
  return self->value->str;
}

gsize
list_scanner_get_current_value_len(ListScanner *self)
{
  return self->value->len;
}

void
list_scanner_init(ListScanner *self)
{
  memset(self, 0, sizeof(*self));
  self->value = g_string_sized_new(32);
  self->argv_buffer = g_ptr_array_new();
}

void
list_scanner_deinit(ListScanner *self)
{
  _free_input(self);
  g_string_free(self->value, TRUE);
  g_ptr_array_free(self->argv_buffer, TRUE);
}

ListScanner *
list_scanner_new(void)
{
  ListScanner *self = g_new0(ListScanner, 1);

  list_scanner_init(self);
  return self;
}

void
list_scanner_free(ListScanner *self)
{
  list_scanner_deinit(self);
  g_free(self);
}