#include <stdio.h>
#include <stdlib.h>
static int read_strings(FILE *strings, char *buffer, size_t bufsize,
char **id, char **str);
static void write_po(FILE *po, const char *what, const char *s);
int
main(int argc,
char *argv[])
{
FILE *strings,
*po;
char iconv[1024],
buffer[8192],
*id,
*str;
int count;
if (argc != 3)
{
puts("Usage: strings2po filename.strings filename.po");
return (1);
}
snprintf(iconv, sizeof(iconv), "iconv -f utf-16 -t utf-8 '%s'", argv[1]);
if ((strings = popen(iconv, "r")) == NULL)
{
perror(argv[1]);
return (1);
}
if ((po = fopen(argv[2], "w")) == NULL)
{
perror(argv[2]);
pclose(strings);
return (1);
}
count = 0;
while (read_strings(strings, buffer, sizeof(buffer), &id, &str))
{
count ++;
write_po(po, "msgid", id);
write_po(po, "msgstr", str);
}
pclose(strings);
fclose(po);
printf("%s: %d messages.\n", argv[2], count);
return (0);
}
static int
read_strings(FILE *strings,
char *buffer,
size_t bufsize,
char **id,
char **str)
{
char *bufptr;
while (fgets(buffer, bufsize, strings))
{
if (buffer[0] != '\"')
continue;
*id = buffer + 1;
for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
if (*bufptr == '\\')
bufptr ++;
if (*bufptr != '\"')
continue;
*bufptr++ = '\0';
while (*bufptr && *bufptr != '\"')
bufptr ++;
if (!*bufptr)
continue;
bufptr ++;
*str = bufptr;
for (; *bufptr && *bufptr != '\"'; bufptr ++)
if (*bufptr == '\\')
bufptr ++;
if (*bufptr != '\"')
continue;
*bufptr = '\0';
return (1);
}
return (0);
}
static void
write_po(FILE *po,
const char *what,
const char *s)
{
fprintf(po, "%s \"%s\"\n", what, s);
}