intl/icu/source/i18n/format.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/i18n/format.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,217 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +* Copyright (C) 1997-2012, International Business Machines Corporation and    *
     1.7 +* others. All Rights Reserved.                                                *
     1.8 +*******************************************************************************
     1.9 +*
    1.10 +* File FORMAT.CPP
    1.11 +*
    1.12 +* Modification History:
    1.13 +*
    1.14 +*   Date        Name        Description
    1.15 +*   02/19/97    aliu        Converted from java.
    1.16 +*   03/17/97    clhuang     Implemented with new APIs.
    1.17 +*   03/27/97    helena      Updated to pass the simple test after code review.
    1.18 +*   07/20/98    stephen        Added explicit init values for Field/ParsePosition
    1.19 +********************************************************************************
    1.20 +*/
    1.21 +// *****************************************************************************
    1.22 +// This file was generated from the java source file Format.java
    1.23 +// *****************************************************************************
    1.24 +
    1.25 +#include "utypeinfo.h"  // for 'typeid' to work
    1.26 +
    1.27 +#include "unicode/utypes.h"
    1.28 +
    1.29 +#ifndef U_I18N_IMPLEMENTATION
    1.30 +#error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see http://userguide.icu-project.org/howtouseicu
    1.31 +#endif
    1.32 +
    1.33 +/*
    1.34 + * Dummy code:
    1.35 + * If all modules in the I18N library are switched off, then there are no
    1.36 + * library exports and MSVC 6 writes a .dll but not a .lib file.
    1.37 + * Unless we export _something_ in that case...
    1.38 + */
    1.39 +#if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
    1.40 +U_CAPI int32_t U_EXPORT2
    1.41 +uprv_icuin_lib_dummy(int32_t i) {
    1.42 +    return -i;
    1.43 +}
    1.44 +#endif
    1.45 +
    1.46 +/* Format class implementation ---------------------------------------------- */
    1.47 +
    1.48 +#if !UCONFIG_NO_FORMATTING
    1.49 +
    1.50 +#include "unicode/format.h"
    1.51 +#include "unicode/ures.h"
    1.52 +#include "cstring.h"
    1.53 +#include "locbased.h"
    1.54 +
    1.55 +// *****************************************************************************
    1.56 +// class Format
    1.57 +// *****************************************************************************
    1.58 +
    1.59 +U_NAMESPACE_BEGIN
    1.60 +
    1.61 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
    1.62 +
    1.63 +FieldPosition::~FieldPosition() {}
    1.64 +
    1.65 +FieldPosition *
    1.66 +FieldPosition::clone() const {
    1.67 +    return new FieldPosition(*this);
    1.68 +}
    1.69 +
    1.70 +// -------------------------------------
    1.71 +// default constructor
    1.72 +
    1.73 +Format::Format()
    1.74 +    : UObject()
    1.75 +{
    1.76 +    *validLocale = *actualLocale = 0;
    1.77 +}
    1.78 +
    1.79 +// -------------------------------------
    1.80 +
    1.81 +Format::~Format()
    1.82 +{
    1.83 +}
    1.84 +
    1.85 +// -------------------------------------
    1.86 +// copy constructor
    1.87 +
    1.88 +Format::Format(const Format &that)
    1.89 +    : UObject(that)
    1.90 +{
    1.91 +    *this = that;
    1.92 +}
    1.93 +
    1.94 +// -------------------------------------
    1.95 +// assignment operator
    1.96 +
    1.97 +Format&
    1.98 +Format::operator=(const Format& that)
    1.99 +{
   1.100 +    if (this != &that) {
   1.101 +        uprv_strcpy(validLocale, that.validLocale);
   1.102 +        uprv_strcpy(actualLocale, that.actualLocale);
   1.103 +    }
   1.104 +    return *this;
   1.105 +}
   1.106 +
   1.107 +// -------------------------------------
   1.108 +// Formats the obj and append the result in the buffer, toAppendTo.
   1.109 +// This calls the actual implementation in the concrete subclasses.
   1.110 +
   1.111 +UnicodeString&
   1.112 +Format::format(const Formattable& obj,
   1.113 +               UnicodeString& toAppendTo,
   1.114 +               UErrorCode& status) const
   1.115 +{
   1.116 +    if (U_FAILURE(status)) return toAppendTo;
   1.117 +
   1.118 +    FieldPosition pos(FieldPosition::DONT_CARE);
   1.119 +
   1.120 +    return format(obj, toAppendTo, pos, status);
   1.121 +}
   1.122 +
   1.123 +// -------------------------------------
   1.124 +// Default implementation sets unsupported error; subclasses should
   1.125 +// override.
   1.126 +
   1.127 +UnicodeString&
   1.128 +Format::format(const Formattable& /* unused obj */,
   1.129 +               UnicodeString& toAppendTo,
   1.130 +               FieldPositionIterator* /* unused posIter */,
   1.131 +               UErrorCode& status) const
   1.132 +{
   1.133 +    if (!U_FAILURE(status)) {
   1.134 +      status = U_UNSUPPORTED_ERROR;
   1.135 +    }
   1.136 +    return toAppendTo;
   1.137 +}
   1.138 +
   1.139 +// -------------------------------------
   1.140 +// Parses the source string and create the corresponding
   1.141 +// result object.  Checks the parse position for errors.
   1.142 +
   1.143 +void
   1.144 +Format::parseObject(const UnicodeString& source,
   1.145 +                    Formattable& result,
   1.146 +                    UErrorCode& status) const
   1.147 +{
   1.148 +    if (U_FAILURE(status)) return;
   1.149 +
   1.150 +    ParsePosition parsePosition(0);
   1.151 +    parseObject(source, result, parsePosition);
   1.152 +    if (parsePosition.getIndex() == 0) {
   1.153 +        status = U_INVALID_FORMAT_ERROR;
   1.154 +    }
   1.155 +}
   1.156 +
   1.157 +// -------------------------------------
   1.158 +
   1.159 +UBool
   1.160 +Format::operator==(const Format& that) const
   1.161 +{
   1.162 +    // Subclasses: Call this method and then add more specific checks.
   1.163 +    return typeid(*this) == typeid(that);
   1.164 +}
   1.165 +//---------------------------------------
   1.166 +
   1.167 +/**
   1.168 + * Simple function for initializing a UParseError from a UnicodeString.
   1.169 + *
   1.170 + * @param pattern The pattern to copy into the parseError
   1.171 + * @param pos The position in pattern where the error occured
   1.172 + * @param parseError The UParseError object to fill in
   1.173 + * @draft ICU 2.4
   1.174 + */
   1.175 +void Format::syntaxError(const UnicodeString& pattern,
   1.176 +                         int32_t pos,
   1.177 +                         UParseError& parseError) {
   1.178 +    parseError.offset = pos;
   1.179 +    parseError.line=0;  // we are not using line number
   1.180 +
   1.181 +    // for pre-context
   1.182 +    int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
   1.183 +                                                             /* subtract 1 so that we have room for null*/));
   1.184 +    int32_t stop  = pos;
   1.185 +    pattern.extract(start,stop-start,parseError.preContext,0);
   1.186 +    //null terminate the buffer
   1.187 +    parseError.preContext[stop-start] = 0;
   1.188 +
   1.189 +    //for post-context
   1.190 +    start = pos+1;
   1.191 +    stop  = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
   1.192 +        pattern.length();
   1.193 +    pattern.extract(start,stop-start,parseError.postContext,0);
   1.194 +    //null terminate the buffer
   1.195 +    parseError.postContext[stop-start]= 0;
   1.196 +}
   1.197 +
   1.198 +Locale
   1.199 +Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
   1.200 +    U_LOCALE_BASED(locBased, *this);
   1.201 +    return locBased.getLocale(type, status);
   1.202 +}
   1.203 +
   1.204 +const char *
   1.205 +Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
   1.206 +    U_LOCALE_BASED(locBased, *this);
   1.207 +    return locBased.getLocaleID(type, status);
   1.208 +}
   1.209 +
   1.210 +void
   1.211 +Format::setLocaleIDs(const char* valid, const char* actual) {
   1.212 +    U_LOCALE_BASED(locBased, *this);
   1.213 +    locBased.setLocaleIDs(valid, actual);
   1.214 +}
   1.215 +
   1.216 +U_NAMESPACE_END
   1.217 +
   1.218 +#endif /* #if !UCONFIG_NO_FORMATTING */
   1.219 +
   1.220 +//eof

mercurial