aboutsummaryrefslogtreecommitdiff
path: root/src/ellinika/utf8scm.c
blob: 1cb7f76ca94e8bbd4f7a92059b6f990f573be705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This file is part of Ellinika project.
   Copyright (C) 2011 Sergey Poznyakoff

   Ellinika 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 3 of the License, or
   (at your option) any later version.

   Ellinika 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.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <libguile.h>
#include "utf8.h"


SCM_DEFINE_PUBLIC(scm_utf8_toupper, "utf8-toupper", 1, 0, 0,
		  (SCM string),
"Convert STRING to uppercase\n")
#define FUNC_NAME s_scm_utf8_toupper
{
	char *str;
	SCM scm;
	
	SCM_ASSERT(scm_is_string(string), string, SCM_ARG1, FUNC_NAME);
	str = scm_to_locale_string(string);
	if (utf8_toupper(str, strlen(str)))
		scm_misc_error(FUNC_NAME,
			       "cannot convert to upper case: ~A",
			       scm_list_1(string));
	scm = scm_from_locale_string(str);
	free(str);
	return scm;
}
#undef FUNC_NAME

SCM_DEFINE_PUBLIC(scm_utf8_tolower, "utf8-tolower", 1, 0, 0,
		  (SCM string),
"Convert STRING to lowercase\n")
#define FUNC_NAME s_scm_utf8_tolower
{
	char *str;
	SCM scm;
	
	SCM_ASSERT(scm_is_string(string), string, SCM_ARG1, FUNC_NAME);
	str = scm_to_locale_string(string);
	if (utf8_tolower(str, strlen(str)))
		scm_misc_error(FUNC_NAME,
			       "cannot convert to lower case: ~A",
			       scm_list_1(string));
	scm = scm_from_locale_string(str);
	free(str);
	return scm;
}
#undef FUNC_NAME

static int
memberof(unsigned *w, size_t len, unsigned c)
{
	while (len--)
		if (*w++ == c)
			return 1;
	return 0;
}

SCM_DEFINE_PUBLIC(scm_utf8_escape, "utf8-escape", 1, 1, 0,
		  (SCM string, SCM escapable),
"Prefix with \\ each occurrence of ESCAPABLE chars in STRING\n")
#define FUNC_NAME s_scm_utf8_escape
{
	SCM scm;
	unsigned *escptr, *escbase;
	size_t esclen;
	char *s;	
	unsigned *wptr, *nptr;
	size_t wlen;
	size_t incr, i;
	
	SCM_ASSERT(scm_is_string(string), string, SCM_ARG1, FUNC_NAME);
	s = scm_to_locale_string(string);

	if (utf8_mbstr_to_wc(s, &wptr, &wlen))
		scm_misc_error(FUNC_NAME,
			       "cannot convert ~A to UTF-8",
			       scm_list_1(string));
	free(s);
	
	if (SCM_UNBNDP(escapable)) {
		static unsigned default_escapable[] = { '\"', '\\' };
		escbase = NULL;
		escptr = default_escapable;
		esclen = 2;
	} else {
		SCM_ASSERT(scm_is_string(escapable), escapable,
			   SCM_ARG2, FUNC_NAME);
		s = scm_to_locale_string(escapable);
		if (utf8_mbstr_to_wc(s, &escbase, &esclen)) {
			free(wptr);
			scm_misc_error(FUNC_NAME,
				       "cannot convert ~A to UTF-8",
				       scm_list_1(escapable));
		}
		escptr = escbase;
		free(s);
	}

	incr = 0;
	for (i = 0; i < wlen; i++)
		if (memberof(escptr, esclen, wptr[i]))
			incr++;
	

	nptr = calloc(sizeof(nptr[0]), wlen + incr);
	if (!nptr)
		scm_memory_error(FUNC_NAME);

	for (i = incr = 0; i < wlen; i++) {
		if (memberof(escptr, esclen, wptr[i]))
			nptr[i + incr++] = '\\';
		nptr[i + incr] = wptr[i];
	}

	free(wptr);
	free(escbase);

	if (utf8_wc_to_mbstr(nptr, wlen + incr, &s))
		scm_misc_error(FUNC_NAME,
			       "cannot convert UTF-8 to Scheme",
			       SCM_EOL);
	scm = scm_from_locale_string(s);
	free(s);
	return scm;
}
#undef FUNC_NAME

void
elmorph_utf8scm_init()
{
#include "utf8scm.x"
}

Return to:

Send suggestions and report system problems to the System administrator.