Code sync
[external/cups.git] / ppdc / genstrings.cxx
1 //
2 // "$Id: genstrings.cxx 9793 2011-05-20 03:49:49Z mike $"
3 //
4 //   GNU gettext message generator for the CUPS PPD Compiler.
5 //
6 //   This program is used to generate a dummy source file containing all of
7 //   the standard media and sample driver strings.  The results are picked up
8 //   by GNU gettext and placed in the CUPS message catalog.
9 //
10 //   Copyright 2008-2011 by Apple Inc.
11 //
12 //   These coded instructions, statements, and computer programs are the
13 //   property of Apple Inc. and are protected by Federal copyright
14 //   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
15 //   which should have been included with this file.  If this file is
16 //   file is missing or damaged, see the license at "http://www.cups.org/".
17 //
18 // Usage:
19 //
20 //   ./genstrings >sample.c
21 //
22 // Contents:
23 //
24 //   main()           - Main entry for the PPD compiler.
25 //   add_ui_strings() - Add all UI strings from the driver.
26 //   write_cstring()  - Write a translation string as a valid C string to
27 //                      stdout.
28 //
29
30 //
31 // Include necessary headers...
32 //
33
34 #include "ppdc-private.h"
35 #include <unistd.h>
36
37
38 //
39 // Local functions...
40 //
41
42 static void     add_ui_strings(ppdcDriver *d, ppdcCatalog *catalog);
43 static void     write_cstring(const char *s);
44
45
46 //
47 // 'main()' - Main entry for the PPD compiler.
48 //
49
50 int                                     // O - Exit status
51 main(void)
52 {
53   ppdcSource    *src;                   // PPD source file data
54   ppdcCatalog   *catalog;               // Catalog to hold all of the UI strings
55
56
57   // Make sure we are in the right place...
58   if (access("../data", 0) || access("sample.drv", 0))
59   {
60     puts("You must run genstrings from the ppdc directory.");
61     return (1);
62   }
63
64   // Load the sample drivers...
65   ppdcSource::add_include("../data");
66
67   src     = new ppdcSource("sample.drv");
68   catalog = new ppdcCatalog(NULL);
69
70   catalog->add_message("ISOLatin1");
71   catalog->add_message("English");
72
73   // Add the media size strings...
74   ppdcMediaSize *size;                  // Current media size
75
76   for (size = (ppdcMediaSize *)src->sizes->first();
77        size;
78        size = (ppdcMediaSize *)src->sizes->next())
79     catalog->add_message(size->text->value);
80
81   // Then collect all of the UI strings from the sample drivers...
82   ppdcDriver    *d;                     // Current driver
83
84   for (d = (ppdcDriver *)src->drivers->first();
85        d;
86        d = (ppdcDriver *)src->drivers->next())
87     add_ui_strings(d, catalog);
88
89   // Finally, write all of the strings...
90   ppdcMessage *message;
91
92   for (message = (ppdcMessage *)catalog->messages->first();
93        message;
94        message = (ppdcMessage *)catalog->messages->next())
95     write_cstring(message->id->value);
96
97   src->release();
98   catalog->release();
99
100   // Return with no errors.
101   return (0);
102 }
103
104
105 //
106 // 'add_ui_strings()' - Add all UI strings from the driver.
107 //
108
109 static void
110 add_ui_strings(ppdcDriver  *d,          // I - Driver data
111                ppdcCatalog *catalog)    // I - Message catalog
112 {
113   // Add the make/model/language strings...
114   catalog->add_message(d->manufacturer->value);
115   catalog->add_message(d->model_name->value);
116
117   // Add the group/option/choice strings...
118   ppdcGroup     *g;                     // Current group
119   ppdcOption    *o;                     // Current option
120   ppdcChoice    *c;                     // Current choice
121
122   for (g = (ppdcGroup *)d->groups->first();
123        g;
124        g = (ppdcGroup *)d->groups->next())
125   {
126     if (!g->options->count)
127       continue;
128
129     if (_cups_strcasecmp(g->name->value, "General"))
130       catalog->add_message(g->text->value);
131
132     for (o = (ppdcOption *)g->options->first();
133          o;
134          o = (ppdcOption *)g->options->next())
135     {
136       if (!o->choices->count)
137         continue;
138
139       if (o->text->value && strcmp(o->name->value, o->text->value))
140         catalog->add_message(o->text->value);
141       else
142         catalog->add_message(o->name->value);
143
144       for (c = (ppdcChoice *)o->choices->first();
145            c;
146            c = (ppdcChoice *)o->choices->next())
147         if (c->text->value && strcmp(c->name->value, c->text->value))
148           catalog->add_message(c->text->value);
149         else
150           catalog->add_message(c->name->value);
151     }
152   }
153
154   // Add profile and preset strings...
155   ppdcAttr *a;                          // Current attribute
156   for (a = (ppdcAttr *)d->attrs->first();
157        a;
158        a = (ppdcAttr *)d->attrs->next())
159   {
160     if (a->text->value && a->text->value[0] &&
161         (a->localizable ||
162          !strncmp(a->name->value, "Custom", 6) ||
163          !strncmp(a->name->value, "ParamCustom", 11) ||
164          !strcmp(a->name->value, "APCustomColorMatchingName") ||
165          !strcmp(a->name->value, "APPrinterPreset") ||
166          !strcmp(a->name->value, "cupsICCProfile") ||
167          !strcmp(a->name->value, "cupsIPPReason") ||
168          !strcmp(a->name->value, "cupsMarkerName")))
169     {
170       catalog->add_message(a->text->value);
171
172       if ((a->localizable && a->value->value[0]) ||
173           !strcmp(a->name->value, "cupsIPPReason"))
174         catalog->add_message(a->value->value);
175     }
176     else if (!strncmp(a->name->value, "Custom", 6) ||
177              !strncmp(a->name->value, "ParamCustom", 11))
178       catalog->add_message(a->name->value);
179   }
180 }
181
182
183 //
184 // 'write_cstring()' - Write a translation string as a valid C string to stdout.
185 //
186
187 static void
188 write_cstring(const char *s)            /* I - String to write */
189 {
190   fputs("_(\"", stdout);
191   if (s)
192   {
193     while (*s)
194     {
195       if (*s == '\\')
196         fputs("\\\\", stdout);
197       else if (*s == '\"')
198         fputs("\\\"", stdout);
199       else if (*s == '\t')
200         fputs("\\t", stdout);
201       else if (*s == '\n')
202         fputs("\\n", stdout);
203       else
204         putchar(*s);
205
206       s ++;
207     }
208   }
209   puts("\");");
210 }
211
212
213 //
214 // End of "$Id: genstrings.cxx 9793 2011-05-20 03:49:49Z mike $".
215 //