]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-catalog.cxx
4 // Shared message catalog class for the CUPS PPD Compiler.
6 // Copyright 2007-2008 by Apple Inc.
7 // Copyright 2002-2006 by Easy Software Products.
9 // These coded instructions, statements, and computer programs are the
10 // property of Apple Inc. and are protected by Federal copyright
11 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 // which should have been included with this file. If this file is
13 // file is missing or damaged, see the license at "http://www.cups.org/".
17 // ppdcCatalog::ppdcCatalog() - Create a shared message catalog.
18 // ppdcCatalog::~ppdcCatalog() - Destroy a shared message catalog.
19 // ppdcCatalog::add_message() - Add a new message.
20 // ppdcCatalog::find_message() - Find a message in a catalog...
21 // ppdcCatalog::load_messages() - Load messages from a .po file.
22 // ppdcCatalog::save_messages() - Save the messages to a .po file.
26 // Include necessary headers...
30 #include <cups/globals.h>
34 // 'ppdcCatalog::ppdcCatalog()' - Create a shared message catalog.
37 ppdcCatalog::ppdcCatalog(const char *l
, // I - Locale
38 const char *f
) // I - Message catalog file
41 _cups_globals_t
*cg
= _cupsGlobals();
45 locale
= new ppdcString(l
);
46 filename
= new ppdcString(f
);
47 messages
= new ppdcArray();
51 // Try loading the base messages for this locale...
52 char pofile
[1024]; // Message catalog file
55 snprintf(pofile
, sizeof(pofile
), "%s/%s/ppdc_%s.po", cg
->localedir
, l
, l
);
57 if (load_messages(pofile
) && strchr(l
, '_'))
59 // Try the base locale...
60 char baseloc
[3]; // Base locale...
63 strlcpy(baseloc
, l
, sizeof(baseloc
));
64 snprintf(pofile
, sizeof(pofile
), "%s/%s/ppdc_%s.po", cg
->localedir
,
67 load_messages(pofile
);
77 // 'ppdcCatalog::~ppdcCatalog()' - Destroy a shared message catalog.
80 ppdcCatalog::~ppdcCatalog()
89 // 'ppdcCatalog::add_message()' - Add a new message.
93 ppdcCatalog::add_message(const char *id
)// I - Message ID to add
95 ppdcMessage
*m
; // Current message
96 char text
[1024]; // Text to translate
99 // Range check input...
103 // Verify that we don't already have the message ID...
104 for (m
= (ppdcMessage
*)messages
->first();
106 m
= (ppdcMessage
*)messages
->next())
107 if (!strcmp(m
->id
->value
, id
))
110 // Add the message...
111 snprintf(text
, sizeof(text
), "TRANSLATE %s", id
);
112 messages
->add(new ppdcMessage(id
, text
));
117 // 'ppdcCatalog::find_message()' - Find a message in a catalog...
120 const char * // O - Message text
121 ppdcCatalog::find_message(
122 const char *id
) // I - Message ID
124 ppdcMessage
*m
; // Current message
127 for (m
= (ppdcMessage
*)messages
->first();
129 m
= (ppdcMessage
*)messages
->next())
130 if (!strcmp(m
->id
->value
, id
))
131 return (m
->string
->value
);
138 // 'ppdcCatalog::load_messages()' - Load messages from a .po file.
141 int // O - 0 on success, -1 on failure
142 ppdcCatalog::load_messages(
143 const char *f
) // I - Message catalog file
145 cups_file_t
*fp
; // Message file
146 ppdcMessage
*temp
; // Current message
147 char line
[4096], // Line buffer
148 *ptr
, // Pointer into buffer
149 id
[4096], // Translation ID
150 str
[4096]; // Translation string
151 int linenum
; // Line number
154 // Open the message catalog file...
155 if ((fp
= cupsFileOpen(f
, "r")) == NULL
)
159 * Read messages from the catalog file until EOF...
161 * The format is the GNU gettext .po format, which is fairly simple:
164 * msgstr "localized text"
166 * The ID and localized text can span multiple lines using the form:
171 * "localized text spanning "
179 while (cupsFileGets(fp
, line
, sizeof(line
)))
183 // Skip blank and comment lines...
184 if (line
[0] == '#' || !line
[0])
187 // Strip the trailing quote...
188 if ((ptr
= strrchr(line
, '\"')) == NULL
)
190 fprintf(stderr
, "load_messages: Expected quoted string on line %d of %s!\n",
198 // Find start of value...
199 if ((ptr
= strchr(line
, '\"')) == NULL
)
201 fprintf(stderr
, "load_messages: Expected quoted string on line %d of %s!\n",
209 // Unquote the text...
210 char *sptr
, *dptr
; // Source/destination pointers
212 for (sptr
= ptr
, dptr
= ptr
; *sptr
;)
221 while (isdigit(*sptr
))
223 *dptr
= *dptr
* 8 + *sptr
- '0';
233 else if (*sptr
== 'r')
235 else if (*sptr
== 't')
249 // Create or add to a message...
250 if (!strncmp(line
, "msgid", 5))
254 temp
= new ppdcMessage(id
, str
);
259 strlcpy(id
, ptr
, sizeof(id
));
262 else if (!strncmp(line
, "msgstr", 6))
266 fprintf(stderr
, "load_messages: Need a msgid line before any "
267 "translation strings on line %d of %s!\n",
273 strlcpy(str
, ptr
, sizeof(str
));
275 else if (line
[0] == '\"' && str
[0])
276 strlcat(str
, ptr
, sizeof(str
));
277 else if (line
[0] == '\"' && id
[0])
278 strlcat(id
, ptr
, sizeof(id
));
281 fprintf(stderr
, "load_messages: Unexpected text on line %d of %s!\n",
290 temp
= new ppdcMessage(id
, str
);
302 // 'ppdcCatalog::save_messages()' - Save the messages to a .po file.
305 int // O - 0 on success, -1 on error
306 ppdcCatalog::save_messages(
307 const char *f
) // I - File to save to
309 cups_file_t
*fp
; // Message file
310 ppdcMessage
*m
; // Current message
311 const char *ptr
; // Pointer into string
314 if ((fp
= cupsFileOpen(f
, "w")) == NULL
)
317 for (m
= (ppdcMessage
*)messages
->first();
319 m
= (ppdcMessage
*)messages
->next())
321 cupsFilePuts(fp
, "msgid \"");
322 for (ptr
= m
->id
->value
; *ptr
; ptr
++)
326 cupsFilePuts(fp
, "\\n");
329 cupsFilePuts(fp
, "\\\\");
332 cupsFilePuts(fp
, "\\\"");
335 cupsFilePutChar(fp
, *ptr
);
338 cupsFilePuts(fp
, "\"\n");
340 cupsFilePuts(fp
, "msgstr \"");
341 for (ptr
= m
->string
->value
; *ptr
; ptr
++)
345 cupsFilePuts(fp
, "\\n");
348 cupsFilePuts(fp
, "\\\\");
351 cupsFilePuts(fp
, "\\\"");
354 cupsFilePutChar(fp
, *ptr
);
357 cupsFilePuts(fp
, "\"\n");
359 cupsFilePutChar(fp
, '\n');