Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ucnvlat1.c

00001 /* 
00002 **********************************************************************
00003 *   Copyright (C) 2000, International Business Machines
00004 *   Corporation and others.  All Rights Reserved.
00005 **********************************************************************
00006 *   file name:  ucnvlat1.cpp
00007 *   encoding:   US-ASCII
00008 *   tab size:   8 (not used)
00009 *   indentation:4
00010 *
00011 *   created on: 2000feb07
00012 *   created by: Markus W. Scherer
00013 *   Change history:
00014 *
00015 *   06/29/2000  helena      Major rewrite of the callback APIs.
00016 */
00017 
00018 #include "unicode/utypes.h"
00019 #include "ucmp16.h"
00020 #include "ucmp8.h"
00021 #include "unicode/ucnv_err.h"
00022 #include "ucnv_bld.h"
00023 #include "unicode/ucnv.h"
00024 #include "ucnv_cnv.h"
00025 
00026 /* ISO 8859-1 --------------------------------------------------------------- */
00027 
00028 U_CFUNC void  T_UConverter_toUnicode_LATIN_1 (UConverterToUnicodeArgs * args,
00029                                       UErrorCode * err)
00030 {
00031   unsigned char *mySource = (unsigned char *)  args->source;
00032   UChar *myTarget = args->target;
00033   int32_t sourceLength = args->sourceLimit - (char *) mySource;
00034   int32_t readLen = 0;
00035   int32_t i = 0;
00036 
00037   /*Since there is no risk of encountering illegal Chars
00038    *we need to pad our latin1 chars to create Unicode codepoints
00039    *we need to go as far a min(targetLen, sourceLen)
00040    *in case we don't have enough buffer space
00041    *we set the error flag accordingly
00042    */
00043   if ((args->targetLimit - args->target) < sourceLength)
00044     {
00045       readLen = args->targetLimit - args->target;
00046       *err = U_BUFFER_OVERFLOW_ERROR;
00047     }
00048   else
00049     {
00050       readLen = args->sourceLimit - (char *) mySource;
00051     }
00052 
00053   for (i = 0; i < readLen; i++) myTarget[i] = (UChar) mySource[i];
00054 
00055   args->target += i;
00056   args->source += i;
00057   return;
00058 }
00059 
00060 U_CFUNC void   T_UConverter_fromUnicode_LATIN_1 (UConverterFromUnicodeArgs * args,
00061                                          UErrorCode * err)
00062 {
00063   const UChar *mySource = args->source;
00064   unsigned char *myTarget = (unsigned char *) args->target;
00065   int32_t mySourceIndex = 0;
00066   int32_t myTargetIndex = 0;
00067   int32_t targetLength = args->targetLimit - (char *) myTarget;
00068   int32_t sourceLength = args->sourceLimit - mySource;
00069   UConverterCallbackReason reason;
00070 
00071   /*writing the char to the output stream */
00072   while (mySourceIndex < sourceLength)
00073     {
00074       if (myTargetIndex < targetLength)
00075         {
00076           if (mySource[mySourceIndex] < 0x0100)
00077             {
00078               /*writes the char to the output stream */
00079               myTarget[myTargetIndex++] = (char) mySource[mySourceIndex++];
00080             }
00081           else
00082             {
00083               *err = U_INVALID_CHAR_FOUND;
00084               reason = UCNV_UNASSIGNED;
00085               args->converter->invalidUCharBuffer[0] = (UChar)mySource[mySourceIndex];
00086               args->converter->invalidUCharLength = 1;
00087               if (UTF_IS_LEAD(mySource[mySourceIndex++]))
00088               {
00089                   if (mySourceIndex < sourceLength)
00090                   {
00091                       if (UTF_IS_TRAIL(mySource[mySourceIndex]))
00092                       {
00093                           args->converter->invalidUCharBuffer[1] = (UChar)mySource[mySourceIndex];
00094                           args->converter->invalidUCharLength++;
00095                           mySourceIndex++;
00096                       }
00097                       else 
00098                       {
00099                           reason = UCNV_ILLEGAL;
00100                       }                          
00101                   }
00102                   else if (args->flush == TRUE)
00103                   {
00104                       reason = UCNV_ILLEGAL;
00105                       *err = U_TRUNCATED_CHAR_FOUND;
00106                   } 
00107                   else 
00108                   {
00109                       args->converter->fromUSurrogateLead = args->converter->invalidUCharBuffer[0];
00110                       /* do not call the callback */
00111                   }
00112               }
00113               if (args->converter->fromUSurrogateLead == 0) 
00114               {
00115                   const UChar *saveSource = args->source;
00116                   char *saveTarget = args->target;
00117                   int32_t *saveOffset = args->offsets;
00118                   
00119     /* Needed explicit cast for myTarget on MVS to make compiler happy - JJD */
00120                   
00121                   args->target = (char*)myTarget + myTargetIndex;;
00122                   args->source = mySource + mySourceIndex;                  
00123 
00124                   FromU_CALLBACK_MACRO(args->converter->fromUContext,
00125                                      args,
00126                                      args->converter->invalidUCharBuffer,
00127                                      args->converter->invalidUCharLength,
00128                                      (UChar32) (args->converter->invalidUCharLength == 2 ? 
00129                                          UTF16_GET_PAIR_VALUE(args->converter->invalidUCharBuffer[0], 
00130                                                               args->converter->invalidUCharBuffer[1]) 
00131                                                 : args->converter->invalidUCharBuffer[0]),
00132                                      reason,
00133                                      err);
00134                   args->source = saveSource;
00135                   args->target = saveTarget;
00136                   args->offsets = saveOffset;
00137                   if (U_FAILURE (*err)) 
00138                   {
00139                       break;
00140                   }
00141                   args->converter->invalidUCharLength = 0;
00142               }
00143             }
00144         }
00145       else
00146         {
00147           *err = U_BUFFER_OVERFLOW_ERROR;
00148           break;
00149         }
00150     }
00151 
00152   args->target += myTargetIndex;
00153   args->source += mySourceIndex;;
00154 
00155   return;
00156 }
00157 
00158 U_CFUNC UChar32 T_UConverter_getNextUChar_LATIN_1(UConverterToUnicodeArgs* args,
00159                                                 UErrorCode* err)
00160 {
00161   
00162   /* Empties the internal buffers if need be
00163    * In this case since ErrorFunctors are never called 
00164    * (LATIN_1 is a subset of Unicode)
00165    */
00166   
00167   if (args->source+1 > args->sourceLimit) 
00168     {
00169       *err = U_INDEX_OUTOFBOUNDS_ERROR;
00170       return 0xffff;
00171     }
00172 
00173   /* make sure that we zero-extend, not sign-extend, the byte */
00174   return  (UChar)(uint8_t)*(args->source++);
00175 }
00176 
00177 static const UConverterImpl _Latin1Impl={
00178     UCNV_LATIN_1,
00179 
00180     NULL,
00181     NULL,
00182 
00183     NULL,
00184     NULL,
00185     NULL,
00186 
00187     T_UConverter_toUnicode_LATIN_1,
00188     NULL,
00189     T_UConverter_fromUnicode_LATIN_1,
00190     NULL,
00191     T_UConverter_getNextUChar_LATIN_1,
00192 
00193     NULL,
00194     NULL
00195 };
00196 
00197 const UConverterStaticData _Latin1StaticData={
00198   sizeof(UConverterStaticData),
00199   "LATIN_1",
00200     819, UCNV_IBM, UCNV_LATIN_1, 1, 1,
00201   { 0x1a, 0, 0, 0 },1,FALSE, FALSE,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
00202 };
00203 
00204 
00205 const UConverterSharedData _Latin1Data={
00206     sizeof(UConverterSharedData), ~((uint32_t) 0),
00207     NULL, NULL, &_Latin1StaticData, FALSE, &_Latin1Impl, 
00208     0
00209 };

Generated at Tue Dec 5 10:48:03 2000 for ICU by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000