Home | History | Annotate | Download | only in i18n
      1 /*
      2 ******************************************************************************
      3 * Copyright (C) 2009-2012, International Business Machines Corporation and
      4 * others. All Rights Reserved.
      5 ******************************************************************************
      6 *   Date        Name        Description
      7 *   12/14/09    doug        Creation.
      8 ******************************************************************************
      9 */
     10 
     11 #include "unicode/utypes.h"
     12 
     13 #if !UCONFIG_NO_FORMATTING
     14 
     15 #include "unicode/fpositer.h"
     16 #include "cmemory.h"
     17 #include "uvectr32.h"
     18 
     19 U_NAMESPACE_BEGIN
     20 
     21 FieldPositionIterator::~FieldPositionIterator() {
     22   delete data;
     23   data = NULL;
     24   pos = -1;
     25 }
     26 
     27 FieldPositionIterator::FieldPositionIterator()
     28     : data(NULL), pos(-1) {
     29 }
     30 
     31 FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs)
     32   : UObject(rhs), data(NULL), pos(rhs.pos) {
     33 
     34   if (rhs.data) {
     35     UErrorCode status = U_ZERO_ERROR;
     36     data = new UVector32(status);
     37     data->assign(*rhs.data, status);
     38     if (status != U_ZERO_ERROR) {
     39       delete data;
     40       data = NULL;
     41       pos = -1;
     42     }
     43   }
     44 }
     45 
     46 UBool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const {
     47   if (&rhs == this) {
     48     return TRUE;
     49   }
     50   if (pos != rhs.pos) {
     51     return FALSE;
     52   }
     53   if (!data) {
     54     return rhs.data == NULL;
     55   }
     56   return rhs.data ? data->operator==(*rhs.data) : FALSE;
     57 }
     58 
     59 // BEGIN android-added
     60 int32_t FieldPositionIterator::getData(int32_t *dest, int32_t capacity) const {
     61   int32_t len = data ? data->size() : 0;
     62   if (len && dest) {
     63     if (capacity < len) {
     64       len = -len; // error, insufficient capacity
     65     } else {
     66       memcpy(dest, data->getBuffer(), len * sizeof(int32_t));
     67     }
     68   }
     69   return len;
     70 }
     71 // END android-added
     72 
     73 void FieldPositionIterator::setData(UVector32 *adopt, UErrorCode& status) {
     74   // Verify that adopt has valid data, and update status if it doesn't.
     75   if (U_SUCCESS(status)) {
     76     if (adopt) {
     77       if ((adopt->size() % 3) != 0) {
     78         status = U_ILLEGAL_ARGUMENT_ERROR;
     79       } else {
     80         for (int i = 1; i < adopt->size(); i += 3) {
     81           if (adopt->elementAti(i) >= adopt->elementAti(i+1)) {
     82             status = U_ILLEGAL_ARGUMENT_ERROR;
     83             break;
     84           }
     85         }
     86       }
     87     }
     88   }
     89 
     90   // We own the data, even if status is in error, so we need to delete it now
     91   // if we're not keeping track of it.
     92   if (!U_SUCCESS(status)) {
     93     delete adopt;
     94     return;
     95   }
     96 
     97   delete data;
     98   data = adopt;
     99   pos = adopt == NULL ? -1 : 0;
    100 }
    101 
    102 UBool FieldPositionIterator::next(FieldPosition& fp) {
    103   if (pos == -1) {
    104     return FALSE;
    105   }
    106 
    107   fp.setField(data->elementAti(pos++));
    108   fp.setBeginIndex(data->elementAti(pos++));
    109   fp.setEndIndex(data->elementAti(pos++));
    110 
    111   if (pos == data->size()) {
    112     pos = -1;
    113   }
    114 
    115   return TRUE;
    116 }
    117 
    118 U_NAMESPACE_END
    119 
    120 #endif /* #if !UCONFIG_NO_FORMATTING */
    121 
    122