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