source: trunk/libdjvu/DjVuMessage.cpp @ 209

Last change on this file since 209 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

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