1/* 2****************************************************************************** 3* Copyright (C) 2009-2010, 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 19U_NAMESPACE_BEGIN 20 21UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPositionIterator) 22 23FieldPositionIterator::~FieldPositionIterator() { 24 delete data; 25 data = NULL; 26 pos = -1; 27} 28 29FieldPositionIterator::FieldPositionIterator() 30 : data(NULL), pos(-1) { 31} 32 33FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs) 34 : UObject(rhs), data(NULL), pos(rhs.pos) { 35 36 if (rhs.data) { 37 UErrorCode status = U_ZERO_ERROR; 38 data = new UVector32(status); 39 data->assign(*rhs.data, status); 40 if (status != U_ZERO_ERROR) { 41 delete data; 42 data = NULL; 43 pos = -1; 44 } 45 } 46} 47 48UBool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const { 49 if (&rhs == this) { 50 return TRUE; 51 } 52 if (pos != rhs.pos) { 53 return FALSE; 54 } 55 if (!data) { 56 return rhs.data == NULL; 57 } 58 return rhs.data ? data->operator==(*rhs.data) : FALSE; 59} 60 61int32_t FieldPositionIterator::getData(int32_t *dest, int32_t capacity) const { 62 int32_t len = data ? data->size() : 0; 63 if (len && dest) { 64 if (capacity < len) { 65 len = -len; // error, insufficient capacity 66 } else { 67 memcpy(dest, data->getBuffer(), len * sizeof(int32_t)); 68 } 69 } 70 return len; 71} 72 73void 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 102UBool 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 118U_NAMESPACE_END 119 120#endif /* #if !UCONFIG_NO_FORMATTING */ 121