michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 1997-2012, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: * michael@0: * File FORMAT.CPP michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 02/19/97 aliu Converted from java. michael@0: * 03/17/97 clhuang Implemented with new APIs. michael@0: * 03/27/97 helena Updated to pass the simple test after code review. michael@0: * 07/20/98 stephen Added explicit init values for Field/ParsePosition michael@0: ******************************************************************************** michael@0: */ michael@0: // ***************************************************************************** michael@0: // This file was generated from the java source file Format.java michael@0: // ***************************************************************************** michael@0: michael@0: #include "utypeinfo.h" // for 'typeid' to work michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #ifndef U_I18N_IMPLEMENTATION michael@0: #error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see http://userguide.icu-project.org/howtouseicu michael@0: #endif michael@0: michael@0: /* michael@0: * Dummy code: michael@0: * If all modules in the I18N library are switched off, then there are no michael@0: * library exports and MSVC 6 writes a .dll but not a .lib file. michael@0: * Unless we export _something_ in that case... michael@0: */ michael@0: #if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uprv_icuin_lib_dummy(int32_t i) { michael@0: return -i; michael@0: } michael@0: #endif michael@0: michael@0: /* Format class implementation ---------------------------------------------- */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/format.h" michael@0: #include "unicode/ures.h" michael@0: #include "cstring.h" michael@0: #include "locbased.h" michael@0: michael@0: // ***************************************************************************** michael@0: // class Format michael@0: // ***************************************************************************** michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition) michael@0: michael@0: FieldPosition::~FieldPosition() {} michael@0: michael@0: FieldPosition * michael@0: FieldPosition::clone() const { michael@0: return new FieldPosition(*this); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // default constructor michael@0: michael@0: Format::Format() michael@0: : UObject() michael@0: { michael@0: *validLocale = *actualLocale = 0; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: Format::~Format() michael@0: { michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // copy constructor michael@0: michael@0: Format::Format(const Format &that) michael@0: : UObject(that) michael@0: { michael@0: *this = that; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // assignment operator michael@0: michael@0: Format& michael@0: Format::operator=(const Format& that) michael@0: { michael@0: if (this != &that) { michael@0: uprv_strcpy(validLocale, that.validLocale); michael@0: uprv_strcpy(actualLocale, that.actualLocale); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // Formats the obj and append the result in the buffer, toAppendTo. michael@0: // This calls the actual implementation in the concrete subclasses. michael@0: michael@0: UnicodeString& michael@0: Format::format(const Formattable& obj, michael@0: UnicodeString& toAppendTo, michael@0: UErrorCode& status) const michael@0: { michael@0: if (U_FAILURE(status)) return toAppendTo; michael@0: michael@0: FieldPosition pos(FieldPosition::DONT_CARE); michael@0: michael@0: return format(obj, toAppendTo, pos, status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // Default implementation sets unsupported error; subclasses should michael@0: // override. michael@0: michael@0: UnicodeString& michael@0: Format::format(const Formattable& /* unused obj */, michael@0: UnicodeString& toAppendTo, michael@0: FieldPositionIterator* /* unused posIter */, michael@0: UErrorCode& status) const michael@0: { michael@0: if (!U_FAILURE(status)) { michael@0: status = U_UNSUPPORTED_ERROR; michael@0: } michael@0: return toAppendTo; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // Parses the source string and create the corresponding michael@0: // result object. Checks the parse position for errors. michael@0: michael@0: void michael@0: Format::parseObject(const UnicodeString& source, michael@0: Formattable& result, michael@0: UErrorCode& status) const michael@0: { michael@0: if (U_FAILURE(status)) return; michael@0: michael@0: ParsePosition parsePosition(0); michael@0: parseObject(source, result, parsePosition); michael@0: if (parsePosition.getIndex() == 0) { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UBool michael@0: Format::operator==(const Format& that) const michael@0: { michael@0: // Subclasses: Call this method and then add more specific checks. michael@0: return typeid(*this) == typeid(that); michael@0: } michael@0: //--------------------------------------- michael@0: michael@0: /** michael@0: * Simple function for initializing a UParseError from a UnicodeString. michael@0: * michael@0: * @param pattern The pattern to copy into the parseError michael@0: * @param pos The position in pattern where the error occured michael@0: * @param parseError The UParseError object to fill in michael@0: * @draft ICU 2.4 michael@0: */ michael@0: void Format::syntaxError(const UnicodeString& pattern, michael@0: int32_t pos, michael@0: UParseError& parseError) { michael@0: parseError.offset = pos; michael@0: parseError.line=0; // we are not using line number michael@0: michael@0: // for pre-context michael@0: int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1 michael@0: /* subtract 1 so that we have room for null*/)); michael@0: int32_t stop = pos; michael@0: pattern.extract(start,stop-start,parseError.preContext,0); michael@0: //null terminate the buffer michael@0: parseError.preContext[stop-start] = 0; michael@0: michael@0: //for post-context michael@0: start = pos+1; michael@0: stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) : michael@0: pattern.length(); michael@0: pattern.extract(start,stop-start,parseError.postContext,0); michael@0: //null terminate the buffer michael@0: parseError.postContext[stop-start]= 0; michael@0: } michael@0: michael@0: Locale michael@0: Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const { michael@0: U_LOCALE_BASED(locBased, *this); michael@0: return locBased.getLocale(type, status); michael@0: } michael@0: michael@0: const char * michael@0: Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const { michael@0: U_LOCALE_BASED(locBased, *this); michael@0: return locBased.getLocaleID(type, status); michael@0: } michael@0: michael@0: void michael@0: Format::setLocaleIDs(const char* valid, const char* actual) { michael@0: U_LOCALE_BASED(locBased, *this); michael@0: locBased.setLocaleIDs(valid, actual); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: //eof