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;
48 while ((iter->l = iter->l->next)) {
50 linelen = strlen(line);
52 if (linelen > G_AT_RESULT_LINE_LENGTH_MAX)
55 if (prefix_len == 0) {
60 if (g_str_has_prefix(line, prefix) == FALSE)
63 iter->line_pos = prefix_len;
65 while (iter->line_pos < strlen(line) &&
66 line[iter->line_pos] == ' ')
75 /* Already checked the length to be no more than buflen */
76 strcpy(iter->buf, line);
80 const char *g_at_result_iter_raw_line(GAtResultIter *iter)
92 line += iter->line_pos;
97 static inline int skip_to_next_field(const char *line, int pos, int len)
99 if (pos < len && line[pos] == ',')
102 while (pos < len && line[pos] == ' ')
108 gboolean g_at_result_iter_next_unquoted_string(GAtResultIter *iter,
122 line = iter->l->data;
125 pos = iter->line_pos;
128 if (line[pos] == ',') {
130 iter->buf[pos] = '\0';
134 if (line[pos] == '"' || line[pos] == ')')
139 while (end < len && line[end] != ',' && line[end] != ')')
142 iter->buf[end] = '\0';
145 iter->line_pos = skip_to_next_field(line, end, len);
148 *str = iter->buf + pos;
153 gboolean g_at_result_iter_next_string(GAtResultIter *iter, const char **str)
166 line = iter->l->data;
169 pos = iter->line_pos;
172 if (line[pos] == ',') {
174 iter->buf[pos] = '\0';
178 if (line[pos++] != '"')
183 while (end < len && line[end] != '"')
186 if (line[end] != '"')
189 iter->buf[end] = '\0';
195 iter->line_pos = skip_to_next_field(line, end, len);
198 *str = iter->buf + pos;
203 gboolean g_at_result_iter_next_hexstring(GAtResultIter *iter,
204 const guint8 **str, gint *length)
218 line = iter->l->data;
221 pos = iter->line_pos;
222 bufpos = iter->buf + pos;
225 if (line[pos] == ',') {
227 iter->buf[pos] = '\0';
231 if (line[pos] == '"')
236 while (end < len && g_ascii_isxdigit(line[end]))
242 *length = (end - pos) / 2;
244 for (; pos < end; pos += 2)
245 sscanf(line + pos, "%02hhx", bufpos++);
247 if (line[end] == '"')
251 iter->line_pos = skip_to_next_field(line, end, len);
254 *str = (guint8 *) bufpos - *length;
259 gboolean g_at_result_iter_next_number(GAtResultIter *iter, gint *number)
273 line = iter->l->data;
276 pos = iter->line_pos;
279 while (line[end] >= '0' && line[end] <= '9') {
280 value = value * 10 + (int)(line[end] - '0');
287 iter->line_pos = skip_to_next_field(line, end, len);
295 gboolean g_at_result_iter_next_range(GAtResultIter *iter, gint *min, gint *max)
310 line = iter->l->data;
313 pos = iter->line_pos;
315 while (pos < len && line[pos] == ' ')
320 while (line[end] >= '0' && line[end] <= '9') {
321 low = low * 10 + (int)(line[end] - '0');
328 if (line[end] != '-') {
335 while (line[end] >= '0' && line[end] <= '9') {
336 high = high * 10 + (int)(line[end] - '0');
344 iter->line_pos = skip_to_next_field(line, end, len);
355 static gint skip_until(const char *line, int start, const char delim)
357 int len = strlen(line);
361 if (line[i] == delim)
364 if (line[i] != '(') {
369 i = skip_until(line, i+1, ')');
378 gboolean g_at_result_iter_skip_next(GAtResultIter *iter)
380 unsigned int skipped_to;
389 line = iter->l->data;
391 skipped_to = skip_until(line, iter->line_pos, ',');
393 if (skipped_to == iter->line_pos && line[skipped_to] != ',')
396 iter->line_pos = skip_to_next_field(line, skipped_to, strlen(line));
401 gboolean g_at_result_iter_open_list(GAtResultIter *iter)
412 line = iter->l->data;
415 if (iter->line_pos >= len)
418 if (line[iter->line_pos] != '(')
423 while (iter->line_pos < strlen(line) &&
424 line[iter->line_pos] == ' ')
430 gboolean g_at_result_iter_close_list(GAtResultIter *iter)
441 line = iter->l->data;
444 if (iter->line_pos >= len)
447 if (line[iter->line_pos] != ')')
452 iter->line_pos = skip_to_next_field(line, iter->line_pos, len);
457 const char *g_at_result_final_response(GAtResult *result)
462 return result->final_or_pdu;
465 const char *g_at_result_pdu(GAtResult *result)
470 return result->final_or_pdu;
473 gint g_at_result_num_response_lines(GAtResult *result)
481 return g_slist_length(result->lines);