• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

DjVuMessageLite.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: DjVuMessageLite.cpp,v 1.13 2005/04/27 16:34:13 leonb Exp $
00055 // $Name: release_3_5_15 $
00056 
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063 
00064 // From: Leon Bottou, 1/31/2002
00065 // All these I18N XML messages are Lizardtech innovations.
00066 // For DjvuLibre, I changed the path extraction logic
00067 // and added support for non I18N messages. 
00068 
00069 #include "DjVuMessageLite.h"
00070 #include "GOS.h"
00071 #include "XMLTags.h"
00072 #include "ByteStream.h"
00073 #include "GURL.h"
00074 #include "debug.h"
00075 #include <ctype.h>
00076 #include <string.h>
00077 #include <stdlib.h>
00078 // #include <stdio.h>
00079 #ifdef WIN32
00080 #include <tchar.h>
00081 #include <atlbase.h>
00082 #include <windows.h>
00083 #include <winreg.h>
00084 #endif
00085 #ifdef UNIX
00086 #include <unistd.h>
00087 #include <pwd.h>
00088 #include <sys/types.h>
00089 #endif
00090 #include <locale.h>
00091 
00092 
00093 #ifdef HAVE_NAMESPACES
00094 namespace DJVU {
00095 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00096 }
00097 #endif
00098 #endif
00099 
00100 
00101 const DjVuMessageLite& (*DjVuMessageLite::create)(void) = 
00102   DjVuMessageLite::create_lite; 
00103 
00104 static const char *failed_to_parse_XML=
00105   ERR_MSG("DjVuMessage.failed_to_parse_XML");
00106 static const char *unrecognized=
00107   ERR_MSG("DjVuMessage.Unrecognized");
00108 static const char *uparameter=
00109   ERR_MSG("DjVuMessage.Parameter");
00110 #ifdef LIZARDTECH_1_800_NUMBER
00111 static const char unrecognized_default[] =
00112   "** Unrecognized DjVu Message: [Contact LizardTech at " 
00113   LIZARDTECH_1_800_NUMBER " \n"
00114   "\t** Message name:  %1!s!";
00115 #else
00116 static const char unrecognized_default[] =
00117   "** Unrecognized DjVu Message:\n"
00118   "\t** Message name:  %1!s!";
00119 #endif
00120 static const char uparameter_default[] = 
00121   "\t   Parameter: %1!s!";
00122 static const char failed_to_parse_XML_default[]=
00123   "Failed to parse XML message file:&#10;&#09;&apos;%1!s!&apos;.";
00124 
00125 static const char namestring[]="name";
00126 static const char valuestring[]="value";
00127 static const char numberstring[]="number";
00128 static const char bodystring[]="BODY";
00129 static const char messagestring[]="MESSAGE";
00130 
00131 GPList<ByteStream> &
00132 DjVuMessageLite::getByteStream(void)
00133 {
00134   static GPList<ByteStream> gbs;
00135   return gbs;
00136 }
00137 
00138 GP<DjVuMessageLite> &
00139 DjVuMessageLite::getDjVuMessageLite(void)
00140 {
00141   static GP<DjVuMessageLite> message;
00142   return message;
00143 }
00144 
00145 void
00146 DjVuMessageLite::AddByteStreamLater(const GP<ByteStream> &bs)
00147 {
00148   getByteStream().append(bs);
00149 }
00150 
00151 //  There is only object of class DjVuMessage in a program, and here it is:
00152 //DjVuMessage  DjVuMsg;
00153 const DjVuMessageLite &
00154 DjVuMessageLite::create_lite(void)
00155 {
00156   GP<DjVuMessageLite> &static_message=getDjVuMessageLite();
00157   if(!static_message)
00158   {
00159     static_message=new DjVuMessageLite;
00160   }
00161   DjVuMessageLite &m=*static_message;
00162   GPList<ByteStream> &bs = getByteStream();
00163   for(GPosition pos;(pos=bs);bs.del(pos))
00164   {
00165     m.AddByteStream(bs[pos]);
00166   }
00167   return m;
00168 }
00169 
00170 // Constructor
00171 DjVuMessageLite::DjVuMessageLite( void ) {}
00172 
00173 // Destructor
00174 DjVuMessageLite::~DjVuMessageLite( ) {}
00175 
00176 
00177 void
00178 DjVuMessageLite::perror( const GUTF8String & MessageList )
00179 {
00180   DjVuPrintErrorUTF8("%s\n",(const char *)DjVuMessageLite::LookUpUTF8(MessageList));
00181 }
00182 
00183 
00184 //  Expands message lists by looking up the message IDs and inserting
00185 //  arguments into the retrieved messages.
00186 //  N.B. The resulting string may be encoded in UTF-8 format (ISO 10646-1 Annex R)
00187 //       and SHOULD NOT BE ASSUMED TO BE ASCII.
00188 GUTF8String
00189 DjVuMessageLite::LookUp( const GUTF8String & MessageList ) const
00190 {
00191 //  DEBUG_MSG( "========== DjVuMessageLite::LookUp ==========\n" <<
00192 //             MessageList <<
00193 //             "\n========== DjVuMessageLite::LookUp ==========\n" );
00194   GUTF8String result;                       // Result string; begins empty
00195   if(errors.length())
00196   {
00197     const GUTF8String err1(errors);
00198     (const_cast<GUTF8String &>(errors)).empty();
00199     result=LookUp(err1)+"\n";
00200   }
00201 
00202   int start = 0;                            // Beginning of next message
00203   int end = MessageList.length();           // End of the message string
00204 
00205   //  Isolate single messages and process them
00206   while( start < end )
00207   {
00208     if( MessageList[start] == '\n' )
00209     {
00210       result += MessageList[start++];       // move the newline to the result
00211                                             // and advance to the next message
00212     }
00213     else
00214     {
00215       //  Find the end of the next message and process it
00216       int next_ending = MessageList.search((unsigned long)'\n', start);
00217       if( next_ending < 0 )
00218         next_ending = end;
00219       result += LookUpSingle( MessageList.substr(start, next_ending-start) );
00220       //  Advance to the next message
00221       start = next_ending;
00222     }
00223   }
00224 
00225   //  All done 
00226   return result;
00227 }
00228 
00229 
00230 // Expands a single message and inserts the arguments. Single_Message
00231 // contains no separators (newlines), but includes all the parameters
00232 // separated by tabs.
00233 GUTF8String
00234 DjVuMessageLite::LookUpSingle( const GUTF8String &Single_Message ) const
00235 {
00236 #if HAS_CTRL_C_IN_ERR_MSG
00237   if (Single_Message[0] != '\003')
00238     return Single_Message;
00239 #endif
00240   //  Isolate the message ID and get the corresponding message text
00241   int ending_posn = Single_Message.contains("\t\v");
00242   if( ending_posn < 0 )
00243     ending_posn = Single_Message.length();
00244   GUTF8String msg_text;
00245   GUTF8String msg_number;
00246   const GUTF8String message=Single_Message.substr(0,ending_posn);
00247   LookUpID( message, msg_text, msg_number );
00248 
00249   //  Check whether we found anything
00250   if( !msg_text.length())
00251   {
00252     if(message == unrecognized)
00253     {
00254       msg_text = unrecognized_default;
00255     }else if(message == uparameter)
00256     {
00257       msg_text = uparameter_default;
00258     }else if(message == failed_to_parse_XML)
00259     {
00260       msg_text = failed_to_parse_XML_default;
00261     }else
00262     {
00263       return LookUpSingle(unrecognized + ("\t" + Single_Message));
00264     }
00265   }
00266     
00267   //  Insert the parameters (if any)
00268   unsigned int param_num = 0;
00269   while( (unsigned int)ending_posn < Single_Message.length() )
00270   {
00271     GUTF8String arg;
00272     const int start_posn = ending_posn+1;
00273     if(Single_Message[ending_posn] == '\v')
00274     {
00275       ending_posn=Single_Message.length();
00276       arg=LookUpSingle(Single_Message.substr(start_posn,ending_posn));
00277     }else
00278     {
00279       ending_posn = Single_Message.contains("\v\t",start_posn);
00280       if( ending_posn < 0 )
00281         ending_posn = Single_Message.length();
00282       arg=Single_Message.substr(start_posn, ending_posn-start_posn);
00283     }
00284     InsertArg( msg_text, ++param_num, arg);
00285   }
00286   //  Insert the message number
00287   InsertArg( msg_text, 0, msg_number );
00288 
00289   return msg_text;
00290 }
00291 
00292 
00293 // Looks up the msgID in the file of messages and returns a pointer to
00294 // the beginning of the translated message, if found; and an empty string
00295 // otherwise.
00296 void
00297 DjVuMessageLite::LookUpID( const GUTF8String &xmsgID,
00298                        GUTF8String &message_text,
00299                        GUTF8String &message_number ) const
00300 {
00301   if(!Map.isempty())
00302   {
00303     GUTF8String msgID = xmsgID;
00304 #if HAS_CTRL_C_IN_ERR_MSG
00305     int start = 0;
00306     while (msgID[start] == '\003') 
00307       start ++;
00308     if (start > 0)
00309       msgID = msgID.substr(start, msgID.length() - start);
00310 #endif
00311     GPosition pos=Map.contains(msgID);
00312     if(pos)
00313     {
00314       const GP<lt_XMLTags> tag=Map[pos];
00315       GPosition valuepos=tag->get_args().contains(valuestring);
00316       if(valuepos)
00317       {
00318         message_text=tag->get_args()[valuepos];
00319       }else
00320       {
00321         const GUTF8String raw(tag->get_raw());
00322         const int start_line=raw.search((unsigned long)'\n',0);
00323       
00324         const int start_text=raw.nextNonSpace(0);
00325         const int end_text=raw.firstEndSpace(0);
00326         if(start_line<0 || start_text<0 || start_text < start_line)
00327         {
00328           message_text=raw.substr(0,end_text).fromEscaped();
00329         }else
00330         {
00331           message_text=raw.substr(start_line+1,end_text-start_line-1).fromEscaped();
00332         }
00333       }
00334       GPosition numberpos=tag->get_args().contains(numberstring);
00335       if(numberpos)
00336       {
00337         message_number=tag->get_args()[numberpos];
00338       }
00339     }
00340   }
00341 }
00342 
00343 
00344 // Insert a string into the message text. Will insert into any field
00345 // description.  Except for an ArgId of zero (message number), if the ArgId
00346 // is not found, the routine adds a line with the parameter so information
00347 // will not be lost.
00348 void
00349 DjVuMessageLite::InsertArg( GUTF8String &message,
00350   const int ArgId, const GUTF8String &arg ) const
00351 {
00352     // argument target string
00353   const GUTF8String target= "%"+GUTF8String(ArgId)+"!";
00354     // location of target string
00355   int format_start = message.search( (const char *)target );
00356   if( format_start >= 0 )
00357   {
00358     do
00359     {
00360       const int n=format_start+target.length()+1;
00361       const int format_end=message.search((unsigned long)'!',n);
00362       if(format_end > format_start)
00363       { 
00364         const int len=1+format_end-n;
00365         if(len && isascii(message[n-1]))
00366         {
00367           GUTF8String narg;
00368           GUTF8String format="%"+message.substr(n-1,len);
00369           switch(format[len])
00370           {
00371             case 'd':
00372             case 'i':
00373               narg.format((const char *)format,arg.toInt());
00374               break;
00375             case 'u':
00376             case 'o':
00377             case 'x':
00378             case 'X':
00379               narg.format((const char *)format,(unsigned int)arg.toInt());
00380               break;
00381             case 'f':
00382               {
00383                 int endpos;
00384                 narg.format((const char *)format, arg.toDouble(0,endpos));
00385                 if( endpos < 0 )
00386                   narg = arg;
00387               }
00388               break;
00389             default:
00390               narg.format((const char *)format,(const char *)arg);
00391               break;
00392           }
00393           message = message.substr( 0, format_start )+narg
00394             +message.substr( format_end+1, -1 );
00395         }else
00396         {
00397           message = message.substr( 0, format_start )+arg
00398             +message.substr( format_end+1, -1 );
00399         }
00400       }
00401       format_start=message.search((const char*)target, format_start+arg.length());
00402     } while(format_start >= 0);
00403   }
00404   else
00405   {
00406     //  Not found, fake it
00407     if( ArgId != 0 )
00408     {
00409       message += "\n"+LookUpSingle(uparameter+("\t"+arg));
00410     }
00411   }
00412 }
00413 
00414 
00415 //  A C function to perform a message lookup. Arguments are a buffer to received the
00416 //  translated message, a buffer size (bytes), and a message_list. The translated
00417 //  result is returned in msg_buffer encoded in UTF-8. In case of error, msg_buffer is
00418 //  empty (i.e., msg_buffer[0] == '\0').
00419 void 
00420 DjVuMessageLite_LookUp( char *msg_buffer, const unsigned int buffer_size, const char *message )
00421 {
00422   GUTF8String converted = DjVuMessageLite::LookUpUTF8( message );
00423   if( converted.length() >= buffer_size )
00424     msg_buffer[0] = '\0';
00425   else
00426     strcpy( msg_buffer, converted );
00427 }
00428 
00429 void
00430 DjVuMessageLite::AddByteStream(const GP<ByteStream> &bs)
00431 {
00432   const GP<lt_XMLTags> gtags(lt_XMLTags::create(bs));
00433   lt_XMLTags &tags=*gtags;
00434   GPList<lt_XMLTags> Bodies=tags.get_Tags(bodystring);
00435   if(! Bodies.isempty())
00436   {
00437     lt_XMLTags::get_Maps(messagestring,namestring,Bodies,Map);
00438   }
00439 }
00440 
00441 
00442 
00443 #ifdef HAVE_NAMESPACES
00444 }
00445 # ifndef NOT_USING_DJVU_NAMESPACE
00446 using namespace DJVU;
00447 # endif
00448 #endif
00449 
00450 void
00451 DjVuWriteError( const char *message )
00452 {
00453   G_TRY {
00454     GP<ByteStream> errout = ByteStream::get_stderr();
00455     if (errout)
00456       {
00457         const GUTF8String external = DjVuMessageLite::LookUpUTF8( message );
00458         errout->writestring(external+"\n");
00459       }
00460     // Need to catch all exceptions because these might be 
00461     // called from an outer exception handler (with prejudice)
00462   } G_CATCH_ALL { } G_ENDCATCH;
00463 }
00464 
00465 void
00466 DjVuWriteMessage( const char *message )
00467 {
00468   G_TRY {
00469     GP<ByteStream> strout = ByteStream::get_stdout();
00470     if (strout)
00471       {
00472         const GUTF8String external = DjVuMessageLite::LookUpUTF8( message );
00473         strout->writestring(external+"\n");
00474       }
00475     // Need to catch all exceptions because these might be 
00476     // called from an outer exception handler (with prejudice)
00477   } G_CATCH_ALL { } G_ENDCATCH;
00478 }

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal