Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

cr-enc-handler.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of version 2.1 of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00020  * USA
00021  */
00022 
00023 /*
00024  *$Id: cr-enc-handler.c,v 1.3 2003/06/12 22:07:47 dodji Exp $
00025  */
00026 
00027 /**
00028  *@file
00029  *The definition of the #CREncHandler class.
00030  */
00031 
00032 
00033 #include "cr-enc-handler.h"
00034 #include "cr-utils.h"
00035 
00036 #include <string.h>
00037 
00038 struct CREncAlias {
00039         const gchar * name ;
00040         enum CREncoding encoding ;
00041 } ;
00042 
00043 static struct CREncAlias gv_default_aliases[] =
00044 {
00045         {"UTF-8", CR_UTF_8},
00046         {"UTF_8", CR_UTF_8},
00047         {"UTF8", CR_UTF_8},
00048         {"UTF-16", CR_UTF_16},
00049         {"UTF_16", CR_UTF_16},
00050         {"UTF16", CR_UTF_16},
00051         {"UCS1", CR_UCS_1},
00052         {"UCS-1", CR_UCS_1},
00053         {"UCS_1", CR_UCS_1},
00054         {"ISO-8859-1", CR_UCS_1},
00055         {"ISO_8859-1", CR_UCS_1},
00056         {"UCS-1", CR_UCS_1},
00057         {"UCS_1", CR_UCS_1},
00058         {"UCS4", CR_UCS_4},
00059         {"UCS-4", CR_UCS_4},
00060         {"UCS_4", CR_UCS_4},
00061         {"ASCII", CR_ASCII},
00062         {0, 0}
00063 } ;
00064 
00065 
00066 static CREncHandler gv_default_enc_handlers[] =
00067 {
00068         {CR_UCS_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
00069         cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
00070 
00071         {CR_ISO_8859_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
00072         cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
00073 
00074         {CR_ASCII, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
00075          cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
00076 
00077         {0, NULL, NULL, NULL, NULL}
00078 } ;
00079 
00080 
00081 /**
00082  *Gets the instance of encoding handler.
00083  *This function implements a singleton pattern.
00084  *@param a_enc the encoding of the Handler.
00085  *@return the instance of #CREncHandler.
00086  */
00087 CREncHandler *
00088 cr_enc_handler_get_instance (enum CREncoding a_enc)
00089 {
00090         gulong i = 0 ;
00091 
00092         for (i = 0 ;gv_default_enc_handlers[i].encoding ; i++)
00093         {
00094                 if (gv_default_enc_handlers[i].encoding == a_enc)
00095                 {
00096                         return 
00097                                 (CREncHandler *)
00098                                 &gv_default_enc_handlers[i].encoding ;
00099                 }
00100         }
00101 
00102         return NULL ;
00103 }
00104 
00105 
00106 /**
00107  *Given an encoding name (called an alias name)
00108  *the function returns the matching encoding type.
00109  *@param a_alias_name the encoding name
00110  *@param a_enc output param. The returned encoding type
00111  *or 0 if the alias is not supported.
00112  *@return CR_OK upon successfull completion, an error code otherwise.
00113  */
00114 enum CRStatus
00115 cr_enc_handler_resolve_enc_alias (const guchar *a_alias_name, 
00116                                   enum CREncoding *a_enc)
00117 {
00118         gulong i = 0 ;
00119         guchar * alias_name_up = NULL ;
00120         enum CRStatus status = CR_ENCODING_NOT_FOUND_ERROR ;
00121 
00122         g_return_val_if_fail (a_alias_name != NULL, CR_BAD_PARAM_ERROR) ;
00123 
00124         alias_name_up = g_strdup (a_alias_name) ;
00125         g_strup (alias_name_up) ;
00126 
00127         for (i = 0 ; gv_default_aliases[i].name ; i++)
00128         {
00129                 if (!strcmp (gv_default_aliases[i].name, alias_name_up))
00130                 {
00131                         *a_enc = gv_default_aliases[i].encoding ;
00132                         status = CR_OK ;
00133                         break ;
00134                 }
00135         }
00136 
00137         return status ;
00138 }
00139 
00140 
00141 /**
00142  *Converts a raw input buffer into an utf8 buffer.
00143  *@param a_this the current instance of #CREncHandler.
00144  *@param a_in the input buffer to convert.
00145  *@param a_in_len in/out parameter. The len of the input
00146  *buffer to convert. After return, contains the number of
00147  *bytes actually consumed.
00148  *@param @a_out output parameter. The converted output buffer.
00149  *Must be freed by the buffer.
00150  *@param a_out_len output parameter. The length of the output buffer.
00151  *@return CR_OK upon successfull completion, an error code otherwise.
00152  */
00153 enum CRStatus
00154 cr_enc_handler_convert_input (CREncHandler *a_this,
00155                               const guchar *a_in,
00156                               gulong *a_in_len,
00157                               guchar **a_out,
00158                               gulong *a_out_len)
00159 {
00160         enum CRStatus status = CR_OK ;
00161 
00162         g_return_val_if_fail (a_this && a_in && a_in_len && a_out,
00163                               CR_BAD_PARAM_ERROR) ;
00164 
00165         if (a_this->decode_input == NULL) return CR_OK ;
00166 
00167         if (a_this->enc_str_len_as_utf8)
00168         {
00169                 status =
00170                         a_this->enc_str_len_as_utf8 (a_in,
00171                                                      &a_in[*a_in_len -1],
00172                                                      a_out_len) ;
00173 
00174                 g_return_val_if_fail (status == CR_OK, status) ;
00175         }
00176         else
00177         {
00178                 *a_out_len = *a_in_len ;
00179         }
00180 
00181         *a_out = g_malloc0 (*a_out_len) ;
00182 
00183         status = a_this->decode_input (a_in, a_in_len, *a_out, a_out_len) ;
00184 
00185         if (status != CR_OK)
00186         {
00187                 g_free (*a_out) ;
00188                 *a_out = NULL ;
00189         }
00190 
00191         g_return_val_if_fail (status == CR_OK, status) ;
00192 
00193         return CR_OK ;
00194 }

Generated on Wed Oct 1 01:36:45 2003 for Libcroco by doxygen 1.3.3