You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ab...@locus.apache.org on 2000/02/14 18:59:50 UTC

cvs commit: xml-xerces/c/src/util/Transcoders/Iconv390 Iconv390TransService.cpp Iconv390TransService.hpp

abagchi     00/02/14 09:59:50

  Modified:    c/src/util/Transcoders/Iconv390 Iconv390TransService.cpp
                        Iconv390TransService.hpp
  Log:
  Reused iconv descriptors
  
  Revision  Changes    Path
  1.5       +137 -73   xml-xerces/c/src/util/Transcoders/Iconv390/Iconv390TransService.cpp
  
  Index: Iconv390TransService.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/util/Transcoders/Iconv390/Iconv390TransService.cpp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Iconv390TransService.cpp	2000/02/11 03:10:20	1.4
  +++ Iconv390TransService.cpp	2000/02/14 17:59:49	1.5
  @@ -56,6 +56,9 @@
   
   /**
    * $Log: Iconv390TransService.cpp,v $
  + * Revision 1.5  2000/02/14 17:59:49  abagchi
  + * Reused iconv descriptors
  + *
    * Revision 1.4  2000/02/11 03:10:20  rahulj
    * Fixed defect in compare[N]IString function. Defect and fix reported
    * by Bill Schindler from developer@bitranch.com.
  @@ -88,6 +91,7 @@
   #ifdef OS390BATCH
   #include <unistd.h>
   #endif
  +#include <ctype.h>
   //
   //  Cannot use the OS/390 c/c++ towupper and towlower functions in the
   //  Unicode environment. We will use mytowupper and mytowlower here.
  @@ -145,7 +149,61 @@
       ,   0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1
       ,   0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF
   };
  +iconvconverter * converterList;
  +XMLMutex  converterListMutex;
  +
  +iconvconverter * addConverter(const char* const EncodingName
  +                             ,XMLTransService::Codes& resValue)
  +{
  +    XMLMutexLock lockConverterlist(&converterListMutex);
  +    iconvconverter *tconv=converterList;
  +    while ( (tconv) &&
  +            (strcmp(tconv->name,EncodingName)) )
  +      tconv = tconv->nextconverter;
  +
  +    if (tconv) {
  +      tconv->usecount++;
  +    }
  +    else {
  +      tconv = new iconvconverter;
  +      strcpy(tconv->name,EncodingName);
  +      tconv->usecount=1;
  +      tconv->fIconv390Descriptor = iconv_open("UCS-2",EncodingName);
  +      if (tconv->fIconv390Descriptor == (iconv_t)(-1)) {
  +         resValue = XMLTransService::UnsupportedEncoding;
  +         delete tconv;
  +         return 0;
  +      }
  +      tconv->nextconverter = converterList;
  +      converterList = tconv;
  +    }
  +    return tconv;
  +}
  +
  +void removeConverter(iconvconverter* const converter)
  +{
  +    iconvconverter *pconv,*tconv;
  +    tconv = 0;
  +    if (converter) {
  +      XMLMutexLock lockConverterlist(&converterListMutex);
  +      if (--converter->usecount==0) {
  +        tconv = converterList;
  +        pconv = (iconvconverter*)&converterList;
  +        while ( (tconv) && (tconv!=converter) ) {
  +          pconv=tconv;
  +          tconv=tconv->nextconverter;
  +        }
  +
  +        pconv->nextconverter=tconv->nextconverter;
  +      }
  +    }
   
  +    if (tconv) {
  +      iconv_close(tconv->fIconv390Descriptor);
  +      delete tconv;
  +    }
  +}
  +
   // ---------------------------------------------------------------------------
   //  Local methods
   // ---------------------------------------------------------------------------
  @@ -177,44 +235,48 @@
   //  Iconv390TransService: The virtual transcoding service API
   // ---------------------------------------------------------------------------
   int Iconv390TransService::compareIString(  const   XMLCh* const    comp1
  -                                           , const XMLCh* const    comp2)
  +                                        , const XMLCh* const    comp2)
   {
       const XMLCh* cptr1 = comp1;
       const XMLCh* cptr2 = comp2;
   
  -    while ( (*cptr1 != 0) && (*cptr2 != 0) )
  +    while ((*cptr1 != 0) && (*cptr2 != 0))
       {
  -        wint_t wch1 = towupper(*cptr1);
  -        wint_t wch2 = towupper(*cptr2);
  -        if (wch1 != wch2)
  -            break;
  -        
  +        wint_t  wch1 = towupper(*cptr1);
  +        wint_t  wch2 = towupper(*cptr2);
  +        if (wch1 < wch2)
  +            return -1;
  +        if (wch1 > wch2)
  +            return 1;
  +
           cptr1++;
           cptr2++;
       }
  -    return (int) ( towupper(*cptr1) - towupper(*cptr2) );
  +    return 0;
   }
   
   int Iconv390TransService::compareNIString( const   XMLCh* const    comp1
  -                                           , const XMLCh* const    comp2
  -                                           , const unsigned int    maxChars)
  +                                        , const XMLCh* const    comp2
  +                                        , const unsigned int    maxChars)
   {
  -    unsigned int  n = 0;
       const XMLCh* cptr1 = comp1;
       const XMLCh* cptr2 = comp2;
   
  -    while ( (*cptr1 != 0) && (*cptr2 != 0) && (n < maxChars) )
  +    unsigned int  n = 0;
  +    while ((*cptr1 != 0) && (*cptr2 != 0) && (n < maxChars))
       {
  -        wint_t wch1 = towupper(*cptr1);
  -        wint_t wch2 = towupper(*cptr2);
  -        if (wch1 != wch2)
  -            break;
  -        
  +        wint_t  wch1 = towupper(*cptr1);
  +        wint_t  wch2 = towupper(*cptr2);
  +        if (wch1 < wch2)
  +            return -1;
  +        if (wch1 > wch2)
  +            return 1;
  +
           cptr1++;
           cptr2++;
           n++;
       }
  -    return (int) ( towupper(*cptr1) - towupper(*cptr2) );
  +    return 0;
   }
   
   const XMLCh* Iconv390TransService::getId() const
  @@ -230,14 +292,15 @@
   
   XMLLCPTranscoder* Iconv390TransService::makeNewLCPTranscoder()
   {
  +    XMLTransService::Codes resValue;
       // native MVS default code page is IBM-037
  -    iconv_t Iconv390Descriptor = iconv_open("UCS-2","IBM-037");
  +    iconvconverter *tconv=addConverter("IBM-037",resValue);
   
  -    if (Iconv390Descriptor == (iconv_t)(-1)) {
  +    if (tconv == 0) {
           return 0;
       }
   
  -    return new Iconv390LCPTranscoder(Iconv390Descriptor);
  +    return new Iconv390LCPTranscoder(tconv);
   }
   
   bool Iconv390TransService::supportsSrcOfs() const
  @@ -258,29 +321,25 @@
       //  Translate the input encodingName from Unicode XMLCh format into
       //  ibm-037 char format via the lookup table.
       //
  -    iconv_t Iconv390Descriptor;
       char charEncodingName[256];
       const XMLCh*  srcPtr = encodingName;
       char*         outPtr = charEncodingName;
       while (*srcPtr != 0)
  -        *outPtr++ = gUnicodeToIBM037XlatTable[*srcPtr++];
  +        *outPtr++ = toupper(gUnicodeToIBM037XlatTable[*srcPtr++]);
       *outPtr=0;
   
  -    Iconv390Descriptor = iconv_open("UCS-2",charEncodingName);
  -    if (Iconv390Descriptor == (iconv_t)(-1)) {
  -       resValue = XMLTransService::UnsupportedEncoding;
  -       return 0;
  -    }
  -    return new Iconv390Transcoder(Iconv390Descriptor, encodingName, 0);
  +    iconvconverter *tconv=addConverter(charEncodingName,resValue);
  +
  +    return new Iconv390Transcoder(tconv, encodingName, 0);
   }
   
   void Iconv390TransService::upperCase(XMLCh* const toUpperCase) const
   {
       XMLCh* outPtr = toUpperCase;
       while (*outPtr != 0) {
  -    if ((*outPtr >= 0x61) && (*outPtr <= 0x7A))
  -        *outPtr = *outPtr - 0x20;
  -    outPtr++;
  +	if ((*outPtr >= 0x61) && (*outPtr <= 0x7A))
  +	    *outPtr = *outPtr - 0x20;
  +	outPtr++;
       }
   }
   
  @@ -357,17 +416,17 @@
       if (toTranscode)
       {
           unsigned int  wLent = getWideCharLength(toTranscode);
  -    //
  -    //  Translate the input from Unicode XMLCh format into
  -    //  ibm-037 char format via the lookup table.
  -    //
  +	//
  +	//  Translate the input from Unicode XMLCh format into
  +	//  ibm-037 char format via the lookup table.
  +	//
           retVal = new char[wLent + 1];
           const XMLCh *srcPtr = toTranscode;
           char *outPtr = retVal;
   
  -    while (*srcPtr != 0)
  -        *outPtr++ = gUnicodeToIBM037XlatTable[*srcPtr++];
  -    *outPtr=0;
  +	while (*srcPtr != 0)
  +	    *outPtr++ = gUnicodeToIBM037XlatTable[*srcPtr++];
  +	*outPtr=0;
       }
       else
       {
  @@ -395,17 +454,14 @@
           return true;
       }
   
  -    size_t retCode;
  -    char *tmpInPtr = (char *)toTranscode;
  -    char *tmpOutPtr = toFill;
  -    size_t inByteLeft = maxBytes;
  -    size_t outByteLeft = maxBytes;
  -    retCode = iconv(fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  -    if (retCode == -1) {
  -        return false;
  -    }
  -    // Cap it off just in case
  -    toFill[maxBytes-outByteLeft] = 0;
  +    const XMLCh *srcPtr = toTranscode;
  +    char *outPtr = toFill;
  +    int bytectr = maxBytes;
  +
  +    while (bytectr--)
  +       *outPtr++ = gUnicodeToIBM037XlatTable[*srcPtr++];
  +    *outPtr=0;
  +
       return true;
   }
   
  @@ -431,7 +487,10 @@
           char *tmpOutPtr = (char*) retVal;
           size_t inByteLeft = len;
           size_t outByteLeft = len*2;
  -        retCode = iconv(fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +        {
  +         XMLMutexLock lockConverter(&converter->fMutex);
  +         retCode = iconv(converter->fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +        }
           if (retCode == -1) {
               delete [] retVal;
               return 0;
  @@ -470,7 +529,10 @@
       char *tmpOutPtr = (char*) toFill;
       size_t inByteLeft = maxChars;
       size_t outByteLeft = maxChars*2;
  -    retCode = iconv(fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +    {
  +     XMLMutexLock lockConverter(&converter->fMutex);
  +     retCode = iconv(converter->fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +    }
       if ( (retCode == -1) && (outByteLeft!=0) ) {
           return false;
       }
  @@ -487,18 +549,15 @@
   {
   }
   
  -Iconv390LCPTranscoder::Iconv390LCPTranscoder(iconv_t const  toAdopt) :
  -        fIconv390Descriptor(toAdopt)
  +Iconv390LCPTranscoder::Iconv390LCPTranscoder(iconvconverter_t* const toAdopt) :
  +        converter (toAdopt)
   {
   }
   
   Iconv390LCPTranscoder::~Iconv390LCPTranscoder()
   {
  -    if (fIconv390Descriptor)
  -    {
  -        iconv_close(fIconv390Descriptor);
  -        fIconv390Descriptor = 0;
  -    }
  +    removeConverter(converter);
  +    converter=0;
   }
   
   // ---------------------------------------------------------------------------
  @@ -510,21 +569,18 @@
   {
   }
   
  -Iconv390Transcoder::Iconv390Transcoder(iconv_t const  toAdopt
  -                ,const XMLCh* const encodingName
  -                ,const unsigned int blockSize) :
  -    XMLTranscoder(encodingName, blockSize)
  -       ,fIconv390Descriptor(toAdopt)
  +Iconv390Transcoder::Iconv390Transcoder(iconvconverter_t* const toAdopt
  +				,const XMLCh* const encodingName
  +				,const unsigned int blockSize) :
  + 	XMLTranscoder(encodingName, blockSize)
  +       ,converter (toAdopt)
   {
   }
   
   Iconv390Transcoder::~Iconv390Transcoder()
   {
  -    if (fIconv390Descriptor)
  -    {
  -        iconv_close(fIconv390Descriptor);
  -        fIconv390Descriptor = 0;
  -    }
  +    removeConverter(converter);
  +    converter=0;
   }
   
   
  @@ -542,7 +598,10 @@
       char *tmpOutPtr = (char*)&toFill;
       size_t inByteLeft = srcBytes;
       size_t outByteLeft = 2;
  -    retCode = iconv(fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +    {
  +     XMLMutexLock lockConverter(&converter->fMutex);
  +     retCode = iconv(converter->fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +    }
       if (retCode == -1) {
           bytesEaten = 0;
           return 0;
  @@ -566,7 +625,7 @@
                                   ,       XMLCh* const            toFill
                                   , const unsigned int            maxChars
                                   ,       unsigned int&           bytesEaten
  -                                ,       unsigned char* const    charSizes)
  +								,       unsigned char* const    charSizes)
   {
       //
       //  For this one, because we have to maintain the offset table, we have
  @@ -580,10 +639,13 @@
       char *tmpOutPtr = (char *) toFill;
       size_t inByteLeft = srcCount;
       size_t outByteLeft = maxChars*2;
  -    retCode = iconv(fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +    {
  +     XMLMutexLock lockConverter(&converter->fMutex);
  +     retCode = iconv(converter->fIconv390Descriptor, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);
  +    }
       if (retCode == -1)
       {
  -    return 0;
  +	return 0;
       }
   
       // Give back the counts of eaten and transcoded
  
  
  
  1.2       +33 -19    xml-xerces/c/src/util/Transcoders/Iconv390/Iconv390TransService.hpp
  
  Index: Iconv390TransService.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/util/Transcoders/Iconv390/Iconv390TransService.hpp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Iconv390TransService.hpp	2000/02/08 02:14:11	1.1
  +++ Iconv390TransService.hpp	2000/02/14 17:59:49	1.2
  @@ -1,37 +1,37 @@
   /*
    * The Apache Software License, Version 1.1
  - * 
  - * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights 
  + *
  + * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
    * reserved.
  - * 
  + *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
  - * 
  + *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  - * 
  + *    notice, this list of conditions and the following disclaimer.
  + *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
    *    the documentation and/or other materials provided with the
    *    distribution.
  - * 
  + *
    * 3. The end-user documentation included with the redistribution,
  - *    if any, must include the following acknowledgment:  
  + *    if any, must include the following acknowledgment:
    *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowledgment may appear in the software itself,
    *    if and wherever such third-party acknowledgments normally appear.
  - * 
  + *
    * 4. The names "Xerces" and "Apache Software Foundation" must
    *    not be used to endorse or promote products derived from this
  - *    software without prior written permission. For written 
  + *    software without prior written permission. For written
    *    permission, please contact apache\@apache.org.
  - * 
  + *
    * 5. Products derived from this software may not be called "Apache",
    *    nor may "Apache" appear in their name, without prior written
    *    permission of the Apache Software Foundation.
  - * 
  + *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  @@ -45,7 +45,7 @@
    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    * SUCH DAMAGE.
    * ====================================================================
  - * 
  + *
    * This software consists of voluntary contributions made by many
    * individuals on behalf of the Apache Software Foundation, and was
    * originally based on software copyright (c) 1999, International
  @@ -56,6 +56,9 @@
   
   /**
    * $Log: Iconv390TransService.hpp,v $
  + * Revision 1.2  2000/02/14 17:59:49  abagchi
  + * Reused iconv descriptors
  + *
    * Revision 1.1  2000/02/08 02:14:11  abagchi
    * Initial checkin
    *
  @@ -66,8 +69,19 @@
   #define ICONV390TRANSSERVICE_HPP
   
   #include <util/TransService.hpp>
  +#include <util/Mutexes.hpp>
   #include <iconv.h>
   
  +/* Max encoding name in characters. */
  +#define UCNV_MAX_CONVERTER_NAME_LENGTH 60
  +typedef struct iconvconverter {
  +   struct iconvconverter *nextconverter;
  +   char                   name [UCNV_MAX_CONVERTER_NAME_LENGTH];
  +   XMLMutex               fMutex;
  +   iconv_t                fIconv390Descriptor;
  +   int                    usecount;
  +} iconvconverter_t;
  +
   class XMLUTIL_EXPORT Iconv390TransService : public XMLTransService
   {
   public :
  @@ -131,8 +145,8 @@
       //  Constructors and Destructor
       // -----------------------------------------------------------------------
       Iconv390Transcoder(const XMLCh* const encodingName, const unsigned int blockSize);
  -    Iconv390Transcoder(iconv_t const toAdopt, const XMLCh* const encodingName, const unsigned int blockSize);
  -
  +    Iconv390Transcoder(iconvconverter_t* const toAdopt,
  +         const XMLCh* const encodingName, const unsigned int blockSize);
       ~Iconv390Transcoder();
   
   
  @@ -163,7 +177,7 @@
       // -----------------------------------------------------------------------
       Iconv390Transcoder(const Iconv390Transcoder&);
       void operator=(const Iconv390Transcoder&);
  -    iconv_t     fIconv390Descriptor;
  +    iconvconverter_t *converter;
   };
   
   
  @@ -175,7 +189,7 @@
       //  Constructors and Destructor
       // -----------------------------------------------------------------------
       Iconv390LCPTranscoder();
  -    Iconv390LCPTranscoder(iconv_t const  toAdopt);
  +    Iconv390LCPTranscoder(iconvconverter_t* const toAdopt);
       ~Iconv390LCPTranscoder();
   
       // -----------------------------------------------------------------------
  @@ -210,7 +224,7 @@
       // -----------------------------------------------------------------------
       Iconv390LCPTranscoder(const Iconv390LCPTranscoder&);
       void operator=(const Iconv390LCPTranscoder&);
  -    iconv_t     fIconv390Descriptor;
  +    iconvconverter_t *converter;
   };
   
   #endif