1 //C-  -*- C++ -*-
2 //C- -------------------------------------------------------------------
3 //C- DjVuLibre-3.5
4 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5 //C- Copyright (c) 2001  AT&T
6 //C-
7 //C- This software is subject to, and may be distributed under, the
8 //C- GNU General Public License, either Version 2 of the license,
9 //C- or (at your option) any later version. The license should have
10 //C- accompanied the software or you may obtain a copy of the license
11 //C- from the Free Software Foundation at http://www.fsf.org .
12 //C-
13 //C- This program is distributed in the hope that it will be useful,
14 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //C- GNU General Public License for more details.
17 //C-
18 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19 //C- Lizardtech Software.  Lizardtech Software has authorized us to
20 //C- replace the original DjVu(r) Reference Library notice by the following
21 //C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22 //C-
23 //C-  ------------------------------------------------------------------
24 //C- | DjVu (r) Reference Library (v. 3.5)
25 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
27 //C- | 6,058,214 and patents pending.
28 //C- |
29 //C- | This software is subject to, and may be distributed under, the
30 //C- | GNU General Public License, either Version 2 of the license,
31 //C- | or (at your option) any later version. The license should have
32 //C- | accompanied the software or you may obtain a copy of the license
33 //C- | from the Free Software Foundation at http://www.fsf.org .
34 //C- |
35 //C- | The computer code originally released by LizardTech under this
36 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
37 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
38 //C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39 //C- | non-exclusive license to make, use, sell, or otherwise dispose of
40 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42 //C- | General Public License.   This grant only confers the right to
43 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44 //C- | the extent such infringement is reasonably necessary to enable
45 //C- | recipient to make, have made, practice, sell, or otherwise dispose
46 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47 //C- | any greater extent that may be necessary to utilize further
48 //C- | modifications or combinations.
49 //C- |
50 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54 //C- +------------------------------------------------------------------
55 
56 #ifndef __DJVU_MESSAGE_LITE_H__
57 #define __DJVU_MESSAGE_LITE_H__
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61 #if NEED_GNUG_PRAGMAS
62 # pragma interface
63 #endif
64 
65 // From: Leon Bottou, 1/31/2002
66 // All these I18N XML messages are Lizardtech innovations.
67 // For DjvuLibre, I changed the path extraction logic
68 // and added support for non I18N messages.
69 
70 
71 #include "GString.h"
72 
73 #ifdef HAVE_NAMESPACES
74 namespace DJVU {
75 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
76 }
77 #endif
78 #endif
79 
80 class lt_XMLTags;
81 class ByteStream;
82 
83 /** Exception causes and external messages are passed as message lists which
84     have the following syntax:
85 
86     message_list ::= single_message |
87                      single_message separator message_list
88 
89     separator ::= newline |
90                   newline | separator
91 
92     single_message ::= CTRLC message_ID |
93                        CTRLC message_ID parameters
94 
95     parameters ::= tab string |
96                    tab string parameters
97 
98     Message_IDs are looked up an external file and replaced by the message
99     text strings they are mapped to. The message text may contain the
100     following:
101 
102     Parameter specifications: These are modelled after printf format
103     specifications and have one of the following forms:
104 
105       %n!s! %n!d! %n!i! %n!u! %n!x! %n!X!
106 
107     where n is the parameter number. The parameter number is indicated
108     explicitly to allow for the possibility that the parameter order may
109     change when the message text is translated into another language.
110     The final letter ('s', 'd', 'i', 'u', 'x', or 'X') indicates the form
111     of the parameter (string, integer, integer, unsigned integer, lowercase
112     hexadecimal, or uppercase hexadecimal, respectively).  In addition
113     formating options such as precision available in sprintf, may be used.
114     So, to print the third argument as 5 digit unsigned number, with leading
115     zero's one could use:
116       %3!05u!
117 
118     All of the arguments are actually strings.  For forms that don't take
119     string input ('d', 'i', 'u', 'x', or 'X'), and atoi() conversion is done
120     on the string before formatting.  In addition the form indicates to the
121     translater whether to expect a word or a number.
122 
123     The strings are read in from XML.  To to format the strings, use the
124     relavent XML escape sequences, such as follows:
125 
126             
        [line feed]
127             	        [horizontal tab]
128             '       [single quote]
129             "        [double quote]
130             <         [less than sign]
131             >         [greater than sign]
132 
133     After parameters have been inserted in the message text, the formatting
134     strings are replaced by their usual equivalents (newline and tab
135     respectively).
136 
137     If a message_id cannot be found in the external file, a message text
138     is fabricated giving the message_id and the parameters (if any).
139 
140     Separators (newlines) are preserved in the translated message list.
141 
142     Expands message lists by looking up the message IDs and inserting
143     arguments into the retrieved messages.
144 
145     N.B. The resulting string may be encoded in UTF-8 format (ISO 10646-1
146     Annex R) and SHOULD NOT BE ASSUMED TO BE ASCII.
147   */
148 
149 class DJVUAPI DjVuMessageLite : public GPEnabled
150 {
151 protected:
152   // Constructor:
153   DjVuMessageLite( void );
154   GMap<GUTF8String,GP<lt_XMLTags> > Map;
155   GUTF8String errors;
156   /// Creates a DjVuMessage class.
157   static const DjVuMessageLite &real_create(void);
158 
159 public:
160   /// Creates a DjVuMessage class.
161   static const DjVuMessageLite& (*create)(void);
162 
163   /// Creates this class specifically.
164   static const DjVuMessageLite &create_lite(void);
165 
166   /** Adds a byte stream to be parsed whenever the next DjVuMessage::create()
167       call is made. */
168   static void AddByteStreamLater(const GP<ByteStream> &bs);
169 
170   /** Destructor: Does any necessary cleanup. Actions depend on how the message
171       file is implemented. */
172   ~DjVuMessageLite();
173 
174   /// Lookup the relavent string and parse the message.
175   GUTF8String LookUp( const GUTF8String & MessageList ) const;
176 
177   //// Same as LookUp, but this is a static method.
LookUpUTF8(const GUTF8String & MessageList)178   static GUTF8String LookUpUTF8( const GUTF8String & MessageList )
179   { return create().LookUp(MessageList); }
180 
181   /** Same as Lookup, but returns the a multibyte character string in the
182       current locale. */
LookUpNative(const GUTF8String & MessageList)183   static GNativeString LookUpNative( const GUTF8String & MessageList )
184   { return create().LookUp(MessageList).getUTF82Native(); }
185 
186   /// This is a simple alias to the above class, but does an fprintf to stderr.
187   static void perror( const GUTF8String & MessageList );
188 
189 protected:
190 
191   /*  Looks up the msgID in the file of messages. The strings message_text
192       and message_number are returned if found. If not found, these strings
193       are empty. */
194   void LookUpID( const GUTF8String & msgID,
195     GUTF8String &message_text, GUTF8String &message_number ) const;
196 
197   /*  Expands a single message and inserts the arguments. Single_Message
198       contains no separators (newlines), but includes all the parameters
199       separated by tabs. */
200   GUTF8String LookUpSingle( const GUTF8String & Single_Message ) const;
201 
202   /*  Insert a string into the message text. Will insert into any field
203       description.  Except for an ArgId of zero (message number), if the
204       #ArgId# is not found, the routine adds a line with the parameter
205       so information will not be lost. */
206   void InsertArg(
207     GUTF8String &message, const int ArgId, const GUTF8String &arg ) const;
208 
209   void AddByteStream(const GP<ByteStream> &bs);
210 
211 protected:
212   /*  Static storage of the DjVuMessage class. */
213   static GP<DjVuMessageLite> &getDjVuMessageLite(void);
214   /*  Static storage of the ByteStream list. */
215   static GPList<ByteStream> &getByteStream(void);
216 };
217 
218 
219 #ifdef HAVE_NAMESPACES
220 }
221 # ifndef NOT_USING_DJVU_NAMESPACE
222 using namespace DJVU;
223 # endif
224 #endif
225 #endif /* __DJVU_MESSAGE_LITE_H__ */
226 
227