Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / libcroco / cr-enc-handler.c
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /*
4  * This file is part of The Croco Library
5  *
6  * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of version 2.1 of the GNU Lesser General Public
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */
23 /*
24  *$Id: cr-enc-handler.c,v 1.7 2004/03/07 13:22:47 dodji Exp $
25  */
27 /**
28  *@file
29  *The definition of the #CREncHandler class.
30  */
32 #include "cr-enc-handler.h"
33 #include "cr-utils.h"
35 #include <string.h>
37 struct CREncAlias {
38         const gchar *name;
39         enum CREncoding encoding;
40 };
42 static struct CREncAlias gv_default_aliases[] = {
43         {"UTF-8", CR_UTF_8},
44         {"UTF_8", CR_UTF_8},
45         {"UTF8", CR_UTF_8},
46         {"UTF-16", CR_UTF_16},
47         {"UTF_16", CR_UTF_16},
48         {"UTF16", CR_UTF_16},
49         {"UCS1", CR_UCS_1},
50         {"UCS-1", CR_UCS_1},
51         {"UCS_1", CR_UCS_1},
52         {"ISO-8859-1", CR_UCS_1},
53         {"ISO_8859-1", CR_UCS_1},
54         {"UCS-1", CR_UCS_1},
55         {"UCS_1", CR_UCS_1},
56         {"UCS4", CR_UCS_4},
57         {"UCS-4", CR_UCS_4},
58         {"UCS_4", CR_UCS_4},
59         {"ASCII", CR_ASCII},
60         {(char *)0, (enum CREncoding)0}
61 };
63 static CREncHandler gv_default_enc_handlers[] = {
64         {CR_UCS_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
65          cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
67         {CR_ISO_8859_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
68          cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
70         {CR_ASCII, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
71          cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
73         {(enum CREncoding)0, NULL, NULL, NULL, NULL}
74 };
76 /**
77  *Gets the instance of encoding handler.
78  *This function implements a singleton pattern.
79  *@param a_enc the encoding of the Handler.
80  *@return the instance of #CREncHandler.
81  */
82 CREncHandler *
83 cr_enc_handler_get_instance (enum CREncoding a_enc)
84 {
85         gulong i = 0;
87         for (i = 0; gv_default_enc_handlers[i].encoding; i++) {
88                 if (gv_default_enc_handlers[i].encoding == a_enc) {
89                         return (CREncHandler *)
90                                 & gv_default_enc_handlers[i].encoding;
91                 }
92         }
94         return NULL;
95 }
97 /**
98  *Given an encoding name (called an alias name)
99  *the function returns the matching encoding type.
100  *@param a_alias_name the encoding name
101  *@param a_enc output param. The returned encoding type
102  *or 0 if the alias is not supported.
103  *@return CR_OK upon successfull completion, an error code otherwise.
104  */
105 enum CRStatus
106 cr_enc_handler_resolve_enc_alias (const guchar * a_alias_name,
107                                   enum CREncoding *a_enc)
109         gulong i = 0;
110         enum CRStatus status = CR_ENCODING_NOT_FOUND_ERROR;
112         g_return_val_if_fail (a_alias_name != NULL, CR_BAD_PARAM_ERROR);
114         gchar *alias_name_up = g_strdup ((gchar *)a_alias_name);
115         g_ascii_strup (alias_name_up, -1);
117         for (i = 0; gv_default_aliases[i].name; i++) {
118                 if (!strcmp (gv_default_aliases[i].name, alias_name_up)) {
119                         *a_enc = gv_default_aliases[i].encoding;
120                         status = CR_OK;
121                         break;
122                 }
123         }
125         return status;
128 /**
129  *Converts a raw input buffer into an utf8 buffer.
130  *@param a_this the current instance of #CREncHandler.
131  *@param a_in the input buffer to convert.
132  *@param a_in_len in/out parameter. The len of the input
133  *buffer to convert. After return, contains the number of
134  *bytes actually consumed.
135  *@param @a_out output parameter. The converted output buffer.
136  *Must be freed by the buffer.
137  *@param a_out_len output parameter. The length of the output buffer.
138  *@return CR_OK upon successfull completion, an error code otherwise.
139  */
140 enum CRStatus
141 cr_enc_handler_convert_input (CREncHandler * a_this,
142                               const guchar * a_in,
143                               gulong * a_in_len,
144                               guchar ** a_out, gulong * a_out_len)
146         enum CRStatus status = CR_OK;
148         g_return_val_if_fail (a_this && a_in && a_in_len && a_out,
149                               CR_BAD_PARAM_ERROR);
151         if (a_this->decode_input == NULL)
152                 return CR_OK;
154         if (a_this->enc_str_len_as_utf8) {
155                 status = a_this->enc_str_len_as_utf8 (a_in,
156                                                       &a_in[*a_in_len - 1],
157                                                       a_out_len);
159                 g_return_val_if_fail (status == CR_OK, status);
160         } else {
161                 *a_out_len = *a_in_len;
162         }
164         *a_out = (guchar *)g_malloc0 (*a_out_len);
166         status = a_this->decode_input (a_in, a_in_len, *a_out, a_out_len);
168         if (status != CR_OK) {
169                 g_free (*a_out);
170                 *a_out = NULL;
171         }
173         g_return_val_if_fail (status == CR_OK, status);
175         return CR_OK;