#include <stdio.h>
#include <stdlib.h>
#include <cups/i18n.h>
#include <cups/string.h>
static void write_string(FILE *strings, const char *s);
int
main(int argc,
char *argv[])
{
int i;
FILE *strings;
cups_array_t *po;
char iconv[1024];
_cups_message_t *msg;
const char *srcfile,
*dstfile;
int use_msgid;
srcfile = NULL;
dstfile = NULL;
use_msgid = 0;
for (i = 1; i < argc; i ++)
if (!strcmp(argv[i], "-m"))
use_msgid = 1;
else if (argv[i][0] == '-')
{
puts("Usage: po2strings [-m] filename.po filename.strings");
return (1);
}
else if (srcfile == NULL)
srcfile = argv[i];
else if (dstfile == NULL)
dstfile = argv[i];
else
{
puts("Usage: po2strings [-m] filename.po filename.strings");
return (1);
}
if (!srcfile || !dstfile)
{
puts("Usage: po2strings [-m] filename.po filename.strings");
return (1);
}
if ((po = _cupsMessageLoad(srcfile, 1)) == NULL)
{
perror(srcfile);
return (1);
}
snprintf(iconv, sizeof(iconv), "iconv -f utf-8 -t utf-16 >'%s'", dstfile);
if ((strings = popen(iconv, "w")) == NULL)
{
perror(argv[2]);
_cupsMessageFree(po);
return (1);
}
for (msg = (_cups_message_t *)cupsArrayFirst(po);
msg;
msg = (_cups_message_t *)cupsArrayNext(po))
{
write_string(strings, msg->id);
fputs(" = ", strings);
write_string(strings, use_msgid ? msg->id : msg->str);
fputs(";\n", strings);
}
printf("%s: %d messages.\n", argv[2], cupsArrayCount(po));
pclose(strings);
_cupsMessageFree(po);
return (0);
}
static void
write_string(FILE *strings,
const char *s)
{
putc('\"', strings);
while (*s)
{
switch (*s)
{
case '\n' :
fputs("\\n", strings);
break;
case '\t' :
fputs("\\t", strings);
break;
case '\\' :
fputs("\\\\", strings);
break;
case '\"' :
fputs("\\\"", strings);
break;
default :
putc(*s, strings);
break;
}
s ++;
}
putc('\"', strings);
}