1/*
2 * "$Id: strings2po.c 11560 2014-02-06 20:10:19Z msweet $"
3 *
4 * Convert Apple .strings file (UTF-16 BE text file) to GNU gettext .po files.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file.  If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * Usage:
15 *
16 *   strings2po filename.strings filename.po
17 *
18 * Compile with:
19 *
20 *   gcc -o strings2po strings2po.c
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25
26
27/*
28 * The .strings file format is simple:
29 *
30 * // comment
31 * "id" = "str";
32 *
33 * Both the id and str strings use standard C quoting for special characters
34 * like newline and the double quote character.
35 */
36
37/*
38 * Local functions...
39 */
40
41static int	read_strings(FILE *strings, char *buffer, size_t bufsize,
42		             char **id, char **str);
43static void	write_po(FILE *po, const char *what, const char *s);
44
45
46/*
47 *   main() - Convert .strings file to .po.
48 */
49
50int					/* O - Exit code */
51main(int  argc,				/* I - Number of command-line args */
52     char *argv[])			/* I - Command-line arguments */
53{
54  FILE	*strings,			/* .strings file */
55	*po;				/* .po file */
56  char	iconv[1024],			/* iconv command */
57	buffer[8192],			/* Line buffer */
58	*id,				/* ID string */
59	*str;				/* Translation string */
60  int	count;				/* Number of messages converted */
61
62
63  if (argc != 3)
64  {
65    puts("Usage: strings2po filename.strings filename.po");
66    return (1);
67  }
68
69 /*
70  * Cheat by using iconv to convert the .strings file from UTF-16 to UTF-8
71  * which is what we need for the .po file (and it makes things a lot
72  * simpler...)
73  */
74
75  snprintf(iconv, sizeof(iconv), "iconv -f utf-16 -t utf-8 '%s'", argv[1]);
76  if ((strings = popen(iconv, "r")) == NULL)
77  {
78    perror(argv[1]);
79    return (1);
80  }
81
82  if ((po = fopen(argv[2], "w")) == NULL)
83  {
84    perror(argv[2]);
85    pclose(strings);
86    return (1);
87  }
88
89  count = 0;
90
91  while (read_strings(strings, buffer, sizeof(buffer), &id, &str))
92  {
93    count ++;
94    write_po(po, "msgid", id);
95    write_po(po, "msgstr", str);
96  }
97
98  pclose(strings);
99  fclose(po);
100
101  printf("%s: %d messages.\n", argv[2], count);
102
103  return (0);
104}
105
106
107/*
108 * 'read_strings()' - Read a line from a .strings file.
109 */
110
111static int				/* O - 1 on success, 0 on failure */
112read_strings(FILE   *strings,		/* I - .strings file */
113             char   *buffer,		/* I - Line buffer */
114	     size_t bufsize,		/* I - Size of line buffer */
115             char   **id,		/* O - Pointer to ID string */
116	     char   **str)		/* O - Pointer to translation string */
117{
118  char	*bufptr;			/* Pointer into buffer */
119
120
121  while (fgets(buffer, (int)bufsize, strings))
122  {
123    if (buffer[0] != '\"')
124      continue;
125
126    *id = buffer + 1;
127
128    for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
129      if (*bufptr == '\\')
130        bufptr ++;
131
132    if (*bufptr != '\"')
133      continue;
134
135    *bufptr++ = '\0';
136
137    while (*bufptr && *bufptr != '\"')
138      bufptr ++;
139
140    if (!*bufptr)
141      continue;
142
143    bufptr ++;
144    *str = bufptr;
145
146    for (; *bufptr && *bufptr != '\"'; bufptr ++)
147      if (*bufptr == '\\')
148        bufptr ++;
149
150    if (*bufptr != '\"')
151      continue;
152
153    *bufptr = '\0';
154
155    return (1);
156  }
157
158  return (0);
159}
160
161
162/*
163 * 'write_po()' - Write a line to the .po file.
164 */
165
166static void
167write_po(FILE       *po,		/* I - .po file */
168         const char *what,		/* I - Type of string */
169	 const char *s)			/* I - String to write */
170{
171  fprintf(po, "%s \"%s\"\n", what, s);
172}
173
174
175/*
176 * End of "$Id: strings2po.c 11560 2014-02-06 20:10:19Z msweet $".
177 */
178