3 * AT chat library with GLib integration
5 * Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "gatresult.h"
33 void g_at_result_iter_init(GAtResultIter *iter, GAtResult *result)
35 iter->result = result;
36 iter->pre.next = result->lines;
37 iter->pre.data = NULL;
42 gboolean g_at_result_iter_next(GAtResultIter *iter, const char *prefix)
45 int prefix_len = prefix ? strlen(prefix) : 0;
47 while ((iter->l = iter->l->next)) {
50 if (prefix_len == 0) {
55 if (g_str_has_prefix(line, prefix) == FALSE)
58 iter->line_pos = prefix_len;
60 while (iter->line_pos < strlen(line) &&
61 line[iter->line_pos] == ' ')
70 const char *g_at_result_iter_raw_line(GAtResultIter *iter)
82 line += iter->line_pos;
87 static inline int skip_to_next_field(const char *line, int pos, int len)
89 if (pos < len && line[pos] == ',')
92 while (pos < len && line[pos] == ' ')
98 gboolean g_at_result_iter_next_string(GAtResultIter *iter, const char **str)
111 line = iter->l->data;
114 pos = iter->line_pos;
117 if (line[pos] == ',') {
119 memset(iter->buf, 0, sizeof(iter->buf));
123 if (line[pos++] != '"')
128 while (end < len && line[end] != '"')
131 if (line[end] != '"')
134 if (end - pos >= sizeof(iter->buf))
137 strncpy(iter->buf, line+pos, end-pos);
138 memset(iter->buf + end - pos, 0, sizeof(iter->buf) - end + pos);
144 iter->line_pos = skip_to_next_field(line, end, len);
152 gboolean g_at_result_iter_next_hexstring(GAtResultIter *iter,
153 const guint8 **str, gint *length)
167 line = iter->l->data;
170 pos = iter->line_pos;
173 if (line[pos] == ',') {
175 memset(iter->buf, 0, sizeof(iter->buf));
181 while (end < len && g_ascii_isxdigit(line[end]))
187 if ((end - pos) / 2 >= sizeof(iter->buf))
189 *length = (end - pos) / 2;
191 for (bufpos = iter->buf; pos < end; pos += 2)
192 sscanf(line + pos, "%02hhx", bufpos++);
193 memset(bufpos, 0, sizeof(iter->buf) - (bufpos - iter->buf));
196 iter->line_pos = skip_to_next_field(line, end, len);
199 *str = (guint8 *) iter->buf;
204 gboolean g_at_result_iter_next_number(GAtResultIter *iter, gint *number)
218 line = iter->l->data;
221 pos = iter->line_pos;
224 while (line[end] >= '0' && line[end] <= '9') {
225 value = value * 10 + (int)(line[end] - '0');
232 iter->line_pos = skip_to_next_field(line, end, len);
240 gboolean g_at_result_iter_next_range(GAtResultIter *iter, gint *min, gint *max)
255 line = iter->l->data;
258 pos = iter->line_pos;
260 while (pos < len && line[pos] == ' ')
265 while (line[end] >= '0' && line[end] <= '9') {
266 low = low * 10 + (int)(line[end] - '0');
273 if (line[end] == ',') {
278 if (line[end] == '-')
283 while (line[end] >= '0' && line[end] <= '9') {
284 high = high * 10 + (int)(line[end] - '0');
292 iter->line_pos = skip_to_next_field(line, end, len);
303 static gint skip_until(const char *line, int start, const char delim)
305 int len = strlen(line);
309 if (line[i] == delim)
312 if (line[i] != '(') {
317 i = skip_until(line, i+1, ')');
326 gboolean g_at_result_iter_skip_next(GAtResultIter *iter)
328 unsigned int skipped_to;
337 line = iter->l->data;
339 skipped_to = skip_until(line, iter->line_pos, ',');
341 if (skipped_to == iter->line_pos && line[skipped_to] != ',')
344 iter->line_pos = skip_to_next_field(line, skipped_to, strlen(line));
349 gboolean g_at_result_iter_open_list(GAtResultIter *iter)
360 line = iter->l->data;
363 if (iter->line_pos >= len)
366 if (line[iter->line_pos] != '(')
371 while (iter->line_pos < strlen(line) &&
372 line[iter->line_pos] == ' ')
378 gboolean g_at_result_iter_close_list(GAtResultIter *iter)
389 line = iter->l->data;
392 if (iter->line_pos >= len)
395 if (line[iter->line_pos] != ')')
400 iter->line_pos = skip_to_next_field(line, iter->line_pos, len);
405 const char *g_at_result_final_response(GAtResult *result)
410 return result->final_or_pdu;
413 const char *g_at_result_pdu(GAtResult *result)
418 return result->final_or_pdu;
421 gint g_at_result_num_response_lines(GAtResult *result)
429 return g_slist_length(result->lines);