#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <alloca.h>
#include "write-tcl.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "xerror.h"
#include "message.h"
#include "msgl-iconv.h"
#include "po-charset.h"
#include "xalloc.h"
#include "pathname.h"
#include "fwriteerror.h"
#include "exit.h"
#include "utf8-ucs4.h"
#include "gettext.h"
#define _(str) gettext (str)
static void
write_tcl_string (FILE *stream, const char *str)
{
static const char hexdigit[] = "0123456789abcdef";
const char *str_limit = str + strlen (str);
fprintf (stream, "\"");
while (str < str_limit)
{
unsigned int uc;
unsigned int count;
count = u8_mbtouc (&uc, (const unsigned char *) str, str_limit - str);
if (uc < 0x10000)
{
if (uc == 0x000a)
fprintf (stream, "\\n");
else if (uc == 0x000d)
fprintf (stream, "\\r");
else if (uc == 0x0022)
fprintf (stream, "\\\"");
else if (uc == 0x0024)
fprintf (stream, "\\$");
else if (uc == 0x005b)
fprintf (stream, "\\[");
else if (uc == 0x005c)
fprintf (stream, "\\\\");
else if (uc == 0x005d)
fprintf (stream, "\\]");
#if 0
else if (uc == 0x007b)
fprintf (stream, "\\{");
else if (uc == 0x007d)
fprintf (stream, "\\}");
#endif
else if (uc >= 0x0020 && uc < 0x007f)
fprintf (stream, "%c", uc);
else
fprintf (stream, "\\u%c%c%c%c",
hexdigit[(uc >> 12) & 0x0f], hexdigit[(uc >> 8) & 0x0f],
hexdigit[(uc >> 4) & 0x0f], hexdigit[uc & 0x0f]);
}
else
fwrite (str, 1, count, stream);
str += count;
}
fprintf (stream, "\"");
}
static void
write_msg (FILE *output_file, message_list_ty *mlp, const char *locale_name)
{
size_t j;
for (j = 0; j < mlp->nitems; j++)
{
message_ty *mp = mlp->item[j];
if (mp->msgid[0] == '\0')
fprintf (output_file, "set ::msgcat::header ");
else
{
fprintf (output_file, "::msgcat::mcset %s ", locale_name);
write_tcl_string (output_file, mp->msgid);
fprintf (output_file, " ");
}
write_tcl_string (output_file, mp->msgstr);
fprintf (output_file, "\n");
}
}
int
msgdomain_write_tcl (message_list_ty *mlp, const char *canon_encoding,
const char *locale_name,
const char *directory)
{
if (mlp->nitems == 0)
return 0;
{
bool has_plural;
size_t j;
has_plural = false;
for (j = 0; j < mlp->nitems; j++)
if (mlp->item[j]->msgid_plural != NULL)
has_plural = true;
if (has_plural)
{
multiline_error (xstrdup (""),
xstrdup (_("\
message catalog has plural form translations\n\
but the Tcl message catalog format doesn't support plural handling\n")));
return 1;
}
}
iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
{
size_t len;
char *frobbed_locale_name;
char *p;
char *file_name;
FILE *output_file;
len = strlen (locale_name);
frobbed_locale_name = (char *) alloca (len + 1);
memcpy (frobbed_locale_name, locale_name, len + 1);
for (p = frobbed_locale_name; *p != '\0'; p++)
if (*p >= 'A' && *p <= 'Z')
*p = *p - 'A' + 'a';
else if (*p == '.')
{
*p = '\0';
break;
}
file_name = concatenated_pathname (directory, frobbed_locale_name, ".msg");
output_file = fopen (file_name, "w");
if (output_file == NULL)
{
error (0, errno, _("error while opening \"%s\" for writing"),
file_name);
return 1;
}
write_msg (output_file, mlp, frobbed_locale_name);
if (fwriteerror (output_file))
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
file_name);
fclose (output_file);
}
return 0;
}