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
30 #include "gatresult.h"
32 void g_at_result_iter_init(GAtResultIter *iter, GAtResult *result)
34 iter->result = result;
35 iter->pre.next = result->lines;
36 iter->pre.data = NULL;
41 gboolean g_at_result_iter_next(GAtResultIter *iter, const char *prefix)
44 int prefix_len = prefix ? strlen(prefix) : 0;
46 while ((iter->l = iter->l->next)) {
49 if (prefix_len == 0) {
54 if (g_str_has_prefix(line, prefix) == FALSE)
57 iter->line_pos = prefix_len;
59 while (iter->line_pos < strlen(line) &&
60 line[iter->line_pos] == ' ')
69 const char *g_at_result_iter_raw_line(GAtResultIter *iter)
81 line += iter->line_pos;
86 static inline int skip_to_next_field(const char *line, int pos, int len)
88 if (pos < len && line[pos] == ',')
91 while (pos < len && line[pos] == ' ')
97 gboolean g_at_result_iter_next_string(GAtResultIter *iter, const char **str)
110 line = iter->l->data;
113 pos = iter->line_pos;
116 if (line[pos] == ',') {
118 memset(iter->buf, 0, sizeof(iter->buf));
122 if (line[pos++] != '"')
127 while (end < len && line[end] != '"')
130 if (line[end] != '"')
133 if (end - pos >= sizeof(iter->buf))
136 strncpy(iter->buf, line+pos, end-pos);
137 memset(iter->buf + end - pos, 0, sizeof(iter->buf) - end + pos);
143 iter->line_pos = skip_to_next_field(line, end, len);
151 gboolean g_at_result_iter_next_number(GAtResultIter *iter, gint *number)
165 line = iter->l->data;
168 pos = iter->line_pos;
171 while (line[end] >= '0' && line[end] <= '9') {
172 value = value * 10 + (int)(line[end] - '0');
179 iter->line_pos = skip_to_next_field(line, end, len);
187 gboolean g_at_result_iter_next_range(GAtResultIter *iter, gint *min, gint *max)
202 line = iter->l->data;
205 pos = iter->line_pos;
207 while (pos < len && line[pos] == ' ')
212 while (line[end] >= '0' && line[end] <= '9') {
213 low = low * 10 + (int)(line[end] - '0');
220 if (line[end] == ',') {
225 if (line[end] == '-')
230 while (line[end] >= '0' && line[end] <= '9') {
231 high = high * 10 + (int)(line[end] - '0');
239 iter->line_pos = skip_to_next_field(line, end, len);
250 static gint skip_until(const char *line, int start, const char delim)
252 int len = strlen(line);
256 if (line[i] == delim)
259 if (line[i] != '(') {
264 i = skip_until(line, i+1, ')');
273 gboolean g_at_result_iter_skip_next(GAtResultIter *iter)
275 unsigned int skipped_to;
284 line = iter->l->data;
286 skipped_to = skip_until(line, iter->line_pos, ',');
288 if (skipped_to == iter->line_pos && line[skipped_to] != ',')
291 iter->line_pos = skip_to_next_field(line, skipped_to, strlen(line));
296 gboolean g_at_result_iter_open_list(GAtResultIter *iter)
307 line = iter->l->data;
310 if (iter->line_pos >= len)
313 if (line[iter->line_pos] != '(')
318 while (iter->line_pos < strlen(line) &&
319 line[iter->line_pos] == ' ')
325 gboolean g_at_result_iter_close_list(GAtResultIter *iter)
336 line = iter->l->data;
339 if (iter->line_pos >= len)
342 if (line[iter->line_pos] != ')')
347 iter->line_pos = skip_to_next_field(line, iter->line_pos, len);
352 const char *g_at_result_final_response(GAtResult *result)
357 return result->final_or_pdu;
360 const char *g_at_result_pdu(GAtResult *result)
365 return result->final_or_pdu;
368 gint g_at_result_num_response_lines(GAtResult *result)
376 return g_slist_length(result->lines);