Add hex-encoded character string getter to AtChat
authorAndrzej Zaborowski <andrew.zaborowski@intel.com>
Fri, 22 May 2009 11:58:49 +0000 (13:58 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Tue, 9 Jun 2009 05:37:46 +0000 (07:37 +0200)
gatchat/gatresult.c
gatchat/gatresult.h

index 87457f1..8da8821 100644 (file)
@@ -24,6 +24,7 @@
 #endif
 
 #include <string.h>
+#include <stdio.h>
 
 #include <glib.h>
 
@@ -148,6 +149,58 @@ out:
        return TRUE;
 }
 
+gboolean g_at_result_iter_next_hexstring(GAtResultIter *iter,
+               const guint8 **str, gint *length)
+{
+       unsigned int pos;
+       unsigned int end;
+       unsigned int len;
+       char *line;
+       char *bufpos;
+
+       if (!iter)
+               return FALSE;
+
+       if (!iter->l)
+               return FALSE;
+
+       line = iter->l->data;
+       len = strlen(line);
+
+       pos = iter->line_pos;
+
+       /* Omitted string */
+       if (line[pos] == ',') {
+               end = pos;
+               memset(iter->buf, 0, sizeof(iter->buf));
+               goto out;
+       }
+
+       end = pos;
+
+       while (end < len && g_ascii_isxdigit(line[end]))
+               end += 1;
+
+       if ((end - pos) & 1)
+               return FALSE;
+
+       if ((end - pos) / 2 >= sizeof(iter->buf))
+               return FALSE;
+       *length = (end - pos) / 2;
+
+       for (bufpos = iter->buf; pos < end; pos += 2)
+               sscanf(line + pos, "%02hhx", bufpos++);
+       memset(bufpos, 0, sizeof(iter->buf) - (bufpos - iter->buf));
+
+out:
+       iter->line_pos = skip_to_next_field(line, end, len);
+
+       if (str)
+               *str = (guint8 *) iter->buf;
+
+       return TRUE;
+}
+
 gboolean g_at_result_iter_next_number(GAtResultIter *iter, gint *number)
 {
        int pos;
index 8259e7c..cbe6d05 100644 (file)
@@ -54,6 +54,8 @@ gboolean g_at_result_iter_skip_next(GAtResultIter *iter);
 gboolean g_at_result_iter_next_range(GAtResultIter *iter, gint *min, gint *max);
 gboolean g_at_result_iter_next_string(GAtResultIter *iter, const char **str);
 gboolean g_at_result_iter_next_number(GAtResultIter *iter, gint *number);
+gboolean g_at_result_iter_next_hexstring(GAtResultIter *iter,
+               const guint8 **str, gint *length);
 
 const char *g_at_result_iter_raw_line(GAtResultIter *iter);