1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
39 #define ICONV_CODEPAGE_LENGTH 20
41 static long iconv_selected;
42 static long iconv_count = 0;
43 static char iconv_converting = 0;
44 static iconv_t **iconv_cd = 0;
46 int register_iconv(iconv_t *new_iconv)
48 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
50 CRIT_ERR(NULL, NULL, "Out of memory");
52 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
53 if (!iconv_cd[iconv_count]) {
54 CRIT_ERR(NULL, NULL, "Out of memory");
56 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
68 for (i = 0; i < iconv_count; i++) {
70 iconv_close(*iconv_cd[i]);
78 void iconv_convert(size_t *a, char *buff_in, char *p, size_t p_max_size)
80 if (*a > 0 && iconv_converting && iconv_selected > 0
81 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
83 size_t dummy1, dummy2;
85 const char *ptr = buff_in;
93 strncpy(buff_in, p, p_max_size);
95 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
97 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
100 NORM_ERR("Iconv codeset conversion failed");
105 /* It is nessecary when we are converting from multibyte to
106 * singlebyte codepage */
108 //(*a) = *a - dummy2;
113 void init_iconv_start(struct text_object *obj, void *free_at_crash, const char *arg)
115 char iconv_from[ICONV_CODEPAGE_LENGTH];
116 char iconv_to[ICONV_CODEPAGE_LENGTH];
118 if (iconv_converting) {
119 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
122 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
123 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
127 new_iconv = iconv_open(iconv_to, iconv_from);
128 if (new_iconv == (iconv_t) (-1)) {
129 NORM_ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
131 obj->data.i = register_iconv(&new_iconv);
132 iconv_converting = 1;
137 void init_iconv_stop(void)
139 iconv_converting = 0;
142 void do_iconv_start(struct text_object *obj)
144 iconv_converting = 1;
145 iconv_selected = obj->data.i;
148 void do_iconv_stop(void)
150 iconv_converting = 0;