write-stringtable.c [plain text]
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "write-stringtable.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "message.h"
#include "msgl-ascii.h"
#include "msgl-iconv.h"
#include "po-charset.h"
#include "strstr.h"
#include "write-po.h"
static void
write_escaped_string (FILE *fp, const char *str)
{
const char *str_limit = str + strlen (str);
putc ('"', fp);
while (str < str_limit)
{
unsigned char c = (unsigned char) *str++;
if (c == '\t')
{
putc ('\\', fp);
putc ('t', fp);
}
else if (c == '\n')
{
putc ('\\', fp);
putc ('n', fp);
}
else if (c == '\r')
{
putc ('\\', fp);
putc ('r', fp);
}
else if (c == '\f')
{
putc ('\\', fp);
putc ('f', fp);
}
else if (c == '\\' || c == '"')
{
putc ('\\', fp);
putc (c, fp);
}
else
putc (c, fp);
}
putc ('"', fp);
}
static void
write_message (FILE *fp, const message_ty *mp, size_t page_width, bool debug)
{
if (mp->comment != NULL)
{
size_t j;
for (j = 0; j < mp->comment->nitems; ++j)
{
const char *s = mp->comment->item[j];
if (strstr (s, "*/") == NULL)
{
fputs ("/*", fp);
if (*s != '\0' && *s != '\n' && *s != ' ')
putc (' ', fp);
fputs (s, fp);
fputs (" */\n", fp);
}
else
do
{
const char *e;
fputs ("//", fp);
if (*s != '\0' && *s != '\n' && *s != ' ')
putc (' ', fp);
e = strchr (s, '\n');
if (e == NULL)
{
fputs (s, fp);
s = NULL;
}
else
{
fwrite (s, 1, e - s, fp);
s = e + 1;
}
putc ('\n', fp);
}
while (s != NULL);
}
}
if (mp->comment_dot != NULL)
{
size_t j;
for (j = 0; j < mp->comment_dot->nitems; ++j)
{
const char *s = mp->comment_dot->item[j];
if (strstr (s, "*/") == NULL)
{
fputs ("/* Comment: ", fp);
fputs (s, fp);
fputs (" */\n", fp);
}
else
{
bool first = true;
do
{
const char *e;
fputs ("//", fp);
if (first || (*s != '\0' && *s != '\n' && *s != ' '))
putc (' ', fp);
if (first)
fputs ("Comment: ", fp);
e = strchr (s, '\n');
if (e == NULL)
{
fputs (s, fp);
s = NULL;
}
else
{
fwrite (s, 1, e - s, fp);
s = e + 1;
}
putc ('\n', fp);
first = false;
}
while (s != NULL);
}
}
}
if (mp->filepos_count != 0)
{
size_t j;
for (j = 0; j < mp->filepos_count; ++j)
{
lex_pos_ty *pp = &mp->filepos[j];
char *cp = pp->file_name;
while (cp[0] == '.' && cp[1] == '/')
cp += 2;
fprintf (fp, "/* File: %s:%ld */\n", cp, (long) pp->line_number);
}
}
if (mp->is_fuzzy || mp->msgstr[0] == '\0')
fputs ("/* Flag: untranslated */\n", fp);
if (mp->obsolete)
fputs ("/* Flag: unmatched */\n", fp);
{
size_t i;
for (i = 0; i < NFORMATS; i++)
if (significant_format_p (mp->is_format[i]))
{
fputs ("/* Flag:", fp);
fputs (make_format_description_string (mp->is_format[i],
format_language[i], debug),
fp);
fputs (" */\n", fp);
}
}
write_escaped_string (fp, mp->msgid);
fputs (" = ", fp);
if (mp->msgstr[0] != '\0')
{
if (mp->is_fuzzy)
{
write_escaped_string (fp, mp->msgid);
if (strstr (mp->msgstr, "*/") == NULL)
{
fputs (" /* = ", fp);
write_escaped_string (fp, mp->msgstr);
fputs (" */", fp);
}
else
{
fputs ("; // = ", fp);
write_escaped_string (fp, mp->msgstr);
}
}
else
write_escaped_string (fp, mp->msgstr);
}
else
{
write_escaped_string (fp, mp->msgid);
}
putc (';', fp);
putc ('\n', fp);
}
static void
write_stringtable (FILE *fp, message_list_ty *mlp, const char *canon_encoding,
size_t page_width, bool debug)
{
bool blank_line;
size_t j;
iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
if (!is_ascii_message_list (mlp))
fputs ("\xef\xbb\xbf", fp);
blank_line = false;
for (j = 0; j < mlp->nitems; ++j)
{
const message_ty *mp = mlp->item[j];
if (mp->msgid_plural == NULL)
{
if (blank_line)
putc ('\n', fp);
write_message (fp, mp, page_width, debug);
blank_line = true;
}
}
}
void
msgdomain_list_print_stringtable (msgdomain_list_ty *mdlp, FILE *fp,
size_t page_width, bool debug)
{
message_list_ty *mlp;
if (mdlp->nitems == 1)
mlp = mdlp->item[0]->messages;
else
mlp = message_list_alloc (false);
write_stringtable (fp, mlp, mdlp->encoding, page_width, debug);
}