Fixed FMT_DATE_GENERAL and FMT_DATE_TIME_SYS cases in
[wine.git] / dlls / oleaut32 / varformat.c
blobead2cb1e30ea11d5d720e96e81c21ea109152fc1
1 /*
2 * Variant formatting functions
4 * Copyright 2003 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * NOTES
21 * Since the formatting functions aren't properly documented, I used the
22 * Visual Basic documentation as a guide to implementing these functions. This
23 * means that some named or user-defined formats may work slightly differently.
24 * Please submit a test case if you find a difference.
27 #include "config.h"
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wine/unicode.h"
39 #include "winerror.h"
40 #include "variant.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(variant);
45 /* Make sure internal conversions to strings use the '.','+'/'-' and ','
46 * format chars from the US locale. This enables us to parse the created
47 * strings to determine the number of decimal places, exponent, etc.
49 #define LCID_US MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)
51 static const WCHAR szPercent_d[] = { '%','d','\0' };
52 static const WCHAR szPercentZeroTwo_d[] = { '%','0','2','d','\0' };
53 static const WCHAR szPercentZeroFour_d[] = { '%','0','4','d','\0' };
54 static const WCHAR szPercentZeroStar_d[] = { '%','0','*','d','\0' };
56 #if 0
57 #define dump_tokens(rgb) do { \
58 int i_; TRACE("Tokens->{ \n"); \
59 for (i_ = 0; i_ < rgb[0]; i_++) \
60 TRACE("%s0x%02x", i_?",":"",rgb[i_]); \
61 TRACE(" }\n"); \
62 } while(0)
63 #endif
65 /******************************************************************************
66 * Variant-Formats {OLEAUT32}
68 * NOTES
69 * When formatting a variant a variety of format strings may be used to generate
70 * different kinds of formatted output. A format string consists of either a named
71 * format, or a user-defined format.
73 * The following named formats are defined:
74 *| Name Description
75 *| ---- -----------
76 *| General Date Display Date, and time for non-integer values
77 *| Short Date Short date format as defined by locale settings
78 *| Medium Date Medium date format as defined by locale settings
79 *| Long Date Long date format as defined by locale settings
80 *| Short Time Short Time format as defined by locale settings
81 *| Medium Time Medium time format as defined by locale settings
82 *| Long Time Long time format as defined by locale settings
83 *| True/False Localised text of "True" or "False"
84 *| Yes/No Localised text of "Yes" or "No"
85 *| On/Off Localised text of "On" or "Off"
86 *| General Number No thousands separator. No decimal points for integers
87 *| Currency General currency format using localised characters
88 *| Fixed At least one whole and two fractional digits
89 *| Standard Same as 'Fixed', but including decimal separators
90 *| Percent Multiply by 100 and display a trailing '%' character
91 *| Scientific Display with exponent
93 * User-defined formats consist of a combination of tokens and literal
94 * characters. Literal characters are copied unmodified to the formatted
95 * output at the position they occupy in the format string. Any character
96 * that is not recognised as a token is treated as a literal. A literal can
97 * also be specified by preceding it with a backslash character
98 * (e.g. "\L\i\t\e\r\a\l") or enclosing it in double quotes.
100 * A user-defined format can have up to 4 sections, depending on the type of
101 * format. The following table lists sections and their meaning:
102 *| Format Type Sections Meaning
103 *| ----------- -------- -------
104 *| Number 1 Use the same format for all numbers
105 *| Number 2 Use format 1 for positive and 2 for negative numbers
106 *| Number 3 Use format 1 for positive, 2 for zero, and 3
107 *| for negative numbers.
108 *| Number 4 Use format 1 for positive, 2 for zero, 3 for
109 *| negative, and 4 for null numbers.
110 *| String 1 Use the same format for all strings
111 *| String 2 Use format 2 for null and empty strings, otherwise
112 *| use format 1.
113 *| Date 1 Use the same format for all dates
115 * The formatting tokens fall into several categories depending on the type
116 * of formatted output. For more information on each type, see
117 * VarFormat-Dates(), VarFormat-Strings() and VarFormat-Numbers().
119 * SEE ALSO
120 * VarTokenizeFormatString(), VarFormatFromTokens(), VarFormat(),
121 * VarFormatDateTime(), VarFormatNumber(), VarFormatCurrency().
124 /******************************************************************************
125 * VarFormat-Strings {OLEAUT32}
127 * NOTES
128 * When formatting a variant as a string, it is first converted to a VT_BSTR.
129 * The user-format string defines which characters are copied into which
130 * positions in the output string. Literals may be inserted in the format
131 * string. When creating the formatted string, excess characters in the string
132 * (those not consumed by a token) are appended to the end of the output. If
133 * there are more tokens than characters in the string to format, spaces will
134 * be inserted at the start of the string if the '@' token was used.
136 * By default strings are converted to lowercase, or uppercase if the '>' token
137 * is encountered. This applies to the whole string: it is not possible to
138 * generate a mixed-case output string.
140 * In user-defined string formats, the following tokens are recognised:
141 *| Token Description
142 *| ----- -----------
143 *| '@' Copy a char from the source, or a space if no chars are left.
144 *| '&' Copy a char from the source, or write nothing if no chars are left.
145 *| '<' Output the whole string as lower-case (the default).
146 *| '>' Output the whole string as upper-case.
147 *| '!' MSDN indicates that this character should cause right-to-left
148 *| copying, however tests show that it is tokenised but not processed.
152 * Common format definitions
155 /* Fomat types */
156 #define FMT_TYPE_UNKNOWN 0x0
157 #define FMT_TYPE_GENERAL 0x1
158 #define FMT_TYPE_NUMBER 0x2
159 #define FMT_TYPE_DATE 0x3
160 #define FMT_TYPE_STRING 0x4
162 #define FMT_TO_STRING 0x0 /* If header->size == this, act like VB's Str() fn */
164 typedef struct tagFMT_SHORT_HEADER
166 BYTE size; /* Size of tokenised block (including header), or FMT_TO_STRING */
167 BYTE type; /* Allowable types (FMT_TYPE_*) */
168 BYTE offset[1]; /* Offset of the first (and only) format section */
169 } FMT_SHORT_HEADER;
171 typedef struct tagFMT_HEADER
173 BYTE size; /* Total size of the whole tokenised block (including header) */
174 BYTE type; /* Allowable types (FMT_TYPE_*) */
175 BYTE starts[4]; /* Offset of each of the 4 format sections, or 0 if none */
176 } FMT_HEADER;
178 #define FmtGetPositive(x) (x->starts[0])
179 #define FmtGetNegative(x) (x->starts[1] ? x->starts[1] : x->starts[0])
180 #define FmtGetZero(x) (x->starts[2] ? x->starts[2] : x->starts[0])
181 #define FmtGetNull(x) (x->starts[3] ? x->starts[3] : x->starts[0])
184 * String formats
187 #define FMT_FLAG_LT 0x1 /* Has '<' (lower case) */
188 #define FMT_FLAG_GT 0x2 /* Has '>' (upper case) */
189 #define FMT_FLAG_RTL 0x4 /* Has '!' (Copy right to left) */
191 typedef struct tagFMT_STRING_HEADER
193 BYTE flags; /* LT, GT, RTL */
194 BYTE unknown1;
195 BYTE unknown2;
196 BYTE copy_chars; /* Number of chars to be copied */
197 BYTE unknown3;
198 } FMT_STRING_HEADER;
201 * Number formats
204 #define FMT_FLAG_PERCENT 0x1 /* Has '%' (Percentage) */
205 #define FMT_FLAG_EXPONENT 0x2 /* Has 'e' (Exponent/Scientific notation) */
206 #define FMT_FLAG_THOUSANDS 0x4 /* Has ',' (Standard use of the thousands separator) */
207 #define FMT_FLAG_BOOL 0x20 /* Boolean format */
209 typedef struct tagFMT_NUMBER_HEADER
211 BYTE flags; /* PERCENT, EXPONENT, THOUSANDS, BOOL */
212 BYTE multiplier; /* Multiplier, 100 for percentages */
213 BYTE divisor; /* Divisor, 1000 if '%%' was used */
214 BYTE whole; /* Number of digits before the decimal point */
215 BYTE fractional; /* Number of digits after the decimal point */
216 } FMT_NUMBER_HEADER;
219 * Date Formats
221 typedef struct tagFMT_DATE_HEADER
223 BYTE flags;
224 BYTE unknown1;
225 BYTE unknown2;
226 BYTE unknown3;
227 BYTE unknown4;
228 } FMT_DATE_HEADER;
231 * Format token values
233 #define FMT_GEN_COPY 0x00 /* \n, "lit" => 0,pos,len: Copy len chars from input+pos */
234 #define FMT_GEN_INLINE 0x01 /* => 1,len,[chars]: Copy len chars from token stream */
235 #define FMT_GEN_END 0x02 /* \0,; => 2: End of the tokenised format */
236 #define FMT_DATE_TIME_SEP 0x03 /* Time separator char */
237 #define FMT_DATE_DATE_SEP 0x04 /* Date separator char */
238 #define FMT_DATE_GENERAL 0x05 /* General format date */
239 #define FMT_DATE_QUARTER 0x06 /* Quarter of the year from 1-4 */
240 #define FMT_DATE_TIME_SYS 0x07 /* System long time format */
241 #define FMT_DATE_DAY 0x08 /* Day with no leading 0 */
242 #define FMT_DATE_DAY_0 0x09 /* Day with leading 0 */
243 #define FMT_DATE_DAY_SHORT 0x0A /* Short day name */
244 #define FMT_DATE_DAY_LONG 0x0B /* Long day name */
245 #define FMT_DATE_SHORT 0x0C /* Short date format */
246 #define FMT_DATE_LONG 0x0D /* Long date format */
247 #define FMT_DATE_MEDIUM 0x0E /* Medium date format */
248 #define FMT_DATE_DAY_WEEK 0x0F /* First day of the week */
249 #define FMT_DATE_WEEK_YEAR 0x10 /* First week of the year */
250 #define FMT_DATE_MON 0x11 /* Month with no leading 0 */
251 #define FMT_DATE_MON_0 0x12 /* Month with leading 0 */
252 #define FMT_DATE_MON_SHORT 0x13 /* Short month name */
253 #define FMT_DATE_MON_LONG 0x14 /* Long month name */
254 #define FMT_DATE_YEAR_DOY 0x15 /* Day of the year with no leading 0 */
255 #define FMT_DATE_YEAR_0 0x16 /* 2 digit year with leading 0 */
256 /* NOTE: token 0x17 is not defined, 'yyy' is not valid */
257 #define FMT_DATE_YEAR_LONG 0x18 /* 4 digit year */
258 #define FMT_DATE_MIN 0x1A /* Minutes with no leading 0 */
259 #define FMT_DATE_MIN_0 0x1B /* Minutes with leading 0 */
260 #define FMT_DATE_SEC 0x1C /* Seconds with no leading 0 */
261 #define FMT_DATE_SEC_0 0x1D /* Seconds with leading 0 */
262 #define FMT_DATE_HOUR 0x1E /* Hours with no leading 0 */
263 #define FMT_DATE_HOUR_0 0x1F /* Hours with leading 0 */
264 #define FMT_DATE_HOUR_12 0x20 /* Hours with no leading 0, 12 hour clock */
265 #define FMT_DATE_HOUR_12_0 0x21 /* Hours with leading 0, 12 hour clock */
266 #define FMT_DATE_TIME_UNK2 0x23
267 /* FIXME: probably missing some here */
268 #define FMT_DATE_AMPM_SYS1 0x2E /* AM/PM as defined by system settings */
269 #define FMT_DATE_AMPM_UPPER 0x2F /* Upper-case AM or PM */
270 #define FMT_DATE_A_UPPER 0x30 /* Upper-case A or P */
271 #define FMT_DATE_AMPM_SYS2 0x31 /* AM/PM as defined by system settings */
272 #define FMT_DATE_AMPM_LOWER 0x32 /* Lower-case AM or PM */
273 #define FMT_DATE_A_LOWER 0x33 /* Lower-case A or P */
274 #define FMT_NUM_COPY_ZERO 0x34 /* Copy 1 digit or 0 if no digit */
275 #define FMT_NUM_COPY_SKIP 0x35 /* Copy 1 digit or skip if no digit */
276 #define FMT_NUM_DECIMAL 0x36 /* Decimal separator */
277 #define FMT_NUM_EXP_POS_U 0x37 /* Scientific notation, uppercase, + sign */
278 #define FMT_NUM_EXP_NEG_U 0x38 /* Scientific notation, uppercase, - sign */
279 #define FMT_NUM_EXP_POS_L 0x39 /* Scientific notation, lowercase, + sign */
280 #define FMT_NUM_EXP_NEG_L 0x3A /* Scientific notation, lowercase, - sign */
281 #define FMT_NUM_CURRENCY 0x3B /* Currency symbol */
282 #define FMT_NUM_TRUE_FALSE 0x3D /* Convert to "True" or "False" */
283 #define FMT_NUM_YES_NO 0x3E /* Convert to "Yes" or "No" */
284 #define FMT_NUM_ON_OFF 0x3F /* Convert to "On" or "Off" */
285 #define FMT_STR_COPY_SPACE 0x40 /* Copy len chars with space if no char */
286 #define FMT_STR_COPY_SKIP 0x41 /* Copy len chars or skip if no char */
287 /* Wine additions */
288 #define FMT_WINE_HOURS_12 0x81 /* Hours using 12 hour clockhourCopy len chars or skip if no char */
290 /* Named Formats and their tokenised values */
291 static const WCHAR szGeneralDate[] = { 'G','e','n','e','r','a','l',' ','D','a','t','e','\0' };
292 static const BYTE fmtGeneralDate[0x0a] =
294 0x0a,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
295 0x0,0x0,0x0,0x0,0x0,
296 FMT_DATE_GENERAL,FMT_GEN_END
299 static const WCHAR szShortDate[] = { 'S','h','o','r','t',' ','D','a','t','e','\0' };
300 static const BYTE fmtShortDate[0x0a] =
302 0x0a,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
303 0x0,0x0,0x0,0x0,0x0,
304 FMT_DATE_SHORT,FMT_GEN_END
307 static const WCHAR szMediumDate[] = { 'M','e','d','i','u','m',' ','D','a','t','e','\0' };
308 static const BYTE fmtMediumDate[0x0a] =
310 0x0a,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
311 0x0,0x0,0x0,0x0,0x0,
312 FMT_DATE_MEDIUM,FMT_GEN_END
315 static const WCHAR szLongDate[] = { 'L','o','n','g',' ','D','a','t','e','\0' };
316 static const BYTE fmtLongDate[0x0a] =
318 0x0a,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
319 0x0,0x0,0x0,0x0,0x0,
320 FMT_DATE_LONG,FMT_GEN_END
323 static const WCHAR szShortTime[] = { 'S','h','o','r','t',' ','T','i','m','e','\0' };
324 static const BYTE fmtShortTime[0x0c] =
326 0x0c,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
327 0x0,0x0,0x0,0x0,0x0,
328 FMT_DATE_TIME_UNK2,FMT_DATE_TIME_SEP,FMT_DATE_MIN_0,FMT_GEN_END
331 static const WCHAR szMediumTime[] = { 'M','e','d','i','u','m',' ','T','i','m','e','\0' };
332 static const BYTE fmtMediumTime[0x11] =
334 0x11,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
335 0x0,0x0,0x0,0x0,0x0,
336 FMT_DATE_HOUR_12_0,FMT_DATE_TIME_SEP,FMT_DATE_MIN_0,
337 FMT_GEN_INLINE,0x01,' ','\0',FMT_DATE_AMPM_SYS1,FMT_GEN_END
340 static const WCHAR szLongTime[] = { 'L','o','n','g',' ','T','i','m','e','\0' };
341 static const BYTE fmtLongTime[0x0d] =
343 0x0a,FMT_TYPE_DATE,sizeof(FMT_SHORT_HEADER),
344 0x0,0x0,0x0,0x0,0x0,
345 FMT_DATE_TIME_SYS,FMT_GEN_END
348 static const WCHAR szTrueFalse[] = { 'T','r','u','e','/','F','a','l','s','e','\0' };
349 static const BYTE fmtTrueFalse[0x0d] =
351 0x0d,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
352 FMT_FLAG_BOOL,0x0,0x0,0x0,0x0,
353 FMT_NUM_TRUE_FALSE,FMT_GEN_END
356 static const WCHAR szYesNo[] = { 'Y','e','s','/','N','o','\0' };
357 static const BYTE fmtYesNo[0x0d] =
359 0x0d,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
360 FMT_FLAG_BOOL,0x0,0x0,0x0,0x0,
361 FMT_NUM_YES_NO,FMT_GEN_END
364 static const WCHAR szOnOff[] = { 'O','n','/','O','f','f','\0' };
365 static const BYTE fmtOnOff[0x0d] =
367 0x0d,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
368 FMT_FLAG_BOOL,0x0,0x0,0x0,0x0,
369 FMT_NUM_ON_OFF,FMT_GEN_END
372 static const WCHAR szGeneralNumber[] = { 'G','e','n','e','r','a','l',' ','N','u','m','b','e','r','\0' };
373 static const BYTE fmtGeneralNumber[sizeof(FMT_HEADER)] =
375 sizeof(FMT_HEADER),FMT_TYPE_GENERAL,sizeof(FMT_HEADER),0x0,0x0,0x0
378 static const WCHAR szCurrency[] = { 'C','u','r','r','e','n','c','y','\0' };
379 static const BYTE fmtCurrency[0x26] =
381 0x26,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x12,0x0,0x0,
382 /* Positive numbers */
383 FMT_FLAG_THOUSANDS,0xcc,0x0,0x1,0x2,
384 FMT_NUM_CURRENCY,FMT_NUM_COPY_ZERO,0x1,FMT_NUM_DECIMAL,FMT_NUM_COPY_ZERO,0x2,
385 FMT_GEN_END,
386 /* Negative numbers */
387 FMT_FLAG_THOUSANDS,0xcc,0x0,0x1,0x2,
388 FMT_GEN_INLINE,0x1,'(','\0',FMT_NUM_CURRENCY,FMT_NUM_COPY_ZERO,0x1,
389 FMT_NUM_DECIMAL,FMT_NUM_COPY_ZERO,0x2,FMT_GEN_INLINE,0x1,')','\0',
390 FMT_GEN_END
393 static const WCHAR szFixed[] = { 'F','i','x','e','d','\0' };
394 static const BYTE fmtFixed[0x11] =
396 0x11,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
397 0x0,0x0,0x0,0x1,0x2,
398 FMT_NUM_COPY_ZERO,0x1,FMT_NUM_DECIMAL,FMT_NUM_COPY_ZERO,0x2,FMT_GEN_END
401 static const WCHAR szStandard[] = { 'S','t','a','n','d','a','r','d','\0' };
402 static const BYTE fmtStandard[0x11] =
404 0x11,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
405 FMT_FLAG_THOUSANDS,0x0,0x0,0x1,0x2,
406 FMT_NUM_COPY_ZERO,0x1,FMT_NUM_DECIMAL,FMT_NUM_COPY_ZERO,0x2,FMT_GEN_END
409 static const WCHAR szPercent[] = { 'P','e','r','c','e','n','t','\0' };
410 static const BYTE fmtPercent[0x15] =
412 0x15,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
413 FMT_FLAG_PERCENT,0x1,0x0,0x1,0x2,
414 FMT_NUM_COPY_ZERO,0x1,FMT_NUM_DECIMAL,FMT_NUM_COPY_ZERO,0x2,
415 FMT_GEN_INLINE,0x1,'%','\0',FMT_GEN_END
418 static const WCHAR szScientific[] = { 'S','c','i','e','n','t','i','f','i','c','\0' };
419 static const BYTE fmtScientific[0x13] =
421 0x13,FMT_TYPE_NUMBER,sizeof(FMT_HEADER),0x0,0x0,0x0,
422 FMT_FLAG_EXPONENT,0x0,0x0,0x1,0x2,
423 FMT_NUM_COPY_ZERO,0x1,FMT_NUM_DECIMAL,FMT_NUM_COPY_ZERO,0x2,FMT_NUM_EXP_POS_U,0x2,FMT_GEN_END
426 typedef struct tagNAMED_FORMAT
428 LPCWSTR name;
429 const BYTE* format;
430 } NAMED_FORMAT;
432 /* Format name to tokenised format. Must be kept sorted by name */
433 static const NAMED_FORMAT VARIANT_NamedFormats[] =
435 { szCurrency, fmtCurrency },
436 { szFixed, fmtFixed },
437 { szGeneralDate, fmtGeneralDate },
438 { szGeneralNumber, fmtGeneralNumber },
439 { szLongDate, fmtLongDate },
440 { szLongTime, fmtLongTime },
441 { szMediumDate, fmtMediumDate },
442 { szMediumTime, fmtMediumTime },
443 { szOnOff, fmtOnOff },
444 { szPercent, fmtPercent },
445 { szScientific, fmtScientific },
446 { szShortDate, fmtShortDate },
447 { szShortTime, fmtShortTime },
448 { szStandard, fmtStandard },
449 { szTrueFalse, fmtTrueFalse },
450 { szYesNo, fmtYesNo }
452 typedef const NAMED_FORMAT *LPCNAMED_FORMAT;
454 static int FormatCompareFn(const void *l, const void *r)
456 return strcmpiW(((LPCNAMED_FORMAT)l)->name, ((LPCNAMED_FORMAT)r)->name);
459 static inline const BYTE *VARIANT_GetNamedFormat(LPCWSTR lpszFormat)
461 NAMED_FORMAT key;
462 LPCNAMED_FORMAT fmt;
464 key.name = lpszFormat;
465 fmt = (LPCNAMED_FORMAT)bsearch(&key, VARIANT_NamedFormats,
466 sizeof(VARIANT_NamedFormats)/sizeof(NAMED_FORMAT),
467 sizeof(NAMED_FORMAT), FormatCompareFn);
468 return fmt ? fmt->format : NULL;
471 /* Return an error if the token for the value will not fit in the destination */
472 #define NEED_SPACE(x) if (cbTok < (int)(x)) return TYPE_E_BUFFERTOOSMALL; cbTok -= (x)
474 /* Non-zero if the format is unknown or a given type */
475 #define COULD_BE(typ) ((!fmt_number && header->type==FMT_TYPE_UNKNOWN)||header->type==typ)
477 /* State during tokenising */
478 #define FMT_STATE_OPEN_COPY 0x1 /* Last token written was a copy */
479 #define FMT_STATE_WROTE_DECIMAL 0x2 /* Already wrote a decimal separator */
480 #define FMT_STATE_SEEN_HOURS 0x4 /* See the hh specifier */
481 #define FMT_STATE_WROTE_MINUTES 0x8 /* Wrote minutes */
483 /**********************************************************************
484 * VarTokenizeFormatString [OLEAUT32.140]
486 * Convert a format string into tokenised form.
488 * PARAMS
489 * lpszFormat [I] Format string to tokenise
490 * rgbTok [O] Destination for tokenised format
491 * cbTok [I] Size of rgbTok in bytes
492 * nFirstDay [I] First day of the week (1-7, or 0 for current system default)
493 * nFirstWeek [I] How to treat the first week (see notes)
494 * lcid [I] Locale Id of the format string
495 * pcbActual [O] If non-NULL, filled with the first token generated
497 * RETURNS
498 * Success: S_OK. rgbTok contains the tokenised format.
499 * Failure: E_INVALIDARG, if any argument is invalid.
500 * TYPE_E_BUFFERTOOSMALL, if rgbTok is not large enough.
502 * NOTES
503 * Valid values for the nFirstWeek parameter are:
504 *| Value Meaning
505 *| ----- -------
506 *| 0 Use the current system default
507 *| 1 The first week is that containing Jan 1
508 *| 2 Four or more days of the first week are in the current year
509 *| 3 The first week is 7 days long
510 * See Variant-Formats(), VarFormatFromTokens().
512 HRESULT WINAPI VarTokenizeFormatString(LPOLESTR lpszFormat, LPBYTE rgbTok,
513 int cbTok, int nFirstDay, int nFirstWeek,
514 LCID lcid, int *pcbActual)
516 /* Note: none of these strings should be NUL terminated */
517 static const WCHAR szTTTTT[] = { 't','t','t','t','t' };
518 static const WCHAR szAMPM[] = { 'A','M','P','M' };
519 static const WCHAR szampm[] = { 'a','m','p','m' };
520 static const WCHAR szAMSlashPM[] = { 'A','M','/','P','M' };
521 static const WCHAR szamSlashpm[] = { 'a','m','/','p','m' };
522 const BYTE *namedFmt;
523 FMT_HEADER *header = (FMT_HEADER*)rgbTok;
524 FMT_STRING_HEADER *str_header = (FMT_STRING_HEADER*)(rgbTok + sizeof(FMT_HEADER));
525 FMT_NUMBER_HEADER *num_header = (FMT_NUMBER_HEADER*)str_header;
526 FMT_DATE_HEADER *date_header = (FMT_DATE_HEADER*)str_header;
527 BYTE* pOut = rgbTok + sizeof(FMT_HEADER) + sizeof(FMT_STRING_HEADER);
528 BYTE* pLastHours = NULL;
529 BYTE fmt_number = 0;
530 DWORD fmt_state = 0;
531 LPCWSTR pFormat = lpszFormat;
533 TRACE("(%s,%p,%d,%d,%d,0x%08lx,%p)\n", debugstr_w(lpszFormat), rgbTok, cbTok,
534 nFirstDay, nFirstWeek, lcid, pcbActual);
536 if (!rgbTok ||
537 nFirstDay < 0 || nFirstDay > 7 || nFirstWeek < 0 || nFirstWeek > 3)
538 return E_INVALIDARG;
540 if (!lpszFormat || !*lpszFormat)
542 /* An empty string means 'general format' */
543 NEED_SPACE(sizeof(BYTE));
544 *rgbTok = FMT_TO_STRING;
545 if (pcbActual)
546 *pcbActual = FMT_TO_STRING;
547 return S_OK;
550 if (cbTok > 255)
551 cbTok = 255; /* Ensure we error instead of wrapping */
553 /* Named formats */
554 namedFmt = VARIANT_GetNamedFormat(lpszFormat);
555 if (namedFmt)
557 NEED_SPACE(namedFmt[0]);
558 memcpy(rgbTok, namedFmt, namedFmt[0]);
559 TRACE("Using pre-tokenised named format %s\n", debugstr_w(lpszFormat));
560 /* FIXME: pcbActual */
561 return S_OK;
564 /* Insert header */
565 NEED_SPACE(sizeof(FMT_HEADER) + sizeof(FMT_STRING_HEADER));
566 memset(header, 0, sizeof(FMT_HEADER));
567 memset(str_header, 0, sizeof(FMT_STRING_HEADER));
569 header->starts[fmt_number] = sizeof(FMT_HEADER);
571 while (*pFormat)
573 /* --------------
574 * General tokens
575 * --------------
577 if (*pFormat == ';')
579 while (*pFormat == ';')
581 TRACE(";\n");
582 if (++fmt_number > 3)
583 return E_INVALIDARG; /* too many formats */
584 pFormat++;
586 if (*pFormat)
588 TRACE("New header\n");
589 NEED_SPACE(sizeof(BYTE) + sizeof(FMT_STRING_HEADER));
590 *pOut++ = FMT_GEN_END;
592 header->starts[fmt_number] = pOut - rgbTok;
593 str_header = (FMT_STRING_HEADER*)pOut;
594 num_header = (FMT_NUMBER_HEADER*)pOut;
595 date_header = (FMT_DATE_HEADER*)pOut;
596 memset(str_header, 0, sizeof(FMT_STRING_HEADER));
597 pOut += sizeof(FMT_STRING_HEADER);
598 fmt_state = 0;
599 pLastHours = NULL;
602 else if (*pFormat == '\\')
604 /* Escaped character */
605 if (pFormat[1])
607 NEED_SPACE(3 * sizeof(BYTE));
608 pFormat++;
609 *pOut++ = FMT_GEN_COPY;
610 *pOut++ = pFormat - lpszFormat;
611 *pOut++ = 0x1;
612 fmt_state |= FMT_STATE_OPEN_COPY;
613 TRACE("'\\'\n");
615 else
616 fmt_state &= ~FMT_STATE_OPEN_COPY;
617 pFormat++;
619 else if (*pFormat == '"')
621 /* Escaped string
622 * Note: Native encodes "" as a copy of length zero. That's just dumb, so
623 * here we avoid encoding anything in this case.
625 if (!pFormat[1])
626 pFormat++;
627 else if (pFormat[1] == '"')
629 pFormat += 2;
631 else
633 LPCWSTR start = ++pFormat;
634 while (*pFormat && *pFormat != '"')
635 pFormat++;
636 NEED_SPACE(3 * sizeof(BYTE));
637 *pOut++ = FMT_GEN_COPY;
638 *pOut++ = start - lpszFormat;
639 *pOut++ = pFormat - start;
640 if (*pFormat == '"')
641 pFormat++;
642 TRACE("Quoted string pos %d, len %d\n", pOut[-2], pOut[-1]);
644 fmt_state &= ~FMT_STATE_OPEN_COPY;
646 /* -------------
647 * Number tokens
648 * -------------
650 else if (*pFormat == '0' && COULD_BE(FMT_TYPE_NUMBER))
652 /* Number formats: Digit from number or '0' if no digits
653 * Other formats: Literal
654 * Types the format if found
656 header->type = FMT_TYPE_NUMBER;
657 NEED_SPACE(2 * sizeof(BYTE));
658 *pOut++ = FMT_NUM_COPY_ZERO;
659 *pOut = 0x0;
660 while (*pFormat == '0')
662 *pOut = *pOut + 1;
663 pFormat++;
665 if (fmt_state & FMT_STATE_WROTE_DECIMAL)
666 num_header->fractional += *pOut;
667 else
668 num_header->whole += *pOut;
669 TRACE("%d 0's\n", *pOut);
670 pOut++;
671 fmt_state &= ~FMT_STATE_OPEN_COPY;
673 else if (*pFormat == '#' && COULD_BE(FMT_TYPE_NUMBER))
675 /* Number formats: Digit from number or blank if no digits
676 * Other formats: Literal
677 * Types the format if found
679 header->type = FMT_TYPE_NUMBER;
680 NEED_SPACE(2 * sizeof(BYTE));
681 *pOut++ = FMT_NUM_COPY_SKIP;
682 *pOut = 0x0;
683 while (*pFormat == '#')
685 *pOut = *pOut + 1;
686 pFormat++;
688 if (fmt_state & FMT_STATE_WROTE_DECIMAL)
689 num_header->fractional += *pOut;
690 else
691 num_header->whole += *pOut;
692 TRACE("%d #'s\n", *pOut);
693 pOut++;
694 fmt_state &= ~FMT_STATE_OPEN_COPY;
696 else if (*pFormat == '.' && COULD_BE(FMT_TYPE_NUMBER) &&
697 !(fmt_state & FMT_STATE_WROTE_DECIMAL))
699 /* Number formats: Decimal separator when 1st seen, literal thereafter
700 * Other formats: Literal
701 * Types the format if found
703 header->type = FMT_TYPE_NUMBER;
704 NEED_SPACE(sizeof(BYTE));
705 *pOut++ = FMT_NUM_DECIMAL;
706 fmt_state |= FMT_STATE_WROTE_DECIMAL;
707 fmt_state &= ~FMT_STATE_OPEN_COPY;
708 pFormat++;
709 TRACE("decimal sep\n");
711 else if ((*pFormat == 'e' || *pFormat == 'E') && (pFormat[1] == '-' ||
712 pFormat[1] == '+') && header->type == FMT_TYPE_NUMBER)
714 /* Number formats: Exponent specifier
715 * Other formats: Literal
717 num_header->flags |= FMT_FLAG_EXPONENT;
718 NEED_SPACE(2 * sizeof(BYTE));
719 if (*pFormat == 'e') {
720 if (pFormat[1] == '+')
721 *pOut = FMT_NUM_EXP_POS_L;
722 else
723 *pOut = FMT_NUM_EXP_NEG_L;
724 } else {
725 if (pFormat[1] == '+')
726 *pOut = FMT_NUM_EXP_POS_U;
727 else
728 *pOut = FMT_NUM_EXP_NEG_U;
730 pFormat += 2;
731 *++pOut = 0x0;
732 while (*pFormat == '0')
734 *pOut = *pOut + 1;
735 pFormat++;
737 pOut++;
738 TRACE("exponent\n");
740 /* FIXME: %% => Divide by 1000 */
741 else if (*pFormat == ',' && header->type == FMT_TYPE_NUMBER)
743 /* Number formats: Use the thousands separator
744 * Other formats: Literal
746 num_header->flags |= FMT_FLAG_THOUSANDS;
747 pFormat++;
748 fmt_state &= ~FMT_STATE_OPEN_COPY;
749 TRACE("thousands sep\n");
751 /* -----------
752 * Date tokens
753 * -----------
755 else if (*pFormat == '/' && COULD_BE(FMT_TYPE_DATE))
757 /* Date formats: Date separator
758 * Other formats: Literal
759 * Types the format if found
761 header->type = FMT_TYPE_DATE;
762 NEED_SPACE(sizeof(BYTE));
763 *pOut++ = FMT_DATE_DATE_SEP;
764 pFormat++;
765 fmt_state &= ~FMT_STATE_OPEN_COPY;
766 TRACE("date sep\n");
768 else if (*pFormat == ':' && COULD_BE(FMT_TYPE_DATE))
770 /* Date formats: Time separator
771 * Other formats: Literal
772 * Types the format if found
774 header->type = FMT_TYPE_DATE;
775 NEED_SPACE(sizeof(BYTE));
776 *pOut++ = FMT_DATE_TIME_SEP;
777 pFormat++;
778 fmt_state &= ~FMT_STATE_OPEN_COPY;
779 TRACE("time sep\n");
781 else if ((*pFormat == 'a' || *pFormat == 'A') &&
782 !strncmpiW(pFormat, szAMPM, sizeof(szAMPM)/sizeof(WCHAR)))
784 /* Date formats: System AM/PM designation
785 * Other formats: Literal
786 * Types the format if found
788 header->type = FMT_TYPE_DATE;
789 NEED_SPACE(sizeof(BYTE));
790 pFormat += sizeof(szAMPM)/sizeof(WCHAR);
791 if (!strncmpW(pFormat, szampm, sizeof(szampm)/sizeof(WCHAR)))
792 *pOut++ = FMT_DATE_AMPM_SYS2;
793 else
794 *pOut++ = FMT_DATE_AMPM_SYS1;
795 if (pLastHours)
796 *pLastHours = *pLastHours + 2;
797 TRACE("ampm\n");
799 else if (*pFormat == 'a' && pFormat[1] == '/' &&
800 (pFormat[2] == 'p' || pFormat[2] == 'P'))
802 /* Date formats: lowercase a or p designation
803 * Other formats: Literal
804 * Types the format if found
806 header->type = FMT_TYPE_DATE;
807 NEED_SPACE(sizeof(BYTE));
808 pFormat += 3;
809 *pOut++ = FMT_DATE_A_LOWER;
810 if (pLastHours)
811 *pLastHours = *pLastHours + 2;
812 TRACE("a/p\n");
814 else if (*pFormat == 'A' && pFormat[1] == '/' &&
815 (pFormat[2] == 'p' || pFormat[2] == 'P'))
817 /* Date formats: Uppercase a or p designation
818 * Other formats: Literal
819 * Types the format if found
821 header->type = FMT_TYPE_DATE;
822 NEED_SPACE(sizeof(BYTE));
823 pFormat += 3;
824 *pOut++ = FMT_DATE_A_UPPER;
825 if (pLastHours)
826 *pLastHours = *pLastHours + 2;
827 TRACE("A/P\n");
829 else if (*pFormat == 'a' &&
830 !strncmpW(pFormat, szamSlashpm, sizeof(szamSlashpm)/sizeof(WCHAR)))
832 /* Date formats: lowercase AM or PM designation
833 * Other formats: Literal
834 * Types the format if found
836 header->type = FMT_TYPE_DATE;
837 NEED_SPACE(sizeof(BYTE));
838 pFormat += sizeof(szamSlashpm)/sizeof(WCHAR);
839 *pOut++ = FMT_DATE_AMPM_LOWER;
840 if (pLastHours)
841 *pLastHours = *pLastHours + 2;
842 TRACE("AM/PM\n");
844 else if (*pFormat == 'A' &&
845 !strncmpW(pFormat, szAMSlashPM, sizeof(szAMSlashPM)/sizeof(WCHAR)))
847 /* Date formats: Uppercase AM or PM designation
848 * Other formats: Literal
849 * Types the format if found
851 header->type = FMT_TYPE_DATE;
852 NEED_SPACE(sizeof(BYTE));
853 pFormat += sizeof(szAMSlashPM)/sizeof(WCHAR);
854 *pOut++ = FMT_DATE_AMPM_UPPER;
855 TRACE("AM/PM\n");
857 else if (*pFormat == 'c' || *pFormat == 'C')
859 /* Date formats: General date format
860 * Other formats: Literal
861 * Types the format if found
863 header->type = FMT_TYPE_DATE;
864 NEED_SPACE(sizeof(BYTE));
865 pFormat += sizeof(szAMSlashPM)/sizeof(WCHAR);
866 *pOut++ = FMT_DATE_GENERAL;
867 TRACE("gen date\n");
869 else if ((*pFormat == 'd' || *pFormat == 'D') && COULD_BE(FMT_TYPE_DATE))
871 /* Date formats: Day specifier
872 * Other formats: Literal
873 * Types the format if found
875 int count = -1;
876 header->type = FMT_TYPE_DATE;
877 while ((*pFormat == 'd' || *pFormat == 'D') && count < 6)
879 pFormat++;
880 count++;
882 NEED_SPACE(sizeof(BYTE));
883 *pOut++ = FMT_DATE_DAY + count;
884 fmt_state &= ~FMT_STATE_OPEN_COPY;
885 /* When we find the days token, reset the seen hours state so that
886 * 'mm' is again written as month when encountered.
888 fmt_state &= ~FMT_STATE_SEEN_HOURS;
889 TRACE("%d d's\n", count + 1);
891 else if ((*pFormat == 'h' || *pFormat == 'H') && COULD_BE(FMT_TYPE_DATE))
893 /* Date formats: Hour specifier
894 * Other formats: Literal
895 * Types the format if found
897 header->type = FMT_TYPE_DATE;
898 NEED_SPACE(sizeof(BYTE));
899 pFormat++;
900 /* Record the position of the hours specifier - if we encounter
901 * an am/pm specifier we will change the hours from 24 to 12.
903 pLastHours = pOut;
904 if (*pFormat == 'h' || *pFormat == 'H')
906 pFormat++;
907 *pOut++ = FMT_DATE_HOUR_0;
908 TRACE("hh\n");
910 else
912 *pOut++ = FMT_DATE_HOUR;
913 TRACE("h\n");
915 fmt_state &= ~FMT_STATE_OPEN_COPY;
916 /* Note that now we have seen an hours token, the next occurrence of
917 * 'mm' indicates minutes, not months.
919 fmt_state |= FMT_STATE_SEEN_HOURS;
921 else if ((*pFormat == 'm' || *pFormat == 'M') && COULD_BE(FMT_TYPE_DATE))
923 /* Date formats: Month specifier (or Minute specifier, after hour specifier)
924 * Other formats: Literal
925 * Types the format if found
927 int count = -1;
928 header->type = FMT_TYPE_DATE;
929 while ((*pFormat == 'm' || *pFormat == 'M') && count < 4)
931 pFormat++;
932 count++;
934 NEED_SPACE(sizeof(BYTE));
935 if (count <= 1 && fmt_state & FMT_STATE_SEEN_HOURS &&
936 !(fmt_state & FMT_STATE_WROTE_MINUTES))
938 /* We have seen an hours specifier and not yet written a minutes
939 * specifier. Write this as minutes and thereafter as months.
941 *pOut++ = count == 1 ? FMT_DATE_MIN_0 : FMT_DATE_MIN;
942 fmt_state |= FMT_STATE_WROTE_MINUTES; /* Hereafter write months */
944 else
945 *pOut++ = FMT_DATE_MON + count; /* Months */
946 fmt_state &= ~FMT_STATE_OPEN_COPY;
947 TRACE("%d m's\n", count + 1);
949 else if ((*pFormat == 'n' || *pFormat == 'N') && COULD_BE(FMT_TYPE_DATE))
951 /* Date formats: Minute specifier
952 * Other formats: Literal
953 * Types the format if found
955 header->type = FMT_TYPE_DATE;
956 NEED_SPACE(sizeof(BYTE));
957 pFormat++;
958 if (*pFormat == 'n' || *pFormat == 'N')
960 pFormat++;
961 *pOut++ = FMT_DATE_MIN_0;
962 TRACE("nn\n");
964 else
966 *pOut++ = FMT_DATE_MIN;
967 TRACE("n\n");
969 fmt_state &= ~FMT_STATE_OPEN_COPY;
971 else if ((*pFormat == 'q' || *pFormat == 'q') && COULD_BE(FMT_TYPE_DATE))
973 /* Date formats: Quarter specifier
974 * Other formats: Literal
975 * Types the format if found
977 header->type = FMT_TYPE_DATE;
978 NEED_SPACE(sizeof(BYTE));
979 *pOut++ = FMT_DATE_QUARTER;
980 pFormat++;
981 fmt_state &= ~FMT_STATE_OPEN_COPY;
982 TRACE("quarter\n");
984 else if ((*pFormat == 's' || *pFormat == 'S') && COULD_BE(FMT_TYPE_DATE))
986 /* Date formats: Second specifier
987 * Other formats: Literal
988 * Types the format if found
990 header->type = FMT_TYPE_DATE;
991 NEED_SPACE(sizeof(BYTE));
992 pFormat++;
993 if (*pFormat == 's' || *pFormat == 'S')
995 pFormat++;
996 *pOut++ = FMT_DATE_SEC_0;
997 TRACE("ss\n");
999 else
1001 *pOut++ = FMT_DATE_SEC;
1002 TRACE("s\n");
1004 fmt_state &= ~FMT_STATE_OPEN_COPY;
1006 else if ((*pFormat == 't' || *pFormat == 'T') &&
1007 !strncmpiW(pFormat, szTTTTT, sizeof(szTTTTT)/sizeof(WCHAR)))
1009 /* Date formats: System time specifier
1010 * Other formats: Literal
1011 * Types the format if found
1013 header->type = FMT_TYPE_DATE;
1014 pFormat += sizeof(szTTTTT)/sizeof(WCHAR);
1015 NEED_SPACE(sizeof(BYTE));
1016 *pOut++ = FMT_DATE_TIME_SYS;
1017 fmt_state &= ~FMT_STATE_OPEN_COPY;
1019 else if ((*pFormat == 'w' || *pFormat == 'W') && COULD_BE(FMT_TYPE_DATE))
1021 /* Date formats: Week of the year/Day of the week
1022 * Other formats: Literal
1023 * Types the format if found
1025 header->type = FMT_TYPE_DATE;
1026 pFormat++;
1027 if (*pFormat == 'w' || *pFormat == 'W')
1029 NEED_SPACE(3 * sizeof(BYTE));
1030 pFormat++;
1031 *pOut++ = FMT_DATE_WEEK_YEAR;
1032 *pOut++ = nFirstDay;
1033 *pOut++ = nFirstWeek;
1034 TRACE("ww\n");
1036 else
1038 NEED_SPACE(2 * sizeof(BYTE));
1039 *pOut++ = FMT_DATE_DAY_WEEK;
1040 *pOut++ = nFirstDay;
1041 TRACE("w\n");
1044 fmt_state &= ~FMT_STATE_OPEN_COPY;
1046 else if ((*pFormat == 'y' || *pFormat == 'Y') && COULD_BE(FMT_TYPE_DATE))
1048 /* Date formats: Day of year/Year specifier
1049 * Other formats: Literal
1050 * Types the format if found
1052 int count = -1;
1053 header->type = FMT_TYPE_DATE;
1054 while ((*pFormat == 'y' || *pFormat == 'Y') && count < 4)
1056 pFormat++;
1057 count++;
1059 if (count == 2)
1061 count--; /* 'yyy' has no meaning, despite what MSDN says */
1062 pFormat--;
1064 NEED_SPACE(sizeof(BYTE));
1065 *pOut++ = FMT_DATE_YEAR_DOY + count;
1066 fmt_state &= ~FMT_STATE_OPEN_COPY;
1067 TRACE("%d y's\n", count + 1);
1069 /* -------------
1070 * String tokens
1071 * -------------
1073 else if (*pFormat == '@' && COULD_BE(FMT_TYPE_STRING))
1075 /* String formats: Character from string or space if no char
1076 * Other formats: Literal
1077 * Types the format if found
1079 header->type = FMT_TYPE_STRING;
1080 NEED_SPACE(2 * sizeof(BYTE));
1081 *pOut++ = FMT_STR_COPY_SPACE;
1082 *pOut = 0x0;
1083 while (*pFormat == '@')
1085 *pOut = *pOut + 1;
1086 str_header->copy_chars++;
1087 pFormat++;
1089 TRACE("%d @'s\n", *pOut);
1090 pOut++;
1091 fmt_state &= ~FMT_STATE_OPEN_COPY;
1093 else if (*pFormat == '&' && COULD_BE(FMT_TYPE_STRING))
1095 /* String formats: Character from string or skip if no char
1096 * Other formats: Literal
1097 * Types the format if found
1099 header->type = FMT_TYPE_STRING;
1100 NEED_SPACE(2 * sizeof(BYTE));
1101 *pOut++ = FMT_STR_COPY_SKIP;
1102 *pOut = 0x0;
1103 while (*pFormat == '&')
1105 *pOut = *pOut + 1;
1106 str_header->copy_chars++;
1107 pFormat++;
1109 TRACE("%d &'s\n", *pOut);
1110 pOut++;
1111 fmt_state &= ~FMT_STATE_OPEN_COPY;
1113 else if ((*pFormat == '<' || *pFormat == '>') && COULD_BE(FMT_TYPE_STRING))
1115 /* String formats: Use upper/lower case
1116 * Other formats: Literal
1117 * Types the format if found
1119 header->type = FMT_TYPE_STRING;
1120 if (*pFormat == '<')
1121 str_header->flags |= FMT_FLAG_LT;
1122 else
1123 str_header->flags |= FMT_FLAG_GT;
1124 TRACE("to %s case\n", *pFormat == '<' ? "lower" : "upper");
1125 pFormat++;
1126 fmt_state &= ~FMT_STATE_OPEN_COPY;
1128 else if (*pFormat == '!' && COULD_BE(FMT_TYPE_STRING))
1130 /* String formats: Copy right to left
1131 * Other formats: Literal
1132 * Types the format if found
1134 header->type = FMT_TYPE_STRING;
1135 str_header->flags |= FMT_FLAG_RTL;
1136 pFormat++;
1137 fmt_state &= ~FMT_STATE_OPEN_COPY;
1138 TRACE("copy right-to-left\n");
1140 /* --------
1141 * Literals
1142 * --------
1144 /* FIXME: [ seems to be ignored */
1145 else
1147 if (*pFormat == '%' && header->type == FMT_TYPE_NUMBER)
1149 /* Number formats: Percentage indicator, also a literal
1150 * Other formats: Literal
1151 * Doesn't type the format
1153 num_header->flags |= FMT_FLAG_PERCENT;
1156 if (fmt_state & FMT_STATE_OPEN_COPY)
1158 pOut[-1] = pOut[-1] + 1; /* Increase the length of the open copy */
1159 TRACE("extend copy (char '%c'), length now %d\n", *pFormat, pOut[-1]);
1161 else
1163 /* Create a new open copy */
1164 TRACE("New copy (char '%c')\n", *pFormat);
1165 NEED_SPACE(3 * sizeof(BYTE));
1166 *pOut++ = FMT_GEN_COPY;
1167 *pOut++ = pFormat - lpszFormat;
1168 *pOut++ = 0x1;
1169 fmt_state |= FMT_STATE_OPEN_COPY;
1171 pFormat++;
1175 *pOut++ = FMT_GEN_END;
1177 header->size = pOut - rgbTok;
1178 if (pcbActual)
1179 *pcbActual = header->size;
1181 return S_OK;
1184 /* Number formatting state flags */
1185 #define NUM_WROTE_DEC 0x01 /* Written the decimal separator */
1186 #define NUM_WRITE_ON 0x02 /* Started to write the number */
1188 /* Format a variant using a number format */
1189 static HRESULT VARIANT_FormatNumber(LPVARIANT pVarIn, LPOLESTR lpszFormat,
1190 LPBYTE rgbTok, ULONG dwFlags,
1191 BSTR *pbstrOut, LCID lcid)
1193 BYTE rgbDig[256], *prgbDig;
1194 NUMPARSE np;
1195 int have_int, need_int = 0, have_frac, need_frac, exponent = 0, pad = 0;
1196 WCHAR buff[256], *pBuff = buff;
1197 VARIANT vString, vBool;
1198 DWORD dwState = 0;
1199 FMT_HEADER *header = (FMT_HEADER*)rgbTok;
1200 FMT_NUMBER_HEADER *numHeader;
1201 const BYTE* pToken = NULL;
1202 HRESULT hRes = S_OK;
1204 TRACE("(%p->(%s%s),%s,%p,0x%08lx,%p,0x%08lx)\n", pVarIn, debugstr_VT(pVarIn),
1205 debugstr_VF(pVarIn), debugstr_w(lpszFormat), rgbTok, dwFlags, pbstrOut,
1206 lcid);
1208 V_VT(&vString) = VT_EMPTY;
1209 V_VT(&vBool) = VT_BOOL;
1211 if (V_TYPE(pVarIn) == VT_EMPTY || V_TYPE(pVarIn) == VT_NULL)
1213 have_int = have_frac = 0;
1214 numHeader = (FMT_NUMBER_HEADER*)(rgbTok + FmtGetNull(header));
1215 V_BOOL(&vBool) = VARIANT_FALSE;
1217 else
1219 /* Get a number string from pVarIn, and parse it */
1220 hRes = VariantChangeTypeEx(&vString, pVarIn, LCID_US, VARIANT_NOUSEROVERRIDE, VT_BSTR);
1221 if (FAILED(hRes))
1222 return hRes;
1224 np.cDig = sizeof(rgbDig);
1225 np.dwInFlags = NUMPRS_STD;
1226 hRes = VarParseNumFromStr(V_BSTR(&vString), LCID_US, 0, &np, rgbDig);
1227 if (FAILED(hRes))
1228 return hRes;
1230 have_int = np.cDig;
1231 have_frac = 0;
1232 exponent = np.nPwr10;
1234 /* Figure out which format to use */
1235 if (np.dwOutFlags & NUMPRS_NEG)
1237 numHeader = (FMT_NUMBER_HEADER*)(rgbTok + FmtGetNegative(header));
1238 V_BOOL(&vBool) = VARIANT_TRUE;
1240 else if (have_int == 1 && !exponent && rgbDig[0] == 0)
1242 numHeader = (FMT_NUMBER_HEADER*)(rgbTok + FmtGetZero(header));
1243 V_BOOL(&vBool) = VARIANT_FALSE;
1245 else
1247 numHeader = (FMT_NUMBER_HEADER*)(rgbTok + FmtGetPositive(header));
1248 V_BOOL(&vBool) = VARIANT_TRUE;
1251 TRACE("num header: flags = 0x%x, mult=%d, div=%d, whole=%d, fract=%d\n",
1252 numHeader->flags, numHeader->multiplier, numHeader->divisor,
1253 numHeader->whole, numHeader->fractional);
1255 need_int = numHeader->whole;
1256 need_frac = numHeader->fractional;
1258 if (numHeader->flags & FMT_FLAG_PERCENT &&
1259 !(have_int == 1 && !exponent && rgbDig[0] == 0))
1260 exponent += 2;
1262 if (numHeader->flags & FMT_FLAG_EXPONENT)
1264 /* Exponent format: length of the integral number part is fixed and
1265 specified by the format. */
1266 pad = need_int - have_int;
1267 if (pad >= 0)
1268 exponent -= pad;
1269 else
1271 have_int = need_int;
1272 have_frac -= pad;
1273 exponent -= pad;
1274 pad = 0;
1277 else
1279 /* Convert the exponent */
1280 pad = max(exponent, -have_int);
1281 exponent -= pad;
1282 if (pad < 0)
1284 have_int += pad;
1285 have_frac = -pad;
1286 pad = 0;
1290 /* Rounding the number */
1291 if (have_frac > need_frac)
1293 prgbDig = &rgbDig[have_int + need_frac];
1294 have_frac = need_frac;
1295 if (*prgbDig >= 5)
1297 while (prgbDig-- > rgbDig && *prgbDig == 9)
1298 *prgbDig = 0;
1299 if (prgbDig < rgbDig)
1301 /* We reached the first digit and that was also a 9 */
1302 rgbDig[0] = 1;
1303 if (numHeader->flags & FMT_FLAG_EXPONENT)
1304 exponent++;
1305 else
1307 rgbDig[have_int + need_frac] = 0;
1308 have_int++;
1311 else
1312 (*prgbDig)++;
1315 TRACE("have_int=%d,need_int=%d,have_frac=%d,need_frac=%d,pad=%d,exp=%d\n",
1316 have_int, need_int, have_frac, need_frac, pad, exponent);
1319 pToken = (const BYTE*)numHeader + sizeof(FMT_NUMBER_HEADER);
1320 prgbDig = rgbDig;
1322 while (SUCCEEDED(hRes) && *pToken != FMT_GEN_END)
1324 WCHAR defaultChar = '?';
1325 DWORD boolFlag, localeValue = 0;
1327 if (pToken - rgbTok > header->size)
1329 ERR("Ran off the end of the format!\n");
1330 hRes = E_INVALIDARG;
1331 goto VARIANT_FormatNumber_Exit;
1334 switch (*pToken)
1336 case FMT_GEN_COPY:
1337 TRACE("copy %s\n", debugstr_wn(lpszFormat + pToken[1], pToken[2]));
1338 memcpy(pBuff, lpszFormat + pToken[1], pToken[2] * sizeof(WCHAR));
1339 pBuff += pToken[2];
1340 pToken += 2;
1341 break;
1343 case FMT_GEN_INLINE:
1344 pToken += 2;
1345 TRACE("copy %s\n", debugstr_a((LPCSTR)pToken));
1346 while (*pToken)
1347 *pBuff++ = *pToken++;
1348 break;
1350 case FMT_NUM_YES_NO:
1351 boolFlag = VAR_BOOLYESNO;
1352 goto VARIANT_FormatNumber_Bool;
1354 case FMT_NUM_ON_OFF:
1355 boolFlag = VAR_BOOLONOFF;
1356 goto VARIANT_FormatNumber_Bool;
1358 case FMT_NUM_TRUE_FALSE:
1359 boolFlag = VAR_LOCALBOOL;
1361 VARIANT_FormatNumber_Bool:
1363 BSTR boolStr = NULL;
1365 if (pToken[1] != FMT_GEN_END)
1367 ERR("Boolean token not at end of format!\n");
1368 hRes = E_INVALIDARG;
1369 goto VARIANT_FormatNumber_Exit;
1371 hRes = VarBstrFromBool(V_BOOL(&vBool), lcid, boolFlag, &boolStr);
1372 if (SUCCEEDED(hRes))
1374 strcpyW(pBuff, boolStr);
1375 SysFreeString(boolStr);
1376 while (*pBuff)
1377 pBuff++;
1380 break;
1382 case FMT_NUM_DECIMAL:
1383 TRACE("write decimal separator\n");
1384 localeValue = LOCALE_SDECIMAL;
1385 defaultChar = '.';
1386 dwState |= NUM_WROTE_DEC;
1387 break;
1389 case FMT_NUM_CURRENCY:
1390 TRACE("write currency symbol\n");
1391 localeValue = LOCALE_SCURRENCY;
1392 defaultChar = '$';
1393 break;
1395 case FMT_NUM_EXP_POS_U:
1396 case FMT_NUM_EXP_POS_L:
1397 case FMT_NUM_EXP_NEG_U:
1398 case FMT_NUM_EXP_NEG_L:
1399 if (*pToken == FMT_NUM_EXP_POS_L || *pToken == FMT_NUM_EXP_NEG_L)
1400 *pBuff++ = 'e';
1401 else
1402 *pBuff++ = 'E';
1403 if (exponent < 0)
1405 *pBuff++ = '-';
1406 sprintfW(pBuff, szPercentZeroStar_d, pToken[1], -exponent);
1408 else
1410 if (*pToken == FMT_NUM_EXP_POS_L || *pToken == FMT_NUM_EXP_POS_U)
1411 *pBuff++ = '+';
1412 sprintfW(pBuff, szPercentZeroStar_d, pToken[1], exponent);
1414 while (*pBuff)
1415 pBuff++;
1416 pToken++;
1417 break;
1419 case FMT_NUM_COPY_ZERO:
1420 dwState |= NUM_WRITE_ON;
1421 /* Fall through */
1423 case FMT_NUM_COPY_SKIP:
1424 TRACE("write %d %sdigits or %s\n", pToken[1],
1425 dwState & NUM_WROTE_DEC ? "fractional " : "",
1426 *pToken == FMT_NUM_COPY_ZERO ? "0" : "skip");
1428 if (dwState & NUM_WROTE_DEC)
1430 int count, i;
1432 if (!(numHeader->flags & FMT_FLAG_EXPONENT) && exponent < 0)
1434 /* Pad with 0 before writing the fractional digits */
1435 pad = max(exponent, -pToken[1]);
1436 exponent -= pad;
1437 count = min(have_frac, pToken[1] + pad);
1438 for (i = 0; i > pad; i--)
1439 *pBuff++ = '0';
1441 else
1442 count = min(have_frac, pToken[1]);
1444 pad += pToken[1] - count;
1445 have_frac -= count;
1446 while (count--)
1447 *pBuff++ = '0' + *prgbDig++;
1448 if (*pToken == FMT_NUM_COPY_ZERO)
1450 for (; pad > 0; pad--)
1451 *pBuff++ = '0'; /* Write zeros for missing trailing digits */
1454 else
1456 int count, count_max;
1458 need_int -= pToken[1];
1459 count_max = have_int + pad - need_int;
1460 if (count_max < 0)
1461 count_max = 0;
1462 if (dwState & NUM_WRITE_ON)
1464 count = pToken[1] - count_max;
1465 TRACE("write %d leading zeros\n", count);
1466 while (count-- > 0)
1467 *pBuff++ = '0';
1469 if (*pToken == FMT_NUM_COPY_ZERO || have_int > 1 || *prgbDig > 0)
1471 dwState |= NUM_WRITE_ON;
1472 count = min(count_max, have_int);
1473 count_max -= count;
1474 have_int -= count;
1475 TRACE("write %d whole number digits\n", count);
1476 while (count--)
1477 *pBuff++ = '0' + *prgbDig++;
1479 count = min(count_max, pad);
1480 count_max -= count;
1481 pad -= count;
1482 TRACE("write %d whole trailing 0's\n", count);
1483 while (count--)
1484 *pBuff++ = '0';
1486 pToken++;
1487 break;
1489 default:
1490 ERR("Unknown token 0x%02x!\n", *pToken);
1491 hRes = E_INVALIDARG;
1492 goto VARIANT_FormatNumber_Exit;
1494 if (localeValue)
1496 if (GetLocaleInfoW(lcid, localeValue, pBuff,
1497 sizeof(buff)/sizeof(WCHAR)-(pBuff-buff)))
1499 TRACE("added %s\n", debugstr_w(pBuff));
1500 while (*pBuff)
1501 pBuff++;
1503 else
1505 TRACE("added %d '%c'\n", defaultChar, defaultChar);
1506 *pBuff++ = defaultChar;
1509 pToken++;
1512 VARIANT_FormatNumber_Exit:
1513 VariantClear(&vString);
1514 *pBuff = '\0';
1515 TRACE("buff is %s\n", debugstr_w(buff));
1516 if (SUCCEEDED(hRes))
1518 *pbstrOut = SysAllocString(buff);
1519 if (!*pbstrOut)
1520 hRes = E_OUTOFMEMORY;
1522 return hRes;
1525 /* Format a variant using a date format */
1526 static HRESULT VARIANT_FormatDate(LPVARIANT pVarIn, LPOLESTR lpszFormat,
1527 LPBYTE rgbTok, ULONG dwFlags,
1528 BSTR *pbstrOut, LCID lcid)
1530 WCHAR buff[256], *pBuff = buff;
1531 VARIANT vDate;
1532 UDATE udate;
1533 FMT_HEADER *header = (FMT_HEADER*)rgbTok;
1534 FMT_DATE_HEADER *dateHeader;
1535 const BYTE* pToken = NULL;
1536 HRESULT hRes;
1538 TRACE("(%p->(%s%s),%s,%p,0x%08lx,%p,0x%08lx)\n", pVarIn, debugstr_VT(pVarIn),
1539 debugstr_VF(pVarIn), debugstr_w(lpszFormat), rgbTok, dwFlags, pbstrOut,
1540 lcid);
1542 V_VT(&vDate) = VT_EMPTY;
1544 if (V_TYPE(pVarIn) == VT_EMPTY || V_TYPE(pVarIn) == VT_NULL)
1546 dateHeader = (FMT_DATE_HEADER*)(rgbTok + FmtGetNegative(header));
1547 V_DATE(&vDate) = 0;
1549 else
1551 USHORT usFlags = dwFlags & VARIANT_CALENDAR_HIJRI ? VAR_CALENDAR_HIJRI : 0;
1553 hRes = VariantChangeTypeEx(&vDate, pVarIn, LCID_US, usFlags, VT_DATE);
1554 if (FAILED(hRes))
1555 return hRes;
1556 dateHeader = (FMT_DATE_HEADER*)(rgbTok + FmtGetPositive(header));
1559 hRes = VarUdateFromDate(V_DATE(&vDate), 0 /* FIXME: flags? */, &udate);
1560 if (FAILED(hRes))
1561 return hRes;
1562 pToken = (const BYTE*)dateHeader + sizeof(FMT_DATE_HEADER);
1564 while (*pToken != FMT_GEN_END)
1566 DWORD dwVal = 0, localeValue = 0, dwFmt = 0;
1567 LPCWSTR szPrintFmt = NULL;
1568 WCHAR defaultChar = '?';
1570 if (pToken - rgbTok > header->size)
1572 ERR("Ran off the end of the format!\n");
1573 hRes = E_INVALIDARG;
1574 goto VARIANT_FormatDate_Exit;
1577 switch (*pToken)
1579 case FMT_GEN_COPY:
1580 TRACE("copy %s\n", debugstr_wn(lpszFormat + pToken[1], pToken[2]));
1581 memcpy(pBuff, lpszFormat + pToken[1], pToken[2] * sizeof(WCHAR));
1582 pBuff += pToken[2];
1583 pToken += 2;
1584 break;
1586 case FMT_DATE_TIME_SEP:
1587 TRACE("time separator\n");
1588 localeValue = LOCALE_STIME;
1589 defaultChar = ':';
1590 break;
1592 case FMT_DATE_DATE_SEP:
1593 TRACE("date separator\n");
1594 localeValue = LOCALE_SDATE;
1595 defaultChar = '/';
1596 break;
1598 case FMT_DATE_GENERAL:
1600 BSTR date = NULL;
1601 WCHAR *pDate;
1602 hRes = VarBstrFromDate(V_DATE(&vDate), lcid, 0, &date);
1603 if (FAILED(hRes))
1604 goto VARIANT_FormatDate_Exit;
1605 pDate = date;
1606 while (*pDate)
1607 *pBuff++ = *pDate++;
1608 SysFreeString(date);
1610 break;
1612 case FMT_DATE_QUARTER:
1613 if (udate.st.wMonth <= 3)
1614 *pBuff++ = '1';
1615 else if (udate.st.wMonth <= 6)
1616 *pBuff++ = '2';
1617 else if (udate.st.wMonth <= 9)
1618 *pBuff++ = '3';
1619 else
1620 *pBuff++ = '4';
1621 break;
1623 case FMT_DATE_TIME_SYS:
1625 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1626 BSTR date = NULL;
1627 WCHAR *pDate;
1628 hRes = VarBstrFromDate(V_DATE(&vDate), lcid, VAR_TIMEVALUEONLY, &date);
1629 if (FAILED(hRes))
1630 goto VARIANT_FormatDate_Exit;
1631 pDate = date;
1632 while (*pDate)
1633 *pBuff++ = *pDate++;
1634 SysFreeString(date);
1636 break;
1638 case FMT_DATE_DAY:
1639 szPrintFmt = szPercent_d;
1640 dwVal = udate.st.wDay;
1641 break;
1643 case FMT_DATE_DAY_0:
1644 szPrintFmt = szPercentZeroTwo_d;
1645 dwVal = udate.st.wDay;
1646 break;
1648 case FMT_DATE_DAY_SHORT:
1649 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1650 TRACE("short day\n");
1651 localeValue = LOCALE_SABBREVDAYNAME1 + udate.st.wMonth - 1;
1652 defaultChar = '?';
1653 break;
1655 case FMT_DATE_DAY_LONG:
1656 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1657 TRACE("long day\n");
1658 localeValue = LOCALE_SDAYNAME1 + udate.st.wMonth - 1;
1659 defaultChar = '?';
1660 break;
1662 case FMT_DATE_SHORT:
1663 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1664 dwFmt = LOCALE_SSHORTDATE;
1665 break;
1667 case FMT_DATE_LONG:
1668 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1669 dwFmt = LOCALE_SLONGDATE;
1670 break;
1672 case FMT_DATE_MEDIUM:
1673 FIXME("Medium date treated as long date\n");
1674 dwFmt = LOCALE_SLONGDATE;
1675 break;
1677 case FMT_DATE_DAY_WEEK:
1678 szPrintFmt = szPercent_d;
1679 if (pToken[1])
1680 dwVal = udate.st.wDayOfWeek + 2 - pToken[1];
1681 else
1683 GetLocaleInfoW(lcid,LOCALE_RETURN_NUMBER|LOCALE_IFIRSTDAYOFWEEK,
1684 (LPWSTR)&dwVal, sizeof(dwVal)/sizeof(WCHAR));
1685 dwVal = udate.st.wDayOfWeek + 1 - dwVal;
1687 pToken++;
1688 break;
1690 case FMT_DATE_WEEK_YEAR:
1691 szPrintFmt = szPercent_d;
1692 dwVal = udate.wDayOfYear / 7 + 1;
1693 pToken += 2;
1694 FIXME("Ignoring nFirstDay of %d, nFirstWeek of %d\n", pToken[0], pToken[1]);
1695 break;
1697 case FMT_DATE_MON:
1698 szPrintFmt = szPercent_d;
1699 dwVal = udate.st.wMonth;
1700 break;
1702 case FMT_DATE_MON_0:
1703 szPrintFmt = szPercentZeroTwo_d;
1704 dwVal = udate.st.wMonth;
1705 break;
1707 case FMT_DATE_MON_SHORT:
1708 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1709 TRACE("short month\n");
1710 localeValue = LOCALE_SABBREVMONTHNAME1 + udate.st.wMonth - 1;
1711 defaultChar = '?';
1712 break;
1714 case FMT_DATE_MON_LONG:
1715 /* FIXME: VARIANT_CALENDAR HIJRI should cause Hijri output */
1716 TRACE("long month\n");
1717 localeValue = LOCALE_SMONTHNAME1 + udate.st.wMonth - 1;
1718 defaultChar = '?';
1719 break;
1721 case FMT_DATE_YEAR_DOY:
1722 szPrintFmt = szPercent_d;
1723 dwVal = udate.wDayOfYear;
1724 break;
1726 case FMT_DATE_YEAR_0:
1727 szPrintFmt = szPercentZeroTwo_d;
1728 dwVal = udate.st.wYear % 100;
1729 break;
1731 case FMT_DATE_YEAR_LONG:
1732 szPrintFmt = szPercent_d;
1733 dwVal = udate.st.wYear;
1734 break;
1736 case FMT_DATE_MIN:
1737 szPrintFmt = szPercent_d;
1738 dwVal = udate.st.wMinute;
1739 break;
1741 case FMT_DATE_MIN_0:
1742 szPrintFmt = szPercentZeroTwo_d;
1743 dwVal = udate.st.wMinute;
1744 break;
1746 case FMT_DATE_SEC:
1747 szPrintFmt = szPercent_d;
1748 dwVal = udate.st.wSecond;
1749 break;
1751 case FMT_DATE_SEC_0:
1752 szPrintFmt = szPercentZeroTwo_d;
1753 dwVal = udate.st.wSecond;
1754 break;
1756 case FMT_DATE_HOUR:
1757 szPrintFmt = szPercent_d;
1758 dwVal = udate.st.wHour;
1759 break;
1761 case FMT_DATE_HOUR_0:
1762 szPrintFmt = szPercentZeroTwo_d;
1763 dwVal = udate.st.wHour;
1764 break;
1766 case FMT_DATE_HOUR_12:
1767 szPrintFmt = szPercent_d;
1768 dwVal = udate.st.wHour ? udate.st.wHour > 12 ? udate.st.wHour - 12 : udate.st.wHour : 12;
1769 break;
1771 case FMT_DATE_HOUR_12_0:
1772 szPrintFmt = szPercentZeroTwo_d;
1773 dwVal = udate.st.wHour ? udate.st.wHour > 12 ? udate.st.wHour - 12 : udate.st.wHour : 12;
1774 break;
1776 case FMT_DATE_AMPM_SYS1:
1777 case FMT_DATE_AMPM_SYS2:
1778 localeValue = udate.st.wHour < 12 ? LOCALE_S1159 : LOCALE_S2359;
1779 defaultChar = '?';
1780 break;
1782 case FMT_DATE_AMPM_UPPER:
1783 *pBuff++ = udate.st.wHour < 12 ? 'A' : 'P';
1784 *pBuff++ = 'M';
1785 break;
1787 case FMT_DATE_A_UPPER:
1788 *pBuff++ = udate.st.wHour < 12 ? 'A' : 'P';
1789 break;
1791 case FMT_DATE_AMPM_LOWER:
1792 *pBuff++ = udate.st.wHour < 12 ? 'a' : 'p';
1793 *pBuff++ = 'm';
1794 break;
1796 case FMT_DATE_A_LOWER:
1797 *pBuff++ = udate.st.wHour < 12 ? 'a' : 'p';
1798 break;
1800 default:
1801 ERR("Unknown token 0x%02x!\n", *pToken);
1802 hRes = E_INVALIDARG;
1803 goto VARIANT_FormatDate_Exit;
1805 if (localeValue)
1807 *pBuff = '\0';
1808 if (GetLocaleInfoW(lcid, localeValue, pBuff,
1809 sizeof(buff)/sizeof(WCHAR)-(pBuff-buff)))
1811 TRACE("added %s\n", debugstr_w(pBuff));
1812 while (*pBuff)
1813 pBuff++;
1815 else
1817 TRACE("added %d %c\n", defaultChar, defaultChar);
1818 *pBuff++ = defaultChar;
1821 else if (dwFmt)
1823 WCHAR fmt_buff[80];
1825 if (!GetLocaleInfoW(lcid, dwFmt, fmt_buff, sizeof(fmt_buff)/sizeof(WCHAR)) ||
1826 !GetDateFormatW(lcid, 0, &udate.st, fmt_buff, pBuff,
1827 sizeof(buff)/sizeof(WCHAR)-(pBuff-buff)))
1829 hRes = E_INVALIDARG;
1830 goto VARIANT_FormatDate_Exit;
1832 while (*pBuff)
1833 pBuff++;
1835 else if (szPrintFmt)
1837 sprintfW(pBuff, szPrintFmt, dwVal);
1838 while (*pBuff)
1839 pBuff++;
1841 pToken++;
1844 VARIANT_FormatDate_Exit:
1845 *pBuff = '\0';
1846 TRACE("buff is %s\n", debugstr_w(buff));
1847 if (SUCCEEDED(hRes))
1849 *pbstrOut = SysAllocString(buff);
1850 if (!*pbstrOut)
1851 hRes = E_OUTOFMEMORY;
1853 return hRes;
1856 /* Format a variant using a string format */
1857 static HRESULT VARIANT_FormatString(LPVARIANT pVarIn, LPOLESTR lpszFormat,
1858 LPBYTE rgbTok, ULONG dwFlags,
1859 BSTR *pbstrOut, LCID lcid)
1861 static const WCHAR szEmpty[] = { '\0' };
1862 WCHAR buff[256], *pBuff = buff;
1863 WCHAR *pSrc;
1864 FMT_HEADER *header = (FMT_HEADER*)rgbTok;
1865 FMT_STRING_HEADER *strHeader;
1866 const BYTE* pToken = NULL;
1867 VARIANT vStr;
1868 int blanks_first;
1869 BOOL bUpper = FALSE;
1870 HRESULT hRes = S_OK;
1872 TRACE("(%p->(%s%s),%s,%p,0x%08lx,%p,0x%08lx)\n", pVarIn, debugstr_VT(pVarIn),
1873 debugstr_VF(pVarIn), debugstr_w(lpszFormat), rgbTok, dwFlags, pbstrOut,
1874 lcid);
1876 V_VT(&vStr) = VT_EMPTY;
1878 if (V_TYPE(pVarIn) == VT_EMPTY || V_TYPE(pVarIn) == VT_NULL)
1880 strHeader = (FMT_STRING_HEADER*)(rgbTok + FmtGetNegative(header));
1881 V_BSTR(&vStr) = (WCHAR*)szEmpty;
1883 else
1885 hRes = VariantChangeTypeEx(&vStr, pVarIn, LCID_US, VARIANT_NOUSEROVERRIDE, VT_BSTR);
1886 if (FAILED(hRes))
1887 return hRes;
1889 if (V_BSTR(pVarIn)[0] == '\0')
1890 strHeader = (FMT_STRING_HEADER*)(rgbTok + FmtGetNegative(header));
1891 else
1892 strHeader = (FMT_STRING_HEADER*)(rgbTok + FmtGetPositive(header));
1894 pSrc = V_BSTR(&vStr);
1895 if ((strHeader->flags & (FMT_FLAG_LT|FMT_FLAG_GT)) == FMT_FLAG_GT)
1896 bUpper = TRUE;
1897 blanks_first = strHeader->copy_chars - strlenW(pSrc);
1898 pToken = (const BYTE*)strHeader + sizeof(FMT_DATE_HEADER);
1900 while (*pToken != FMT_GEN_END)
1902 int dwCount = 0;
1904 if (pToken - rgbTok > header->size)
1906 ERR("Ran off the end of the format!\n");
1907 hRes = E_INVALIDARG;
1908 goto VARIANT_FormatString_Exit;
1911 switch (*pToken)
1913 case FMT_GEN_COPY:
1914 TRACE("copy %s\n", debugstr_wn(lpszFormat + pToken[1], pToken[2]));
1915 memcpy(pBuff, lpszFormat + pToken[1], pToken[2] * sizeof(WCHAR));
1916 pBuff += pToken[2];
1917 pToken += 2;
1918 break;
1920 case FMT_STR_COPY_SPACE:
1921 case FMT_STR_COPY_SKIP:
1922 dwCount = pToken[1];
1923 if (*pToken == FMT_STR_COPY_SPACE && blanks_first > 0)
1925 TRACE("insert %d initial spaces\n", blanks_first);
1926 while (dwCount > 0 && blanks_first > 0)
1928 *pBuff++ = ' ';
1929 dwCount--;
1930 blanks_first--;
1933 TRACE("copy %d chars%s\n", dwCount,
1934 *pToken == FMT_STR_COPY_SPACE ? " with space" :"");
1935 while (dwCount > 0 && *pSrc)
1937 if (bUpper)
1938 *pBuff++ = toupperW(*pSrc);
1939 else
1940 *pBuff++ = tolowerW(*pSrc);
1941 dwCount--;
1942 pSrc++;
1944 if (*pToken == FMT_STR_COPY_SPACE && dwCount > 0)
1946 TRACE("insert %d spaces\n", dwCount);
1947 while (dwCount-- > 0)
1948 *pBuff++ = ' ';
1950 pToken++;
1951 break;
1953 default:
1954 ERR("Unknown token 0x%02x!\n", *pToken);
1955 hRes = E_INVALIDARG;
1956 goto VARIANT_FormatString_Exit;
1958 pToken++;
1961 VARIANT_FormatString_Exit:
1962 /* Copy out any remaining chars */
1963 while (*pSrc)
1965 if (bUpper)
1966 *pBuff++ = toupperW(*pSrc);
1967 else
1968 *pBuff++ = tolowerW(*pSrc);
1969 pSrc++;
1971 VariantClear(&vStr);
1972 *pBuff = '\0';
1973 TRACE("buff is %s\n", debugstr_w(buff));
1974 if (SUCCEEDED(hRes))
1976 *pbstrOut = SysAllocString(buff);
1977 if (!*pbstrOut)
1978 hRes = E_OUTOFMEMORY;
1980 return hRes;
1983 #define NUMBER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2| \
1984 VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8| \
1985 VTBIT_R4|VTBIT_R8|VTBIT_CY|VTBIT_DECIMAL| \
1986 (1<<VT_BOOL)|(1<<VT_INT)|(1<<VT_UINT))
1988 /**********************************************************************
1989 * VarFormatFromTokens [OLEAUT32.139]
1991 HRESULT WINAPI VarFormatFromTokens(LPVARIANT pVarIn, LPOLESTR lpszFormat,
1992 LPBYTE rgbTok, ULONG dwFlags,
1993 BSTR *pbstrOut, LCID lcid)
1995 FMT_SHORT_HEADER *header = (FMT_SHORT_HEADER *)rgbTok;
1996 VARIANT vTmp;
1997 HRESULT hres;
1999 TRACE("(%p,%s,%p,%lx,%p,0x%08lx)\n", pVarIn, debugstr_w(lpszFormat),
2000 rgbTok, dwFlags, pbstrOut, lcid);
2002 if (!pbstrOut)
2003 return E_INVALIDARG;
2005 *pbstrOut = NULL;
2007 if (!pVarIn || !rgbTok)
2008 return E_INVALIDARG;
2010 if (*rgbTok == FMT_TO_STRING || header->type == FMT_TYPE_GENERAL)
2012 /* According to MSDN, general format acts somewhat like the 'Str'
2013 * function in Visual Basic.
2015 VarFormatFromTokens_AsStr:
2016 V_VT(&vTmp) = VT_EMPTY;
2017 hres = VariantChangeTypeEx(&vTmp, pVarIn, lcid, dwFlags, VT_BSTR);
2018 *pbstrOut = V_BSTR(&vTmp);
2020 else
2022 if (header->type == FMT_TYPE_NUMBER ||
2023 (header->type == FMT_TYPE_UNKNOWN && ((1 << V_TYPE(pVarIn)) & NUMBER_VTBITS)))
2025 hres = VARIANT_FormatNumber(pVarIn, lpszFormat, rgbTok, dwFlags, pbstrOut, lcid);
2027 else if (header->type == FMT_TYPE_DATE ||
2028 (header->type == FMT_TYPE_UNKNOWN && V_TYPE(pVarIn) == VT_DATE))
2030 hres = VARIANT_FormatDate(pVarIn, lpszFormat, rgbTok, dwFlags, pbstrOut, lcid);
2032 else if (header->type == FMT_TYPE_STRING || V_TYPE(pVarIn) == VT_BSTR)
2034 hres = VARIANT_FormatString(pVarIn, lpszFormat, rgbTok, dwFlags, pbstrOut, lcid);
2036 else
2038 ERR("unrecognised format type 0x%02x\n", header->type);
2039 return E_INVALIDARG;
2041 /* If the coercion failed, still try to create output, unless the
2042 * VAR_FORMAT_NOSUBSTITUTE flag is set.
2044 if ((hres == DISP_E_OVERFLOW || hres == DISP_E_TYPEMISMATCH) &&
2045 !(dwFlags & VAR_FORMAT_NOSUBSTITUTE))
2046 goto VarFormatFromTokens_AsStr;
2049 return hres;
2052 /**********************************************************************
2053 * VarFormat [OLEAUT32.87]
2055 * Format a variant from a format string.
2057 * PARAMS
2058 * pVarIn [I] Variant to format
2059 * lpszFormat [I] Format string (see notes)
2060 * nFirstDay [I] First day of the week, (See VarTokenizeFormatString() for details)
2061 * nFirstWeek [I] First week of the year (See VarTokenizeFormatString() for details)
2062 * dwFlags [I] Flags for the format (VAR_ flags from "oleauto.h")
2063 * pbstrOut [O] Destination for formatted string.
2065 * RETURNS
2066 * Success: S_OK. pbstrOut contains the formatted value.
2067 * Failure: E_INVALIDARG, if any parameter is invalid.
2068 * E_OUTOFMEMORY, if enough memory cannot be allocated.
2069 * DISP_E_TYPEMISMATCH, if the variant cannot be formatted.
2071 * NOTES
2072 * - See Variant-Formats for details concerning creating format strings.
2073 * - This function uses LOCALE_USER_DEFAULT when calling VarTokenizeFormatString()
2074 * and VarFormatFromTokens().
2076 HRESULT WINAPI VarFormat(LPVARIANT pVarIn, LPOLESTR lpszFormat,
2077 int nFirstDay, int nFirstWeek, ULONG dwFlags,
2078 BSTR *pbstrOut)
2080 BYTE buff[256];
2081 HRESULT hres;
2083 TRACE("(%p->(%s%s),%s,%d,%d,0x%08lx,%p)\n", pVarIn, debugstr_VT(pVarIn),
2084 debugstr_VF(pVarIn), debugstr_w(lpszFormat), nFirstDay, nFirstWeek,
2085 dwFlags, pbstrOut);
2087 if (!pbstrOut)
2088 return E_INVALIDARG;
2089 *pbstrOut = NULL;
2091 hres = VarTokenizeFormatString(lpszFormat, buff, sizeof(buff), nFirstDay,
2092 nFirstWeek, LOCALE_USER_DEFAULT, NULL);
2093 if (SUCCEEDED(hres))
2094 hres = VarFormatFromTokens(pVarIn, lpszFormat, buff, dwFlags,
2095 pbstrOut, LOCALE_USER_DEFAULT);
2096 TRACE("returning 0x%08lx, %s\n", hres, debugstr_w(*pbstrOut));
2097 return hres;
2100 /**********************************************************************
2101 * VarFormatDateTime [OLEAUT32.97]
2103 * Format a variant value as a date and/or time.
2105 * PARAMS
2106 * pVarIn [I] Variant to format
2107 * nFormat [I] Format type (see notes)
2108 * dwFlags [I] Flags for the format (VAR_ flags from "oleauto.h")
2109 * pbstrOut [O] Destination for formatted string.
2111 * RETURNS
2112 * Success: S_OK. pbstrOut contains the formatted value.
2113 * Failure: E_INVALIDARG, if any parameter is invalid.
2114 * E_OUTOFMEMORY, if enough memory cannot be allocated.
2115 * DISP_E_TYPEMISMATCH, if the variant cannot be formatted.
2117 * NOTES
2118 * This function uses LOCALE_USER_DEFAULT when determining the date format
2119 * characters to use.
2120 * Possible values for the nFormat parameter are:
2121 *| Value Meaning
2122 *| ----- -------
2123 *| 0 General date format
2124 *| 1 Long date format
2125 *| 2 Short date format
2126 *| 3 Long time format
2127 *| 4 Short time format
2129 HRESULT WINAPI VarFormatDateTime(LPVARIANT pVarIn, INT nFormat, ULONG dwFlags, BSTR *pbstrOut)
2131 static const WCHAR szEmpty[] = { '\0' };
2132 const BYTE* lpFmt = NULL;
2134 TRACE("(%p->(%s%s),%d,0x%08lx,%p)\n", pVarIn, debugstr_VT(pVarIn),
2135 debugstr_VF(pVarIn), nFormat, dwFlags, pbstrOut);
2137 if (!pVarIn || !pbstrOut || nFormat < 0 || nFormat > 4)
2138 return E_INVALIDARG;
2140 switch (nFormat)
2142 case 0: lpFmt = fmtGeneralDate; break;
2143 case 1: lpFmt = fmtLongDate; break;
2144 case 2: lpFmt = fmtShortDate; break;
2145 case 3: lpFmt = fmtLongTime; break;
2146 case 4: lpFmt = fmtShortTime; break;
2148 return VarFormatFromTokens(pVarIn, (LPWSTR)szEmpty, (BYTE*)lpFmt, dwFlags,
2149 pbstrOut, LOCALE_USER_DEFAULT);
2152 #define GETLOCALENUMBER(type,field) GetLocaleInfoW(LOCALE_USER_DEFAULT, \
2153 type|LOCALE_RETURN_NUMBER, \
2154 (LPWSTR)&numfmt.field, \
2155 sizeof(numfmt.field)/sizeof(WCHAR))
2157 /**********************************************************************
2158 * VarFormatNumber [OLEAUT32.107]
2160 * Format a variant value as a number.
2162 * PARAMS
2163 * pVarIn [I] Variant to format
2164 * nDigits [I] Number of digits following the decimal point (-1 = user default)
2165 * nLeading [I] Use a leading zero (-2 = user default, -1 = yes, 0 = no)
2166 * nParens [I] Use brackets for values < 0 (-2 = user default, -1 = yes, 0 = no)
2167 * nGrouping [I] Use grouping characters (-2 = user default, -1 = yes, 0 = no)
2168 * dwFlags [I] Currently unused, set to zero
2169 * pbstrOut [O] Destination for formatted string.
2171 * RETURNS
2172 * Success: S_OK. pbstrOut contains the formatted value.
2173 * Failure: E_INVALIDARG, if any parameter is invalid.
2174 * E_OUTOFMEMORY, if enough memory cannot be allocated.
2175 * DISP_E_TYPEMISMATCH, if the variant cannot be formatted.
2177 * NOTES
2178 * This function uses LOCALE_USER_DEFAULT when determining the number format
2179 * characters to use.
2181 HRESULT WINAPI VarFormatNumber(LPVARIANT pVarIn, INT nDigits, INT nLeading, INT nParens,
2182 INT nGrouping, ULONG dwFlags, BSTR *pbstrOut)
2184 HRESULT hRet;
2185 VARIANT vStr;
2187 TRACE("(%p->(%s%s),%d,%d,%d,%d,0x%08lx,%p)\n", pVarIn, debugstr_VT(pVarIn),
2188 debugstr_VF(pVarIn), nDigits, nLeading, nParens, nGrouping, dwFlags, pbstrOut);
2190 if (!pVarIn || !pbstrOut || nDigits > 9)
2191 return E_INVALIDARG;
2193 *pbstrOut = NULL;
2195 V_VT(&vStr) = VT_EMPTY;
2196 hRet = VariantCopyInd(&vStr, pVarIn);
2198 if (SUCCEEDED(hRet))
2199 hRet = VariantChangeTypeEx(&vStr, &vStr, LOCALE_USER_DEFAULT, 0, VT_BSTR);
2201 if (SUCCEEDED(hRet))
2203 WCHAR buff[256], decimal[8], thousands[8];
2204 NUMBERFMTW numfmt;
2206 /* Although MSDN makes it clear that the native versions of these functions
2207 * are implemented using VarTokenizeFormatString()/VarFormatFromTokens(),
2208 * using NLS gives us the same result.
2210 if (nDigits < 0)
2211 GETLOCALENUMBER(LOCALE_IDIGITS, NumDigits);
2212 else
2213 numfmt.NumDigits = nDigits;
2215 if (nLeading == -2)
2216 GETLOCALENUMBER(LOCALE_ILZERO, LeadingZero);
2217 else if (nLeading == -1)
2218 numfmt.LeadingZero = 1;
2219 else
2220 numfmt.LeadingZero = 0;
2222 if (nGrouping == -2)
2224 WCHAR grouping[16];
2225 grouping[2] = '\0';
2226 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, grouping,
2227 sizeof(grouping)/sizeof(WCHAR));
2228 numfmt.Grouping = grouping[2] == '2' ? 32 : grouping[0] - '0';
2230 else if (nGrouping == -1)
2231 numfmt.Grouping = 3; /* 3 = "n,nnn.nn" */
2232 else
2233 numfmt.Grouping = 0; /* 0 = No grouping */
2235 if (nParens == -2)
2236 GETLOCALENUMBER(LOCALE_INEGNUMBER, NegativeOrder);
2237 else if (nParens == -1)
2238 numfmt.NegativeOrder = 0; /* 0 = "(xxx)" */
2239 else
2240 numfmt.NegativeOrder = 1; /* 1 = "-xxx" */
2242 numfmt.lpDecimalSep = decimal;
2243 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, decimal,
2244 sizeof(decimal)/sizeof(WCHAR));
2245 numfmt.lpThousandSep = thousands;
2246 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, thousands,
2247 sizeof(thousands)/sizeof(WCHAR));
2249 if (GetNumberFormatW(LOCALE_USER_DEFAULT, 0, V_BSTR(&vStr), &numfmt,
2250 buff, sizeof(buff)/sizeof(WCHAR)))
2252 *pbstrOut = SysAllocString(buff);
2253 if (!*pbstrOut)
2254 hRet = E_OUTOFMEMORY;
2256 else
2257 hRet = DISP_E_TYPEMISMATCH;
2259 SysFreeString(V_BSTR(&vStr));
2261 return hRet;
2264 /**********************************************************************
2265 * VarFormatPercent [OLEAUT32.117]
2267 * Format a variant value as a percentage.
2269 * PARAMS
2270 * pVarIn [I] Variant to format
2271 * nDigits [I] Number of digits following the decimal point (-1 = user default)
2272 * nLeading [I] Use a leading zero (-2 = user default, -1 = yes, 0 = no)
2273 * nParens [I] Use brackets for values < 0 (-2 = user default, -1 = yes, 0 = no)
2274 * nGrouping [I] Use grouping characters (-2 = user default, -1 = yes, 0 = no)
2275 * dwFlags [I] Currently unused, set to zero
2276 * pbstrOut [O] Destination for formatted string.
2278 * RETURNS
2279 * Success: S_OK. pbstrOut contains the formatted value.
2280 * Failure: E_INVALIDARG, if any parameter is invalid.
2281 * E_OUTOFMEMORY, if enough memory cannot be allocated.
2282 * DISP_E_OVERFLOW, if overflow occurs during the conversion.
2283 * DISP_E_TYPEMISMATCH, if the variant cannot be formatted.
2285 * NOTES
2286 * This function uses LOCALE_USER_DEFAULT when determining the number format
2287 * characters to use.
2289 HRESULT WINAPI VarFormatPercent(LPVARIANT pVarIn, INT nDigits, INT nLeading, INT nParens,
2290 INT nGrouping, ULONG dwFlags, BSTR *pbstrOut)
2292 static const WCHAR szPercent[] = { '%','\0' };
2293 static const WCHAR szPercentBracket[] = { '%',')','\0' };
2294 WCHAR buff[256];
2295 HRESULT hRet;
2296 VARIANT vDbl;
2298 TRACE("(%p->(%s%s),%d,%d,%d,%d,0x%08lx,%p)\n", pVarIn, debugstr_VT(pVarIn),
2299 debugstr_VF(pVarIn), nDigits, nLeading, nParens, nGrouping,
2300 dwFlags, pbstrOut);
2302 if (!pVarIn || !pbstrOut || nDigits > 9)
2303 return E_INVALIDARG;
2305 *pbstrOut = NULL;
2307 V_VT(&vDbl) = VT_EMPTY;
2308 hRet = VariantCopyInd(&vDbl, pVarIn);
2310 if (SUCCEEDED(hRet))
2312 hRet = VariantChangeTypeEx(&vDbl, &vDbl, LOCALE_USER_DEFAULT, 0, VT_R8);
2314 if (SUCCEEDED(hRet))
2316 if (V_R8(&vDbl) > (R8_MAX / 100.0))
2317 return DISP_E_OVERFLOW;
2319 V_R8(&vDbl) *= 100.0;
2320 hRet = VarFormatNumber(&vDbl, nDigits, nLeading, nParens,
2321 nGrouping, dwFlags, pbstrOut);
2323 if (SUCCEEDED(hRet))
2325 DWORD dwLen = strlenW(*pbstrOut);
2326 BOOL bBracket = (*pbstrOut)[dwLen] == ')' ? TRUE : FALSE;
2328 dwLen -= bBracket;
2329 memcpy(buff, *pbstrOut, dwLen * sizeof(WCHAR));
2330 strcpyW(buff + dwLen, bBracket ? szPercentBracket : szPercent);
2331 SysFreeString(*pbstrOut);
2332 *pbstrOut = SysAllocString(buff);
2333 if (!*pbstrOut)
2334 hRet = E_OUTOFMEMORY;
2338 return hRet;
2341 /**********************************************************************
2342 * VarFormatCurrency [OLEAUT32.127]
2344 * Format a variant value as a currency.
2346 * PARAMS
2347 * pVarIn [I] Variant to format
2348 * nDigits [I] Number of digits following the decimal point (-1 = user default)
2349 * nLeading [I] Use a leading zero (-2 = user default, -1 = yes, 0 = no)
2350 * nParens [I] Use brackets for values < 0 (-2 = user default, -1 = yes, 0 = no)
2351 * nGrouping [I] Use grouping characters (-2 = user default, -1 = yes, 0 = no)
2352 * dwFlags [I] Currently unused, set to zero
2353 * pbstrOut [O] Destination for formatted string.
2355 * RETURNS
2356 * Success: S_OK. pbstrOut contains the formatted value.
2357 * Failure: E_INVALIDARG, if any parameter is invalid.
2358 * E_OUTOFMEMORY, if enough memory cannot be allocated.
2359 * DISP_E_TYPEMISMATCH, if the variant cannot be formatted.
2361 * NOTES
2362 * This function uses LOCALE_USER_DEFAULT when determining the currency format
2363 * characters to use.
2365 HRESULT WINAPI VarFormatCurrency(LPVARIANT pVarIn, INT nDigits, INT nLeading,
2366 INT nParens, INT nGrouping, ULONG dwFlags,
2367 BSTR *pbstrOut)
2369 HRESULT hRet;
2370 VARIANT vStr;
2372 TRACE("(%p->(%s%s),%d,%d,%d,%d,0x%08lx,%p)\n", pVarIn, debugstr_VT(pVarIn),
2373 debugstr_VF(pVarIn), nDigits, nLeading, nParens, nGrouping, dwFlags, pbstrOut);
2375 if (!pVarIn || !pbstrOut || nDigits > 9)
2376 return E_INVALIDARG;
2378 *pbstrOut = NULL;
2380 V_VT(&vStr) = VT_EMPTY;
2381 hRet = VariantCopyInd(&vStr, pVarIn);
2383 if (SUCCEEDED(hRet))
2384 hRet = VariantChangeTypeEx(&vStr, &vStr, LOCALE_USER_DEFAULT, 0, VT_BSTR);
2386 if (SUCCEEDED(hRet))
2388 WCHAR buff[256], decimal[8], thousands[8], currency[8];
2389 CURRENCYFMTW numfmt;
2391 if (nDigits < 0)
2392 GETLOCALENUMBER(LOCALE_IDIGITS, NumDigits);
2393 else
2394 numfmt.NumDigits = nDigits;
2396 if (nLeading == -2)
2397 GETLOCALENUMBER(LOCALE_ILZERO, LeadingZero);
2398 else if (nLeading == -1)
2399 numfmt.LeadingZero = 1;
2400 else
2401 numfmt.LeadingZero = 0;
2403 if (nGrouping == -2)
2405 WCHAR nGrouping[16];
2406 nGrouping[2] = '\0';
2407 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, nGrouping,
2408 sizeof(nGrouping)/sizeof(WCHAR));
2409 numfmt.Grouping = nGrouping[2] == '2' ? 32 : nGrouping[0] - '0';
2411 else if (nGrouping == -1)
2412 numfmt.Grouping = 3; /* 3 = "n,nnn.nn" */
2413 else
2414 numfmt.Grouping = 0; /* 0 = No grouping */
2416 if (nParens == -2)
2417 GETLOCALENUMBER(LOCALE_INEGCURR, NegativeOrder);
2418 else if (nParens == -1)
2419 numfmt.NegativeOrder = 0; /* 0 = "(xxx)" */
2420 else
2421 numfmt.NegativeOrder = 1; /* 1 = "-xxx" */
2423 GETLOCALENUMBER(LOCALE_ICURRENCY, PositiveOrder);
2425 numfmt.lpDecimalSep = decimal;
2426 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, decimal,
2427 sizeof(decimal)/sizeof(WCHAR));
2428 numfmt.lpThousandSep = thousands;
2429 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, thousands,
2430 sizeof(thousands)/sizeof(WCHAR));
2431 numfmt.lpCurrencySymbol = currency;
2432 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, currency,
2433 sizeof(currency)/sizeof(WCHAR));
2435 /* use NLS as per VarFormatNumber() */
2436 if (GetCurrencyFormatW(LOCALE_USER_DEFAULT, 0, V_BSTR(&vStr), &numfmt,
2437 buff, sizeof(buff)/sizeof(WCHAR)))
2439 *pbstrOut = SysAllocString(buff);
2440 if (!*pbstrOut)
2441 hRet = E_OUTOFMEMORY;
2443 else
2444 hRet = DISP_E_TYPEMISMATCH;
2446 SysFreeString(V_BSTR(&vStr));
2448 return hRet;
2451 /**********************************************************************
2452 * VarMonthName [OLEAUT32.129]
2454 * Print the specified month as localized name.
2456 * PARAMS
2457 * iMonth [I] month number 1..12
2458 * fAbbrev [I] 0 - full name, !0 - abbreviated name
2459 * dwFlags [I] flag stuff. only VAR_CALENDAR_HIJRI possible.
2460 * pbstrOut [O] Destination for month name
2462 * RETURNS
2463 * Success: S_OK. pbstrOut contains the name.
2464 * Failure: E_INVALIDARG, if any parameter is invalid.
2465 * E_OUTOFMEMORY, if enough memory cannot be allocated.
2467 HRESULT WINAPI VarMonthName(INT iMonth, INT fAbbrev, ULONG dwFlags, BSTR *pbstrOut)
2469 DWORD localeValue;
2470 INT size;
2471 WCHAR *str;
2473 if ((iMonth < 1) || (iMonth > 12))
2474 return E_INVALIDARG;
2476 if (dwFlags)
2477 FIXME("Does not support dwFlags 0x%lx, ignoring.\n", dwFlags);
2479 if (fAbbrev)
2480 localeValue = LOCALE_SABBREVMONTHNAME1 + iMonth - 1;
2481 else
2482 localeValue = LOCALE_SMONTHNAME1 + iMonth - 1;
2484 size = GetLocaleInfoW(LOCALE_USER_DEFAULT,localeValue, NULL, 0);
2485 if (!size) {
2486 FIXME("GetLocaleInfo 0x%lx failed.\n", localeValue);
2487 return E_INVALIDARG;
2489 str = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*size);
2490 if (!str)
2491 return E_OUTOFMEMORY;
2492 size = GetLocaleInfoW(LOCALE_USER_DEFAULT,localeValue, str, size);
2493 if (!size) {
2494 FIXME("GetLocaleInfo of 0x%lx failed in 2nd stage?!\n", localeValue);
2495 HeapFree(GetProcessHeap(),0,str);
2496 return E_INVALIDARG;
2498 *pbstrOut = SysAllocString(str);
2499 HeapFree(GetProcessHeap(),0,str);
2500 if (!*pbstrOut)
2501 return E_OUTOFMEMORY;
2502 return S_OK;