source: trunk/libdjvu/DjVuMessage.cpp @ 15

Last change on this file since 15 was 15, checked in by Eugene Romanenko, 15 years ago

needed libs update

File size: 18.0 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, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: DjVuMessage.cpp,v 1.17 2005/08/07 21:32:28 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64// From: Leon Bottou, 1/31/2002
65// All these XML messages are Lizardtech innovations.
66
67#include "DjVuMessage.h"
68#include "GOS.h"
69#include "XMLTags.h"
70#include "ByteStream.h"
71#include "GURL.h"
72#include "debug.h"
73#include <ctype.h>
74#include <string.h>
75#include <stdlib.h>
76#ifdef WIN32
77# include <tchar.h>
78# include <atlbase.h>
79# include <windows.h>
80# include <winreg.h>
81#endif
82#ifdef UNIX
83# include <unistd.h>
84# include <pwd.h>
85# include <sys/types.h>
86#endif
87#include <locale.h>
88#ifndef LC_MESSAGES
89# define LC_MESSAGES LC_ALL
90#endif
91
92
93#ifdef HAVE_NAMESPACES
94namespace DJVU {
95# ifdef NOT_DEFINED // Just to fool emacs c++ mode
96}
97#endif
98#endif
99
100GUTF8String &
101DjVuMessage::programname(void)
102{
103  static GUTF8String xprogramname;
104  use_language();
105  return xprogramname;
106}
107
108static const char namestring[]="name";
109static const char srcstring[]="src";
110
111static const char *failed_to_parse_XML=ERR_MSG("DjVuMessage.failed_to_parse_XML");
112static const char bodystring[]="BODY";
113static const char languagestring[]="LANGUAGE";
114static const char headstring[]="HEAD";
115static const char includestring[]="INCLUDE";
116static const char messagestring[]="MESSAGE";
117static const char localestring[]="locale";
118
119
120// directory names for searching messages
121static const char opensourcedir[]="osi";
122#ifdef AUTOCONF
123//static const char DjVuDataDir[] = DIR_DATADIR "/djvu";
124static const char ModuleDjVuDir[] ="share/djvu";
125#else /* !AUTOCONF */
126static const char ModuleDjVuDir[] ="profiles";
127#endif /* !AUTOCONF */
128static const char LocalDjVuDir[] =".DjVu";      // relative to ${HOME}
129#ifdef LT_DEFAULT_PREFIX
130static const char DjVuPrefixDir[] = LT_DEFAULT_PREFIX "/profiles";
131#endif
132#ifndef NDEBUG
133static const char DebugModuleDjVuDir[] ="../TOPDIR/SRCDIR/profiles";
134#endif
135#ifdef WIN32
136static const char RootDjVuDir[] ="C:/Program Files/LizardTech/Profiles";
137static const TCHAR registrypath[]= TEXT("Software\\LizardTech\\DjVu\\Profile Path");
138#else
139static const char RootDjVuDir[] ="/etc/DjVu/";  // global last resort
140#endif
141
142static const char DjVuEnv[] = "DJVU_CONFIG_DIR";
143
144//  The name of the message file
145static const char MessageFile[]="messages.xml";
146static const char LanguageFile[]="languages.xml";
147
148#ifdef WIN32
149static GURL
150RegOpenReadConfig ( HKEY hParentKey )
151{
152  GURL retval;
153   // To do:  This needs to be shared with SetProfile.cpp
154  LPCTSTR path = registrypath;
155
156  HKEY hKey = 0;
157  // MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,argv[1],strlen(argv[1])+1,wszSrcFile,sizeof(wszSrcFile));
158  if (RegOpenKeyEx(hParentKey, path, 0,
159              KEY_READ, &hKey) == ERROR_SUCCESS )
160  {
161    TCHAR path[1024];
162    // Success
163    TCHAR *szPathValue = path;
164    LPCTSTR lpszEntry = (LPCTSTR &)TEXT("");
165    DWORD dwCount = (sizeof(path)/sizeof(TCHAR))-1;
166    DWORD dwType;
167
168    LONG lResult = RegQueryValueEx(hKey, lpszEntry, NULL,
169             &dwType, (LPBYTE) szPathValue, &dwCount);
170
171    RegCloseKey(hKey);
172
173    if ((lResult == ERROR_SUCCESS))
174    {
175      szPathValue[dwCount] = 0;
176      USES_CONVERSION;
177      retval=GURL::Filename::Native(T2CA(path));
178    }
179  } 
180//  if (hKey)  RegCloseKey(hKey);
181  return retval;
182}
183
184static GURL
185GetModulePath( void )
186{
187  const GUTF8String cwd(GOS::cwd());
188  TCHAR path[1024];
189  DWORD dwCount = (sizeof(path)/sizeof(TCHAR))-1;
190  GetModuleFileName(0, path, dwCount);
191  USES_CONVERSION;
192  GURL retval=GURL::Filename::Native(T2CA(path)).base();
193  GOS::cwd(cwd);
194  return retval;
195}
196#elif defined(UNIX)
197
198static GList<GURL>
199parsePATH(void)
200{
201  GList<GURL> retval;
202  const char *path=getenv("PATH");
203  if(path)
204  {
205    GNativeString p(path);
206    int from=0;
207    for(int to;(to=p.search(':',from))>0;from=to+1)
208    {
209      if(to > from)
210      {
211        retval.append(GURL::Filename::Native(p.substr(from,to-from)));
212      }
213    }
214    if((from+1)<(int)p.length())
215    {
216      retval.append(GURL::Filename::Native(p.substr(from,-1)));
217    }
218  }
219  return retval;
220}
221
222static GURL
223GetModulePath( void )
224{
225 GURL retval;
226 GUTF8String &xprogramname=DjVuMessage::programname();
227 if(xprogramname.length())
228 {
229   if(xprogramname[1]=='/'
230     ||!xprogramname.cmp("../",3)
231     ||!xprogramname.cmp("./",2))
232   {
233     retval=GURL::Filename::UTF8(xprogramname);
234   }
235   if(retval.is_empty() || !retval.is_file())
236   {
237     GList<GURL> paths(parsePATH());
238     GMap<GUTF8String,void *> pathMAP;
239     for(GPosition pos=paths;pos;++pos)
240     {
241       retval=GURL::UTF8(xprogramname,paths[pos]);
242       const GUTF8String path(retval.get_string());
243       if(!pathMAP.contains(path))
244       {
245         if(retval.is_file())
246           break;
247         pathMAP[path]=0;
248       }
249     }
250   }
251   if (! retval.is_empty() )
252     retval = retval.follow_symlinks();
253   if (! retval.is_empty() )
254     retval = retval.base();
255 }
256 return retval;
257}
258#endif
259
260static void
261appendPath(const GURL &url, 
262           GMap<GUTF8String,void *> &map,
263           GList<GURL> &list)
264{
265  if( !url.is_empty() 
266      && !map.contains(url.get_string()) && url.is_dir() )
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   // 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    GList<GURL> osilocalepaths;
356
357    // Need to do it the right way!
358    GUTF8String defaultlocale = getenv("LANGUAGE");
359    if (! defaultlocale) 
360      {
361        const GUTF8String oldlocale(setlocale(LC_MESSAGES,0));
362        defaultlocale = setlocale(LC_MESSAGES,"");
363        setlocale(LC_MESSAGES,(const char *)oldlocale);
364      }
365    // Unfathomable search.
366    for(int loop=0; loop<2; loop++)
367    {
368      static const char sepchars[]=" _.@";
369      const char *p=sepchars+sizeof(sepchars)-1;
370      do
371      {
372        int sepcharpos=p[0]?defaultlocale.search(p[0]):defaultlocale.length();
373        if(sepcharpos > 0)
374        {
375          const GUTF8String sublocale(defaultlocale,sepcharpos);
376          const GUTF8String downcasesublocale("downcase^"+sublocale.downcase());
377          for(pos=localemaps;pos;++pos) 
378          {
379            const GMap<GUTF8String,GP<lt_XMLTags> > &localemap=localemaps[pos];
380            GPosition pos=localemap.contains(sublocale);
381            if(!pos)
382              pos=localemap.contains(downcasesublocale);
383            if(pos)
384            {
385              const GMap<GUTF8String,GUTF8String>&args
386                = localemap[pos]->get_args();
387              pos = args.contains(srcstring);
388              if (pos)
389              {
390                const GUTF8String src(args[pos]);
391                for(pos=paths;pos;++pos)
392                {
393                  path=GURL::UTF8(src,paths[pos]);
394                  if(path.is_dir())
395                    localepaths.append(path);
396                  path=GURL::UTF8(GUTF8String(opensourcedir)+"/"+src,paths[pos]);
397                  if(path.is_dir())
398                    osilocalepaths.append(path);
399                }
400              }
401              // We don't need to check anymore language files.
402              p=sepchars;
403              break;
404            }
405          }
406          if(!pos)
407          {
408            for(pos=paths;pos;++pos)
409            {
410              path=GURL::UTF8(sublocale,paths[pos]);
411              if(path.is_dir())
412              {
413                localepaths.append(path);
414              }
415              path=GURL::UTF8(GUTF8String(opensourcedir)+"/"+sublocale,paths[pos]);
416              if(path.is_dir())
417              {
418                osilocalepaths.append(path);
419              }
420            }
421          }
422        }
423      } while(p-- != sepchars);
424      if((GPosition) localepaths)
425        break;
426      defaultlocale="C";
427    }
428    for(pos=localepaths;pos;++pos)
429      appendPath(localepaths[pos],pathsmap,realpaths);
430    for(pos=paths;pos;++pos)
431      appendPath(paths[pos],pathsmap,realpaths);
432    for(pos=osilocalepaths;pos;++pos)
433      appendPath(osilocalepaths[pos],pathsmap,realpaths);
434    for(pos=paths;pos;++pos)
435      {
436        path=GURL::UTF8(opensourcedir,paths[pos]);
437        appendPath(path,pathsmap,realpaths);
438      }
439  }
440  return realpaths;
441}
442
443static GUTF8String
444getbodies(
445  GList<GURL> &paths,
446  const GUTF8String &MessageFileName,
447  GPList<lt_XMLTags> &body, 
448  GMap<GUTF8String, void *> & map )
449{
450  GUTF8String errors;
451  bool isdone=false;
452  GPosition firstpathpos=paths;
453  for(GPosition pathpos=firstpathpos;!isdone && pathpos;++pathpos)
454  {
455    const GURL::UTF8 url(MessageFileName,paths[pathpos]);
456    if(url.is_file())
457    {
458      map[MessageFileName]=0;
459      GP<lt_XMLTags> gtags;
460      {
461        GP<ByteStream> bs=ByteStream::create(url,"rb");
462        G_TRY
463        {
464          gtags=lt_XMLTags::create(bs);
465        }
466        G_CATCH(ex)
467        {
468          GUTF8String mesg(failed_to_parse_XML+("\t"+url.get_string()));
469          if(errors.length())
470          {
471            errors+="\n"+mesg;
472          }else
473          {
474            errors=mesg;
475          }
476          errors+="\n"+GUTF8String(ex.get_cause());
477        }
478        G_ENDCATCH;
479      }
480      if(gtags)
481      {
482        lt_XMLTags &tags=*gtags;
483        GPList<lt_XMLTags> Bodies=tags.get_Tags(bodystring);
484        if(! Bodies.isempty())
485        {
486          isdone=true;
487          for(GPosition pos=Bodies;pos;++pos)
488          {
489            body.append(Bodies[pos]);
490          }
491        }
492        GPList<lt_XMLTags> Head=tags.get_Tags(headstring);
493        if(! Head.isempty())
494        {
495          isdone=true;
496          GMap<GUTF8String, GP<lt_XMLTags> > includes;
497          lt_XMLTags::get_Maps(includestring,namestring,Head,includes);
498          for(GPosition pos=includes;pos;++pos)
499          {
500            const GUTF8String file=includes.key(pos);
501            if(! map.contains(file))
502            {
503              GList<GURL> xpaths;
504              xpaths.append(url.base());
505              const GUTF8String err2(getbodies(xpaths,file,body,map));
506              if(err2.length())
507              {
508                if(errors.length())
509                {
510                  errors+="\n"+err2;
511                }else
512                {
513                  errors=err2;
514                }
515              }
516            }
517          }
518        }
519      }
520    }
521  }
522  return errors;
523}
524
525static GUTF8String
526parse(GMap<GUTF8String,GP<lt_XMLTags> > &retval)
527{
528  GUTF8String errors;
529  GPList<lt_XMLTags> body;
530  {
531    GList<GURL> paths=DjVuMessage::GetProfilePaths();
532    GMap<GUTF8String, void *> map;
533    GUTF8String m(MessageFile);
534    errors=getbodies(paths,m,body,map);
535  }
536  if(! body.isempty())
537  {
538    lt_XMLTags::get_Maps(messagestring,namestring,body,retval);
539  }
540  return errors;
541}
542
543
544const DjVuMessageLite &
545DjVuMessage::create_full(void)
546{
547  GP<DjVuMessageLite> &static_message=getDjVuMessageLite();
548  if(!static_message)
549  {
550    DjVuMessage *mesg=new DjVuMessage;
551    static_message=mesg;
552    mesg->init();
553  }
554  return DjVuMessageLite::create_lite();
555}
556
557void
558DjVuMessage::set_programname(const GUTF8String &xprogramname)
559{
560  programname()=xprogramname;
561  DjVuMessageLite::create=create_full; 
562}
563
564void
565DjVuMessage::use_language(void)
566{ 
567  DjVuMessageLite::create=create_full; 
568}
569
570
571// Constructor
572DjVuMessage::DjVuMessage( void ) {}
573
574void
575DjVuMessage::init(void)
576{
577  errors=parse(Map);
578}
579
580// Destructor
581DjVuMessage::~DjVuMessage( )
582{
583}
584
585
586//  A C function to perform a message lookup. Arguments are a buffer to receiv
587//  translated message, a buffer size (bytes), and a message_list. The transla
588//  result is returned in msg_buffer encoded in Native MBS encoding. In case
589// of error, msg_b empty (i.e., msg_buffer[0] == '\0').
590void
591DjVuMessageLookUpNative( 
592  char *msg_buffer, const unsigned int buffer_size, const char *message)
593{
594  const GNativeString converted(DjVuMessage::LookUpNative( message ));
595  if( converted.length() >= buffer_size )
596    msg_buffer[0] = '\0';
597  else
598    strcpy( msg_buffer, converted );
599}
600
601//  A C function to perform a message lookup. Arguments are a buffer to receiv
602//  translated message, a buffer size (bytes), and a message_list. The transla
603//  result is returned in msg_buffer encoded in UTF8 encoding. In case
604// of error, msg_b empty (i.e., msg_buffer[0] == '\0').
605void
606DjVuMessageLookUpUTF8( 
607  char *msg_buffer, const unsigned int buffer_size, const char *message)
608{
609  const GUTF8String converted(DjVuMessage::LookUpUTF8( message ));
610  if( converted.length() >= buffer_size )
611    msg_buffer[0] = '\0';
612  else
613    strcpy( msg_buffer, converted );
614}
615
616
617
618#ifdef HAVE_NAMESPACES
619}
620# ifndef NOT_USING_DJVU_NAMESPACE
621using namespace DJVU;
622# endif
623#endif
624
625void
626DjVuFormatErrorUTF8( const char *fmt, ... )
627{
628  va_list args;
629  va_start(args, fmt); 
630  const GUTF8String message(fmt,args);
631  DjVuWriteError( message );
632}
633
634void
635DjVuFormatErrorNative( const char *fmt, ... )
636{
637  va_list args;
638  va_start(args, fmt); 
639  const GNativeString message(fmt,args);
640  DjVuWriteError( message );
641}
642
643const char *
644djvu_programname(const char *xprogramname)
645{
646  if(xprogramname)
647    DjVuMessage::programname()=GNativeString(xprogramname);
648  return DjVuMessage::programname();
649}
Note: See TracBrowser for help on using the repository browser.