Move the string parser with regexp from huawei plugin to generic utility function.

It's useful for other modems too that need to strip unsolicited messages from
responses.
This commit is contained in:
Tambet Ingo
2008-10-24 16:20:22 +03:00
parent 0bd4f4a604
commit f82b187c2b
4 changed files with 80 additions and 53 deletions

View File

@@ -7,6 +7,7 @@
#include "mm-modem-gsm-network.h" #include "mm-modem-gsm-network.h"
#include "mm-errors.h" #include "mm-errors.h"
#include "mm-callback-info.h" #include "mm-callback-info.h"
#include "mm-util.h"
#include "mm-serial-parsers.h" #include "mm-serial-parsers.h"
static gpointer mm_modem_huawei_parent_class = NULL; static gpointer mm_modem_huawei_parent_class = NULL;
@@ -406,63 +407,13 @@ reg_state_changed (const char *str, gpointer data)
mm_generic_gsm_set_reg_status (MM_GENERIC_GSM (data), status); mm_generic_gsm_set_reg_status (MM_GENERIC_GSM (data), status);
} }
static gboolean
remove_eval_cb (const GMatchInfo *match_info,
GString *result,
gpointer user_data)
{
int *result_len = (int *) user_data;
int start;
int end;
if (g_match_info_fetch_pos (match_info, 0, &start, &end))
*result_len -= (end - start);
return FALSE;
}
typedef void (*HuaweiStripFn) (const char *str, gpointer data);
static void
huawei_strip (GRegex *r, GString *string, HuaweiStripFn fn, gpointer data)
{
GMatchInfo *match_info;
gboolean matches;
char *str;
matches = g_regex_match_full (r, string->str, string->len, 0, 0, &match_info, NULL);
if (fn) {
while (g_match_info_matches (match_info)) {
str = g_match_info_fetch (match_info, 1);
fn (str, data);
g_free (str);
g_match_info_next (match_info, NULL);
}
}
g_match_info_free (match_info);
if (matches) {
/* Remove matches */
int result_len = string->len;
str = g_regex_replace_eval (r, string->str, string->len, 0, 0,
remove_eval_cb, &result_len, NULL);
g_string_truncate (string, 0);
g_string_append_len (string, str, result_len);
g_free (str);
}
}
static gboolean static gboolean
huawei_parse_response (gpointer data, GString *response, GError **error) huawei_parse_response (gpointer data, GString *response, GError **error)
{ {
MMModemHuaweiPrivate *priv = MM_MODEM_HUAWEI_GET_PRIVATE (data); MMModemHuaweiPrivate *priv = MM_MODEM_HUAWEI_GET_PRIVATE (data);
huawei_strip (priv->status_regex, response, handle_status_change, data); mm_util_strip_string (response, priv->status_regex, handle_status_change, data);
huawei_strip (priv->reg_state_regex, response, reg_state_changed, data); mm_util_strip_string (response, priv->reg_state_regex, reg_state_changed, data);
return mm_serial_parser_v1_parse (priv->std_parser, response, error); return mm_serial_parser_v1_parse (priv->std_parser, response, error);
} }

View File

@@ -36,7 +36,9 @@ modem_manager_SOURCES = \
mm-serial.c \ mm-serial.c \
mm-serial.h \ mm-serial.h \
mm-serial-parsers.c \ mm-serial-parsers.c \
mm-serial-parsers.h mm-serial-parsers.h \
mm-util.c \
mm-util.h
mm-manager-glue.h: $(top_srcdir)/introspection/mm-manager.xml mm-manager-glue.h: $(top_srcdir)/introspection/mm-manager.xml
dbus-binding-tool --prefix=mm_manager --mode=glib-server --output=$@ $< dbus-binding-tool --prefix=mm_manager --mode=glib-server --output=$@ $<

54
src/mm-util.c Normal file
View File

@@ -0,0 +1,54 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "mm-util.h"
static gboolean
remove_eval_cb (const GMatchInfo *match_info,
GString *result,
gpointer user_data)
{
int *result_len = (int *) user_data;
int start;
int end;
if (g_match_info_fetch_pos (match_info, 0, &start, &end))
*result_len -= (end - start);
return FALSE;
}
void
mm_util_strip_string (GString *string,
GRegex *regex,
MMUtilStripFn callback,
gpointer user_data)
{
GMatchInfo *match_info;
gboolean matches;
char *str;
matches = g_regex_match_full (regex, string->str, string->len, 0, 0, &match_info, NULL);
if (callback) {
while (g_match_info_matches (match_info)) {
str = g_match_info_fetch (match_info, 1);
callback (str, user_data);
g_free (str);
g_match_info_next (match_info, NULL);
}
}
g_match_info_free (match_info);
if (matches) {
/* Remove matches */
int result_len = string->len;
str = g_regex_replace_eval (regex, string->str, string->len, 0, 0,
remove_eval_cb, &result_len, NULL);
g_string_truncate (string, 0);
g_string_append_len (string, str, result_len);
g_free (str);
}
}

20
src/mm-util.h Normal file
View File

@@ -0,0 +1,20 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#ifndef MM_UTIL_H
#define MM_UTIL_H
#include <glib.h>
typedef void (*MMUtilStripFn) (const char *str,
gpointer user_data);
/* Applies the regexp on string and calls the callback (if provided)
with each match and user_data. After that, the matches are removed
from the string.
*/
void mm_util_strip_string (GString *string,
GRegex *regex,
MMUtilStripFn callback,
gpointer user_data);
#endif /* MM_UTIL_H */