source: trunk/libdjvu/DjVuMessage.cpp @ 280

Last change on this file since 280 was 280, checked in by rbri, 11 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 17.5 KB
Line 
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// $Id: DjVuMessage.cpp,v 1.24 2008/01/27 17:32:56 leonb Exp $
57// $Name: release_3_5_22 $
58
59#ifdef HAVE_CONFIG_H
60# include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma implementation
64#endif
65
66// From: Leon Bottou, 1/31/2002
67// All these XML messages are Lizardtech innovations.
68
69#include "DjVuMessage.h"
70#include "GOS.h"
71#include "XMLTags.h"
72#include "ByteStream.h"
73#include "GURL.h"
74#include "debug.h"
75#include <ctype.h>
76#include <string.h>
77#include <stdlib.h>
78#ifdef WIN32
79# include <tchar.h>
80# include <windows.h>
81# include <winreg.h>
82#endif
83#ifdef UNIX
84# include <unistd.h>
85# include <pwd.h>
86# include <sys/types.h>
87#endif
88#include <locale.h>
89#ifndef LC_MESSAGES
90# define LC_MESSAGES LC_ALL
91#endif
92
93
94#ifdef HAVE_NAMESPACES
95namespace DJVU {
96# ifdef NOT_DEFINED // Just to fool emacs c++ mode
97}
98#endif
99#endif
100
101GUTF8String &
102DjVuMessage::programname(void)
103{
104  static GUTF8String xprogramname;
105  use_language();
106  return xprogramname;
107}
108
109static const char namestring[]="name";
110static const char srcstring[]="src";
111
112static const char *failed_to_parse_XML=ERR_MSG("DjVuMessage.failed_to_parse_XML");
113static const char bodystring[]="BODY";
114static const char languagestring[]="LANGUAGE";
115static const char headstring[]="HEAD";
116static const char includestring[]="INCLUDE";
117static const char messagestring[]="MESSAGE";
118static const char localestring[]="locale";
119
120
121// directory names for searching messages
122#ifdef AUTOCONF
123static const char DjVuDataDir[] = DIR_DATADIR "/djvu/osi";
124#endif /* AUTOCONF */
125static const char ModuleDjVuDir[] ="share/djvu/osi";
126static const char ProfilesDjVuDir[] ="profiles";
127static const char LocalDjVuDir[] =".DjVu";      // relative to ${HOME}
128#ifdef LT_DEFAULT_PREFIX
129static const char DjVuPrefixDir[] = LT_DEFAULT_PREFIX "/profiles";
130#endif
131#ifndef NDEBUG
132static const char DebugModuleDjVuDir[] ="../TOPDIR/SRCDIR/profiles";
133#endif
134#ifdef WIN32
135static const char RootDjVuDir[] ="C:/Program Files/LizardTech/Profiles";
136static const TCHAR registrypath[]= TEXT("Software\\LizardTech\\DjVu\\Profile Path");
137#else
138static const char RootDjVuDir[] ="/etc/DjVu/";  // global last resort
139#endif
140
141static const char DjVuEnv[] = "DJVU_CONFIG_DIR";
142
143//  The name of the message file
144static const char MessageFile[]="messages.xml";
145static const char LanguageFile[]="languages.xml";
146
147#ifdef WIN32
148static GURL
149RegOpenReadConfig ( HKEY hParentKey )
150{
151  GURL retval;
152  LPCTSTR path = registrypath;
153  HKEY hKey = 0;
154  if (RegOpenKeyEx(hParentKey, path, 0,
155                   KEY_READ, &hKey) == ERROR_SUCCESS )
156  {
157    CHAR path[1024];
158    CHAR *szPathValue = path;
159    LPCSTR lpszEntry = "";
160    DWORD dwCount = (sizeof(path)/sizeof(CHAR))-1;
161    DWORD dwType;
162    LONG lResult = RegQueryValueExA(hKey, lpszEntry, NULL,
163                        &dwType, (LPBYTE)szPathValue, &dwCount);
164    RegCloseKey(hKey);
165    if ((lResult == ERROR_SUCCESS))
166    {
167      szPathValue[dwCount] = 0;
168      retval=GURL::Filename::Native(path);
169    }
170  } 
171  return retval;
172}
173
174static GURL
175GetModulePath( void )
176{
177  const GUTF8String cwd(GOS::cwd());
178  CHAR path[1024];
179  DWORD dwCount = (sizeof(path)/sizeof(CHAR))-1;
180  GetModuleFileNameA(0, path, dwCount);
181  GURL retval=GURL::Filename::Native(path).base();
182  GOS::cwd(cwd);
183  return retval;
184}
185#elif defined(UNIX)
186
187static GList<GURL>
188parsePATH(void)
189{
190  GList<GURL> retval;
191  const char *path=getenv("PATH");
192  if(path)
193  {
194    GNativeString p(path);
195    int from=0;
196    for(int to;(to=p.search(':',from))>0;from=to+1)
197    {
198      if(to > from)
199      {
200        retval.append(GURL::Filename::Native(p.substr(from,to-from)));
201      }
202    }
203    if((from+1)<(int)p.length())
204    {
205      retval.append(GURL::Filename::Native(p.substr(from,-1)));
206    }
207  }
208  return retval;
209}
210
211static GURL
212GetModulePath( void )
213{
214 GURL retval;
215 GUTF8String &xprogramname=DjVuMessage::programname();
216 if(xprogramname.length())
217 {
218   if(xprogramname[1]=='/'
219     ||!xprogramname.cmp("../",3)
220     ||!xprogramname.cmp("./",2))
221   {
222     retval=GURL::Filename::UTF8(xprogramname);
223   }
224   if(retval.is_empty() || !retval.is_file())
225   {
226     GList<GURL> paths(parsePATH());
227     GMap<GUTF8String,void *> pathMAP;
228     for(GPosition pos=paths;pos;++pos)
229     {
230       retval=GURL::UTF8(xprogramname,paths[pos]);
231       const GUTF8String path(retval.get_string());
232       if(!pathMAP.contains(path))
233       {
234         if(retval.is_file())
235           break;
236         pathMAP[path]=0;
237       }
238     }
239   }
240   if (! retval.is_empty() )
241     retval = retval.follow_symlinks();
242   if (! retval.is_empty() )
243     retval = retval.base();
244 }
245 return retval;
246}
247#endif
248
249static void
250appendPath(const GURL &url, 
251           GMap<GUTF8String,void *> &map,
252           GList<GURL> &list)
253{
254  if( !url.is_empty() && !map.contains(url.get_string()) )
255    {
256      map[url.get_string()]=0;
257      list.append(url);
258    }
259}
260
261GList<GURL>
262DjVuMessage::GetProfilePaths(void)
263{
264  static bool first=true;
265  static GList<GURL> realpaths;
266  if(first)
267  {
268    first=false;
269    GMap<GUTF8String,void *> pathsmap;
270    GList<GURL> paths;
271    GURL path;
272    const GUTF8String envp(GOS::getenv(DjVuEnv));
273    if(envp.length())
274      appendPath(GURL::Filename::UTF8(envp),pathsmap,paths);
275#if defined(WIN32) || defined(UNIX)
276    GURL mpath(GetModulePath());
277    if(!mpath.is_empty() && mpath.is_dir())
278    {
279#if defined(UNIX) && !defined(AUTOCONF) && !defined(NDEBUG)
280      appendPath(GURL::UTF8(DebugModuleDjVuDir,mpath),pathsmap,paths);
281#endif
282      appendPath(mpath,pathsmap,paths);
283      appendPath(GURL::UTF8(ModuleDjVuDir,mpath),pathsmap,paths);
284      appendPath(GURL::UTF8(ProfilesDjVuDir,mpath),pathsmap,paths);
285      mpath=mpath.base();
286      appendPath(GURL::UTF8(ModuleDjVuDir,mpath),pathsmap,paths);
287      appendPath(GURL::UTF8(ProfilesDjVuDir,mpath),pathsmap,paths);
288      mpath=mpath.base();
289      appendPath(GURL::UTF8(ModuleDjVuDir,mpath),pathsmap,paths);
290      appendPath(GURL::UTF8(ProfilesDjVuDir,mpath),pathsmap,paths);
291      mpath=mpath.base();
292      appendPath(GURL::UTF8(ModuleDjVuDir,mpath),pathsmap,paths);
293      appendPath(GURL::UTF8(ProfilesDjVuDir,mpath),pathsmap,paths);
294    }
295#endif
296#if defined(AUTOCONF)
297    GURL dpath = GURL::Filename::UTF8(DjVuDataDir);
298    appendPath(dpath,pathsmap,paths);
299#endif
300#ifdef WIN32
301    appendPath(RegOpenReadConfig(HKEY_CURRENT_USER),pathsmap,paths);
302    appendPath(RegOpenReadConfig(HKEY_LOCAL_MACHINE),pathsmap,paths);
303#else
304    GUTF8String home=GOS::getenv("HOME");
305# if HAVE_GETPWUID
306    if (! home.length()) {
307      struct passwd *pw=0;
308      if ((pw = getpwuid(getuid())))
309        home=GNativeString(pw->pw_dir);
310    }
311# endif
312    if (home.length()) {
313      GURL hpath = GURL::UTF8(LocalDjVuDir,GURL::Filename::UTF8(home));
314      appendPath(hpath,pathsmap,paths);
315    }
316#endif
317#ifdef LT_DEFAULT_PREFIX
318    appendPath(GURL::Filename::UTF8(DjVuPrefixDir),pathsmap,paths);
319#endif
320    appendPath(GURL::Filename::UTF8(RootDjVuDir),pathsmap,paths);
321    pathsmap.empty();
322
323    GPosition pos;
324    GList< GMap<GUTF8String,GP<lt_XMLTags> > > localemaps;
325    for(pos=paths;pos;++pos)
326    {
327      path=GURL::UTF8(LanguageFile,paths[pos]);
328      if(path.is_file())
329      {
330        const GP<lt_XMLTags> xml(lt_XMLTags::create(ByteStream::create(path,"rb")));
331        const GPList<lt_XMLTags> Body(xml->get_Tags(bodystring));
332        GPosition pos=Body;
333        if(!pos || (pos != Body.lastpos()))
334        {
335          G_THROW( ERR_MSG("XMLAnno.extra_body") );
336        }
337        const GP<lt_XMLTags> GBody(Body[pos]);
338        if(!GBody)
339        {
340          G_THROW( ERR_MSG("XMLAnno.no_body") );
341        }
342        GMap<GUTF8String,GP<lt_XMLTags> > localemap;
343        lt_XMLTags::get_Maps(languagestring,localestring,Body,localemap);
344        localemaps.append(localemap);
345      }
346    } 
347    GList<GURL> localepaths;
348
349    // Need to do it the right way!
350    GUTF8String defaultlocale = getenv("LANGUAGE");
351    if (! defaultlocale) 
352      {
353        const GUTF8String oldlocale(setlocale(LC_MESSAGES,0));
354        defaultlocale = setlocale(LC_MESSAGES,"");
355        setlocale(LC_MESSAGES,(const char *)oldlocale);
356      }
357    // Unfathomable search.
358    for(int loop=0; loop<2; loop++)
359    {
360      static const char sepchars[]=" _.@";
361      const char *p=sepchars+sizeof(sepchars)-1;
362      do
363      {
364        int sepcharpos=p[0]?defaultlocale.search(p[0]):defaultlocale.length();
365        if(sepcharpos > 0)
366        {
367          const GUTF8String sublocale(defaultlocale,sepcharpos);
368          const GUTF8String downcasesublocale("downcase^"+sublocale.downcase());
369          for(pos=localemaps;pos;++pos) 
370          {
371            const GMap<GUTF8String,GP<lt_XMLTags> > &localemap=localemaps[pos];
372            GPosition pos=localemap.contains(sublocale);
373            if(!pos)
374              pos=localemap.contains(downcasesublocale);
375            if(pos)
376            {
377              const GMap<GUTF8String,GUTF8String>&args
378                = localemap[pos]->get_args();
379              pos = args.contains(srcstring);
380              if (pos)
381              {
382                const GUTF8String src(args[pos]);
383                for(pos=paths;pos;++pos)
384                {
385                  path=GURL::UTF8(src,paths[pos]);
386                  if(path.is_dir())
387                    localepaths.append(path);
388                }
389              }
390              // We don't need to check anymore language files.
391              p=sepchars;
392              break;
393            }
394          }
395          if(!pos)
396            {
397            for(pos=paths;pos;++pos)
398              {
399              path=GURL::UTF8(sublocale,paths[pos]);
400              if(path.is_dir())
401                localepaths.append(path);
402            }
403          }
404        }
405      } while(p-- != sepchars);
406      if((GPosition) localepaths)
407        break;
408      defaultlocale="C";
409    }
410    for(pos=localepaths;pos;++pos)
411      appendPath(localepaths[pos],pathsmap,realpaths);
412    for(pos=paths;pos;++pos)
413      appendPath(paths[pos],pathsmap,realpaths);
414  }
415  return realpaths;
416}
417
418static GUTF8String
419getbodies(
420  GList<GURL> &paths,
421  const GUTF8String &MessageFileName,
422  GPList<lt_XMLTags> &body, 
423  GMap<GUTF8String, void *> & map )
424{
425  GUTF8String errors;
426  bool isdone=false;
427  GPosition firstpathpos=paths;
428  for(GPosition pathpos=firstpathpos;!isdone && pathpos;++pathpos)
429  {
430    const GURL::UTF8 url(MessageFileName,paths[pathpos]);
431    if(url.is_file())
432    {
433      map[MessageFileName]=0;
434      GP<lt_XMLTags> gtags;
435      {
436        GP<ByteStream> bs=ByteStream::create(url,"rb");
437        G_TRY
438        {
439          gtags=lt_XMLTags::create(bs);
440        }
441        G_CATCH(ex)
442        {
443          GUTF8String mesg(failed_to_parse_XML+("\t"+url.get_string()));
444          if(errors.length())
445          {
446            errors+="\n"+mesg;
447          }else
448          {
449            errors=mesg;
450          }
451          errors+="\n"+GUTF8String(ex.get_cause());
452        }
453        G_ENDCATCH;
454      }
455      if(gtags)
456      {
457        lt_XMLTags &tags=*gtags;
458        GPList<lt_XMLTags> Bodies=tags.get_Tags(bodystring);
459        if(! Bodies.isempty())
460        {
461          isdone=true;
462          for(GPosition pos=Bodies;pos;++pos)
463          {
464            body.append(Bodies[pos]);
465          }
466        }
467        GPList<lt_XMLTags> Head=tags.get_Tags(headstring);
468        if(! Head.isempty())
469        {
470          isdone=true;
471          GMap<GUTF8String, GP<lt_XMLTags> > includes;
472          lt_XMLTags::get_Maps(includestring,namestring,Head,includes);
473          for(GPosition pos=includes;pos;++pos)
474          {
475            const GUTF8String file=includes.key(pos);
476            if(! map.contains(file))
477            {
478              GList<GURL> xpaths;
479              xpaths.append(url.base());
480              const GUTF8String err2(getbodies(xpaths,file,body,map));
481              if(err2.length())
482              {
483                if(errors.length())
484                {
485                  errors+="\n"+err2;
486                }else
487                {
488                  errors=err2;
489                }
490              }
491            }
492          }
493        }
494      }
495    }
496  }
497  return errors;
498}
499
500static GUTF8String
501parse(GMap<GUTF8String,GP<lt_XMLTags> > &retval)
502{
503  GUTF8String errors;
504  GPList<lt_XMLTags> body;
505  {
506    GList<GURL> paths=DjVuMessage::GetProfilePaths();
507    GMap<GUTF8String, void *> map;
508    GUTF8String m(MessageFile);
509    errors=getbodies(paths,m,body,map);
510  }
511  if(! body.isempty())
512  {
513    lt_XMLTags::get_Maps(messagestring,namestring,body,retval);
514  }
515  return errors;
516}
517
518
519const DjVuMessageLite &
520DjVuMessage::create_full(void)
521{
522  GP<DjVuMessageLite> &static_message=getDjVuMessageLite();
523  if(!static_message)
524  {
525    DjVuMessage *mesg=new DjVuMessage;
526    static_message=mesg;
527    mesg->init();
528  }
529  return DjVuMessageLite::create_lite();
530}
531
532void
533DjVuMessage::set_programname(const GUTF8String &xprogramname)
534{
535  programname()=xprogramname;
536  DjVuMessageLite::create=create_full; 
537}
538
539void
540DjVuMessage::use_language(void)
541{ 
542  DjVuMessageLite::create=create_full; 
543}
544
545
546// Constructor
547DjVuMessage::DjVuMessage( void ) {}
548
549void
550DjVuMessage::init(void)
551{
552  errors=parse(Map);
553}
554
555// Destructor
556DjVuMessage::~DjVuMessage( )
557{
558}
559
560
561//  A C function to perform a message lookup. Arguments are a buffer to receiv
562//  translated message, a buffer size (bytes), and a message_list. The transla
563//  result is returned in msg_buffer encoded in Native MBS encoding. In case
564// of error, msg_b empty (i.e., msg_buffer[0] == '\0').
565void
566DjVuMessageLookUpNative( 
567  char *msg_buffer, const unsigned int buffer_size, const char *message)
568{
569  const GNativeString converted(DjVuMessage::LookUpNative( message ));
570  if( converted.length() >= buffer_size )
571    msg_buffer[0] = '\0';
572  else
573    strcpy( msg_buffer, converted );
574}
575
576//  A C function to perform a message lookup. Arguments are a buffer to receiv
577//  translated message, a buffer size (bytes), and a message_list. The transla
578//  result is returned in msg_buffer encoded in UTF8 encoding. In case
579// of error, msg_b empty (i.e., msg_buffer[0] == '\0').
580void
581DjVuMessageLookUpUTF8( 
582  char *msg_buffer, const unsigned int buffer_size, const char *message)
583{
584  const GUTF8String converted(DjVuMessage::LookUpUTF8( message ));
585  if( converted.length() >= buffer_size )
586    msg_buffer[0] = '\0';
587  else
588    strcpy( msg_buffer, converted );
589}
590
591
592
593#ifdef HAVE_NAMESPACES
594}
595# ifndef NOT_USING_DJVU_NAMESPACE
596using namespace DJVU;
597# endif
598#endif
599
600void
601DjVuFormatErrorUTF8( const char *fmt, ... )
602{
603  va_list args;
604  va_start(args, fmt); 
605  const GUTF8String message(fmt,args);
606  DjVuWriteError( message );
607}
608
609void
610DjVuFormatErrorNative( const char *fmt, ... )
611{
612  va_list args;
613  va_start(args, fmt); 
614  const GNativeString message(fmt,args);
615  DjVuWriteError( message );
616}
617
618const char *
619djvu_programname(const char *xprogramname)
620{
621  if(xprogramname)
622    DjVuMessage::programname()=GNativeString(xprogramname);
623  return DjVuMessage::programname();
624}
Note: See TracBrowser for help on using the repository browser.