Build fix due to bad include ordering.
[monky] / src / iconv_tools.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include "config.h"
32 #include "text_object.h"
33 #include "logging.h"
34 #include <iconv.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #define ICONV_CODEPAGE_LENGTH 20
40
41 static long iconv_selected;
42 static long iconv_count = 0;
43 static char iconv_converting = 0;
44 static iconv_t **iconv_cd = 0;
45
46 int register_iconv(iconv_t *new_iconv)
47 {
48         iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
49         if (!iconv_cd) {
50                 CRIT_ERR(NULL, NULL, "Out of memory");
51         }
52         iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
53         if (!iconv_cd[iconv_count]) {
54                 CRIT_ERR(NULL, NULL, "Out of memory");
55         }
56         memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
57         iconv_count++;
58         return iconv_count;
59 }
60
61 void free_iconv(void)
62 {
63         long i;
64
65         if (!iconv_cd)
66                 return;
67
68         for (i = 0; i < iconv_count; i++) {
69                 if (iconv_cd[i]) {
70                         iconv_close(*iconv_cd[i]);
71                         free(iconv_cd[i]);
72                 }
73         }
74         free(iconv_cd);
75         iconv_cd = 0;
76 }
77
78 void iconv_convert(size_t *a, char *buff_in, char *p, size_t p_max_size)
79 {
80         if (*a > 0 && iconv_converting && iconv_selected > 0
81                         && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
82                 int bytes;
83                 size_t dummy1, dummy2;
84 #ifdef __FreeBSD__
85                 const char *ptr = buff_in;
86 #else
87                 char *ptr = buff_in;
88 #endif
89                 char *outptr = p;
90
91                 dummy1 = dummy2 = *a;
92
93                 strncpy(buff_in, p, p_max_size);
94
95                 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
96                 while (dummy1 > 0) {
97                         bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
98                                         &outptr, &dummy2);
99                         if (bytes == -1) {
100                                 NORM_ERR("Iconv codeset conversion failed");
101                                 break;
102                         }
103                 }
104
105                 /* It is nessecary when we are converting from multibyte to
106                  * singlebyte codepage */
107                 //a = outptr - p;
108                 //(*a) = *a - dummy2;
109                 (*a) = outptr - p;
110         }
111 }
112
113 void init_iconv_start(struct text_object *obj, void *free_at_crash, const char *arg)
114 {
115         char iconv_from[ICONV_CODEPAGE_LENGTH];
116         char iconv_to[ICONV_CODEPAGE_LENGTH];
117
118         if (iconv_converting) {
119                 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
120                                 "starting another");
121         }
122         if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
123                 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
124         } else {
125                 iconv_t new_iconv;
126
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);
130                 } else {
131                         obj->data.i = register_iconv(&new_iconv);
132                         iconv_converting = 1;
133                 }
134         }
135 }
136
137 void init_iconv_stop(void)
138 {
139         iconv_converting = 0;
140 }
141
142 void do_iconv_start(struct text_object *obj)
143 {
144         iconv_converting = 1;
145         iconv_selected = obj->data.i;
146 }
147
148 void do_iconv_stop(void)
149 {
150         iconv_converting = 0;
151         iconv_selected = 0;
152 }