]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/ucnv_cb.c
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / common / ucnv_cb.c
index 9cbf25e607277ff894e03487fd0f8bb761f18df5..fa34b65832726cbb1c8aa21601cbcaa515ca1403 100644 (file)
@@ -1,6 +1,6 @@
 /*
 **********************************************************************
-*   Copyright (C) 2000-2001, International Business Machines
+*   Copyright (C) 2000-2006, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
  *  ucnv_cb.c:
@@ -19,6 +19,9 @@
  */
 
 #include "unicode/utypes.h"
+
+#if !UCONFIG_NO_CONVERSION
+
 #include "unicode/ucnv_cb.h"
 #include "ucnv_bld.h"
 #include "ucnv_cnv.h"
@@ -35,50 +38,16 @@ ucnv_cbFromUWriteBytes (UConverterFromUnicodeArgs *args,
                        int32_t offsetIndex,
                        UErrorCode * err)
 {
-    int32_t togo;
-    int8_t toerr;
-    int32_t i;
-
-    if((args->targetLimit - args->target) >= length) /* If the buffer fits.. */
-    {
-        uprv_memcpy(args->target, source, length);
-        args->target += length;
-        if(args->offsets) /* set all the offsets to the same # */
-        {
-            for(i=0;i<length;i++)
-            {
-                *(args->offsets++) = offsetIndex;
-            }
-        }
+    if(U_FAILURE(*err)) {
+        return;
     }
-    else
-    {
-        togo = (int32_t)(args->targetLimit - args->target);
-
-        uprv_memcpy(args->target, source, togo);
-        args->target += togo;
-
-        if(args->offsets)
-        {
-            for(i=0;i<togo;i++)
-            {
-                *(args->offsets++) = offsetIndex;
-            }
-        }
-
-        /* Now, copy the remainder into the errbuff */
-        source += togo;
-        toerr = (int8_t)(length - togo);
-
-        uprv_memcpy(args->converter->charErrorBuffer +
-            args->converter->charErrorBufferLength,
-            source,
-            toerr * sizeof(source[0]));
-        args->converter->charErrorBufferLength += toerr;
 
-        *err = U_BUFFER_OVERFLOW_ERROR;
-
-    }
+    ucnv_fromUWriteBytes(
+        args->converter,
+        source, length,
+        &args->target, args->targetLimit,
+        &args->offsets, offsetIndex,
+        err);
 }
 
 U_CAPI void  U_EXPORT2
@@ -135,7 +104,7 @@ ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs *args,
     /* && (*source < sourceLimit && args->target >= args->targetLimit)
     -- S. Hrcek */
     {
-    /* Overflowed the target.  Now, we'll write into the charErrorBuffer.
+        /* Overflowed the target.  Now, we'll write into the charErrorBuffer.
         It's a fixed size. If we overflow it... Hmm */
         char *newTarget;
         const char *newTargetLimit;
@@ -157,49 +126,48 @@ ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs *args,
             return;
         }
 
-       /* We're going to tell the converter that the errbuff len is empty.
-       This prevents the existing errbuff from being 'flushed' out onto
-       itself.  If the errbuff is needed by the converter this time,
-       we're hosed - we're out of space! */
-
-       args->converter->charErrorBufferLength = 0;
-
-       ucnv_fromUnicode(args->converter,
-                        &newTarget,
-                        newTargetLimit,
-                        source,
-                        sourceLimit,
-                        NULL,
-                        FALSE,
-                        &err2);
-
-       /* We can go ahead and overwrite the  length here. We know just how
-       to recalculate it. */
-
-       args->converter->charErrorBufferLength = (int8_t)(
-           newTarget - (char*)args->converter->charErrorBuffer);
-
-       if((newTarget >= newTargetLimit) || (err2 == U_BUFFER_OVERFLOW_ERROR))
-       {
-           /* now we're REALLY in trouble.
-           Internal program error - callback shouldn't have written this much
-           data!
-           */
-           *err = U_INTERNAL_PROGRAM_ERROR;
-           return;
-       }
-       else
-       {
-           /* sub errs could be invalid/truncated/illegal chars or w/e.
-           These might want to be passed on up.. But the problem is, we already
-           need to pass U_BUFFER_OVERFLOW_ERROR. That has to override these
-           other errs.. */
-
-           /*
-           if(U_FAILURE(err2))
-           ??
-           */
-       }
+        /* We're going to tell the converter that the errbuff len is empty.
+        This prevents the existing errbuff from being 'flushed' out onto
+        itself.  If the errbuff is needed by the converter this time,
+        we're hosed - we're out of space! */
+
+        args->converter->charErrorBufferLength = 0;
+
+        ucnv_fromUnicode(args->converter,
+                         &newTarget,
+                         newTargetLimit,
+                         source,
+                         sourceLimit,
+                         NULL,
+                         FALSE,
+                         &err2);
+
+        /* We can go ahead and overwrite the  length here. We know just how
+        to recalculate it. */
+
+        args->converter->charErrorBufferLength = (int8_t)(
+            newTarget - (char*)args->converter->charErrorBuffer);
+
+        if((newTarget >= newTargetLimit) || (err2 == U_BUFFER_OVERFLOW_ERROR))
+        {
+            /* now we're REALLY in trouble.
+            Internal program error - callback shouldn't have written this much
+            data!
+            */
+            *err = U_INTERNAL_PROGRAM_ERROR;
+            return;
+        }
+        /*else {*/
+            /* sub errs could be invalid/truncated/illegal chars or w/e.
+            These might want to be passed on up.. But the problem is, we already
+            need to pass U_BUFFER_OVERFLOW_ERROR. That has to override these
+            other errs.. */
+
+            /*
+            if(U_FAILURE(err2))
+            ??
+            */
+        /*}*/
     }
 }
 
@@ -208,19 +176,48 @@ ucnv_cbFromUWriteSub (UConverterFromUnicodeArgs *args,
                            int32_t offsetIndex,
                            UErrorCode * err)
 {
+    UConverter *converter;
+    int32_t length;
+
     if(U_FAILURE(*err)) {
         return;
     }
+    converter = args->converter;
+    length = converter->subCharLen;
 
-    if(args->converter->sharedData->impl->writeSub!=NULL) {
-        args->converter->sharedData->impl->writeSub(args, offsetIndex, err);
-    } else if(args->converter->subChar1!=0 && args->converter->invalidUCharBuffer[0]<=0xff) {
+    if(length == 0) {
+        return;
+    }
+
+    if(length < 0) {
+        /*
+         * Write/convert the substitution string. Its real length is -length.
+         * Unlike the escape callback, we need not change the converter's
+         * callback function because ucnv_setSubstString() verified that
+         * the string can be converted, so we will not get a conversion error
+         * and will not recurse.
+         * At worst we should get a U_BUFFER_OVERFLOW_ERROR.
+         */
+        const UChar *source = (const UChar *)converter->subChars;
+        ucnv_cbFromUWriteUChars(args, &source, source - length, offsetIndex, err);
+        return;
+    }
+
+    if(converter->sharedData->impl->writeSub!=NULL) {
+        converter->sharedData->impl->writeSub(args, offsetIndex, err);
+    }
+    else if(converter->subChar1!=0 && (uint16_t)converter->invalidUCharBuffer[0]<=(uint16_t)0xffu) {
+        /*
+        TODO: Is this untestable because the MBCS converter has a writeSub function to call
+        and the other converters don't use subChar1?
+        */
         ucnv_cbFromUWriteBytes(args,
-                               (const char *)&args->converter->subChar1, 1,
+                               (const char *)&converter->subChar1, 1,
                                offsetIndex, err);
-    } else {
+    }
+    else {
         ucnv_cbFromUWriteBytes(args,
-                               (const char *)args->converter->subChar, args->converter->subCharLen,
+                               (const char *)converter->subChars, length,
                                offsetIndex, err);
     }
 }
@@ -232,55 +229,16 @@ ucnv_cbToUWriteUChars (UConverterToUnicodeArgs *args,
                             int32_t offsetIndex,
                             UErrorCode * err)
 {
-    int32_t togo;
-    int8_t toerr;
-    int32_t i;
-
-    if(U_FAILURE(*err))
-    {
+    if(U_FAILURE(*err)) {
         return;
     }
 
-
-    if((args->targetLimit - args->target) >= length) /* If the buffer fits.. */
-    {
-        uprv_memcpy(args->target, source, length * sizeof(args->target[0]) );
-        args->target += length;
-        if(args->offsets) /* set all the offsets to the same # */
-        {
-            for(i=0;i<length;i++)
-            {
-                *(args->offsets++) = offsetIndex;
-            }
-        }
-    }
-    else
-    {
-        togo = (int32_t)(args->targetLimit - args->target);
-
-        uprv_memcpy(args->target, source, togo * sizeof(args->target[0])  );
-        args->target += togo;
-
-        if(args->offsets)
-        {
-            for(i=0;i<togo;i++)
-            {
-                *(args->offsets++) = offsetIndex;
-            }
-        }
-
-        /* Now, copy the remainder into the errbuff */
-        source += togo;
-        toerr = (int8_t)(length - togo);
-
-        uprv_memcpy(args->converter->UCharErrorBuffer +
-                    args->converter->UCharErrorBufferLength,
-                    source,
-                    toerr * sizeof(source[0]));
-        args->converter->UCharErrorBufferLength += toerr;
-
-        *err = U_BUFFER_OVERFLOW_ERROR;
-    }
+    ucnv_toUWriteUChars(
+        args->converter,
+        source, length,
+        &args->target, args->targetLimit,
+        &args->offsets, offsetIndex,
+        err);
 }
 
 U_CAPI void  U_EXPORT2
@@ -297,3 +255,5 @@ ucnv_cbToUWriteSub (UConverterToUnicodeArgs *args,
         ucnv_cbToUWriteUChars(args, &kSubstituteChar, 1, offsetIndex, err);
     }
 }
+
+#endif