1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************
3b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho* Copyright (c) 2007-2011, International Business Machines
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Corporation and others.  All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "fldset.h"
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "intltest.h"
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_FORMATTING
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/regex.h"
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFieldsSet::FieldsSet() {
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // NOTREACHED
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFieldsSet::FieldsSet(int32_t fieldCount) {
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    construct((UDebugEnumType)-1, fieldCount);
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFieldsSet::FieldsSet(UDebugEnumType field) {
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    construct(field, udbg_enumCount(field));
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFieldsSet::~FieldsSet() {
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t FieldsSet::fieldCount() const {
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return fFieldCount;
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::construct(UDebugEnumType field, int32_t fieldCount) {
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fEnum = field;
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(fieldCount > U_FIELDS_SET_MAX) {
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fieldCount = U_FIELDS_SET_MAX;
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fFieldCount = fieldCount;
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    clear();
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeString FieldsSet::diffFrom(const FieldsSet& other, UErrorCode& status) const {
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UnicodeString str;
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(!isSameType(other)) {
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        status = U_ILLEGAL_ARGUMENT_ERROR;
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return UnicodeString("U_ILLEGAL_ARGUMENT_ERROR: FieldsSet of a different type!");
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i=0; i<fieldCount(); i++) {
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (isSet((UCalendarDateFields)i)) {
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int32_t myVal = get(i);
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int32_t theirVal = other.get(i);
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(fEnum != -1) {
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                const UnicodeString& fieldName = udbg_enumString(
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                        fEnum, i);
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                str = str + fieldName + UnicodeString("=")+myVal+UnicodeString(" not ")+theirVal+UnicodeString(", ");
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else {
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                str = str + UnicodeString("some field") + "=" + myVal+" not " + theirVal+", ";
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return str;
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic UnicodeString *split(const UnicodeString &src, UChar ch, int32_t &splits)
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t offset = -1;
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    splits = 1;
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while((offset = src.indexOf(ch, offset + 1)) >= 0) {
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        splits += 1;
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UnicodeString *result = new UnicodeString[splits];
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t start = 0;
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t split = 0;
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t end;
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while((end = src.indexOf(ch, start)) >= 0) {
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        src.extractBetween(start, end, result[split++]);
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        start = end + 1;
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    src.extractBetween(start, src.length(), result[split]);
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return result;
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t FieldsSet::parseFrom(const UnicodeString& str, const
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        FieldsSet* inheritFrom, UErrorCode& status) {
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int goodFields = 0;
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(U_FAILURE(status)) {
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return -1;
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t destCount = 0;
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UnicodeString *dest = split(str, 0x002C /* ',' */, destCount);
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for(int i = 0; i < destCount; i += 1) {
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int32_t dc = 0;
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        UnicodeString *kv = split(dest[i], 0x003D /* '=' */, dc);
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(dc != 2) {
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            it_errln(UnicodeString("dc == ") + dc + UnicodeString("?"));
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int32_t field = handleParseName(inheritFrom, kv[0], kv[1], status);
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(U_FAILURE(status)) {
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            char ch[256];
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            const UChar *u = kv[0].getBuffer();
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int32_t len = kv[0].length();
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            u_UCharsToChars(u, ch, len);
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ch[len] = 0; /* include terminating \0 */
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            it_errln(UnicodeString("Parse Failed: Field ") + UnicodeString(ch) + UnicodeString(", err ") + UnicodeString(u_errorName(status)));
122b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho            delete[] kv;
123b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho            delete[] dest;
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return -1;
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(field != -1) {
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            handleParseValue(inheritFrom, field, kv[1], status);
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(U_FAILURE(status)) {
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                char ch[256];
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                const UChar *u = kv[1].getBuffer();
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                int32_t len = kv[1].length();
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                u_UCharsToChars(u, ch, len);
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ch[len] = 0; /* include terminating \0 */
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                it_errln(UnicodeString("Parse Failed: Value ") + UnicodeString(ch) + UnicodeString(", err ") + UnicodeString(u_errorName(status)));
137b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho                delete[] kv;
138b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho                delete[] dest;
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return -1;
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            goodFields += 1;
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        delete[] kv;
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    delete[] dest;
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return goodFields;
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool FieldsSet::isSameType(const FieldsSet& other) const {
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return((&other==this)||
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           ((other.fFieldCount==fFieldCount) && (other.fEnum==fEnum)));
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::clear() {
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i=0; i<fieldCount(); i++) {
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fValue[i]=-1;
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fIsSet[i]=FALSE;
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::clear(int32_t field) {
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (field<0|| field>=fieldCount()) {
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return;
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fValue[field] = -1;
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fIsSet[field] = FALSE;
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::set(int32_t field, int32_t amount) {
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (field<0|| field>=fieldCount()) {
174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return;
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fValue[field] = amount;
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fIsSet[field] = TRUE;
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool FieldsSet::isSet(int32_t field) const {
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (field<0|| field>=fieldCount()) {
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return FALSE;
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return fIsSet[field];
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t FieldsSet::get(int32_t field) const {
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (field<0|| field>=fieldCount()) {
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return -1;
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return fValue[field];
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t FieldsSet::handleParseName(const FieldsSet* /* inheritFrom */, const UnicodeString& name, const UnicodeString& /* substr*/ , UErrorCode& status) {
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(fEnum > -1) {
19685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t which = udbg_enumByString(fEnum, name);
19785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if(which == UDBG_INVALID_ENUM) {
19885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            status = U_UNSUPPORTED_ERROR;
19985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
20085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return which;
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        status = U_UNSUPPORTED_ERROR;
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return -1;
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::parseValueDefault(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t value = -1;
209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(substr.length()==0) { // inherit requested
210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // inherit
211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if((inheritFrom == NULL) || !inheritFrom->isSet((UCalendarDateFields)field)) {
212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            // couldn't inherit from field
213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            it_errln(UnicodeString("Parse Failed: Couldn't inherit field ") + field + UnicodeString(" [") + UnicodeString(udbg_enumName(fEnum, field)) + UnicodeString("]"));
214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            status = U_ILLEGAL_ARGUMENT_ERROR;
215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return;
216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        value = inheritFrom->get((UCalendarDateFields)field);
218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        value = udbg_stoi(substr);
220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    set(field, value);
222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::parseValueEnum(UDebugEnumType type, const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t value = udbg_enumByString(type, substr);
226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(value>=0) {
227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        set(field, value);
228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // fallback
230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        parseValueDefault(inheritFrom,field,substr,status);
231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FieldsSet::handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    parseValueDefault(inheritFrom, field, substr, status);
236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/// CAL FIELDS
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruCalendarFieldsSet::CalendarFieldsSet() :
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFieldsSet(UDBG_UCalendarDateFields) {
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // base class will call clear.
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruCalendarFieldsSet::~CalendarFieldsSet() {
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid CalendarFieldsSet::handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(field==UCAL_MONTH) {
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        parseValueEnum(UDBG_UCalendarMonths, inheritFrom, field, substr, status);
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // will fallback to default.
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        parseValueDefault(inheritFrom, field, substr, status);
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * set the specified fields on this calendar. Doesn't clear first. Returns any errors the caller
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid CalendarFieldsSet::setOnCalendar(Calendar *cal, UErrorCode& /*status*/) const {
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i=0; i<UDAT_FIELD_COUNT; i++) {
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (isSet((UCalendarDateFields)i)) {
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int32_t value = get((UCalendarDateFields)i);
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            cal->set((UCalendarDateFields)i, value);
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * return true if the calendar matches in these fields
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool CalendarFieldsSet::matches(Calendar *cal, CalendarFieldsSet &diffSet,
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        UErrorCode& status) const {
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool match = TRUE;
27685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (U_FAILURE(status)) {
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return FALSE;
27885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i=0; i<UDAT_FIELD_COUNT; i++) {
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (isSet((UCalendarDateFields)i)) {
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int32_t calVal = cal->get((UCalendarDateFields)i, status);
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if (U_FAILURE(status))
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return FALSE;
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if (calVal != get((UCalendarDateFields)i)) {
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                match = FALSE;
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                diffSet.set((UCalendarDateFields)i, calVal);
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                //fprintf(stderr, "match failed: %s#%d=%d != %d\n",udbg_enumName(UDBG_UCalendarDateFields,i),i,cal->get((UCalendarDateFields)i,status), get((UCalendarDateFields)i));;
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return match;
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
29585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
29685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * DateTimeStyleSet has two 'fields' -- date, and time.
29785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
29885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoenum DateTimeStyleSetFields {
29985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    DTS_DATE = 0,  /** Field one: the date (long, medium, short, etc). */
30085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    DTS_TIME,      /** Field two: the time (long, medium, short, etc). */
30185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    DTS_COUNT      /** The number of fields */
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * DateTimeSet
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * */
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruDateTimeStyleSet::DateTimeStyleSet() :
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    FieldsSet(DTS_COUNT) {
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruDateTimeStyleSet::~DateTimeStyleSet() {
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUDateFormatStyle DateTimeStyleSet::getDateStyle() const {
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(!isSet(DTS_DATE)) {
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return UDAT_NONE;
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return (UDateFormatStyle)get(DTS_DATE);
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUDateFormatStyle DateTimeStyleSet::getTimeStyle() const {
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(!isSet(DTS_TIME)) {
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return UDAT_NONE;
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return (UDateFormatStyle)get(DTS_TIME);
329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid DateTimeStyleSet::handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UnicodeString kRELATIVE_("RELATIVE_");
334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(substr.startsWith(kRELATIVE_)) {
335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        UnicodeString relativeas(substr,kRELATIVE_.length());
336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        parseValueEnum(UDBG_UDateFormatStyle, inheritFrom, field, relativeas, status);
337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // fix relative value
338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(isSet(field) && U_SUCCESS(status)) {
339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            set(field, get(field) | UDAT_RELATIVE);
340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        parseValueEnum(UDBG_UDateFormatStyle, inheritFrom, field, substr, status);
343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t DateTimeStyleSet::handleParseName(const FieldsSet* /* inheritFrom */, const UnicodeString& name, const UnicodeString& /* substr */, UErrorCode& status) {
347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UnicodeString kDATE("DATE"); // TODO: static
348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UnicodeString kTIME("TIME"); // TODO: static
349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(name == kDATE ) {
350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return DTS_DATE;
351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else if(name == kTIME) {
352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return DTS_TIME;
353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        status = U_ILLEGAL_ARGUMENT_ERROR;
355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return -1;
356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif /*!UCONFIG_NO_FORMAT*/
360