Ignore:
Timestamp:
Jan 3, 2010, 7:13:32 PM (11 years ago)
Author:
rbri
Message:

DJVU plugin: djvulibre updated to version 3.5.22

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libdjvu/GString.cpp

    r207 r280  
    5454//C- +------------------------------------------------------------------
    5555//
    56 // $Id: GString.cpp,v 1.24 2007/03/25 20:48:32 leonb Exp $
    57 // $Name: release_3_5_19 $
     56// $Id: GString.cpp,v 1.27 2009/05/17 23:57:42 leonb Exp $
     57// $Name: release_3_5_22 $
    5858
    5959// From: Leon Bottou, 1/31/2002
     
    204204GP<GStringRep>
    205205GStringRep::UTF8::create(
    206   const unsigned short *s,const int start,const int length)
     206  const uint16_t *s,const int start,const int length)
    207207{
    208208  GStringRep::UTF8 dummy;
     
    212212GP<GStringRep>
    213213GStringRep::UTF8::create(
    214   const unsigned long *s,const int start,const int length)
     214  const uint32_t *s,const int start,const int length)
    215215{
    216216  GStringRep::UTF8 dummy;
     
    337337    const char *s,const int start,const int length=(-1));
    338338  static GP<GStringRep> create(
    339     const unsigned short *s,const int start,const int length=(-1));
     339    const uint16_t *s,const int start,const int length=(-1));
    340340  static GP<GStringRep> create(
    341     const unsigned long *s,const int start,const int length=(-1));
     341    const uint32_t *s,const int start,const int length=(-1));
    342342
    343343    // Create with an sprintf()
     
    346346
    347347  virtual unsigned char *UCS4toString(
    348     const unsigned long w,unsigned char *ptr, mbstate_t *ps=0) const;
     348    const uint32_t w,unsigned char *ptr, mbstate_t *ps=0) const;
    349349
    350350  // Tests if a string is legally encoded in the current character set.
     
    356356protected:
    357357  // Return the next character and increment the source pointer.
    358   virtual unsigned long getValidUCS4(const char *&source) const;
     358  virtual uint32_t getValidUCS4(const char *&source) const;
    359359};
    360360
     
    411411GP<GStringRep>
    412412GStringRep::Native::create(
    413     const unsigned short *s,const int start,const int length)
     413    const uint16_t *s,const int start,const int length)
    414414{
    415415  GStringRep::Native dummy;
     
    419419GP<GStringRep>
    420420GStringRep::Native::create(
    421   const unsigned long *s,const int start,const int length)
     421  const uint32_t *s,const int start,const int length)
    422422{
    423423  GStringRep::Native dummy;
     
    533533unsigned char *
    534534GStringRep::Native::UCS4toString(
    535   const unsigned long w0,unsigned char *ptr, mbstate_t *ps) const
     535  const uint32_t w0,unsigned char *ptr, mbstate_t *ps) const
    536536{
    537537  return UCS4toNative(w0,ptr,ps);
     
    544544unsigned char *
    545545GStringRep::UCS4toNative(
    546   const unsigned long w0,unsigned char *ptr, mbstate_t *ps)
    547 {
    548   unsigned short w1;
    549   unsigned short w2=1;
     546  const uint32_t w0,unsigned char *ptr, mbstate_t *ps)
     547{
     548  uint16_t w1;
     549  uint16_t w2=1;
    550550  for(int count=(sizeof(wchar_t)==sizeof(w1)) ? UCS4toUTF16(w0,w1,w2) : 1;
    551551      count;
     
    589589    memset(&ps,0,sizeof(mbstate_t));
    590590    int i=0;
    591     if(sizeof(wchar_t) == sizeof(unsigned long))
     591    if(sizeof(wchar_t) == sizeof(uint32_t))
    592592      {
    593593        wchar_t w = 0;
    594594        for(;(n>0)&&((i=mbrtowc(&w,source,n,&ps))>=0); n-=i,source+=i)
    595595          {
    596             ptr=UCS4toUTF8(w,ptr);
     596            ptr=UCS4toUTF8((uint32_t)w,ptr);
    597597          }
    598598      }
     
    602602        for(;(n>0)&&((i=mbrtowc(&w,source,n,&ps))>=0);n-=i,source+=i)
    603603          {
    604             unsigned short s[2];
     604            uint16_t s[2];
    605605            s[0]=w;
    606             unsigned long w0;
     606            uint32_t w0;
    607607            if(UTF16toUCS4(w0,s,s+1)<=0)
    608608              {
     
    682682  if(slen>1)
    683683  {
    684 #ifndef OS2  // ER 
     684/* Lucide */
     685#ifndef OS2
    685686    retval=UTF8ToNative(false,escape) ;
    686687#endif
     
    830831}
    831832
    832 unsigned long
     833uint32_t
    833834GStringRep::Native::getValidUCS4(const char *&source) const
    834835{
    835   unsigned long retval=0;
     836  uint32_t retval=0;
    836837  int n=(int)((size_t)size+(size_t)data-(size_t)source);
    837838  if(source && (n > 0))
     
    844845    if(len>=0)
    845846    {
    846       if(sizeof(wchar_t) == sizeof(unsigned short))
     847      if(sizeof(wchar_t) == sizeof(uint16_t))
    847848      {
    848849        source+=len;
    849         unsigned short s[2];
    850         s[0]=(unsigned short)wt;
     850        uint16_t s[2];
     851        s[0]=(uint16_t)wt;
    851852        if(UTF16toUCS4(retval,s,s+1)<=0)
    852853        {
     
    856857            if(len>=0)
    857858            {
    858               s[1]=(unsigned short)wt;
    859               unsigned long w;
     859              s[1]=(uint16_t)wt;
     860              uint32_t w;
    860861              if(UTF16toUCS4(w,s,s+2)>0)
    861862              {
     
    868869      }else
    869870      {
    870         retval=(unsigned long)wt;
     871        retval=(uint32_t)wt;
    871872        source++;
    872873      }
     
    942943}
    943944
    944 GNativeString::GNativeString(const unsigned short *str)
     945GNativeString::GNativeString(const uint16_t *str)
    945946{
    946947  init(GStringRep::Native::create(str,0,-1));
    947948}
    948949
    949 GNativeString::GNativeString(const unsigned long *str)
     950GNativeString::GNativeString(const uint32_t *str)
    950951{
    951952  init(GStringRep::Native::create(str,0,-1));
     
    958959}
    959960
    960 GNativeString::GNativeString(const unsigned short *dat, unsigned int len)
     961GNativeString::GNativeString(const uint16_t *dat, unsigned int len)
    961962{
    962963  init(
     
    964965}
    965966
    966 GNativeString::GNativeString(const unsigned long *dat, unsigned int len)
     967GNativeString::GNativeString(const uint32_t *dat, unsigned int len)
    967968{
    968969  init(
     
    10971098
    10981099GP<GStringRep>
    1099 GStringRep::substr(const unsigned short *s,const int start,const int len) const
     1100GStringRep::substr(const uint16_t *s,const int start,const int len) const
    11001101{
    11011102  GP<GStringRep> retval;
    11021103  if(s && s[0])
    11031104  {
    1104     unsigned short const *eptr;
     1105    uint16_t const *eptr;
    11051106    if(len<0)
    11061107    {
     
    11201121      for(ptr=buf;s[0];)
    11211122      {
    1122         unsigned long w;
     1123        uint32_t w;
    11231124        int i=UTF16toUCS4(w,s,eptr);
    11241125        if(i<=0)
     
    11351136
    11361137GP<GStringRep>
    1137 GStringRep::substr(const unsigned long *s,const int start,const int len) const
     1138GStringRep::substr(const uint32_t *s,const int start,const int len) const
    11381139{
    11391140  GP<GStringRep> retval;
    11401141  if(s && s[0])
    11411142  {
    1142     unsigned long const *eptr;
     1143    uint32_t const *eptr;
    11431144    if(len<0)
    11441145    {
     
    12981299{
    12991300#if HAS_WCTYPE
    1300   return
    1301     ((sizeof(wchar_t) == 2)&&(w&~0xffff))
    1302     ||((unsigned long)iswspace((wchar_t)w))
    1303     ||((w == '\r')||(w == '\n'));
     1301  return iswspace((wchar_t)w);
    13041302#else
    1305   return
    1306     (w&~0xff)?(true):(((unsigned long)isspace((char)w))||((w == '\r')||(w == '\n')));
     1303  if (w & ~0xff) || isspace((int)(w & 0xff));
    13071304#endif
    13081305}
     
    13121309{
    13131310#if HAS_WCTYPE
    1314   return ((sizeof(wchar_t) == 2)&&(w&~0xffff))
    1315     ?(true):((unsigned long)iswupper((wchar_t)w)?true:false);
     1311  return iswupper((wchar_t)w);
    13161312#else
    1317   return (w&~0xff)?(true):((unsigned long)isupper((char)w)?true:false);
     1313  return (w & ~0xff) || isupper((int)(w & 0xff));
    13181314#endif
    13191315}
     
    13231319{
    13241320#if HAS_WCTYPE
    1325   return ((sizeof(wchar_t) == 2)&&(w&~0xffff))
    1326     ?(true):((unsigned long)iswlower((wchar_t)w)?true:false);
     1321  return iswlower((wchar_t)w);
    13271322#else
    1328   return (w&~0xff)?(true):((unsigned long)islower((char)w)?true:false);
     1323  return (w & ~0xff) || islower((int)(w & 0xff));
    13291324#endif
    13301325}
     
    13341329{
    13351330#if HAS_WCTYPE
    1336   return ((sizeof(wchar_t) == 2)&&(w&~0xffff))
    1337     ?w:((unsigned long)towupper((wchar_t)w));
     1331  return (unsigned long)towupper((wchar_t)w);
    13381332#else
    1339   return (w&~0xff)?w:((unsigned long)toupper((char)w));
     1333  return (w&~0xff) ? w : (unsigned long)toupper(w & 0xff);
    13401334#endif
    13411335}
     
    13451339{
    13461340#if HAS_WCTYPE
    1347   return ((sizeof(wchar_t) == 2)&&(w&~0xffff))
    1348     ?w:((unsigned long)towlower((wchar_t)w));
     1341  return (unsigned long)towlower((wchar_t)w);
    13491342#else
    1350   return (w&~0xff)?w:((unsigned long)tolower((char)w));
     1343  return (w&~0xff) ? w : (unsigned long)tolower(w & 0xff);
    13511344#endif
    13521345}
     
    16061599}
    16071600
    1608 #ifdef WIN32
    1609 #define USE_VSNPRINTF _vsnprintf
    1610 #endif
    1611 
    1612 #ifdef AUTOCONF
    1613 # ifdef HAVE_VSNPRINTF
    1614 #  define USE_VSNPRINTF vsnprintf
    1615 # endif
    1616 #else
    1617 # ifdef linux
    1618 #  define USE_VSNPRINTF vsnprintf
    1619 # endif
    1620 #endif
    1621 
     1601#if defined(AUTOCONF) && defined(HAVE_VSNPRINTF)
     1602# define USE_VSNPRINTF vsnprintf
     1603#elif defined(WIN32) && !defined(__CYGWIN32__)
     1604# define USE_VSNPRINTF _vsnprintf
     1605#elif defined(linux)
     1606# define USE_VSNPRINTF vsnprintf
     1607#endif
     1608 
    16221609GUTF8String &
    16231610GUTF8String::format(const char fmt[], ... )
     
    16421629  if(size)
    16431630  {
    1644 #ifndef WIN32
    1645     char *nfmt;
    1646     GPBuffer<char> gnfmt(nfmt,size+1);
    1647     nfmt[0]=0;
    1648     int start=0;
    1649 #endif
    1650     int from=0;
    1651     while((from=search('%',from)) >= 0)
    1652     {
    1653       if(data[++from] != '%')
    1654       {
    1655         int m,n=0;
    1656         sscanf(data+from,"%d!%n",&m,&n);
    1657         if(n)
    1658         {
    1659 #ifdef WIN32
    1660           char *lpszFormat=data;
    1661           LPTSTR lpszTemp;
    1662           if((!::FormatMessage(
    1663             FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
    1664               lpszFormat, 0, 0, (LPTSTR)&lpszTemp,0,&args))
    1665             || !lpszTemp)
    1666           {
    1667             G_THROW(GException::outofmemory);
    1668           }
    1669           va_end(args);
    1670           retval=strdup((const char *)lpszTemp);
    1671           LocalFree(lpszTemp);
    1672           break;
    1673 #else
    1674           from+=n;
    1675           const int end=search('!',from);
    1676           if(end>=0)
    1677           {
    1678             strncat(nfmt,data+start,(int)(end-start));
    1679             strncat(nfmt,"$",1);
    1680             start=from=end+1;
    1681           }else
    1682           {
    1683             gnfmt.resize(0);
    1684             from=(-1);
    1685             break;
    1686           }
    1687 #endif
    1688         }else
    1689         {
    1690 #ifndef WIN32
    1691           gnfmt.resize(0);
    1692 #endif
    1693           from=(-1);
    1694           break;
    1695         }
    1696       }
    1697     }
    1698     if(from < 0)
    1699     {
    1700 #ifndef WIN32
    1701       char const * const fmt=(nfmt&&nfmt[0])?nfmt:data;
    1702 #else
    1703       char const * const fmt=data;
    1704 #endif
    1705       int buflen=32768;
    1706       char *buffer;
    1707       GPBuffer<char> gbuffer(buffer,buflen);
    1708 
    1709       ChangeLocale locale(LC_NUMERIC,(isNative()?0:"C"));
    1710 
    1711       // Format string
     1631    char const * const fmt=data;
     1632    int buflen=32768;
     1633    char *buffer;
     1634    GPBuffer<char> gbuffer(buffer,buflen);
     1635    ChangeLocale locale(LC_NUMERIC,(isNative()?0:"C"));
     1636    // Format string
    17121637#ifdef USE_VSNPRINTF
    1713       while(USE_VSNPRINTF(buffer, buflen, fmt, args)<0)
     1638    while(USE_VSNPRINTF(buffer, buflen, fmt, args)<0)
    17141639      {
    17151640        gbuffer.resize(0);
    17161641        gbuffer.resize(buflen+32768);
    17171642      }
    1718       va_end(args);
     1643    va_end(args);
    17191644#else
    1720       buffer[buflen-1] = 0;
    1721       vsprintf(buffer, fmt, args);
    1722       va_end(args);
    1723       if (buffer[buflen-1])
     1645    buffer[buflen-1] = 0;
     1646    vsprintf(buffer, fmt, args);
     1647    va_end(args);
     1648    if (buffer[buflen-1])
    17241649      {
    17251650        // This isn't as fatal since it is on the stack, but we
     
    17281653      }
    17291654#endif
    1730       retval=strdup((const char *)buffer);
    1731     }
     1655    retval=strdup((const char *)buffer);
    17321656  }
    17331657  // Go altering the string
     
    18861810unsigned char *
    18871811GStringRep::UTF8::UCS4toString(
    1888   const unsigned long w0,unsigned char *ptr, mbstate_t *) const
     1812  const uint32_t w0,unsigned char *ptr, mbstate_t *) const
    18891813{
    18901814  return UCS4toUTF8(w0,ptr);
     
    19071831      for(const unsigned char *s=(const unsigned char *)data;(r<rend)&&(s<eptr)&&*s;)
    19081832          {
    1909         const unsigned long w0=UTF8toUCS4(s,eptr);
    1910         unsigned short w1;
    1911         unsigned short w2=1;
     1833        const uint32_t w0=UTF8toUCS4(s,eptr);
     1834        uint16_t w1;
     1835        uint16_t w2=1;
    19121836        for(int count=(sizeof(wchar_t) == sizeof(w1))?UCS4toUTF16(w0,w1,w2):1;
    19131837            count&&(r<rend);
     
    19451869    for(const unsigned char *s=(const unsigned char *)data;(s<eptr)&& *s;)
    19461870    {
    1947       const unsigned long w0=UTF8toUCS4(s,eptr);
     1871      const uint32_t w0=UTF8toUCS4(s,eptr);
    19481872      const unsigned char * const r0=r;
    19491873      r=UCS4toNative(w0,r,&ps);
     
    19521876        if(escape == IS_ESCAPED)
    19531877        {
    1954           sprintf((char *)r,"&#%lu;",w0);
     1878          sprintf((char *)r,"&#%lu;",(unsigned long)w0);
    19551879          r+=strlen((char *)r);
    19561880        }else
     
    20001924}
    20011925
    2002 static inline unsigned long
    2003 add_char(unsigned long const U, unsigned char const * const r)
    2004 {
    2005   unsigned long const C=r[0];
     1926static inline uint32_t
     1927add_char(uint32_t const U, unsigned char const * const r)
     1928{
     1929  uint32_t const C=r[0];
    20061930  return ((C|0x3f) == 0xbf)?((U<<6)|(C&0x3f)):0;
    20071931}
    20081932
    2009 unsigned long
     1933uint32_t
    20101934GStringRep::UTF8toUCS4(
    20111935  unsigned char const *&s,void const * const eptr)
    20121936{
    2013   unsigned long U=0;
     1937  uint32_t U=0;
    20141938  unsigned char const *r=s;
    20151939  if(r < eptr)
    20161940  {
    2017     unsigned long const C1=r++[0];
     1941    uint32_t const C1=r++[0];
    20181942    if(C1&0x80)
    20191943    {
     
    21132037
    21142038unsigned char *
    2115 GStringRep::UCS4toUTF8(const unsigned long w,unsigned char *ptr)
     2039GStringRep::UCS4toUTF8(const uint32_t w,unsigned char *ptr)
    21162040{
    21172041  if(w <= 0x7f)
     
    22402164  return locale;
    22412165}
     2166const char *setlocale_win32_var = setlocale_win32();
    22422167#endif
    22432168
    22442169GStringRep::GStringRep(void)
    22452170{
    2246 #ifdef WIN32
    2247   static const char *locale=setlocale_win32();
    2248 #endif
    22492171  size=0;
    22502172  data=0;
     
    24752397
    24762398int
    2477 GStringRep::getUCS4(unsigned long &w, const int from) const
     2399GStringRep::getUCS4(uint32_t &w, const int from) const
    24782400{
    24792401  int retval;
     
    24962418
    24972419
    2498 unsigned long
     2420uint32_t
    24992421GStringRep::UTF8::getValidUCS4(const char *&source) const
    25002422{
     
    25502472int
    25512473GStringRep::UCS4toUTF16(
    2552   const unsigned long w,unsigned short &w1, unsigned short &w2)
     2474  const uint32_t w,uint16_t &w1, uint16_t &w2)
    25532475{
    25542476  int retval;
    25552477  if(w<0x10000)
    25562478  {
    2557     w1=(unsigned short)w;
     2479    w1=(uint16_t)w;
    25582480    w2=0;
    25592481    retval=1;
    25602482  }else
    25612483  {
    2562     w1=(unsigned short)((((w-0x10000)>>10)&0x3ff)+0xD800);
    2563     w2=(unsigned short)((w&0x3ff)+0xDC00);
     2484    w1=(uint16_t)((((w-0x10000)>>10)&0x3ff)+0xD800);
     2485    w2=(uint16_t)((w&0x3ff)+0xDC00);
    25642486    retval=2;
    25652487  }
     
    25692491int
    25702492GStringRep::UTF16toUCS4(
    2571   unsigned long &U,unsigned short const * const s,void const * const eptr)
     2493  uint32_t &U,uint16_t const * const s,void const * const eptr)
    25722494{
    25732495  int retval=0;
    25742496  U=0;
    2575   unsigned short const * const r=s+1;
     2497  uint16_t const * const r=s+1;
    25762498  if(r <= eptr)
    25772499  {
    2578     unsigned long const W1=s[0];
     2500    uint32_t const W1=s[0];
    25792501    if((W1<0xD800)||(W1>0xDFFF))
    25802502    {
     
    25852507    }else if(W1<=0xDBFF)
    25862508    {
    2587       unsigned short const * const rr=r+1;
     2509      uint16_t const * const rr=r+1;
    25882510      if(rr <= eptr)
    25892511      {
    2590         unsigned long const W2=s[1];
     2512        uint32_t const W2=s[1];
    25912513        if(((W2>=0xDC00)||(W2<=0xDFFF))&&((U=(0x10000+((W1&0x3ff)<<10))|(W2&0x3ff))))
    25922514        {
     
    26882610{ init(GStringRep::UTF8::create((const char *)str)); }
    26892611
    2690 GUTF8String::GUTF8String(const unsigned short *str)
     2612GUTF8String::GUTF8String(const uint16_t *str)
    26912613{ init(GStringRep::UTF8::create(str,0,-1)); }
    26922614
    2693 GUTF8String::GUTF8String(const unsigned long *str)
     2615GUTF8String::GUTF8String(const uint32_t *str)
    26942616{ init(GStringRep::UTF8::create(str,0,-1)); }
    26952617
     
    26972619{ init(GStringRep::UTF8::create(dat,0,((int)len<0)?(-1):(int)len)); }
    26982620
    2699 GUTF8String::GUTF8String(const unsigned short *dat, unsigned int len)
     2621GUTF8String::GUTF8String(const uint16_t *dat, unsigned int len)
    27002622{ init(GStringRep::UTF8::create(dat,0,((int)len<0)?(-1):(int)len)); }
    27012623
    2702 GUTF8String::GUTF8String(const unsigned long *dat, unsigned int len)
     2624GUTF8String::GUTF8String(const uint32_t *dat, unsigned int len)
    27032625{ init(GStringRep::UTF8::create(dat,0,((int)len<0)?(-1):(int)len)); }
    27042626
Note: See TracChangeset for help on using the changeset viewer.