michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 2009-2012, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * Date Name Description michael@0: * 12/14/09 doug Creation. michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/fpositer.h" michael@0: #include "cmemory.h" michael@0: #include "uvectr32.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: FieldPositionIterator::~FieldPositionIterator() { michael@0: delete data; michael@0: data = NULL; michael@0: pos = -1; michael@0: } michael@0: michael@0: FieldPositionIterator::FieldPositionIterator() michael@0: : data(NULL), pos(-1) { michael@0: } michael@0: michael@0: FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs) michael@0: : UObject(rhs), data(NULL), pos(rhs.pos) { michael@0: michael@0: if (rhs.data) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: data = new UVector32(status); michael@0: data->assign(*rhs.data, status); michael@0: if (status != U_ZERO_ERROR) { michael@0: delete data; michael@0: data = NULL; michael@0: pos = -1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: UBool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const { michael@0: if (&rhs == this) { michael@0: return TRUE; michael@0: } michael@0: if (pos != rhs.pos) { michael@0: return FALSE; michael@0: } michael@0: if (!data) { michael@0: return rhs.data == NULL; michael@0: } michael@0: return rhs.data ? data->operator==(*rhs.data) : FALSE; michael@0: } michael@0: michael@0: void FieldPositionIterator::setData(UVector32 *adopt, UErrorCode& status) { michael@0: // Verify that adopt has valid data, and update status if it doesn't. michael@0: if (U_SUCCESS(status)) { michael@0: if (adopt) { michael@0: if ((adopt->size() % 3) != 0) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else { michael@0: for (int i = 1; i < adopt->size(); i += 3) { michael@0: if (adopt->elementAti(i) >= adopt->elementAti(i+1)) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // We own the data, even if status is in error, so we need to delete it now michael@0: // if we're not keeping track of it. michael@0: if (!U_SUCCESS(status)) { michael@0: delete adopt; michael@0: return; michael@0: } michael@0: michael@0: delete data; michael@0: data = adopt; michael@0: pos = adopt == NULL ? -1 : 0; michael@0: } michael@0: michael@0: UBool FieldPositionIterator::next(FieldPosition& fp) { michael@0: if (pos == -1) { michael@0: return FALSE; michael@0: } michael@0: michael@0: fp.setField(data->elementAti(pos++)); michael@0: fp.setBeginIndex(data->elementAti(pos++)); michael@0: fp.setEndIndex(data->elementAti(pos++)); michael@0: michael@0: if (pos == data->size()) { michael@0: pos = -1; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: