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