[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 8/8] make hello unicode aware
From: |
Sami Kerola |
Subject: |
[PATCH 8/8] make hello unicode aware |
Date: |
Wed, 9 Oct 2013 20:20:17 +0100 |
* bootstrap.conf: add necessary gnulib modules for unicode support.
* hello.c: allow user message, or translated, greeting to have unicode
characters.
---
bootstrap.conf | 2 ++
src/hello.c | 33 +++++++++++++++++++++++----------
src/system.h | 4 ++++
3 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf
index 85cc678..5b1cddf 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -28,8 +28,10 @@ gnulib_modules="
gnu-web-doc-update
gnupload
maintainer-makefile
+ mbsrtowcs
progname
readme-release
+ wchar
"
# Additional xgettext options to use. Use "\\\newline" to break lines.
diff --git a/src/hello.c b/src/hello.c
index 8ecdf7b..8c36915 100644
--- a/src/hello.c
+++ b/src/hello.c
@@ -19,6 +19,7 @@
#include <config.h>
#include "system.h"
#include "progname.h"
+#include "xalloc.h"
static const struct option longopts[] = {
{"greeting", required_argument, NULL, 'g'},
@@ -39,7 +40,7 @@ typedef enum
/* Forward declarations. */
static void print_help (void);
static void print_version (void);
-static void print_frame (const char *greeting_msg);
+static void print_frame (const size_t len);
int
main (int argc, char *argv[])
@@ -47,6 +48,8 @@ main (int argc, char *argv[])
int optc;
int lose = 0;
const char *greeting_msg = _("Hello, world!");
+ wchar_t *mb_greeting;
+ size_t len;
greeting_type g = greet_traditional;
set_program_name (argv[0]);
@@ -104,15 +107,25 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE);
}
+ len = mbsrtowcs(NULL, &greeting_msg, 0, NULL);
+ if (len == (size_t)-1)
+ {
+ fprintf (stderr, _("%s: conversion to a multibyte string failed\n"),
program_name);
+ exit (EXIT_FAILURE);
+ }
+ mb_greeting = xmalloc((len + 1) * sizeof(wchar_t));
+ mbsrtowcs(mb_greeting, &greeting_msg, len + 1, NULL);
+
/* Print greeting message and exit. */
if (g != greet_new)
- puts (greeting_msg);
+ wprintf (L"%ls\n", mb_greeting);
else
{
- print_frame (greeting_msg);
- printf ("| %s |\n", greeting_msg);
- print_frame (greeting_msg);
+ print_frame (len);
+ wprintf (L"| %ls |\n", mb_greeting);
+ print_frame (len);
}
+ free(mb_greeting);
exit (EXIT_SUCCESS);
}
@@ -121,13 +134,13 @@ main (int argc, char *argv[])
/* Print new format upper and lower frame. */
void
-print_frame (const char *greeting_msg)
+print_frame (const size_t len)
{
- size_t i, len = strlen (greeting_msg);
- fputs ("+-", stdout);
+ size_t i;
+ fputws (L"+-", stdout);
for (i = 0; i < len; i++)
- putchar ('-');
- fputs ("-+\n", stdout);
+ putwchar (L'-');
+ fputws (L"-+\n", stdout);
}
diff --git a/src/system.h b/src/system.h
index eb3d306..5dd372c 100644
--- a/src/system.h
+++ b/src/system.h
@@ -29,6 +29,10 @@
# include <getopt.h>
# include <unistd.h>
+/* Unicode. */
+#include <wchar.h>
+#include <wctype.h>
+
/* Internationalization. */
# include "gettext.h"
# define _(str) gettext (str)
--
1.8.2.3
- [PATCH 1/8] allow user message to use new-generation greeting format, Sami Kerola, 2013/10/09
- [PATCH 2/8] bootstrap: update from gnulib, Sami Kerola, 2013/10/09
- [PATCH 4/8] clean up configure.ac syntax, Sami Kerola, 2013/10/09
- [PATCH 3/8] reuse AM_INIT_AUTOMAKE bug-email address, Sami Kerola, 2013/10/09
- [PATCH 5/8] use PACKAGE_NAME from config.h, Sami Kerola, 2013/10/09
- [PATCH 6/8] re-indent to match with gnu coding style, Sami Kerola, 2013/10/09
- [PATCH 7/8] make copyright year pseudo dynamic, Sami Kerola, 2013/10/09
- [PATCH 8/8] make hello unicode aware,
Sami Kerola <=