Imported Upstream version 0.18.3.2
[platform/upstream/gettext.git] / gettext-tools / src / message.h
1 /* GNU gettext - internationalization aids
2    Copyright (C) 1995-1998, 2000-2009 Free Software Foundation, Inc.
3
4    This file was written by Peter Miller <millerp@canb.auug.org.au>
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #ifndef _MESSAGE_H
20 #define _MESSAGE_H
21
22 #include "str-list.h"
23 #include "pos.h"
24 #include "hash.h"
25
26 #include <stdbool.h>
27
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33
34 /* According to Sun's Uniforum proposal the default message domain is
35    named 'messages'.  */
36 #define MESSAGE_DOMAIN_DEFAULT "messages"
37
38
39 /* Separator between msgctxt and msgid in .mo files.  */
40 #define MSGCTXT_SEPARATOR '\004'  /* EOT */
41
42
43 /* Kinds of format strings.  */
44 enum format_type
45 {
46   format_c,
47   format_objc,
48   format_sh,
49   format_python,
50   format_python_brace,
51   format_lisp,
52   format_elisp,
53   format_librep,
54   format_scheme,
55   format_smalltalk,
56   format_java,
57   format_csharp,
58   format_awk,
59   format_pascal,
60   format_ycp,
61   format_tcl,
62   format_perl,
63   format_perl_brace,
64   format_php,
65   format_gcc_internal,
66   format_gfc_internal,
67   format_qt,
68   format_qt_plural,
69   format_kde,
70   format_boost,
71   format_lua,
72   format_javascript
73 };
74 #define NFORMATS 27     /* Number of format_type enum values.  */
75 extern DLL_VARIABLE const char *const format_language[NFORMATS];
76 extern DLL_VARIABLE const char *const format_language_pretty[NFORMATS];
77
78 /* Is current msgid a format string?  */
79 enum is_format
80 {
81   undecided,
82   yes,
83   no,
84   yes_according_to_context,
85   possible,
86   impossible
87 };
88
89 extern bool
90        possible_format_p (enum is_format);
91
92
93 /* Range of an unsigned integer argument.  */
94 struct argument_range
95 {
96   int min;
97   int max;
98 };
99
100 /* Tests whether a range is present.  */
101 #define has_range_p(range)  ((range).min >= 0 && (range).max >= 0)
102
103
104 /* Is current msgid wrappable?  */
105 #if 0
106 enum is_wrap
107 {
108   undecided,
109   yes,
110   no
111 };
112 #else /* HACK - C's enum concept is so stupid */
113 #define is_wrap is_format
114 #endif
115
116
117 struct altstr
118 {
119   const char *msgstr;
120   size_t msgstr_len;
121   const char *msgstr_end;
122   string_list_ty *comment;
123   string_list_ty *comment_dot;
124   char *id;
125 };
126
127
128 typedef struct message_ty message_ty;
129 struct message_ty
130 {
131   /* The msgctxt string, if present.  */
132   const char *msgctxt;
133
134   /* The msgid string.  */
135   const char *msgid;
136
137   /* The msgid's plural, if present.  */
138   const char *msgid_plural;
139
140   /* The msgstr strings.  */
141   const char *msgstr;
142   /* The number of bytes in msgstr, including the terminating NUL.  */
143   size_t msgstr_len;
144
145   /* Position in the source PO file.  */
146   lex_pos_ty pos;
147
148   /* Plain comments (#) appearing before the message.  */
149   string_list_ty *comment;
150
151   /* Extracted comments (#.) appearing before the message.  */
152   string_list_ty *comment_dot;
153
154   /* File position comments (#:) appearing before the message, one for
155      each unique file position instance, sorted by file name and then
156      by line.  */
157   size_t filepos_count;
158   lex_pos_ty *filepos;
159
160   /* Informations from special comments (#,).
161      Some of them come from extracted comments.  They are manipulated by
162      the tools, e.g. msgmerge.  */
163
164   /* Fuzzy means "needs translator review".  */
165   bool is_fuzzy;
166
167   /* Designation of format string syntax requirements for specific
168      programming languages.  */
169   enum is_format is_format[NFORMATS];
170
171   /* Lower and upper bound for the argument whose format directive can be
172      omitted in specific cases of singular or plural.  */
173   struct argument_range range;
174
175   /* Do we want the string to be wrapped in the emitted PO file?  */
176   enum is_wrap do_wrap;
177
178   /* The prev_msgctxt, prev_msgid and prev_msgid_plural strings appearing
179      before the message, if present.  Generated by msgmerge.  */
180   const char *prev_msgctxt;
181   const char *prev_msgid;
182   const char *prev_msgid_plural;
183
184   /* If set the message is obsolete and while writing out it should be
185      commented out.  */
186   bool obsolete;
187
188   /* Used for checking that messages have been used, in the msgcmp,
189      msgmerge, msgcomm and msgcat programs.  */
190   int used;
191
192   /* Used for looking up the target message, in the msgcat program.  */
193   message_ty *tmp;
194
195   /* Used for combining alternative translations, in the msgcat program.  */
196   int alternative_count;
197   struct altstr *alternative;
198 };
199
200 extern message_ty *
201        message_alloc (const char *msgctxt,
202                       const char *msgid, const char *msgid_plural,
203                       const char *msgstr, size_t msgstr_len,
204                       const lex_pos_ty *pp);
205 #define is_header(mp) ((mp)->msgctxt == NULL && (mp)->msgid[0] == '\0')
206 extern void
207        message_free (message_ty *mp);
208 extern void
209        message_comment_append (message_ty *mp, const char *comment);
210 extern void
211        message_comment_dot_append (message_ty *mp, const char *comment);
212 extern void
213        message_comment_filepos (message_ty *mp, const char *name, size_t line);
214 extern message_ty *
215        message_copy (message_ty *mp);
216
217
218 typedef struct message_list_ty message_list_ty;
219 struct message_list_ty
220 {
221   message_ty **item;
222   size_t nitems;
223   size_t nitems_max;
224   bool use_hashtable;
225   hash_table htable;    /* Table mapping msgid to 'message_ty *'.  */
226 };
227
228 /* Create a fresh message list.
229    If USE_HASHTABLE is true, a hash table will be used to speed up
230    message_list_search().  USE_HASHTABLE can only be set to true if it is
231    known that the message list will not contain duplicate msgids.  */
232 extern message_list_ty *
233        message_list_alloc (bool use_hashtable);
234 /* Free a message list.
235    If keep_messages = 0, also free the messages.  If keep_messages = 1, don't
236    free the messages.  */
237 extern void
238        message_list_free (message_list_ty *mlp, int keep_messages);
239 extern void
240        message_list_append (message_list_ty *mlp, message_ty *mp);
241 extern void
242        message_list_prepend (message_list_ty *mlp, message_ty *mp);
243 extern void
244        message_list_insert_at (message_list_ty *mlp, size_t n, message_ty *mp);
245 extern void
246        message_list_delete_nth (message_list_ty *mlp, size_t n);
247 typedef bool message_predicate_ty (const message_ty *mp);
248 extern void
249        message_list_remove_if_not (message_list_ty *mlp,
250                                    message_predicate_ty *predicate);
251 /* Recompute the hash table of a message list after the msgids or msgctxts
252    changed.  */
253 extern bool
254        message_list_msgids_changed (message_list_ty *mlp);
255 /* Copy a message list.
256    If copy_level = 0, also copy the messages.  If copy_level = 1, share the
257    messages.  */
258 extern message_list_ty *
259        message_list_copy (message_list_ty *mlp, int copy_level);
260 extern message_ty *
261        message_list_search (message_list_ty *mlp,
262                             const char *msgctxt, const char *msgid);
263 /* Return the message in MLP which maximizes the fuzzy_search_goal_function.
264    Only messages with a fuzzy_search_goal_function > FUZZY_THRESHOLD are
265    considered.  In case of several messages with the same goal function value,
266    the one with the smaller index is returned.  */
267 extern message_ty *
268        message_list_search_fuzzy (message_list_ty *mlp,
269                                   const char *msgctxt, const char *msgid);
270
271
272 typedef struct message_list_list_ty message_list_list_ty;
273 struct message_list_list_ty
274 {
275   message_list_ty **item;
276   size_t nitems;
277   size_t nitems_max;
278 };
279
280 extern message_list_list_ty *
281        message_list_list_alloc (void);
282 /* Free a list of message lists.
283    If keep_level = 0, also free the messages.  If keep_level = 1, don't free
284    the messages but free the lists.  If keep_level = 2, don't free the
285    the messages and the lists.  */
286 extern void
287        message_list_list_free (message_list_list_ty *mllp, int keep_level);
288 extern void
289        message_list_list_append (message_list_list_ty *mllp,
290                                  message_list_ty *mlp);
291 extern void
292        message_list_list_append_list (message_list_list_ty *mllp,
293                                       message_list_list_ty *mllp2);
294 extern message_ty *
295        message_list_list_search (message_list_list_ty *mllp,
296                                  const char *msgctxt, const char *msgid);
297 extern message_ty *
298        message_list_list_search_fuzzy (message_list_list_ty *mllp,
299                                        const char *msgctxt, const char *msgid);
300
301
302 typedef struct msgdomain_ty msgdomain_ty;
303 struct msgdomain_ty
304 {
305   const char *domain;
306   message_list_ty *messages;
307 };
308
309 extern msgdomain_ty *
310        msgdomain_alloc (const char *domain, bool use_hashtable);
311 extern void
312        msgdomain_free (msgdomain_ty *mdp);
313
314
315 typedef struct msgdomain_list_ty msgdomain_list_ty;
316 struct msgdomain_list_ty
317 {
318   msgdomain_ty **item;
319   size_t nitems;
320   size_t nitems_max;
321   bool use_hashtable;
322   const char *encoding;         /* canonicalized encoding or NULL if unknown */
323 };
324
325 extern msgdomain_list_ty *
326        msgdomain_list_alloc (bool use_hashtable);
327 extern void
328        msgdomain_list_free (msgdomain_list_ty *mdlp);
329 extern void
330        msgdomain_list_append (msgdomain_list_ty *mdlp, msgdomain_ty *mdp);
331 extern void
332        msgdomain_list_append_list (msgdomain_list_ty *mdlp,
333                                    msgdomain_list_ty *mdlp2);
334 extern message_list_ty *
335        msgdomain_list_sublist (msgdomain_list_ty *mdlp, const char *domain,
336                                bool create);
337 /* Copy a message domain list.
338    If copy_level = 0, also copy the messages.  If copy_level = 1, share the
339    messages but copy the domains.  If copy_level = 2, share the domains.  */
340 extern msgdomain_list_ty *
341        msgdomain_list_copy (msgdomain_list_ty *mdlp, int copy_level);
342 extern message_ty *
343        msgdomain_list_search (msgdomain_list_ty *mdlp,
344                               const char *msgctxt, const char *msgid);
345 extern message_ty *
346        msgdomain_list_search_fuzzy (msgdomain_list_ty *mdlp,
347                                     const char *msgctxt, const char *msgid);
348
349
350 /* The goal function used in fuzzy search.
351    Higher values indicate a closer match.
352    If the result is < LOWER_BOUND, an arbitrary other value < LOWER_BOUND can
353    be returned.  */
354 extern double
355        fuzzy_search_goal_function (const message_ty *mp,
356                                    const char *msgctxt, const char *msgid,
357                                    double lower_bound);
358
359 /* The threshold for fuzzy-searching.
360    A message is considered only if
361    fuzzy_search_goal_function (mp, given, 0.0) > FUZZY_THRESHOLD.  */
362 #define FUZZY_THRESHOLD 0.6
363
364
365 #ifdef __cplusplus
366 }
367 #endif
368
369
370 #endif /* message.h */