core: move charset enum/string conversion to it's own file

This commit is contained in:
Dan Williams
2010-03-13 16:37:24 -08:00
parent ac7310ab10
commit 2a94d38edc
5 changed files with 112 additions and 64 deletions

View File

@@ -54,6 +54,8 @@ modem_manager_SOURCES = \
main.c \
mm-callback-info.c \
mm-callback-info.h \
mm-charsets.c \
mm-charsets.h \
$(auth_sources) \
mm-manager.c \
mm-manager.h \

71
src/mm-charsets.c Normal file
View File

@@ -0,0 +1,71 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "mm-charsets.h"
typedef struct {
const char *name;
MMModemCharset charset;
} CharsetEntry;
static CharsetEntry charset_map[] = {
{ "UTF-8", MM_MODEM_CHARSET_UTF8 },
{ "UCS2", MM_MODEM_CHARSET_UCS2 },
{ "IRA", MM_MODEM_CHARSET_IRA },
{ "GSM", MM_MODEM_CHARSET_GSM },
{ "8859-1", MM_MODEM_CHARSET_8859_1 },
{ "PCCP437", MM_MODEM_CHARSET_PCCP437 },
{ "PCDN", MM_MODEM_CHARSET_PCDN },
{ "HEX", MM_MODEM_CHARSET_HEX },
{ NULL, MM_MODEM_CHARSET_UNKNOWN }
};
const char *
mm_modem_charset_to_string (MMModemCharset charset)
{
CharsetEntry *iter = &charset_map[0];
g_return_val_if_fail (charset != MM_MODEM_CHARSET_UNKNOWN, NULL);
while (iter->name) {
if (iter->charset == charset)
return iter->name;
iter++;
}
g_warn_if_reached ();
return NULL;
}
MMModemCharset
mm_modem_charset_from_string (const char *string)
{
CharsetEntry *iter = &charset_map[0];
g_return_val_if_fail (string != NULL, MM_MODEM_CHARSET_UNKNOWN);
while (iter->name) {
if (strcasestr (string, iter->name))
return iter->charset;
iter++;
}
return MM_MODEM_CHARSET_UNKNOWN;
}

38
src/mm-charsets.h Normal file
View File

@@ -0,0 +1,38 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#ifndef MM_CHARSETS_H
#define MM_CHARSETS_H
#include <glib.h>
typedef enum {
MM_MODEM_CHARSET_UNKNOWN = 0x00000000,
MM_MODEM_CHARSET_GSM = 0x00000001,
MM_MODEM_CHARSET_IRA = 0x00000002,
MM_MODEM_CHARSET_8859_1 = 0x00000004,
MM_MODEM_CHARSET_UTF8 = 0x00000008,
MM_MODEM_CHARSET_UCS2 = 0x00000010,
MM_MODEM_CHARSET_PCCP437 = 0x00000020,
MM_MODEM_CHARSET_PCDN = 0x00000040,
MM_MODEM_CHARSET_HEX = 0x00000080
} MMModemCharset;
const char *mm_modem_charset_to_string (MMModemCharset charset);
MMModemCharset mm_modem_charset_from_string (const char *string);
#endif /* MM_CHARSETS_H */

View File

@@ -514,54 +514,6 @@ mm_modem_set_charset (MMModem *self,
}
}
typedef struct {
const char *name;
MMModemCharset charset;
} CharsetEntry;
static CharsetEntry charset_map[] = {
{ "UTF-8", MM_MODEM_CHARSET_UTF8 },
{ "UCS2", MM_MODEM_CHARSET_UCS2 },
{ "IRA", MM_MODEM_CHARSET_IRA },
{ "GSM", MM_MODEM_CHARSET_GSM },
{ "8859-1", MM_MODEM_CHARSET_8859_1 },
{ "PCCP437", MM_MODEM_CHARSET_PCCP437 },
{ "PCDN", MM_MODEM_CHARSET_PCDN },
{ "HEX", MM_MODEM_CHARSET_HEX },
{ NULL, MM_MODEM_CHARSET_UNKNOWN }
};
const char *
mm_modem_charset_to_string (MMModemCharset charset)
{
CharsetEntry *iter = &charset_map[0];
g_return_val_if_fail (charset != MM_MODEM_CHARSET_UNKNOWN, NULL);
while (iter->name) {
if (iter->charset == charset)
return iter->name;
iter++;
}
g_warn_if_reached ();
return NULL;
}
MMModemCharset
mm_modem_charset_from_string (const char *string)
{
CharsetEntry *iter = &charset_map[0];
g_return_val_if_fail (string != NULL, MM_MODEM_CHARSET_UNKNOWN);
while (iter->name) {
if (strcasestr (string, iter->name))
return iter->charset;
iter++;
}
return MM_MODEM_CHARSET_UNKNOWN;
}
/*****************************************************************************/
gboolean

View File

@@ -22,6 +22,7 @@
#include "mm-port.h"
#include "mm-auth-provider.h"
#include "mm-charsets.h"
typedef enum {
MM_MODEM_STATE_UNKNOWN = 0,
@@ -42,18 +43,6 @@ typedef enum {
MM_MODEM_STATE_REASON_NONE = 0
} MMModemStateReason;
typedef enum {
MM_MODEM_CHARSET_UNKNOWN = 0x00000000,
MM_MODEM_CHARSET_GSM = 0x00000001,
MM_MODEM_CHARSET_IRA = 0x00000002,
MM_MODEM_CHARSET_8859_1 = 0x00000004,
MM_MODEM_CHARSET_UTF8 = 0x00000008,
MM_MODEM_CHARSET_UCS2 = 0x00000010,
MM_MODEM_CHARSET_PCCP437 = 0x00000020,
MM_MODEM_CHARSET_PCDN = 0x00000040,
MM_MODEM_CHARSET_HEX = 0x00000080
} MMModemCharset;
#define DBUS_PATH_TAG "dbus-path"
#define MM_TYPE_MODEM (mm_modem_get_type ())
@@ -253,10 +242,6 @@ void mm_modem_set_charset (MMModem *self,
MMModemFn callback,
gpointer user_data);
const char *mm_modem_charset_to_string (MMModemCharset charset);
MMModemCharset mm_modem_charset_from_string (const char *string);
gboolean mm_modem_get_valid (MMModem *self);
char *mm_modem_get_device (MMModem *self);