1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2001-08-16 00:55:16 +00:00
|
|
|
*
|
2011-04-25 20:47:32 +00:00
|
|
|
* Copyright (C) 1999-2011, International Business Machines
|
1999-11-23 01:30:04 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
2001-08-16 00:55:16 +00:00
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
2001-08-16 00:55:16 +00:00
|
|
|
* file name: umsg.cpp
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* This is a C wrapper to MessageFormat C++ API.
|
|
|
|
*
|
|
|
|
* Change history:
|
|
|
|
*
|
|
|
|
* 08/5/2001 Ram Added C wrappers for C++ API. Changed implementation of old API's
|
|
|
|
* Removed pattern parser.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/umsg.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/fmtable.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
|
|
|
#include "unicode/unistr.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cpputils.h"
|
2004-04-29 19:29:18 +00:00
|
|
|
#include "uassert.h"
|
2005-05-27 22:07:16 +00:00
|
|
|
#include "ustr_imp.h"
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
/**
|
|
|
|
* This class isolates our access to private internal methods of
|
|
|
|
* MessageFormat. It is never instantiated; it exists only for C++
|
|
|
|
* access management.
|
|
|
|
*/
|
|
|
|
class MessageFormatAdapter {
|
|
|
|
public:
|
|
|
|
static const Formattable::Type* getArgTypeList(const MessageFormat& m,
|
|
|
|
int32_t& count);
|
|
|
|
static UBool hasArgTypeConflicts(const MessageFormat& m) {
|
|
|
|
return m.hasArgTypeConflicts;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const Formattable::Type*
|
|
|
|
MessageFormatAdapter::getArgTypeList(const MessageFormat& m,
|
|
|
|
int32_t& count) {
|
|
|
|
return m.getArgTypeList(count);
|
|
|
|
}
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
U_CAPI int32_t
|
|
|
|
u_formatMessage(const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
UChar *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UErrorCode *status,
|
|
|
|
...)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
va_list ap;
|
2002-07-09 12:47:45 +00:00
|
|
|
int32_t actLen;
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
|
|
|
// start vararg processing
|
|
|
|
va_start(ap, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
actLen = u_vformatMessage(locale,pattern,patternLength,result,resultLength,ap,status);
|
|
|
|
// end vararg processing
|
|
|
|
va_end(ap);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
return actLen;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
u_vformatMessage( const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
UChar *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
va_list ap,
|
|
|
|
UErrorCode *status)
|
|
|
|
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
2001-08-16 00:55:16 +00:00
|
|
|
UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,NULL,status);
|
|
|
|
int32_t retVal = umsg_vformat(fmt,result,resultLength,ap,status);
|
|
|
|
umsg_close(fmt);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t
|
|
|
|
u_formatMessageWithError(const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
UChar *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode *status,
|
|
|
|
...)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
va_list ap;
|
|
|
|
int32_t actLen;
|
|
|
|
//argument checking defered to subsequent method calls
|
|
|
|
// start vararg processing
|
|
|
|
va_start(ap, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
actLen = u_vformatMessageWithError(locale,pattern,patternLength,result,resultLength,parseError,ap,status);
|
2002-07-09 12:47:45 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
// end vararg processing
|
|
|
|
va_end(ap);
|
|
|
|
return actLen;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
u_vformatMessageWithError( const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
UChar *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UParseError *parseError,
|
|
|
|
va_list ap,
|
|
|
|
UErrorCode *status)
|
|
|
|
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
2001-08-16 00:55:16 +00:00
|
|
|
UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,parseError,status);
|
|
|
|
int32_t retVal = umsg_vformat(fmt,result,resultLength,ap,status);
|
|
|
|
umsg_close(fmt);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// For parse, do the reverse of format:
|
|
|
|
// 1. Call through to the C++ APIs
|
|
|
|
// 2. Just assume the user passed in enough arguments.
|
|
|
|
// 3. Iterate through each formattable returned, and assign to the arguments
|
|
|
|
U_CAPI void
|
|
|
|
u_parseMessage( const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
const UChar *source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
UErrorCode *status,
|
|
|
|
...)
|
|
|
|
{
|
2002-01-30 06:18:26 +00:00
|
|
|
va_list ap;
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-01-30 06:18:26 +00:00
|
|
|
// start vararg processing
|
|
|
|
va_start(ap, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-01-30 06:18:26 +00:00
|
|
|
u_vparseMessage(locale,pattern,patternLength,source,sourceLength,ap,status);
|
|
|
|
// end vararg processing
|
|
|
|
va_end(ap);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
u_vparseMessage(const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
const UChar *source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
va_list ap,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
2001-08-16 00:55:16 +00:00
|
|
|
UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,NULL,status);
|
|
|
|
int32_t count = 0;
|
2002-04-05 02:48:28 +00:00
|
|
|
umsg_vparse(fmt,source,sourceLength,&count,ap,status);
|
2001-08-16 00:55:16 +00:00
|
|
|
umsg_close(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void
|
|
|
|
u_parseMessageWithError(const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
const UChar *source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
UParseError *error,
|
|
|
|
UErrorCode *status,
|
|
|
|
...)
|
|
|
|
{
|
2002-01-30 06:18:26 +00:00
|
|
|
va_list ap;
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-01-30 06:18:26 +00:00
|
|
|
// start vararg processing
|
|
|
|
va_start(ap, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-01-30 06:18:26 +00:00
|
|
|
u_vparseMessageWithError(locale,pattern,patternLength,source,sourceLength,ap,error,status);
|
|
|
|
// end vararg processing
|
|
|
|
va_end(ap);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
u_vparseMessageWithError(const char *locale,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
const UChar *source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
va_list ap,
|
|
|
|
UParseError *error,
|
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to subsequent method calls
|
2001-08-16 00:55:16 +00:00
|
|
|
UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,error,status);
|
|
|
|
int32_t count = 0;
|
|
|
|
umsg_vparse(fmt,source,sourceLength,&count,ap,status);
|
|
|
|
umsg_close(fmt);
|
|
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Message format C API
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UMessageFormat* U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
umsg_open( const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
const char *locale,
|
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(status==NULL || U_FAILURE(*status))
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2002-04-05 02:48:28 +00:00
|
|
|
if(pattern==NULL||patternLength<-1){
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
UParseError tErr;
|
2002-04-05 02:48:28 +00:00
|
|
|
if(parseError==NULL)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
|
|
|
parseError = &tErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength);
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeString patString(patternLength == -1, pattern, len);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
MessageFormat* retVal = new MessageFormat(patString,Locale(locale),*parseError,*status);
|
|
|
|
if(retVal == NULL) {
|
2001-08-16 00:55:16 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2011-04-25 20:47:32 +00:00
|
|
|
return NULL;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_SUCCESS(*status) && MessageFormatAdapter::hasArgTypeConflicts(*retVal)) {
|
|
|
|
*status = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
}
|
|
|
|
return (UMessageFormat*)retVal;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
umsg_close(UMessageFormat* format)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(format==NULL){
|
|
|
|
return;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
delete (MessageFormat*) format;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UMessageFormat U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
umsg_clone(const UMessageFormat *fmt,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(status==NULL || U_FAILURE(*status)){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(fmt==NULL){
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
UMessageFormat retVal = (UMessageFormat)((MessageFormat*)fmt)->clone();
|
|
|
|
if(retVal == 0) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
umsg_setLocale(UMessageFormat *fmt, const char* locale)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(fmt==NULL){
|
|
|
|
return;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
((MessageFormat*)fmt)->setLocale(Locale(locale));
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI const char* U_EXPORT2
|
2004-03-27 07:21:32 +00:00
|
|
|
umsg_getLocale(const UMessageFormat *fmt)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(fmt==NULL){
|
|
|
|
return "";
|
|
|
|
}
|
2004-03-27 07:21:32 +00:00
|
|
|
return ((const MessageFormat*)fmt)->getLocale().getName();
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
umsg_applyPattern(UMessageFormat *fmt,
|
|
|
|
const UChar* pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
UParseError* parseError,
|
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
UParseError tErr;
|
|
|
|
if(status ==NULL||U_FAILURE(*status)){
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
if(fmt==NULL||pattern==NULL||patternLength<-1){
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parseError==NULL){
|
2001-08-16 00:55:16 +00:00
|
|
|
parseError = &tErr;
|
2002-04-05 02:48:28 +00:00
|
|
|
}
|
|
|
|
if(patternLength<-1){
|
|
|
|
patternLength=u_strlen(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
((MessageFormat*)fmt)->applyPattern(UnicodeString(pattern,patternLength),*parseError,*status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2004-03-27 07:21:32 +00:00
|
|
|
umsg_toPattern(const UMessageFormat *fmt,
|
2001-08-16 00:55:16 +00:00
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(status ==NULL||U_FAILURE(*status)){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(fmt==NULL||resultLength<0 || (resultLength>0 && result==0)){
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
|
2002-11-16 01:14:40 +00:00
|
|
|
UnicodeString res;
|
|
|
|
if(!(result==NULL && resultLength==0)) {
|
|
|
|
// NULL destination for pure preflighting: empty dummy string
|
|
|
|
// otherwise, alias the destination buffer
|
|
|
|
res.setTo(result, 0, resultLength);
|
|
|
|
}
|
2004-03-27 07:21:32 +00:00
|
|
|
((const MessageFormat*)fmt)->toPattern(res);
|
2001-09-05 23:39:38 +00:00
|
|
|
return res.extract(result, resultLength, *status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t
|
2004-03-27 07:21:32 +00:00
|
|
|
umsg_format( const UMessageFormat *fmt,
|
2001-08-16 00:55:16 +00:00
|
|
|
UChar *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UErrorCode *status,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2002-07-09 12:47:45 +00:00
|
|
|
int32_t actLen;
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to last method call umsg_vformat which
|
|
|
|
//saves time when arguments are valid and we dont care when arguments are not
|
|
|
|
//since we return an error anyway
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
// start vararg processing
|
|
|
|
va_start(ap, status);
|
|
|
|
|
|
|
|
actLen = umsg_vformat(fmt,result,resultLength,ap,status);
|
|
|
|
|
|
|
|
// end vararg processing
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return actLen;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2004-03-27 07:21:32 +00:00
|
|
|
umsg_vformat( const UMessageFormat *fmt,
|
2001-08-16 00:55:16 +00:00
|
|
|
UChar *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
va_list ap,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
2001-09-18 23:50:55 +00:00
|
|
|
if(status==0 || U_FAILURE(*status))
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2002-04-05 02:48:28 +00:00
|
|
|
if(fmt==NULL||resultLength<0 || (resultLength>0 && result==0)) {
|
2001-09-18 23:50:55 +00:00
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
int32_t count =0;
|
2003-01-28 22:21:14 +00:00
|
|
|
const Formattable::Type* argTypes =
|
2004-03-27 07:21:32 +00:00
|
|
|
MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, count);
|
2003-01-28 22:21:14 +00:00
|
|
|
// Allocate at least one element. Allocating an array of length
|
|
|
|
// zero causes problems on some platforms (e.g. Win32).
|
|
|
|
Formattable* args = new Formattable[count ? count : 1];
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
// iterate through the vararg list, and get the arguments out
|
|
|
|
for(int32_t i = 0; i < count; ++i) {
|
|
|
|
|
|
|
|
UChar *stringVal;
|
|
|
|
double tDouble=0;
|
|
|
|
int32_t tInt =0;
|
2003-11-19 04:42:44 +00:00
|
|
|
int64_t tInt64 = 0;
|
2001-08-16 00:55:16 +00:00
|
|
|
UDate tempDate = 0;
|
|
|
|
switch(argTypes[i]) {
|
|
|
|
case Formattable::kDate:
|
|
|
|
tempDate = va_arg(ap, UDate);
|
|
|
|
args[i].setDate(tempDate);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Formattable::kDouble:
|
|
|
|
tDouble =va_arg(ap, double);
|
|
|
|
args[i].setDouble(tDouble);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Formattable::kLong:
|
|
|
|
tInt = va_arg(ap, int32_t);
|
|
|
|
args[i].setLong(tInt);
|
|
|
|
break;
|
2003-11-19 04:42:44 +00:00
|
|
|
|
|
|
|
case Formattable::kInt64:
|
|
|
|
tInt64 = va_arg(ap, int64_t);
|
|
|
|
args[i].setInt64(tInt64);
|
|
|
|
break;
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
case Formattable::kString:
|
|
|
|
// For some reason, a temporary is needed
|
|
|
|
stringVal = va_arg(ap, UChar*);
|
2002-04-03 03:41:31 +00:00
|
|
|
if(stringVal){
|
|
|
|
args[i].setString(stringVal);
|
|
|
|
}else{
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Formattable::kArray:
|
|
|
|
// throw away this argument
|
|
|
|
// this is highly platform-dependent, and probably won't work
|
|
|
|
// so, if you try to skip arguments in the list (and not use them)
|
|
|
|
// you'll probably crash
|
|
|
|
va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
|
2004-04-29 19:29:18 +00:00
|
|
|
case Formattable::kObject:
|
2011-04-25 20:47:32 +00:00
|
|
|
default:
|
2004-04-29 19:29:18 +00:00
|
|
|
// This will never happen because MessageFormat doesn't
|
|
|
|
// support kObject. When MessageFormat is changed to
|
|
|
|
// understand MeasureFormats, modify this code to do the
|
|
|
|
// right thing. [alan]
|
|
|
|
U_ASSERT(FALSE);
|
2011-04-25 20:47:32 +00:00
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
2004-04-29 19:29:18 +00:00
|
|
|
break;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UnicodeString resultStr;
|
|
|
|
FieldPosition fieldPosition(0);
|
|
|
|
|
|
|
|
/* format the message */
|
2004-03-27 07:21:32 +00:00
|
|
|
((const MessageFormat*)fmt)->format(args,count,resultStr,fieldPosition,*status);
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2003-01-28 22:21:14 +00:00
|
|
|
delete[] args;
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-09-19 18:53:51 +00:00
|
|
|
return resultStr.extract(result, resultLength, *status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void
|
2004-03-27 07:21:32 +00:00
|
|
|
umsg_parse( const UMessageFormat *fmt,
|
2001-08-16 00:55:16 +00:00
|
|
|
const UChar *source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
int32_t *count,
|
|
|
|
UErrorCode *status,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2002-04-05 02:48:28 +00:00
|
|
|
//argument checking defered to last method call umsg_vparse which
|
|
|
|
//saves time when arguments are valid and we dont care when arguments are not
|
|
|
|
//since we return an error anyway
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
// start vararg processing
|
|
|
|
va_start(ap, status);
|
|
|
|
|
|
|
|
umsg_vparse(fmt,source,sourceLength,count,ap,status);
|
|
|
|
|
|
|
|
// end vararg processing
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2004-03-27 07:21:32 +00:00
|
|
|
umsg_vparse(const UMessageFormat *fmt,
|
2001-08-16 00:55:16 +00:00
|
|
|
const UChar *source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
int32_t *count,
|
|
|
|
va_list ap,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2002-04-05 02:48:28 +00:00
|
|
|
//check arguments
|
|
|
|
if(status==NULL||U_FAILURE(*status))
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2002-04-05 02:48:28 +00:00
|
|
|
if(fmt==NULL||source==NULL || sourceLength<-1 || count==NULL){
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(sourceLength==-1){
|
|
|
|
sourceLength=u_strlen(source);
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2002-04-05 02:48:28 +00:00
|
|
|
UnicodeString srcString(source,sourceLength);
|
2004-03-27 07:21:32 +00:00
|
|
|
Formattable *args = ((const MessageFormat*)fmt)->parse(source,*count,*status);
|
2001-08-16 00:55:16 +00:00
|
|
|
UDate *aDate;
|
|
|
|
double *aDouble;
|
|
|
|
UChar *aString;
|
2002-04-03 03:41:31 +00:00
|
|
|
int32_t* aInt;
|
2003-11-19 04:42:44 +00:00
|
|
|
int64_t* aInt64;
|
2001-08-16 00:55:16 +00:00
|
|
|
UnicodeString temp;
|
2001-08-17 22:18:03 +00:00
|
|
|
int len =0;
|
2001-08-16 00:55:16 +00:00
|
|
|
// assign formattables to varargs
|
|
|
|
for(int32_t i = 0; i < *count; i++) {
|
|
|
|
switch(args[i].getType()) {
|
|
|
|
|
|
|
|
case Formattable::kDate:
|
|
|
|
aDate = va_arg(ap, UDate*);
|
2002-04-03 03:41:31 +00:00
|
|
|
if(aDate){
|
|
|
|
*aDate = args[i].getDate();
|
|
|
|
}else{
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Formattable::kDouble:
|
|
|
|
aDouble = va_arg(ap, double*);
|
2002-04-03 03:41:31 +00:00
|
|
|
if(aDouble){
|
|
|
|
*aDouble = args[i].getDouble();
|
|
|
|
}else{
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Formattable::kLong:
|
2002-04-03 03:41:31 +00:00
|
|
|
aInt = va_arg(ap, int32_t*);
|
|
|
|
if(aInt){
|
|
|
|
*aInt = (int32_t) args[i].getLong();
|
|
|
|
}else{
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
|
2003-11-19 04:42:44 +00:00
|
|
|
case Formattable::kInt64:
|
|
|
|
aInt64 = va_arg(ap, int64_t*);
|
|
|
|
if(aInt64){
|
|
|
|
*aInt64 = args[i].getInt64();
|
|
|
|
}else{
|
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
case Formattable::kString:
|
|
|
|
aString = va_arg(ap, UChar*);
|
2002-04-03 03:41:31 +00:00
|
|
|
if(aString){
|
|
|
|
args[i].getString(temp);
|
|
|
|
len = temp.length();
|
|
|
|
temp.extract(0,len,aString);
|
|
|
|
aString[len]=0;
|
|
|
|
}else{
|
|
|
|
*status= U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
|
2004-04-29 19:29:18 +00:00
|
|
|
case Formattable::kObject:
|
|
|
|
// This will never happen because MessageFormat doesn't
|
|
|
|
// support kObject. When MessageFormat is changed to
|
|
|
|
// understand MeasureFormats, modify this code to do the
|
|
|
|
// right thing. [alan]
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
break;
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
// better not happen!
|
|
|
|
case Formattable::kArray:
|
2004-04-29 19:29:18 +00:00
|
|
|
U_ASSERT(FALSE);
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete [] args;
|
|
|
|
}
|
|
|
|
|
2005-05-27 22:07:16 +00:00
|
|
|
#define SINGLE_QUOTE ((UChar)0x0027)
|
|
|
|
#define CURLY_BRACE_LEFT ((UChar)0x007B)
|
|
|
|
#define CURLY_BRACE_RIGHT ((UChar)0x007D)
|
|
|
|
|
|
|
|
#define STATE_INITIAL 0
|
|
|
|
#define STATE_SINGLE_QUOTE 1
|
|
|
|
#define STATE_IN_QUOTE 2
|
|
|
|
#define STATE_MSG_ELEMENT 3
|
|
|
|
|
2005-06-09 16:45:36 +00:00
|
|
|
#define MAppend(c) if (len < destCapacity) dest[len++] = c; else len++
|
2005-05-27 22:07:16 +00:00
|
|
|
|
|
|
|
int32_t umsg_autoQuoteApostrophe(const UChar* pattern,
|
2006-08-19 21:27:08 +00:00
|
|
|
int32_t patternLength,
|
|
|
|
UChar* dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
UErrorCode* ec)
|
2005-05-27 22:07:16 +00:00
|
|
|
{
|
2006-08-19 21:27:08 +00:00
|
|
|
int32_t state = STATE_INITIAL;
|
|
|
|
int32_t braceCount = 0;
|
|
|
|
int32_t len = 0;
|
|
|
|
|
|
|
|
if (ec == NULL || U_FAILURE(*ec)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pattern == NULL || patternLength < -1 || (dest == NULL && destCapacity > 0)) {
|
|
|
|
*ec = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patternLength == -1) {
|
|
|
|
patternLength = u_strlen(pattern);
|
2005-05-27 22:07:16 +00:00
|
|
|
}
|
|
|
|
|
2006-08-19 21:27:08 +00:00
|
|
|
for (int i = 0; i < patternLength; ++i) {
|
|
|
|
UChar c = pattern[i];
|
|
|
|
switch (state) {
|
|
|
|
case STATE_INITIAL:
|
|
|
|
switch (c) {
|
|
|
|
case SINGLE_QUOTE:
|
|
|
|
state = STATE_SINGLE_QUOTE;
|
|
|
|
break;
|
|
|
|
case CURLY_BRACE_LEFT:
|
|
|
|
state = STATE_MSG_ELEMENT;
|
|
|
|
++braceCount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SINGLE_QUOTE:
|
|
|
|
switch (c) {
|
|
|
|
case SINGLE_QUOTE:
|
|
|
|
state = STATE_INITIAL;
|
|
|
|
break;
|
|
|
|
case CURLY_BRACE_LEFT:
|
|
|
|
case CURLY_BRACE_RIGHT:
|
|
|
|
state = STATE_IN_QUOTE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MAppend(SINGLE_QUOTE);
|
|
|
|
state = STATE_INITIAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2005-05-27 22:07:16 +00:00
|
|
|
|
2006-08-19 21:27:08 +00:00
|
|
|
case STATE_IN_QUOTE:
|
|
|
|
switch (c) {
|
|
|
|
case SINGLE_QUOTE:
|
|
|
|
state = STATE_INITIAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_MSG_ELEMENT:
|
|
|
|
switch (c) {
|
|
|
|
case CURLY_BRACE_LEFT:
|
|
|
|
++braceCount;
|
|
|
|
break;
|
|
|
|
case CURLY_BRACE_RIGHT:
|
|
|
|
if (--braceCount == 0) {
|
|
|
|
state = STATE_INITIAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // Never happens.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAppend(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of scan
|
|
|
|
if (state == STATE_SINGLE_QUOTE || state == STATE_IN_QUOTE) {
|
|
|
|
MAppend(SINGLE_QUOTE);
|
|
|
|
}
|
2005-05-27 22:07:16 +00:00
|
|
|
|
2006-08-19 21:27:08 +00:00
|
|
|
return u_terminateUChars(dest, destCapacity, len, ec);
|
2005-05-27 22:07:16 +00:00
|
|
|
}
|
2003-11-14 21:58:52 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|