chiark
/
gitweb
/
~mdw
/
disorder
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
disobedience/disobedience.h: Declare variables as `extern'.
[disorder]
/
lib
/
charset.c
diff --git
a/lib/charset.c
b/lib/charset.c
index fbcf2753b011747e9cf1bf6554c7de1c4ffed227..b8af6e68fdbf5b49edde7b018c1c085257937ad5 100644
(file)
--- a/
lib/charset.c
+++ b/
lib/charset.c
@@
-1,31
+1,31
@@
/*
* This file is part of DisOrder.
/*
* This file is part of DisOrder.
- * Copyright (C) 2004, 2005 Richard Kettlewell
+ * Copyright (C) 2004, 2005
, 2007, 2008, 2013
Richard Kettlewell
*
*
- * This program is free software
;
you can redistribute it and/or modify
+ * This program is free software
:
you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation
; either version 2
of the License, or
+ * the Free Software Foundation
, either version 3
of the License, or
* (at your option) any later version.
* (at your option) any later version.
- *
- * This program 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.
- *
+ *
+ * This program 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
+ * G
NU G
eneral Public License for more details.
+ *
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file lib/charset.c @brief Character set conversion */
*/
/** @file lib/charset.c @brief Character set conversion */
-#include <config.h>
-#include "types.h"
+#include "common.h"
-#include <iconv.h>
-#include <string.h>
+#if HAVE_ICONV_H
+# include <iconv.h>
+#endif
#include <errno.h>
#include <errno.h>
-#include <langinfo.h>
+#if HAVE_LANGINFO_H
+# include <langinfo.h>
+#endif
#include "mem.h"
#include "log.h"
#include "mem.h"
#include "log.h"
@@
-34,6
+34,17
@@
#include "vector.h"
#include "unicode.h"
#include "vector.h"
#include "unicode.h"
+#if _WIN32
+// TODO WIN32 we assume UTF-8 here, which is *definitely wrong*
+char *mb2utf8(const char *mb) {
+ return xstrdup(mb);
+}
+
+char *utf82mb(const char *utf8) {
+ return xstrdup(utf8);
+}
+#else
+
/** @brief Low-level converstion routine
* @param from Source encoding
* @param to Destination encoding
/** @brief Low-level converstion routine
* @param from Source encoding
* @param to Destination encoding
@@
-49,7
+60,7
@@
static void *convert(const char *from, const char *to,
size_t bufsize = 0, sl, dl;
if((i = iconv_open(to, from)) == (iconv_t)-1)
size_t bufsize = 0, sl, dl;
if((i = iconv_open(to, from)) == (iconv_t)-1)
- fatal(errno, "error calling iconv_open");
+
disorder_
fatal(errno, "error calling iconv_open");
do {
bufsize = bufsize ? 2 * bufsize : 32;
buf = xrealloc_noptr(buf, bufsize);
do {
bufsize = bufsize ? 2 * bufsize : 32;
buf = xrealloc_noptr(buf, bufsize);
@@
-63,34
+74,53
@@
static void *convert(const char *from, const char *to,
} while(len == (size_t)-1 && errno == E2BIG);
iconv_close(i);
if(len == (size_t)-1) {
} while(len == (size_t)-1 && errno == E2BIG);
iconv_close(i);
if(len == (size_t)-1) {
- error(errno, "error converting from %s to %s", from, to);
+
disorder_
error(errno, "error converting from %s to %s", from, to);
return 0;
}
return buf;
}
return 0;
}
return buf;
}
-/** @brief Convert from the local multibyte encoding to UTF-8 */
+/** @brief Convert from the local multibyte encoding to UTF-8
+ * @param mb String in current locale's multibyte encoding
+ * @return Same string in UTF-8
+ */
char *mb2utf8(const char *mb) {
return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1);
}
char *mb2utf8(const char *mb) {
return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1);
}
-/** @brief Convert from UTF-8 to the local multibyte encoding */
+/** @brief Convert from UTF-8 to the local multibyte encoding
+ * @param utf8 String in UTF-8
+ * @return Same string in current locale's multibyte encoding
+ */
char *utf82mb(const char *utf8) {
return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1);
}
char *utf82mb(const char *utf8) {
return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1);
}
-/** @brief Convert from encoding @p from to UTF-8 */
+/** @brief Convert from encoding @p from to UTF-8
+ * @param from Source encoding
+ * @param any String in encoding @p from
+ * @return @p any converted to UTF-8
+ */
char *any2utf8(const char *from, const char *any) {
return convert(from, "UTF-8", any, strlen(any) + 1);
}
char *any2utf8(const char *from, const char *any) {
return convert(from, "UTF-8", any, strlen(any) + 1);
}
-/** @brief Convert from encoding @p from to the local multibyte encoding */
+/** @brief Convert from encoding @p from to the local multibyte encoding
+ * @param from Source encoding
+ * @param any String in encoding @p from
+ * @return @p any converted to current locale's multibyte encoding
+ */
char *any2mb(const char *from, const char *any) {
if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1);
else return xstrdup(any);
}
char *any2mb(const char *from, const char *any) {
if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1);
else return xstrdup(any);
}
-/** @brief Convert from encoding @p from to encoding @p to */
+/** @brief Convert from encoding @p from to encoding @p to
+ * @param from Source encoding
+ * @param to Destination encoding
+ * @param any String in encoding @p from
+ * @return @p any converted to encoding @p to
+ */
char *any2any(const char *from,
const char *to,
const char *any) {
char *any2any(const char *from,
const char *to,
const char *any) {
@@
-98,6
+128,8
@@
char *any2any(const char *from,
else return xstrdup(any);
}
else return xstrdup(any);
}
+#endif
+
/** @brief Truncate a string for display purposes
* @param s Pointer to UTF-8 string
* @param max Maximum number of columns
/** @brief Truncate a string for display purposes
* @param s Pointer to UTF-8 string
* @param max Maximum number of columns
@@
-115,20
+147,22
@@
const char *truncate_for_display(const char *s, long max) {
uint32_t *s32;
size_t l32, cut;
utf32_iterator it;
uint32_t *s32;
size_t l32, cut;
utf32_iterator it;
+ long graphemes;
/* Convert to UTF-32 for processing */
if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
return 0;
it = utf32_iterator_new(s32, l32);
cut = l32;
/* Convert to UTF-32 for processing */
if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
return 0;
it = utf32_iterator_new(s32, l32);
cut = l32;
- while(max && utf32_iterator_where(it) < l32) {
+ graphemes = 0; /* # of graphemes left of it */
+ while(graphemes <= max && utf32_iterator_where(it) < l32) {
+ if(graphemes == max - 1)
+ cut = utf32_iterator_where(it);
utf32_iterator_advance(it, 1);
if(utf32_iterator_grapheme_boundary(it))
utf32_iterator_advance(it, 1);
if(utf32_iterator_grapheme_boundary(it))
- --max;
- if(max == 1)
- cut = utf32_iterator_where(it);
+ ++graphemes;
}
}
- if(
max == 0) {
/* we need to cut */
+ if(
graphemes > max) {
/* we need to cut */
s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */
l32 = cut + 1;
s = utf32_to_utf8(s32, l32, 0);
s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */
l32 = cut + 1;
s = utf32_to_utf8(s32, l32, 0);