kviewshell
DjVuMessageLite.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063
00064
00065
00066
00067
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
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: 	'%1!s!'.";
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
00152
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
00171 DjVuMessageLite::DjVuMessageLite( void ) {}
00172
00173
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
00185
00186
00187
00188 GUTF8String
00189 DjVuMessageLite::LookUp( const GUTF8String & MessageList ) const
00190 {
00191
00192
00193
00194 GUTF8String result;
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;
00203 int end = MessageList.length();
00204
00205
00206 while( start < end )
00207 {
00208 if( MessageList[start] == '\n' )
00209 {
00210 result += MessageList[start++];
00211
00212 }
00213 else
00214 {
00215
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
00221 start = next_ending;
00222 }
00223 }
00224
00225
00226 return result;
00227 }
00228
00229
00230
00231
00232
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
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
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
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
00287 InsertArg( msg_text, 0, msg_number );
00288
00289 return msg_text;
00290 }
00291
00292
00293
00294
00295
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
00345
00346
00347
00348 void
00349 DjVuMessageLite::InsertArg( GUTF8String &message,
00350 const int ArgId, const GUTF8String &arg ) const
00351 {
00352
00353 const GUTF8String target= "%"+GUTF8String(ArgId)+"!";
00354
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
00407 if( ArgId != 0 )
00408 {
00409 message += "\n"+LookUpSingle(uparameter+("\t"+arg));
00410 }
00411 }
00412 }
00413
00414
00415
00416
00417
00418
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
00461
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
00476
00477 } G_CATCH_ALL { } G_ENDCATCH;
00478 }