variant.c 103 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * VARIANT
 *
 * Copyright 1998 Jean-Claude Cote
 *
 * NOTES
 *   This implements the low-level and hi-level APIs for manipulating VARIANTs.
 *   The low-level APIs are used to do data coercion between different data types.
 *   The hi-level APIs are built on top of these low-level APIs and handle
 *   initialization, copying, destroying and changing the type of VARIANTs.
 *
 * TODO:
13
 *   - The Variant APIs do not support international languages, currency
14 15 16 17
 *     types, number formating and calendar.  They only support U.S. English format.
 *   - The Variant APIs do not the following types: IUknown, IDispatch, DECIMAL and SafeArray.
 *     The prototypes for these are commented out in the oleauto.h file.  They need
 *     to be implemented and cases need to be added to the switches of the  existing APIs.
18 19 20
 *   - The parsing of date for the VarDateFromStr is not complete.
 *   - The date manipulations do not support date prior to 1900.
 *   - The parsing does not accept has many formats has the Windows implementation.
21
 */
22 23

#include "config.h"
24 25 26
 
#include <string.h>
#include <stdlib.h>
27
#include <stdio.h>
28 29 30
#include <math.h>
#include <time.h>

31 32 33 34
#ifdef HAVE_FLOAT_H
# include <float.h>
#endif

35 36 37 38 39 40 41 42 43
#include "windef.h"
#include "oleauto.h"
#include "heap.h"
#include "debugtools.h"
#include "winerror.h"
#include "parsedt.h"

DEFAULT_DEBUG_CHANNEL(ole);

44 45 46 47 48 49 50
#ifndef FLT_MAX
# ifdef MAXFLOAT
#  define FLT_MAX MAXFLOAT
# else
#  error "Can't find #define for MAXFLOAT/FLT_MAX"
# endif
#endif
51

52 53
#undef CHAR_MAX
#undef CHAR_MIN
54 55 56 57 58 59 60 61
static const char CHAR_MAX = 127;
static const char CHAR_MIN = -128;
static const BYTE UI1_MAX = 255;
static const BYTE UI1_MIN = 0;
static const unsigned short UI2_MAX = 65535;
static const unsigned short UI2_MIN = 0;
static const short I2_MAX = 32767;
static const short I2_MIN =  -32768;
62
static const unsigned long UI4_MAX = 4294967295U;
63 64
static const unsigned long UI4_MIN = 0;
static const long I4_MAX = 2147483647;
65
static const long I4_MIN = -(2147483648U);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static const DATE DATE_MIN = -657434;
static const DATE DATE_MAX = 2958465;


/* This mask is used to set a flag in wReserved1 of
 * the VARIANTARG structure. The flag indicates if
 * the API function is using an inner variant or not.
 */
#define PROCESSING_INNER_VARIANT 0x0001

/* General use buffer.
 */
#define BUFFER_MAX 1024
static char pBuffer[BUFFER_MAX];

81 82 83 84 85 86 87 88 89 90
/*
 * Note a leap year is one that is a multiple of 4
 * but not of a 100.  Except if it is a multiple of
 * 400 then it is a leap year.
 */
/* According to postgeSQL date parsing functions there is
 * a leap year when this expression is true.
 * (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
 * So according to this there is 365.2515 days in one year.
 * One + every four years: 1/4 -> 365.25
91
 * One - every 100 years: 1/100 -> 365.01
92 93
 * One + every 400 years: 1/400 -> 365.0025
 */
94 95 96 97 98 99
/* static const double DAYS_IN_ONE_YEAR = 365.2515;
 *
 *  ^^  Might this be the key to an easy way to factor large prime numbers?
 *  Let's try using arithmetic.  <lawson_whitney@juno.com> 7 Mar 2000
 */
static const double DAYS_IN_ONE_YEAR = 365.2425;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

/******************************************************************************
 *	   DateTimeStringToTm	[INTERNAL]
 *
 * Converts a string representation of a date and/or time to a tm structure.
 *
 * Note this function uses the postgresql date parsing functions found
 * in the parsedt.c file.
 *
 * Returns TRUE if successfull.
 *
 * Note: This function does not parse the day of the week,
 * daylight savings time. It will only fill the followin fields in
 * the tm struct, tm_sec, tm_min, tm_hour, tm_year, tm_day, tm_mon.
 *
 ******************************************************************************/
116
static BOOL DateTimeStringToTm( OLECHAR* strIn, LCID lcid, struct tm* pTm )
117
{
118
	BOOL res = FALSE;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	double		fsec;
	int 		tzp;
	int 		dtype;
	int 		nf;
	char	   *field[MAXDATEFIELDS];
	int 		ftype[MAXDATEFIELDS];
	char		lowstr[MAXDATELEN + 1];
	char* strDateTime = NULL;

	/* Convert the string to ASCII since this is the only format
	 * postgesql can handle.
	 */
	strDateTime = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );

	if( strDateTime != NULL )
	{
		/* Make sure we don't go over the maximum length
		 * accepted by postgesql.
		 */
		if( strlen( strDateTime ) <= MAXDATELEN )
		{
			if( ParseDateTime( strDateTime, lowstr, field, ftype, MAXDATEFIELDS, &nf) == 0 )
			{
				if( lcid & VAR_DATEVALUEONLY )
				{
					/* Get the date information.
					 * It returns 0 if date information was
					 * present and 1 if only time information was present.
					 * -1 if an error occures.
					 */
					if( DecodeDateTime(field, ftype, nf, &dtype, pTm, &fsec, &tzp) == 0 )
					{
						/* Eliminate the time information since we
						 * were asked to get date information only.
						 */
						pTm->tm_sec = 0;
						pTm->tm_min = 0;
						pTm->tm_hour = 0;
						res = TRUE;
					}
				}
				if( lcid & VAR_TIMEVALUEONLY )
				{
					/* Get time information only.
					 */
					if( DecodeTimeOnly(field, ftype, nf, &dtype, pTm, &fsec) == 0 )
					{
						res = TRUE;
					}
				}
				else
				{
					/* Get both date and time information.
					 * It returns 0 if date information was
					 * present and 1 if only time information was present.
					 * -1 if an error occures.
					 */
					if( DecodeDateTime(field, ftype, nf, &dtype, pTm, &fsec, &tzp) != -1 )
					{
						res = TRUE;
					}
				}
			}
		}
		HeapFree( GetProcessHeap(), 0, strDateTime );
	}

	return res;
}






/******************************************************************************
 *	   TmToDATE 	[INTERNAL]
 *
 * The date is implemented using an 8 byte floating-point number.
 * Days are represented by whole numbers increments starting with 0.00 has
 * being December 30 1899, midnight.
 * The hours are expressed as the fractional part of the number.
 * December 30 1899 at midnight = 0.00
 * January 1 1900 at midnight = 2.00
 * January 4 1900 at 6 AM = 5.25
 * January 4 1900 at noon = 5.50
 * December 29 1899 at midnight = -1.00
 * December 18 1899 at midnight = -12.00
 * December 18 1899 at 6AM = -12.25
 * December 18 1899 at 6PM = -12.75
 * December 19 1899 at midnight = -11.00
 * The tm structure is as follows:
 * struct tm {
 *		  int tm_sec;	   seconds after the minute - [0,59]
 *		  int tm_min;	   minutes after the hour - [0,59]
 *		  int tm_hour;	   hours since midnight - [0,23]
 *		  int tm_mday;	   day of the month - [1,31]
 *		  int tm_mon;	   months since January - [0,11]
 *		  int tm_year;	   years
 *		  int tm_wday;	   days since Sunday - [0,6]
 *		  int tm_yday;	   days since January 1 - [0,365]
 *		  int tm_isdst;    daylight savings time flag
 *		  };
 *
 * Note: This function does not use the tm_wday, tm_yday, tm_wday,
 * and tm_isdst fields of the tm structure. And only converts years
 * after 1900.
 *
 * Returns TRUE if successfull.
 */
229
static BOOL TmToDATE( struct tm* pTm, DATE *pDateOut )
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
{
	if( (pTm->tm_year - 1900) >= 0 )
	{
		int leapYear = 0;
		
		/* Start at 1. This is the way DATE is defined.
		 * January 1, 1900 at Midnight is 1.00.
		 * January 1, 1900 at 6AM is 1.25.
		 * and so on.
		 */
		*pDateOut = 1;

		/* Add the number of days corresponding to
		 * tm_year.
		 */
		*pDateOut += (pTm->tm_year - 1900) * 365;

		/* Add the leap days in the previous years between now and 1900.
		 * Note a leap year is one that is a multiple of 4
		 * but not of a 100.  Except if it is a multiple of
		 * 400 then it is a leap year.
		 */
		*pDateOut += ( (pTm->tm_year - 1) / 4 ) - ( 1900 / 4 );
		*pDateOut -= ( (pTm->tm_year - 1) / 100 ) - ( 1900 / 100 );
		*pDateOut += ( (pTm->tm_year - 1) / 400 ) - ( 1900 / 400 );

		/* Set the leap year flag if the
		 * current year specified by tm_year is a
		 * leap year. This will be used to add a day
		 * to the day count.
		 */
		if( isleap( pTm->tm_year ) )
			leapYear = 1;
		
		/* Add the number of days corresponding to
		 * the month.
		 */
		switch( pTm->tm_mon )
		{
		case 2:
			*pDateOut += 31;
			break;
		case 3:
			*pDateOut += ( 59 + leapYear );
			break;
		case 4:
			*pDateOut += ( 90 + leapYear );
			break;
		case 5:
			*pDateOut += ( 120 + leapYear );
			break;
		case 6:
			*pDateOut += ( 151 + leapYear );
			break;
		case 7:
			*pDateOut += ( 181 + leapYear );
			break;
		case 8:
			*pDateOut += ( 212 + leapYear );
			break;
		case 9:
			*pDateOut += ( 243 + leapYear );
			break;
		case 10:
			*pDateOut += ( 273 + leapYear );
			break;
		case 11:
			*pDateOut += ( 304 + leapYear );
			break;
		case 12:
			*pDateOut += ( 334 + leapYear );
			break;
		}
		/* Add the number of days in this month.
		 */
		*pDateOut += pTm->tm_mday;
	
		/* Add the number of seconds, minutes, and hours
		 * to the DATE. Note these are the fracionnal part
		 * of the DATE so seconds / number of seconds in a day.
		 */
		*pDateOut += pTm->tm_hour / 24.0;
		*pDateOut += pTm->tm_min / 1440.0;
		*pDateOut += pTm->tm_sec / 86400.0;
		return TRUE;
	}
	return FALSE;
}

/******************************************************************************
 *	   DateToTm 	[INTERNAL]
 *
 * This function converst a windows DATE to a tm structure.
 *
 * It does not fill all the fields of the tm structure.
 * Here is a list of the fields that are filled:
 * tm_sec, tm_min, tm_hour, tm_year, tm_day, tm_mon.
 *
 * Note this function does not support dates before the January 1, 1900
 * or ( dateIn < 2.0 ).
 *
 * Returns TRUE if successfull.
 */
333
static BOOL DateToTm( DATE dateIn, LCID lcid, struct tm* pTm )
334 335 336 337 338 339 340 341 342
{
	/* Do not process dates smaller than January 1, 1900.
	 * Which corresponds to 2.0 in the windows DATE format.
	 */
	if( dateIn >= 2.0 )
	{
		double decimalPart = 0.0;
		double wholePart = 0.0;

343
		memset(pTm,0,sizeof(*pTm));
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	
		/* Because of the nature of DATE format witch
		 * associates 2.0 to January 1, 1900. We will
		 * remove 1.0 from the whole part of the DATE
		 * so that in the following code 1.0
		 * will correspond to January 1, 1900.
		 * This simplyfies the processing of the DATE value.
		 */
		dateIn -= 1.0;

		wholePart = (double) floor( dateIn );
		decimalPart = fmod( dateIn, wholePart );

		if( !(lcid & VAR_TIMEVALUEONLY) )
		{
			int nDay = 0;
			int leapYear = 0;
			double yearsSince1900 = 0;
			/* Start at 1900, this where the DATE time 0.0 starts.
			 */
			pTm->tm_year = 1900;
			/* find in what year the day in the "wholePart" falls into.
			 * add the value to the year field.
			 */
368
			yearsSince1900 = floor( (wholePart / DAYS_IN_ONE_YEAR) + 0.001 );
369 370 371 372
			pTm->tm_year += yearsSince1900;
			/* determine if this is a leap year.
			 */
			if( isleap( pTm->tm_year ) )
373
			{
374
				leapYear = 1;
375 376 377
				wholePart++;
			}

378 379 380 381 382 383 384
			/* find what day of that year does the "wholePart" corresponds to.
			 * Note: nDay is in [1-366] format
			 */
			nDay = (int) ( wholePart - floor( yearsSince1900 * DAYS_IN_ONE_YEAR ) );
			/* Set the tm_yday value.
			 * Note: The day is must be converted from [1-366] to [0-365]
			 */
385
			/*pTm->tm_yday = nDay - 1;*/
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			/* find which mount this day corresponds to.
			 */
			if( nDay <= 31 )
			{
				pTm->tm_mday = nDay;
				pTm->tm_mon = 0;
			}
			else if( nDay <= ( 59 + leapYear ) )
			{
				pTm->tm_mday = nDay - 31;
				pTm->tm_mon = 1;
			}
			else if( nDay <= ( 90 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 59 + leapYear );
				pTm->tm_mon = 2;
			}
			else if( nDay <= ( 120 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 90 + leapYear );
				pTm->tm_mon = 3;
			}
			else if( nDay <= ( 151 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 120 + leapYear );
				pTm->tm_mon = 4;
			}
			else if( nDay <= ( 181 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 151 + leapYear );
				pTm->tm_mon = 5;
			}
			else if( nDay <= ( 212 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 181 + leapYear );
				pTm->tm_mon = 6;
			}
			else if( nDay <= ( 243 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 212 + leapYear );
				pTm->tm_mon = 7;
			}
			else if( nDay <= ( 273 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 243 + leapYear );
				pTm->tm_mon = 8;
			}
			else if( nDay <= ( 304 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 273 + leapYear );
				pTm->tm_mon = 9;
			}
			else if( nDay <= ( 334 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 304 + leapYear );
				pTm->tm_mon = 10;
			}
			else if( nDay <= ( 365 + leapYear ) )
			{
				pTm->tm_mday = nDay - ( 334 + leapYear );
				pTm->tm_mon = 11;
			}
		}
		if( !(lcid & VAR_DATEVALUEONLY) )
		{
			/* find the number of seconds in this day.
			 * fractional part times, hours, minutes, seconds.
			 */
			pTm->tm_hour = (int) ( decimalPart * 24 );
			pTm->tm_min = (int) ( ( ( decimalPart * 24 ) - pTm->tm_hour ) * 60 );
			pTm->tm_sec = (int) ( ( ( decimalPart * 24 * 60 ) - ( pTm->tm_hour * 60 ) - pTm->tm_min ) * 60 );
		}
		return TRUE;
	}
	return FALSE;
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516


/******************************************************************************
 *	   SizeOfVariantData   	[INTERNAL]
 *
 * This function finds the size of the data referenced by a Variant based
 * the type "vt" of the Variant.
 */
static int SizeOfVariantData( VARIANT* parg )
{
    int size = 0;
    switch( parg->vt & VT_TYPEMASK )
    {
    case( VT_I2 ):
        size = sizeof(short);
        break;
    case( VT_INT ):
        size = sizeof(int);
        break;
    case( VT_I4 ):
        size = sizeof(long);
        break;
    case( VT_UI1 ):
        size = sizeof(BYTE);
        break;
    case( VT_UI2 ):
        size = sizeof(unsigned short);
        break;
    case( VT_UINT ):
        size = sizeof(unsigned int);
        break;
    case( VT_UI4 ):
        size = sizeof(unsigned long);
        break;
    case( VT_R4 ):
        size = sizeof(float);
        break;
    case( VT_R8 ):
        size = sizeof(double);
        break;
    case( VT_DATE ):
        size = sizeof(DATE);
        break;
    case( VT_BOOL ):
        size = sizeof(VARIANT_BOOL);
        break;
    case( VT_BSTR ):
        size = sizeof(void*);
        break;
    case( VT_CY ):
    case( VT_DISPATCH ):
    case( VT_UNKNOWN ):
    case( VT_DECIMAL ):
    default:
517
        FIXME("Add size information for type vt=%d\n", parg->vt & VT_TYPEMASK );
518 519 520 521 522 523 524 525 526
        break;
    }

    return size;
}
/******************************************************************************
 *	   StringDupAtoBstr		[INTERNAL]
 * 
 */
527
static BSTR StringDupAtoBstr( char* strIn )
528
{
529 530
	BSTR bstr = NULL;
	OLECHAR* pNewString = NULL;
531
	pNewString = HEAP_strdupAtoW( GetProcessHeap(), 0, strIn );
532
	bstr = SysAllocString( pNewString );
533 534 535 536 537 538 539 540 541 542 543
	HeapFree( GetProcessHeap(), 0, pNewString );
	return bstr;
}

/******************************************************************************
 *		round		[INTERNAL]
 *
 * Round the double value to the nearest integer value.
 */
static double round( double d )
{
544
   double decimals = 0.0, integerValue = 0.0, roundedValue = 0.0;
545
    BOOL bEvenNumber = FALSE;
546 547 548 549
    int nSign = 0;

    /* Save the sign of the number
     */
550
   nSign = (d >= 0.0) ? 1 : -1;
551 552 553 554
    d = fabs( d );
    
	/* Remove the decimals.
	 */
555
   integerValue = floor( d );
556 557 558 559 560 561 562

    /* Set the Even flag.  This is used to round the number when
     * the decimals are exactly 1/2.  If the integer part is
     * odd the number is rounded up. If the integer part
     * is even the number is rounded down.  Using this method
     * numbers are rounded up|down half the time.
     */
563
   bEvenNumber = (((short)fmod(integerValue, 2)) == 0) ? TRUE : FALSE;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

    /* Remove the integral part of the number.
     */
    decimals = d - integerValue;

	/* Note: Ceil returns the smallest integer that is greater that x.
	 * and floor returns the largest integer that is less than or equal to x.
	 */
    if( decimals > 0.5 )
    {
        /* If the decimal part is greater than 1/2
         */
        roundedValue = ceil( d );
    }
    else if( decimals < 0.5 )
    {
        /* If the decimal part is smaller than 1/2
         */
        roundedValue = floor( d );
    }
    else
    {
        /* the decimals are exactly 1/2 so round according to
         * the bEvenNumber flag.
         */
        if( bEvenNumber )
        {
            roundedValue = floor( d );
        }
        else
        {
            roundedValue = ceil( d );
        }
    }

	return roundedValue * nSign;
}

/******************************************************************************
 *		RemoveCharacterFromString		[INTERNAL]
 *
 * Removes any of the characters in "strOfCharToRemove" from the "str" argument.
 */
static void RemoveCharacterFromString( LPSTR str, LPSTR strOfCharToRemove )
{
	LPSTR pNewString = NULL;
	LPSTR strToken = NULL;


	/* Check if we have a valid argument
	 */
	if( str != NULL )
	{
		pNewString = strdup( str );
		str[0] = '\0';
		strToken = strtok( pNewString, strOfCharToRemove );
		while( strToken != NULL ) { 
			strcat( str, strToken );
			strToken = strtok( NULL, strOfCharToRemove );
		}
		free( pNewString );
	}
	return;
}

/******************************************************************************
 *		GetValidRealString		[INTERNAL]
 *
 * Checks if the string is of proper format to be converted to a real value.
 */
634
static BOOL IsValidRealString( LPSTR strRealString )
635 636 637 638 639 640
{
	/* Real values that have a decimal point are required to either have
	 * digits before or after the decimal point.  We will assume that
	 * we do not have any digits at either position. If we do encounter
	 * some we will disable this flag.
	 */
641
	BOOL bDigitsRequired = TRUE;
642 643
	/* Processed fields in the string representation of the real number.
	 */
644 645 646 647 648 649 650 651
	BOOL bWhiteSpaceProcessed = FALSE;
	BOOL bFirstSignProcessed = FALSE;
	BOOL bFirstDigitsProcessed = FALSE;
	BOOL bDecimalPointProcessed = FALSE;
	BOOL bSecondDigitsProcessed = FALSE;
	BOOL bExponentProcessed = FALSE;
	BOOL bSecondSignProcessed = FALSE;
	BOOL bThirdDigitsProcessed = FALSE;
652 653
	/* Assume string parameter "strRealString" is valid and try to disprove it.
	 */
654
	BOOL bValidRealString = TRUE;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

	/* Used to count the number of tokens in the "strRealString".
	 */
	LPSTR strToken = NULL;
	int nTokens = 0;
	LPSTR pChar = NULL;
	
	/* Check if we have a valid argument
	 */
	if( strRealString == NULL )
	{
		bValidRealString = FALSE;
	}

	if( bValidRealString == TRUE )
	{
		/* Make sure we only have ONE token in the string.
		 */
		strToken = strtok( strRealString, " " );
		while( strToken != NULL ) { 
			nTokens++;		
			strToken = strtok( NULL, " " );	
		}

		if( nTokens != 1 )
		{
			bValidRealString = FALSE;
		}
	}


	/* Make sure this token contains only valid characters.
	 * The string argument to atof has the following form:
	 * [whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
	 * Whitespace consists of space and|or <TAB> characters, which are ignored.
     * Sign is either plus '+' or minus '-'.
     * Digits are one or more decimal digits.
     * Note: If no digits appear before the decimal point, at least one must
     * appear after the decimal point.
     * The decimal digits may be followed by an exponent.
     * An Exponent consists of an introductory letter ( D, d, E, or e) and
	 * an optionally signed decimal integer.
	 */
	pChar = strRealString;
	while( bValidRealString == TRUE && *pChar != '\0' )
	{
		switch( *pChar )
		{
		/* If whitespace...
		 */
		case ' ':
		case '\t':
			if( bWhiteSpaceProcessed ||
				bFirstSignProcessed ||
				bFirstDigitsProcessed ||
				bDecimalPointProcessed ||
				bSecondDigitsProcessed ||
				bExponentProcessed ||
				bSecondSignProcessed ||
				bThirdDigitsProcessed )
			{
				bValidRealString = FALSE;
			}
			break;
		/* If sign...
		 */
		case '+':
		case '-':
			if( bFirstSignProcessed == FALSE )
			{
				if( bFirstDigitsProcessed ||
					bDecimalPointProcessed ||
					bSecondDigitsProcessed ||
					bExponentProcessed ||
					bSecondSignProcessed ||
					bThirdDigitsProcessed )
				{
					bValidRealString = FALSE;
				}
				bWhiteSpaceProcessed = TRUE;
				bFirstSignProcessed = TRUE;
			}
			else if( bSecondSignProcessed == FALSE )
			{
                /* Note: The exponent must be present in
				 * order to accept the second sign...
				 */
				if( bExponentProcessed == FALSE ||
					bThirdDigitsProcessed ||
					bDigitsRequired )
				{
					bValidRealString = FALSE;
				}
				bFirstSignProcessed = TRUE;
				bWhiteSpaceProcessed = TRUE;
				bFirstDigitsProcessed = TRUE;
				bDecimalPointProcessed = TRUE;
				bSecondDigitsProcessed = TRUE;
				bSecondSignProcessed = TRUE;
			}
			break;

		/* If decimals...
		 */
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9': 
			if( bFirstDigitsProcessed == FALSE )
			{
				if( bDecimalPointProcessed ||
					bSecondDigitsProcessed ||
					bExponentProcessed ||
					bSecondSignProcessed ||
					bThirdDigitsProcessed )
				{
					bValidRealString = FALSE;
				}
				bFirstSignProcessed = TRUE;
				bWhiteSpaceProcessed = TRUE;
				/* We have found some digits before the decimal point
				 * so disable the "Digits required" flag.
				 */
				bDigitsRequired = FALSE;
			}
			else if( bSecondDigitsProcessed == FALSE )
			{
				if( bExponentProcessed ||
					bSecondSignProcessed ||
					bThirdDigitsProcessed )
				{
					bValidRealString = FALSE;
				}
				bFirstSignProcessed = TRUE;
				bWhiteSpaceProcessed = TRUE;
				bFirstDigitsProcessed = TRUE;
				bDecimalPointProcessed = TRUE;
				/* We have found some digits after the decimal point
				 * so disable the "Digits required" flag.
				 */
				bDigitsRequired = FALSE;
			}
			else if( bThirdDigitsProcessed == FALSE )
			{
				/* Getting here means everything else should be processed.
                 * If we get anything else than a decimal following this
                 * digit it will be flagged by the other cases, so
				 * we do not really need to do anything in here.
				 */
			}
			break;
		/* If DecimalPoint...
		 */
		case '.': 
			if( bDecimalPointProcessed ||
				bSecondDigitsProcessed ||
				bExponentProcessed ||
				bSecondSignProcessed ||
				bThirdDigitsProcessed )
			{
				bValidRealString = FALSE;
			}
			bFirstSignProcessed = TRUE;
			bWhiteSpaceProcessed = TRUE;
			bFirstDigitsProcessed = TRUE;
			bDecimalPointProcessed = TRUE;
			break;
		/* If Exponent...
		 */
		case 'e':
		case 'E':
		case 'd':
		case 'D':
			if( bExponentProcessed ||
				bSecondSignProcessed ||
				bThirdDigitsProcessed ||
				bDigitsRequired )
			{
				bValidRealString = FALSE;
			}
			bFirstSignProcessed = TRUE;
			bWhiteSpaceProcessed = TRUE;
			bFirstDigitsProcessed = TRUE;
			bDecimalPointProcessed = TRUE;
			bSecondDigitsProcessed = TRUE;
			bExponentProcessed = TRUE;
			break;
		default:
			bValidRealString = FALSE;
			break;
		}
		/* Process next character.
		 */
		pChar++;
	}

	/* If the required digits were not present we have an invalid
	 * string representation of a real number.
	 */
	if( bDigitsRequired == TRUE )
	{
		bValidRealString = FALSE;
	}

	return bValidRealString;
}


/******************************************************************************
 *		Coerce	[INTERNAL]
 *
 * This function dispatches execution to the proper conversion API
 * to do the necessary coercion.
 */
static HRESULT Coerce( VARIANTARG* pd, LCID lcid, ULONG dwFlags, VARIANTARG* ps, VARTYPE vt )
{
	HRESULT res = S_OK;
	unsigned short vtFrom = 0;
	vtFrom = ps->vt & VT_TYPEMASK;
	
881 882 883 884
	/* Note: Since "long" and "int" values both have 4 bytes and are
	 * both signed integers "int" will be treated as "long" in the
	 * following code.
	 * The same goes for their unsigned versions.
885 886
	 */

887 888 889 890 891 892 893 894
	/* Trivial Case: If the coercion is from two types that are 
	 * identical then we can blindly copy from one argument to another.*/
	if ((vt==vtFrom))
	{
	   return VariantCopy(pd,ps);
	}

	/* Cases requiring thought*/
895 896 897 898
	switch( vt )
	{

    case( VT_EMPTY ):
899
        res = VariantClear( pd );
900 901
        break;
    case( VT_NULL ):
902
        res = VariantClear( pd );
903 904 905 906 907 908 909 910 911
        if( res == S_OK )
        {
            pd->vt = VT_NULL;
        }
        break;
	case( VT_I1 ):
		switch( vtFrom )
        {
        case( VT_I1 ):
912
            res = VariantCopy( pd, ps );
913 914
            break;
		case( VT_I2 ):
915
			res = VarI1FromI2( ps->u.iVal, &(pd->u.cVal) );
916 917 918
			break;
		case( VT_INT ):
		case( VT_I4 ):
919
			res = VarI1FromI4( ps->u.lVal, &(pd->u.cVal) );
920 921
			break;
		case( VT_UI1 ):
922
			res = VarI1FromUI1( ps->u.bVal, &(pd->u.cVal) );
923 924
			break;
		case( VT_UI2 ):
925
			res = VarI1FromUI2( ps->u.uiVal, &(pd->u.cVal) );
926 927 928
			break;
		case( VT_UINT ):
		case( VT_UI4 ):
929
			res = VarI1FromUI4( ps->u.ulVal, &(pd->u.cVal) );
930 931
			break;
		case( VT_R4 ):
932
			res = VarI1FromR4( ps->u.fltVal, &(pd->u.cVal) );
933 934
			break;
		case( VT_R8 ):
935
			res = VarI1FromR8( ps->u.dblVal, &(pd->u.cVal) );
936 937
			break;
		case( VT_DATE ):
938
			res = VarI1FromDate( ps->u.date, &(pd->u.cVal) );
939 940
			break;
		case( VT_BOOL ):
941
			res = VarI1FromBool( ps->u.boolVal, &(pd->u.cVal) );
942 943
			break;
		case( VT_BSTR ):
944
			res = VarI1FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.cVal) );
945 946
			break;
		case( VT_CY ):
947
	     res = VarI1FromCy( ps->u.cyVal, &(pd->u.cVal) );
948 949 950 951 952 953 954 955
		case( VT_DISPATCH ):
			/*res = VarI1FromDisp32( ps->u.pdispVal, lcid, &(pd->u.cVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarI1From32( ps->u.lVal, &(pd->u.cVal) );*/
		case( VT_DECIMAL ):
			/*res = VarI1FromDec32( ps->u.decVal, &(pd->u.cVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
956
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
957 958 959 960 961 962 963 964
			break;
		}
		break;

	case( VT_I2 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
965
			res = VarI2FromI1( ps->u.cVal, &(pd->u.iVal) );
966 967
			break;
        case( VT_I2 ):
968
            res = VariantCopy( pd, ps );
969 970 971
            break;
		case( VT_INT ):
		case( VT_I4 ):
972
			res = VarI2FromI4( ps->u.lVal, &(pd->u.iVal) );
973 974
			break;
		case( VT_UI1 ):
975
			res = VarI2FromUI1( ps->u.bVal, &(pd->u.iVal) );
976 977
			break;
		case( VT_UI2 ):
978
			res = VarI2FromUI2( ps->u.uiVal, &(pd->u.iVal) );
979 980 981
			break;
		case( VT_UINT ):
		case( VT_UI4 ):
982
			res = VarI2FromUI4( ps->u.ulVal, &(pd->u.iVal) );
983 984
			break;
		case( VT_R4 ):
985
			res = VarI2FromR4( ps->u.fltVal, &(pd->u.iVal) );
986 987
			break;
		case( VT_R8 ):
988
			res = VarI2FromR8( ps->u.dblVal, &(pd->u.iVal) );
989 990
			break;
		case( VT_DATE ):
991
			res = VarI2FromDate( ps->u.date, &(pd->u.iVal) );
992 993
			break;
		case( VT_BOOL ):
994
			res = VarI2FromBool( ps->u.boolVal, &(pd->u.iVal) );
995 996
			break;
		case( VT_BSTR ):
997
			res = VarI2FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.iVal) );
998 999
			break;
		case( VT_CY ):
1000
	     res = VarI2FromCy( ps->u.cyVal, &(pd->u.iVal) );
1001 1002 1003 1004 1005 1006 1007 1008
		case( VT_DISPATCH ):
			/*res = VarI2FromDisp32( ps->u.pdispVal, lcid, &(pd->u.iVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarI2From32( ps->u.lVal, &(pd->u.iVal) );*/
		case( VT_DECIMAL ):
			/*res = VarI2FromDec32( ps->u.deiVal, &(pd->u.iVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1009
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1010 1011 1012 1013 1014 1015 1016 1017 1018
			break;
		}
		break;

	case( VT_INT ):
	case( VT_I4 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1019
			res = VarI4FromI1( ps->u.cVal, &(pd->u.lVal) );
1020 1021
			break;
		case( VT_I2 ):
1022
			res = VarI4FromI2( ps->u.iVal, &(pd->u.lVal) );
1023 1024 1025
            break;
        case( VT_INT ):
        case( VT_I4 ):
1026
            res = VariantCopy( pd, ps );
1027 1028
            break;
		case( VT_UI1 ):
1029
			res = VarI4FromUI1( ps->u.bVal, &(pd->u.lVal) );
1030 1031
			break;
		case( VT_UI2 ):
1032
			res = VarI4FromUI2( ps->u.uiVal, &(pd->u.lVal) );
1033 1034 1035
			break;
		case( VT_UINT ):
		case( VT_UI4 ):
1036
			res = VarI4FromUI4( ps->u.ulVal, &(pd->u.lVal) );
1037 1038
			break;
		case( VT_R4 ):
1039
			res = VarI4FromR4( ps->u.fltVal, &(pd->u.lVal) );
1040 1041
			break;
		case( VT_R8 ):
1042
			res = VarI4FromR8( ps->u.dblVal, &(pd->u.lVal) );
1043 1044
			break;
		case( VT_DATE ):
1045
			res = VarI4FromDate( ps->u.date, &(pd->u.lVal) );
1046 1047
			break;
		case( VT_BOOL ):
1048
			res = VarI4FromBool( ps->u.boolVal, &(pd->u.lVal) );
1049 1050
			break;
		case( VT_BSTR ):
1051
			res = VarI4FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.lVal) );
1052 1053
			break;
		case( VT_CY ):
1054
	     res = VarI4FromCy( ps->u.cyVal, &(pd->u.lVal) );
1055 1056 1057 1058 1059 1060 1061 1062
		case( VT_DISPATCH ):
			/*res = VarI4FromDisp32( ps->u.pdispVal, lcid, &(pd->u.lVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarI4From32( ps->u.lVal, &(pd->u.lVal) );*/
		case( VT_DECIMAL ):
			/*res = VarI4FromDec32( ps->u.deiVal, &(pd->u.lVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1063
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1064 1065 1066 1067 1068 1069 1070 1071
			break;
		}
		break;

	case( VT_UI1 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1072
			res = VarUI1FromI1( ps->u.cVal, &(pd->u.bVal) );
1073 1074
			break;
		case( VT_I2 ):
1075
			res = VarUI1FromI2( ps->u.iVal, &(pd->u.bVal) );
1076 1077 1078
			break;
		case( VT_INT ):
		case( VT_I4 ):
1079
			res = VarUI1FromI4( ps->u.lVal, &(pd->u.bVal) );
1080 1081
			break;
        case( VT_UI1 ):
1082
            res = VariantCopy( pd, ps );
1083 1084
            break;
		case( VT_UI2 ):
1085
			res = VarUI1FromUI2( ps->u.uiVal, &(pd->u.bVal) );
1086 1087 1088
			break;
		case( VT_UINT ):
		case( VT_UI4 ):
1089
			res = VarUI1FromUI4( ps->u.ulVal, &(pd->u.bVal) );
1090 1091
			break;
		case( VT_R4 ):
1092
			res = VarUI1FromR4( ps->u.fltVal, &(pd->u.bVal) );
1093 1094
			break;
		case( VT_R8 ):
1095
			res = VarUI1FromR8( ps->u.dblVal, &(pd->u.bVal) );
1096 1097
			break;
		case( VT_DATE ):
1098
			res = VarUI1FromDate( ps->u.date, &(pd->u.bVal) );
1099 1100
			break;
		case( VT_BOOL ):
1101
			res = VarUI1FromBool( ps->u.boolVal, &(pd->u.bVal) );
1102 1103
			break;
		case( VT_BSTR ):
1104
			res = VarUI1FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.bVal) );
1105 1106
			break;
		case( VT_CY ):
1107
	     res = VarUI1FromCy( ps->u.cyVal, &(pd->u.bVal) );
1108 1109 1110 1111 1112 1113 1114 1115
		case( VT_DISPATCH ):
			/*res = VarUI1FromDisp32( ps->u.pdispVal, lcid, &(pd->u.bVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarUI1From32( ps->u.lVal, &(pd->u.bVal) );*/
		case( VT_DECIMAL ):
			/*res = VarUI1FromDec32( ps->u.deiVal, &(pd->u.bVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1116
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1117 1118 1119 1120 1121 1122 1123 1124
			break;
		}
		break;

	case( VT_UI2 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1125
			res = VarUI2FromI1( ps->u.cVal, &(pd->u.uiVal) );
1126 1127
			break;
		case( VT_I2 ):
1128
			res = VarUI2FromI2( ps->u.iVal, &(pd->u.uiVal) );
1129 1130 1131
			break;
		case( VT_INT ):
		case( VT_I4 ):
1132
			res = VarUI2FromI4( ps->u.lVal, &(pd->u.uiVal) );
1133 1134
			break;
		case( VT_UI1 ):
1135
			res = VarUI2FromUI1( ps->u.bVal, &(pd->u.uiVal) );
1136 1137
			break;
        case( VT_UI2 ):
1138
            res = VariantCopy( pd, ps );
1139 1140 1141
            break;
		case( VT_UINT ):
		case( VT_UI4 ):
1142
			res = VarUI2FromUI4( ps->u.ulVal, &(pd->u.uiVal) );
1143 1144
			break;
		case( VT_R4 ):
1145
			res = VarUI2FromR4( ps->u.fltVal, &(pd->u.uiVal) );
1146 1147
			break;
		case( VT_R8 ):
1148
			res = VarUI2FromR8( ps->u.dblVal, &(pd->u.uiVal) );
1149 1150
			break;
		case( VT_DATE ):
1151
			res = VarUI2FromDate( ps->u.date, &(pd->u.uiVal) );
1152 1153
			break;
		case( VT_BOOL ):
1154
			res = VarUI2FromBool( ps->u.boolVal, &(pd->u.uiVal) );
1155 1156
			break;
		case( VT_BSTR ):
1157
			res = VarUI2FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.uiVal) );
1158 1159
			break;
		case( VT_CY ):
1160
	     res = VarUI2FromCy( ps->u.cyVal, &(pd->u.uiVal) );
1161 1162 1163 1164 1165 1166 1167 1168
		case( VT_DISPATCH ):
			/*res = VarUI2FromDisp32( ps->u.pdispVal, lcid, &(pd->u.uiVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarUI2From32( ps->u.lVal, &(pd->u.uiVal) );*/
		case( VT_DECIMAL ):
			/*res = VarUI2FromDec32( ps->u.deiVal, &(pd->u.uiVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1169
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1170 1171 1172 1173 1174 1175 1176 1177 1178
			break;
		}
		break;

	case( VT_UINT ):
	case( VT_UI4 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1179
			res = VarUI4FromI1( ps->u.cVal, &(pd->u.ulVal) );
1180 1181
			break;
		case( VT_I2 ):
1182
			res = VarUI4FromI2( ps->u.iVal, &(pd->u.ulVal) );
1183 1184 1185
			break;
		case( VT_INT ):
		case( VT_I4 ):
1186
			res = VarUI4FromI4( ps->u.lVal, &(pd->u.ulVal) );
1187 1188
			break;
		case( VT_UI1 ):
1189
			res = VarUI4FromUI1( ps->u.bVal, &(pd->u.ulVal) );
1190 1191
			break;
		case( VT_UI2 ):
1192
			res = VarUI4FromUI2( ps->u.uiVal, &(pd->u.ulVal) );
1193 1194
			break;
        case( VT_UI4 ):
1195
            res = VariantCopy( pd, ps );
1196 1197
            break;
		case( VT_R4 ):
1198
			res = VarUI4FromR4( ps->u.fltVal, &(pd->u.ulVal) );
1199 1200
			break;
		case( VT_R8 ):
1201
			res = VarUI4FromR8( ps->u.dblVal, &(pd->u.ulVal) );
1202 1203
			break;
		case( VT_DATE ):
1204
			res = VarUI4FromDate( ps->u.date, &(pd->u.ulVal) );
1205 1206
			break;
		case( VT_BOOL ):
1207
			res = VarUI4FromBool( ps->u.boolVal, &(pd->u.ulVal) );
1208 1209
			break;
		case( VT_BSTR ):
1210
			res = VarUI4FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.ulVal) );
1211 1212
			break;
		case( VT_CY ):
1213
	     res = VarUI4FromCy( ps->u.cyVal, &(pd->u.ulVal) );
1214 1215 1216 1217 1218 1219 1220 1221
		case( VT_DISPATCH ):
			/*res = VarUI4FromDisp32( ps->u.pdispVal, lcid, &(pd->u.ulVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarUI4From32( ps->u.lVal, &(pd->u.ulVal) );*/
		case( VT_DECIMAL ):
			/*res = VarUI4FromDec32( ps->u.deiVal, &(pd->u.ulVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1222
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1223 1224 1225 1226 1227 1228 1229 1230
			break;
		}
		break;
		
	case( VT_R4 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1231
			res = VarR4FromI1( ps->u.cVal, &(pd->u.fltVal) );
1232 1233
			break;
		case( VT_I2 ):
1234
			res = VarR4FromI2( ps->u.iVal, &(pd->u.fltVal) );
1235 1236 1237
			break;
		case( VT_INT ):
		case( VT_I4 ):
1238
			res = VarR4FromI4( ps->u.lVal, &(pd->u.fltVal) );
1239 1240
			break;
		case( VT_UI1 ):
1241
			res = VarR4FromUI1( ps->u.bVal, &(pd->u.fltVal) );
1242 1243
			break;
		case( VT_UI2 ):
1244
			res = VarR4FromUI2( ps->u.uiVal, &(pd->u.fltVal) );
1245 1246 1247
			break;
		case( VT_UINT ):
		case( VT_UI4 ):
1248
			res = VarR4FromUI4( ps->u.ulVal, &(pd->u.fltVal) );
1249 1250
			break;
        case( VT_R4 ):
1251
            res = VariantCopy( pd, ps );
1252 1253
            break;
		case( VT_R8 ):
1254
			res = VarR4FromR8( ps->u.dblVal, &(pd->u.fltVal) );
1255 1256
			break;
		case( VT_DATE ):
1257
			res = VarR4FromDate( ps->u.date, &(pd->u.fltVal) );
1258 1259
			break;
		case( VT_BOOL ):
1260
			res = VarR4FromBool( ps->u.boolVal, &(pd->u.fltVal) );
1261 1262
			break;
		case( VT_BSTR ):
1263
			res = VarR4FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.fltVal) );
1264 1265
			break;
		case( VT_CY ):
1266
	     res = VarR4FromCy( ps->u.cyVal, &(pd->u.fltVal) );
1267 1268 1269 1270 1271 1272 1273 1274
		case( VT_DISPATCH ):
			/*res = VarR4FromDisp32( ps->u.pdispVal, lcid, &(pd->u.fltVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarR4From32( ps->u.lVal, &(pd->u.fltVal) );*/
		case( VT_DECIMAL ):
			/*res = VarR4FromDec32( ps->u.deiVal, &(pd->u.fltVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1275
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1276 1277 1278 1279 1280 1281 1282 1283
			break;
		}
		break;

	case( VT_R8 ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1284
			res = VarR8FromI1( ps->u.cVal, &(pd->u.dblVal) );
1285 1286
			break;
		case( VT_I2 ):
1287
			res = VarR8FromI2( ps->u.iVal, &(pd->u.dblVal) );
1288 1289 1290
			break;
		case( VT_INT ):
		case( VT_I4 ):
1291
			res = VarR8FromI4( ps->u.lVal, &(pd->u.dblVal) );
1292 1293
			break;
		case( VT_UI1 ):
1294
			res = VarR8FromUI1( ps->u.bVal, &(pd->u.dblVal) );
1295 1296
			break;
		case( VT_UI2 ):
1297
			res = VarR8FromUI2( ps->u.uiVal, &(pd->u.dblVal) );
1298 1299 1300
			break;
		case( VT_UINT ):
		case( VT_UI4 ):
1301
			res = VarR8FromUI4( ps->u.ulVal, &(pd->u.dblVal) );
1302 1303
			break;
		case( VT_R4 ):
1304
			res = VarR8FromR4( ps->u.fltVal, &(pd->u.dblVal) );
1305 1306
			break;
        case( VT_R8 ):
1307
            res = VariantCopy( pd, ps );
1308 1309
            break;
		case( VT_DATE ):
1310
			res = VarR8FromDate( ps->u.date, &(pd->u.dblVal) );
1311 1312
			break;
		case( VT_BOOL ):
1313
			res = VarR8FromBool( ps->u.boolVal, &(pd->u.dblVal) );
1314 1315
			break;
		case( VT_BSTR ):
1316
			res = VarR8FromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.dblVal) );
1317 1318
			break;
		case( VT_CY ):
1319
	     res = VarR8FromCy( ps->u.cyVal, &(pd->u.dblVal) );
1320 1321 1322 1323 1324 1325 1326 1327
		case( VT_DISPATCH ):
			/*res = VarR8FromDisp32( ps->u.pdispVal, lcid, &(pd->u.dblVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarR8From32( ps->u.lVal, &(pd->u.dblVal) );*/
		case( VT_DECIMAL ):
			/*res = VarR8FromDec32( ps->u.deiVal, &(pd->u.dblVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1328
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1329 1330 1331 1332 1333 1334 1335 1336
			break;
		}
		break;

	case( VT_DATE ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1337
			res = VarDateFromI1( ps->u.cVal, &(pd->u.date) );
1338 1339
			break;
		case( VT_I2 ):
1340
			res = VarDateFromI2( ps->u.iVal, &(pd->u.date) );
1341 1342
			break;
		case( VT_INT ):
1343
			res = VarDateFromInt( ps->u.intVal, &(pd->u.date) );
1344 1345
			break;
		case( VT_I4 ):
1346
			res = VarDateFromI4( ps->u.lVal, &(pd->u.date) );
1347 1348
			break;
		case( VT_UI1 ):
1349
			res = VarDateFromUI1( ps->u.bVal, &(pd->u.date) );
1350 1351
			break;
		case( VT_UI2 ):
1352
			res = VarDateFromUI2( ps->u.uiVal, &(pd->u.date) );
1353 1354
			break;
		case( VT_UINT ):
1355
			res = VarDateFromUint( ps->u.uintVal, &(pd->u.date) );
1356 1357
			break;
		case( VT_UI4 ):
1358
			res = VarDateFromUI4( ps->u.ulVal, &(pd->u.date) );
1359 1360
			break;
		case( VT_R4 ):
1361
			res = VarDateFromR4( ps->u.fltVal, &(pd->u.date) );
1362 1363
			break;
		case( VT_R8 ):
1364
			res = VarDateFromR8( ps->u.dblVal, &(pd->u.date) );
1365 1366
			break;
        case( VT_DATE ):
1367
            res = VariantCopy( pd, ps );
1368 1369
            break;
		case( VT_BOOL ):
1370
			res = VarDateFromBool( ps->u.boolVal, &(pd->u.date) );
1371 1372
			break;
		case( VT_BSTR ):
1373
			res = VarDateFromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.date) );
1374 1375
			break;
		case( VT_CY ):
1376
	     res = VarDateFromCy( ps->u.cyVal, &(pd->u.date) );
1377 1378 1379 1380 1381 1382 1383 1384
		case( VT_DISPATCH ):
			/*res = VarDateFromDisp32( ps->u.pdispVal, lcid, &(pd->u.date) );*/
		case( VT_UNKNOWN ):
			/*res = VarDateFrom32( ps->u.lVal, &(pd->u.date) );*/
		case( VT_DECIMAL ):
			/*res = VarDateFromDec32( ps->u.deiVal, &(pd->u.date) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1385
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1386 1387 1388 1389 1390 1391 1392 1393
			break;
		}
		break;

	case( VT_BOOL ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1394
			res = VarBoolFromI1( ps->u.cVal, &(pd->u.boolVal) );
1395 1396
			break;
		case( VT_I2 ):
1397
			res = VarBoolFromI2( ps->u.iVal, &(pd->u.boolVal) );
1398 1399
			break;
		case( VT_INT ):
1400
			res = VarBoolFromInt( ps->u.intVal, &(pd->u.boolVal) );
1401 1402
			break;
		case( VT_I4 ):
1403
			res = VarBoolFromI4( ps->u.lVal, &(pd->u.boolVal) );
1404 1405
			break;
		case( VT_UI1 ):
1406
			res = VarBoolFromUI1( ps->u.bVal, &(pd->u.boolVal) );
1407 1408
			break;
		case( VT_UI2 ):
1409
			res = VarBoolFromUI2( ps->u.uiVal, &(pd->u.boolVal) );
1410 1411
			break;
		case( VT_UINT ):
1412
			res = VarBoolFromUint( ps->u.uintVal, &(pd->u.boolVal) );
1413 1414
			break;
		case( VT_UI4 ):
1415
			res = VarBoolFromUI4( ps->u.ulVal, &(pd->u.boolVal) );
1416 1417
			break;
		case( VT_R4 ):
1418
			res = VarBoolFromR4( ps->u.fltVal, &(pd->u.boolVal) );
1419 1420
			break;
		case( VT_R8 ):
1421
			res = VarBoolFromR8( ps->u.dblVal, &(pd->u.boolVal) );
1422 1423
			break;
		case( VT_DATE ):
1424
			res = VarBoolFromDate( ps->u.date, &(pd->u.boolVal) );
1425 1426
			break;
        case( VT_BOOL ):
1427
            res = VariantCopy( pd, ps );
1428 1429
            break;
		case( VT_BSTR ):
1430
			res = VarBoolFromStr( ps->u.bstrVal, lcid, dwFlags, &(pd->u.boolVal) );
1431 1432
			break;
		case( VT_CY ):
1433
	     res = VarBoolFromCy( ps->u.cyVal, &(pd->u.boolVal) );
1434 1435 1436 1437 1438 1439 1440 1441
		case( VT_DISPATCH ):
			/*res = VarBoolFromDisp32( ps->u.pdispVal, lcid, &(pd->u.boolVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarBoolFrom32( ps->u.lVal, &(pd->u.boolVal) );*/
		case( VT_DECIMAL ):
			/*res = VarBoolFromDec32( ps->u.deiVal, &(pd->u.boolVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1442
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1443 1444 1445 1446 1447 1448 1449 1450
			break;
		}
		break;

	case( VT_BSTR ):
		switch( vtFrom )
		{
		case( VT_I1 ):
1451
			res = VarBstrFromI1( ps->u.cVal, lcid, dwFlags, &(pd->u.bstrVal) );
1452 1453
			break;
		case( VT_I2 ):
1454
			res = VarBstrFromI2( ps->u.iVal, lcid, dwFlags, &(pd->u.bstrVal) );
1455 1456
			break;
		case( VT_INT ):
1457
			res = VarBstrFromInt( ps->u.intVal, lcid, dwFlags, &(pd->u.bstrVal) );
1458 1459
			break;
		case( VT_I4 ):
1460
			res = VarBstrFromI4( ps->u.lVal, lcid, dwFlags, &(pd->u.bstrVal) );
1461 1462
			break;
		case( VT_UI1 ):
1463
			res = VarBstrFromUI1( ps->u.bVal, lcid, dwFlags, &(pd->u.bstrVal) );
1464 1465
			break;
		case( VT_UI2 ):
1466
			res = VarBstrFromUI2( ps->u.uiVal, lcid, dwFlags, &(pd->u.bstrVal) );
1467 1468
			break;
		case( VT_UINT ):
1469
			res = VarBstrFromUint( ps->u.uintVal, lcid, dwFlags, &(pd->u.bstrVal) );
1470 1471
			break;
		case( VT_UI4 ):
1472
			res = VarBstrFromUI4( ps->u.ulVal, lcid, dwFlags, &(pd->u.bstrVal) );
1473 1474
			break;
		case( VT_R4 ):
1475
			res = VarBstrFromR4( ps->u.fltVal, lcid, dwFlags, &(pd->u.bstrVal) );
1476 1477
			break;
		case( VT_R8 ):
1478
			res = VarBstrFromR8( ps->u.dblVal, lcid, dwFlags, &(pd->u.bstrVal) );
1479 1480
			break;
		case( VT_DATE ):
1481
			res = VarBstrFromDate( ps->u.date, lcid, dwFlags, &(pd->u.bstrVal) );
1482 1483
			break;
		case( VT_BOOL ):
1484
			res = VarBstrFromBool( ps->u.boolVal, lcid, dwFlags, &(pd->u.bstrVal) );
1485 1486
			break;
        case( VT_BSTR ):
1487
            res = VariantCopy( pd, ps );
1488 1489
            break;
		case( VT_CY ):
1490
	     /*res = VarBstrFromCy32( ps->u.cyVal, lcid, dwFlags, &(pd->u.bstrVal) );*/
1491 1492 1493 1494 1495 1496 1497 1498
		case( VT_DISPATCH ):
			/*res = VarBstrFromDisp32( ps->u.pdispVal, lcid, lcid, dwFlags, &(pd->u.bstrVal) );*/
		case( VT_UNKNOWN ):
			/*res = VarBstrFrom32( ps->u.lVal, lcid, dwFlags, &(pd->u.bstrVal) );*/
		case( VT_DECIMAL ):
			/*res = VarBstrFromDec32( ps->u.deiVal, lcid, dwFlags, &(pd->u.bstrVal) );*/
		default:
			res = DISP_E_TYPEMISMATCH;
1499
			FIXME("Coercion from %d to %d\n", vtFrom, vt );
1500 1501 1502 1503
			break;
		}
		break;

1504 1505 1506 1507
     case( VT_CY ):
	switch( vtFrom )
	  {
	  case( VT_I1 ):
1508
	     res = VarCyFromI1( ps->u.cVal, &(pd->u.cyVal) );
1509 1510
	     break;
	  case( VT_I2 ):
1511
	     res = VarCyFromI2( ps->u.iVal, &(pd->u.cyVal) );
1512 1513
	     break;
	  case( VT_INT ):
1514
	     res = VarCyFromInt( ps->u.intVal, &(pd->u.cyVal) );
1515 1516
	     break;
	  case( VT_I4 ):
1517
	     res = VarCyFromI4( ps->u.lVal, &(pd->u.cyVal) );
1518 1519
	     break;
	  case( VT_UI1 ):
1520
	     res = VarCyFromUI1( ps->u.bVal, &(pd->u.cyVal) );
1521 1522
	     break;
	  case( VT_UI2 ):
1523
	     res = VarCyFromUI2( ps->u.uiVal, &(pd->u.cyVal) );
1524 1525
	     break;
	  case( VT_UINT ):
1526
	     res = VarCyFromUint( ps->u.uintVal, &(pd->u.cyVal) );
1527 1528
	     break;
	  case( VT_UI4 ):
1529
	     res = VarCyFromUI4( ps->u.ulVal, &(pd->u.cyVal) );
1530 1531
	     break;
	  case( VT_R4 ):
1532
	     res = VarCyFromR4( ps->u.fltVal, &(pd->u.cyVal) );
1533 1534
	     break;
	  case( VT_R8 ):
1535
	     res = VarCyFromR8( ps->u.dblVal, &(pd->u.cyVal) );
1536 1537
	     break;
	  case( VT_DATE ):
1538
	     res = VarCyFromDate( ps->u.date, &(pd->u.cyVal) );
1539 1540
	     break;
	  case( VT_BOOL ):
1541
	     res = VarCyFromBool( ps->u.date, &(pd->u.cyVal) );
1542 1543
	     break;
	  case( VT_CY ):
1544
	     res = VariantCopy( pd, ps );
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
	     break;
	  case( VT_BSTR ):
	     /*res = VarCyFromStr32( ps->u.bstrVal, lcid, dwFlags, &(pd->u.cyVal) );*/
	  case( VT_DISPATCH ):
	     /*res = VarCyFromDisp32( ps->u.pdispVal, lcid, &(pd->u.boolVal) );*/
	  case( VT_UNKNOWN ):
	     /*res = VarCyFrom32( ps->u.lVal, &(pd->u.boolVal) );*/
	  case( VT_DECIMAL ):
	     /*res = VarCyFromDec32( ps->u.deiVal, &(pd->u.boolVal) );*/
	  default:
	     res = DISP_E_TYPEMISMATCH;
1556
	     FIXME("Coercion from %d to %d\n", vtFrom, vt );
1557 1558 1559 1560
	     break;
	  }
	break;

1561 1562
	default:
		res = DISP_E_TYPEMISMATCH;
1563
		FIXME("Coercion from %d to %d\n", vtFrom, vt );
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
		break;
	}
	
	return res;
}

/******************************************************************************
 *		ValidateVtRange	[INTERNAL]
 *
 * Used internally by the hi-level Variant API to determine
 * if the vartypes are valid.
 */
1576
static HRESULT WINAPI ValidateVtRange( VARTYPE vt )
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
{
    /* if by value we must make sure it is in the
     * range of the valid types.
     */
    if( ( vt & VT_TYPEMASK ) > VT_MAXVALIDTYPE )
    {
        return DISP_E_BADVARTYPE;
    }
    return S_OK;
}


/******************************************************************************
 *		ValidateVartype	[INTERNAL]
 *
 * Used internally by the hi-level Variant API to determine
 * if the vartypes are valid.
 */
1595
static HRESULT WINAPI ValidateVariantType( VARTYPE vt )
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
{
	HRESULT res = S_OK;

	/* check if we have a valid argument.
	 */
	if( vt & VT_BYREF )
    {
        /* if by reference check that the type is in
         * the valid range and that it is not of empty or null type
         */
        if( ( vt & VT_TYPEMASK ) == VT_EMPTY ||
            ( vt & VT_TYPEMASK ) == VT_NULL ||
			( vt & VT_TYPEMASK ) > VT_MAXVALIDTYPE )
		{
			res = E_INVALIDARG;
		}
			
    }
    else
    {
        res = ValidateVtRange( vt );
    }
		
	return res;
}

/******************************************************************************
 *		ValidateVt	[INTERNAL]
 *
 * Used internally by the hi-level Variant API to determine
 * if the vartypes are valid.
 */
1628
static HRESULT WINAPI ValidateVt( VARTYPE vt )
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
{
	HRESULT res = S_OK;

	/* check if we have a valid argument.
	 */
	if( vt & VT_BYREF )
    {
        /* if by reference check that the type is in
         * the valid range and that it is not of empty or null type
         */
        if( ( vt & VT_TYPEMASK ) == VT_EMPTY ||
            ( vt & VT_TYPEMASK ) == VT_NULL ||
			( vt & VT_TYPEMASK ) > VT_MAXVALIDTYPE )
		{
			res = DISP_E_BADVARTYPE;
		}
			
    }
    else
    {
        res = ValidateVtRange( vt );
    }
		
	return res;
}





/******************************************************************************
1660
 *		VariantInit	[OLEAUT32.8]
1661 1662 1663 1664
 *
 * Initializes the Variant.  Unlike VariantClear it does not interpret the current
 * contents of the Variant.
 */
1665
void WINAPI VariantInit(VARIANTARG* pvarg)
1666
{
1667
  TRACE("(%p),stub\n",pvarg);
1668

1669 1670
  memset(pvarg, 0, sizeof (VARIANTARG));
  pvarg->vt = VT_EMPTY;
1671

1672
  return;
1673 1674 1675
}

/******************************************************************************
1676
 *		VariantClear	[OLEAUT32.9]
1677 1678 1679 1680 1681 1682
 *
 * This function clears the VARIANT by setting the vt field to VT_EMPTY. It also
 * sets the wReservedX field to 0.	The current contents of the VARIANT are
 * freed.  If the vt is VT_BSTR the string is freed. If VT_DISPATCH the object is
 * released. If VT_ARRAY the array is freed.
 */
1683
HRESULT WINAPI VariantClear(VARIANTARG* pvarg)
1684
{
1685
  HRESULT res = S_OK;
1686
  TRACE("(%p)\n",pvarg);
1687

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
  res = ValidateVariantType( pvarg->vt );
  if( res == S_OK )
  {
    if( !( pvarg->vt & VT_BYREF ) )
    {
      /*
       * The VT_ARRAY flag is a special case of a safe array.
       */
      if ( (pvarg->vt & VT_ARRAY) != 0)
      {
	SafeArrayDestroy(pvarg->u.parray);
      }
      else
      {
	switch( pvarg->vt & VT_TYPEMASK )
1703
	{
1704 1705 1706 1707
	  case( VT_BSTR ):
	    SysFreeString( pvarg->u.bstrVal );
	    break;
	  case( VT_DISPATCH ):
1708 1709
	    if(pvarg->u.pdispVal!=NULL)
	      ICOM_CALL(Release,pvarg->u.pdispVal);
1710 1711
	    break;
	  case( VT_VARIANT ):
1712
	    VariantClear(pvarg->u.pvarVal);
1713 1714
	    break;
	  case( VT_UNKNOWN ):
1715 1716
	    if(pvarg->u.punkVal!=NULL)
	      ICOM_CALL(Release,pvarg->u.punkVal);
1717 1718 1719 1720 1721 1722
	    break;
	  case( VT_SAFEARRAY ):
	    SafeArrayDestroy(pvarg->u.parray);
	    break;
	  default:
	    break;
1723
	}
1724 1725 1726 1727 1728 1729 1730 1731 1732
      }
    }
	
    /*
     * Empty all the fields and mark the type as empty.
     */
    memset(pvarg, 0, sizeof (VARIANTARG));
    pvarg->vt = VT_EMPTY;
  }
1733

1734
  return res;
1735 1736 1737
}

/******************************************************************************
1738
 *		VariantCopy	[OLEAUT32.10]
1739 1740 1741
 *
 * Frees up the designation variant and makes a copy of the source.
 */
1742
HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
1743
{
1744 1745
  HRESULT res = S_OK;

1746
  TRACE("(%p, %p)\n", pvargDest, pvargSrc);
1747

1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
  res = ValidateVariantType( pvargSrc->vt );

  /* If the pointer are to the same variant we don't need
   * to do anything.
   */
  if( pvargDest != pvargSrc && res == S_OK )
  {
    res = VariantClear( pvargDest );
		
    if( res == S_OK )
    {
      if( pvargSrc->vt & VT_BYREF )
      {
	/* In the case of byreference we only need
	 * to copy the pointer.
	 */
	pvargDest->u = pvargSrc->u;
	pvargDest->vt = pvargSrc->vt;
      }
      else
      {
	/*
	 * The VT_ARRAY flag is another way to designate a safe array.
1771
	 */
1772
	if (pvargSrc->vt & VT_ARRAY)
1773
	{
1774
	  SafeArrayCopy(pvargSrc->u.parray, &pvargDest->u.parray);
1775
	}
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
	else
	{
	  /* In the case of by value we need to
	   * copy the actuall value. In the case of
	   * VT_BSTR a copy of the string is made,
	   * if VT_DISPATCH or VT_IUNKNOWN AddReff is
	   * called to increment the object's reference count.
	   */
	  switch( pvargSrc->vt & VT_TYPEMASK )
	  {
	    case( VT_BSTR ):
	      pvargDest->u.bstrVal = SysAllocString( pvargSrc->u.bstrVal );
	      break;
	    case( VT_DISPATCH ):
1790 1791 1792
	      pvargDest->u.pdispVal = pvargSrc->u.pdispVal;
	      if (pvargDest->u.pdispVal!=NULL)
		ICOM_CALL(AddRef,pvargDest->u.pdispVal);
1793 1794
	      break;
	    case( VT_VARIANT ):
1795
	      VariantCopy(pvargDest->u.pvarVal,pvargSrc->u.pvarVal);
1796 1797
	      break;
	    case( VT_UNKNOWN ):
1798 1799 1800
	      pvargDest->u.punkVal = pvargSrc->u.punkVal;
	      if (pvargDest->u.pdispVal!=NULL)
		ICOM_CALL(AddRef,pvargDest->u.punkVal);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	      break;
	    case( VT_SAFEARRAY ):
	      SafeArrayCopy(pvargSrc->u.parray, &pvargDest->u.parray);
	      break;
	    default:
	      pvargDest->u = pvargSrc->u;
	      break;
	  }
	}
	
	pvargDest->vt = pvargSrc->vt;
      }      
    }
  }

  return res;
1817 1818 1819 1820
}


/******************************************************************************
1821
 *		VariantCopyInd	[OLEAUT32.11]
1822 1823 1824 1825
 *
 * Frees up the destination variant and makes a copy of the source.  If
 * the source is of type VT_BYREF it performs the necessary indirections.
 */
1826
HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
1827
{
1828
  HRESULT res = S_OK;
1829

1830
  TRACE("(%p, %p)\n", pvargDest, pvargSrc);
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865

  res = ValidateVariantType( pvargSrc->vt );

  if( res != S_OK )
    return res;
  
  if( pvargSrc->vt & VT_BYREF )
  {
    VARIANTARG varg;
    VariantInit( &varg );

    /* handle the in place copy.
     */
    if( pvargDest == pvargSrc )
    {
      /* we will use a copy of the source instead.
       */
      res = VariantCopy( &varg, pvargSrc );
      pvargSrc = &varg;
    }

    if( res == S_OK )
    {
      res = VariantClear( pvargDest );

      if( res == S_OK )
      {
	/*
	 * The VT_ARRAY flag is another way to designate a safearray variant.
	 */
	if ( pvargSrc->vt & VT_ARRAY)
	{
	  SafeArrayCopy(*pvargSrc->u.pparray, &pvargDest->u.parray);
	}
	else
1866
	{
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
	  /* In the case of by reference we need
	   * to copy the date pointed to by the variant.
	   */

	  /* Get the variant type.
	   */
	  switch( pvargSrc->vt & VT_TYPEMASK )
	  {
	    case( VT_BSTR ):
	      pvargDest->u.bstrVal = SysAllocString( *(pvargSrc->u.pbstrVal) );
	      break;
	    case( VT_DISPATCH ):
	      break;
	    case( VT_VARIANT ):
	      {
		/* Prevent from cycling.  According to tests on
		 * VariantCopyInd in Windows and the documentation
		 * this API dereferences the inner Variants to only one depth.
		 * If the inner Variant itself contains an
		 * other inner variant the E_INVALIDARG error is
		 * returned. 
1888
		 */
1889
		if( pvargSrc->wReserved1 & PROCESSING_INNER_VARIANT )
1890
		{
1891 1892 1893 1894 1895
		  /* If we get here we are attempting to deference
		   * an inner variant that that is itself contained
		   * in an inner variant so report E_INVALIDARG error.
		   */
		  res = E_INVALIDARG;
1896
		}
1897
		else
1898
		{
1899 1900 1901 1902 1903 1904 1905 1906 1907
		  /* Set the processing inner variant flag.
		   * We will set this flag in the inner variant
		   * that will be passed to the VariantCopyInd function.
		   */
		  (pvargSrc->u.pvarVal)->wReserved1 |= PROCESSING_INNER_VARIANT;
		  
		  /* Dereference the inner variant.
		   */
		  res = VariantCopyInd( pvargDest, pvargSrc->u.pvarVal );
1908 1909 1910
		  /* We must also copy its type, I think.
		   */
		  pvargSrc->vt = pvargSrc->u.pvarVal->vt; 
1911
		}
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
	      }
	      break;
	    case( VT_UNKNOWN ):
	      break;
	    case( VT_SAFEARRAY ):
	      SafeArrayCopy(*pvargSrc->u.pparray, &pvargDest->u.parray);
	      break;
	    default:
	      /* This is a by reference Variant which means that the union
	       * part of the Variant contains a pointer to some data of
	       * type "pvargSrc->vt & VT_TYPEMASK".
	       * We will deference this data in a generic fashion using
	       * the void pointer "Variant.u.byref".
	       * We will copy this data into the union of the destination
	       * Variant.
	       */
	      memcpy( &pvargDest->u, pvargSrc->u.byref, SizeOfVariantData( pvargSrc ) );
	      break;
	  }
1931
	}
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
	
	pvargDest->vt = pvargSrc->vt & VT_TYPEMASK;
      }
    }

    /* this should not fail.
     */
    VariantClear( &varg );
  }
  else
  {
    res = VariantCopy( pvargDest, pvargSrc );
  }

  return res;
1947 1948 1949
}

/******************************************************************************
1950
 *		VariantChangeType	[OLEAUT32.12]
1951
 */
1952
HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
1953 1954
							USHORT wFlags, VARTYPE vt)
{
1955
	return VariantChangeTypeEx( pvargDest, pvargSrc, 0, wFlags, vt );
1956 1957 1958
}

/******************************************************************************
1959
 *		VariantChangeTypeEx	[OLEAUT32.147]
1960
 */
1961
HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
1962 1963 1964 1965
							  LCID lcid, USHORT wFlags, VARTYPE vt)
{
	HRESULT res = S_OK;
	VARIANTARG varg;
1966
	VariantInit( &varg );
1967
	
1968
	TRACE("(%p, %p, %ld, %u, %u),stub\n", pvargDest, pvargSrc, lcid, wFlags, vt);
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984

	/* validate our source argument.
	 */
	res = ValidateVariantType( pvargSrc->vt );

	/* validate the vartype.
	 */
	if( res == S_OK )
	{
		res = ValidateVt( vt );
	}

	/* if we are doing an in-place conversion make a copy of the source.
	 */
	if( res == S_OK && pvargDest == pvargSrc )
	{
1985
		res = VariantCopy( &varg, pvargSrc );
1986 1987 1988 1989 1990 1991 1992
		pvargSrc = &varg;
	}

	if( res == S_OK )
	{
		/* free up the destination variant.
		 */
1993
		res = VariantClear( pvargDest );
1994 1995 1996 1997 1998 1999 2000 2001 2002
	}

	if( res == S_OK )
	{
		if( pvargSrc->vt & VT_BYREF )
		{
			/* Convert the source variant to a "byvalue" variant.
			 */
			VARIANTARG Variant;
2003 2004
			VariantInit( &Variant );
			res = VariantCopyInd( &Variant, pvargSrc );
2005 2006 2007 2008 2009
			if( res == S_OK )
			{
				res = Coerce( pvargDest, lcid, wFlags, &Variant, vt );
				/* this should not fail.
				 */
2010
				VariantClear( &Variant );
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
			}
	
		}
		else
		{
			/* Use the current "byvalue" source variant.
			 */
			res = Coerce( pvargDest, lcid, wFlags, pvargSrc, vt );
		}
	}
	/* this should not fail.
	 */
2023
	VariantClear( &varg );
2024
	
2025 2026 2027 2028 2029
	/* set the type of the destination
	 */
	if ( res == S_OK )
		pvargDest->vt = vt;

2030 2031 2032 2033 2034 2035 2036
	return res;
}




/******************************************************************************
2037
 *		VarUI1FromI2		[OLEAUT32.130]
2038
 */
2039
HRESULT WINAPI VarUI1FromI2(short sIn, BYTE* pbOut)
2040
{
2041
	TRACE("( %d, %p ), stub\n", sIn, pbOut );
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055

	/* Check range of value.
	 */
	if( sIn < UI1_MIN || sIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) sIn;
	
	return S_OK;
}

/******************************************************************************
2056
 *		VarUI1FromI4		[OLEAUT32.131]
2057
 */
2058
HRESULT WINAPI VarUI1FromI4(LONG lIn, BYTE* pbOut)
2059
{
2060
	TRACE("( %ld, %p ), stub\n", lIn, pbOut );
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075

	/* Check range of value.
	 */
	if( lIn < UI1_MIN || lIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) lIn;
	
	return S_OK;
}


/******************************************************************************
2076
 *		VarUI1FromR4		[OLEAUT32.132]
2077
 */
2078
HRESULT WINAPI VarUI1FromR4(FLOAT fltIn, BYTE* pbOut)
2079
{
2080
	TRACE("( %f, %p ), stub\n", fltIn, pbOut );
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095

	/* Check range of value.
     */
    fltIn = round( fltIn );
	if( fltIn < UI1_MIN || fltIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) fltIn;
	
	return S_OK;
}

/******************************************************************************
2096
 *		VarUI1FromR8		[OLEAUT32.133]
2097
 */
2098
HRESULT WINAPI VarUI1FromR8(double dblIn, BYTE* pbOut)
2099
{
2100
	TRACE("( %f, %p ), stub\n", dblIn, pbOut );
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

	/* Check range of value.
     */
    dblIn = round( dblIn );
	if( dblIn < UI1_MIN || dblIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) dblIn;

	return S_OK;
}

/******************************************************************************
2116
 *		VarUI1FromDate		[OLEAUT32.135]
2117
 */
2118
HRESULT WINAPI VarUI1FromDate(DATE dateIn, BYTE* pbOut)
2119
{
2120
	TRACE("( %f, %p ), stub\n", dateIn, pbOut );
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135

	/* Check range of value.
     */
    dateIn = round( dateIn );
	if( dateIn < UI1_MIN || dateIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) dateIn;

	return S_OK;
}

/******************************************************************************
2136
 *		VarUI1FromBool		[OLEAUT32.138]
2137
 */
2138
HRESULT WINAPI VarUI1FromBool(VARIANT_BOOL boolIn, BYTE* pbOut)
2139
{
2140
	TRACE("( %d, %p ), stub\n", boolIn, pbOut );
2141 2142 2143 2144 2145 2146 2147

	*pbOut = (BYTE) boolIn;

	return S_OK;
}

/******************************************************************************
2148
 *		VarUI1FromI1		[OLEAUT32.237]
2149
 */
2150
HRESULT WINAPI VarUI1FromI1(CHAR cIn, BYTE* pbOut)
2151
{
2152
	TRACE("( %c, %p ), stub\n", cIn, pbOut );
2153 2154 2155 2156 2157 2158 2159

	*pbOut = cIn;

	return S_OK;
}

/******************************************************************************
2160
 *		VarUI1FromUI2		[OLEAUT32.238]
2161
 */
2162
HRESULT WINAPI VarUI1FromUI2(USHORT uiIn, BYTE* pbOut)
2163
{
2164
	TRACE("( %d, %p ), stub\n", uiIn, pbOut );
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178

	/* Check range of value.
	 */
	if( uiIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) uiIn;

	return S_OK;
}

/******************************************************************************
2179
 *		VarUI1FromUI4		[OLEAUT32.239]
2180
 */
2181
HRESULT WINAPI VarUI1FromUI4(ULONG ulIn, BYTE* pbOut)
2182
{
2183
	TRACE("( %ld, %p ), stub\n", ulIn, pbOut );
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198

	/* Check range of value.
	 */
	if( ulIn > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) ulIn;

	return S_OK;
}


/******************************************************************************
2199
 *		VarUI1FromStr		[OLEAUT32.54]
2200
 */
2201
HRESULT WINAPI VarUI1FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, BYTE* pbOut)
2202 2203 2204 2205
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

2206
	TRACE("( %p, 0x%08lx, 0x%08lx, %p ), stub\n", strIn, lcid, dwFlags, pbOut );
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
	
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0 , pNewString );

	/* Check range of value.
     */
    dValue = round( dValue );
	if( dValue < UI1_MIN || dValue > UI1_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pbOut = (BYTE) dValue;

	return S_OK;
}

2238
/**********************************************************************
2239
 *              VarUI1FromCy [OLEAUT32.134]
2240 2241
 * Convert currency to unsigned char
 */
2242
HRESULT WINAPI VarUI1FromCy(CY cyIn, BYTE* pbOut) {
2243
   double t = round((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
2244 2245 2246 2247 2248 2249 2250
   
   if (t > UI1_MAX || t < UI1_MIN) return DISP_E_OVERFLOW;
   
   *pbOut = (BYTE)t;
   return S_OK;
}

2251
/******************************************************************************
2252
 *		VarI2FromUI1		[OLEAUT32.48]
2253
 */
2254
HRESULT WINAPI VarI2FromUI1(BYTE bIn, short* psOut)
2255
{
2256
	TRACE("( 0x%08x, %p ), stub\n", bIn, psOut );
2257 2258 2259 2260 2261 2262 2263

	*psOut = (short) bIn;
	
	return S_OK;
}

/******************************************************************************
2264
 *		VarI2FromI4		[OLEAUT32.49]
2265
 */
2266
HRESULT WINAPI VarI2FromI4(LONG lIn, short* psOut)
2267
{
2268
	TRACE("( %lx, %p ), stub\n", lIn, psOut );
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282

	/* Check range of value.
	 */
	if( lIn < I2_MIN || lIn > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short) lIn;
	
	return S_OK;
}

/******************************************************************************
2283
 *		VarI2FromR4		[OLEAUT32.50]
2284
 */
2285
HRESULT WINAPI VarI2FromR4(FLOAT fltIn, short* psOut)
2286
{
2287
	TRACE("( %f, %p ), stub\n", fltIn, psOut );
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302

	/* Check range of value.
     */
    fltIn = round( fltIn );
	if( fltIn < I2_MIN || fltIn > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short) fltIn;

	return S_OK;
}

/******************************************************************************
2303
 *		VarI2FromR8		[OLEAUT32.51]
2304
 */
2305
HRESULT WINAPI VarI2FromR8(double dblIn, short* psOut)
2306
{
2307
	TRACE("( %f, %p ), stub\n", dblIn, psOut );
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

	/* Check range of value.
     */
    dblIn = round( dblIn );
	if( dblIn < I2_MIN || dblIn > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short) dblIn;

	return S_OK;
}

/******************************************************************************
2323
 *		VarI2FromDate		[OLEAUT32.53]
2324
 */
2325
HRESULT WINAPI VarI2FromDate(DATE dateIn, short* psOut)
2326
{
2327
	TRACE("( %f, %p ), stub\n", dateIn, psOut );
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342

	/* Check range of value.
     */
    dateIn = round( dateIn );
	if( dateIn < I2_MIN || dateIn > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short) dateIn;

	return S_OK;
}

/******************************************************************************
2343
 *		VarI2FromBool		[OLEAUT32.56]
2344
 */
2345
HRESULT WINAPI VarI2FromBool(VARIANT_BOOL boolIn, short* psOut)
2346
{
2347
	TRACE("( %d, %p ), stub\n", boolIn, psOut );
2348 2349 2350 2351 2352 2353 2354

	*psOut = (short) boolIn;

	return S_OK;
}

/******************************************************************************
2355
 *		VarI2FromI1		[OLEAUT32.48]
2356
 */
2357
HRESULT WINAPI VarI2FromI1(CHAR cIn, short* psOut)
2358
{
2359
	TRACE("( %c, %p ), stub\n", cIn, psOut );
2360 2361 2362 2363 2364 2365 2366

	*psOut = (short) cIn;

	return S_OK;
}

/******************************************************************************
2367
 *		VarI2FromUI2		[OLEAUT32.206]
2368
 */
2369
HRESULT WINAPI VarI2FromUI2(USHORT uiIn, short* psOut)
2370
{
2371
	TRACE("( %d, %p ), stub\n", uiIn, psOut );
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385

	/* Check range of value.
	 */
	if( uiIn > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short) uiIn;

	return S_OK;
}

/******************************************************************************
2386
 *		VarI2FromUI4		[OLEAUT32.49]
2387
 */
2388
HRESULT WINAPI VarI2FromUI4(ULONG ulIn, short* psOut)
2389
{
2390
	TRACE("( %lx, %p ), stub\n", ulIn, psOut );
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404

	/* Check range of value.
	 */
	if( ulIn < I2_MIN || ulIn > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short) ulIn;

	return S_OK;
}

/******************************************************************************
2405
 *		VarI2FromStr		[OLEAUT32.54]
2406
 */
2407
HRESULT WINAPI VarI2FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, short* psOut)
2408 2409 2410 2411
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

2412
	TRACE("( %p, 0x%08lx, 0x%08lx, %p ), stub\n", strIn, lcid, dwFlags, psOut );
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
	
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	/* Check range of value.
     */
    dValue = round( dValue );
	if( dValue < I2_MIN || dValue > I2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*psOut = (short)  dValue;

	return S_OK;
}

2444
/**********************************************************************
2445
 *              VarI2FromCy [OLEAUT32.52]
2446 2447
 * Convert currency to signed short
 */
2448
HRESULT WINAPI VarI2FromCy(CY cyIn, short* psOut) {
2449
   double t = round((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
2450 2451 2452 2453 2454 2455 2456
   
   if (t > I2_MAX || t < I2_MIN) return DISP_E_OVERFLOW;
   
   *psOut = (SHORT)t;
   return S_OK;
}

2457
/******************************************************************************
2458
 *		VarI4FromUI1		[OLEAUT32.58]
2459
 */
2460
HRESULT WINAPI VarI4FromUI1(BYTE bIn, LONG* plOut)
2461
{
2462
	TRACE("( %X, %p ), stub\n", bIn, plOut );
2463 2464 2465 2466 2467 2468 2469 2470

	*plOut = (LONG) bIn;

	return S_OK;
}


/******************************************************************************
2471
 *		VarI4FromR4		[OLEAUT32.60]
2472
 */
2473
HRESULT WINAPI VarI4FromR4(FLOAT fltIn, LONG* plOut)
2474
{
2475
	TRACE("( %f, %p ), stub\n", fltIn, plOut );
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490

	/* Check range of value.
     */
    fltIn = round( fltIn );
	if( fltIn < I4_MIN || fltIn > I4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*plOut = (LONG) fltIn;

	return S_OK;
}

/******************************************************************************
2491
 *		VarI4FromR8		[OLEAUT32.61]
2492
 */
2493
HRESULT WINAPI VarI4FromR8(double dblIn, LONG* plOut)
2494
{
2495
	TRACE("( %f, %p ), stub\n", dblIn, plOut );
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510

	/* Check range of value.
     */
    dblIn = round( dblIn );
	if( dblIn < I4_MIN || dblIn > I4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*plOut = (LONG) dblIn;

	return S_OK;
}

/******************************************************************************
2511
 *		VarI4FromDate		[OLEAUT32.63]
2512
 */
2513
HRESULT WINAPI VarI4FromDate(DATE dateIn, LONG* plOut)
2514
{
2515
	TRACE("( %f, %p ), stub\n", dateIn, plOut );
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530

	/* Check range of value.
     */
    dateIn = round( dateIn );
	if( dateIn < I4_MIN || dateIn > I4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*plOut = (LONG) dateIn;

	return S_OK;
}

/******************************************************************************
2531
 *		VarI4FromBool		[OLEAUT32.66]
2532
 */
2533
HRESULT WINAPI VarI4FromBool(VARIANT_BOOL boolIn, LONG* plOut)
2534
{
2535
	TRACE("( %d, %p ), stub\n", boolIn, plOut );
2536 2537 2538 2539 2540 2541 2542

	*plOut = (LONG) boolIn;

	return S_OK;
}

/******************************************************************************
2543
 *		VarI4FromI1		[OLEAUT32.209]
2544
 */
2545
HRESULT WINAPI VarI4FromI1(CHAR cIn, LONG* plOut)
2546
{
2547
	TRACE("( %c, %p ), stub\n", cIn, plOut );
2548 2549 2550 2551 2552 2553 2554

	*plOut = (LONG) cIn;

	return S_OK;
}

/******************************************************************************
2555
 *		VarI4FromUI2		[OLEAUT32.210]
2556
 */
2557
HRESULT WINAPI VarI4FromUI2(USHORT uiIn, LONG* plOut)
2558
{
2559
	TRACE("( %d, %p ), stub\n", uiIn, plOut );
2560 2561 2562 2563 2564 2565 2566

	*plOut = (LONG) uiIn;

	return S_OK;
}

/******************************************************************************
2567
 *		VarI4FromUI4		[OLEAUT32.211]
2568
 */
2569
HRESULT WINAPI VarI4FromUI4(ULONG ulIn, LONG* plOut)
2570
{
2571
	TRACE("( %lx, %p ), stub\n", ulIn, plOut );
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585

	/* Check range of value.
	 */
	if( ulIn < I4_MIN || ulIn > I4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*plOut = (LONG) ulIn;

	return S_OK;
}

/******************************************************************************
2586
 *		VarI4FromI2		[OLEAUT32.59]
2587
 */
2588
HRESULT WINAPI VarI4FromI2(short sIn, LONG* plOut)
2589
{
2590
	TRACE("( %d, %p ), stub\n", sIn, plOut );
2591 2592 2593 2594 2595 2596 2597

	*plOut = (LONG) sIn;

	return S_OK;
}

/******************************************************************************
2598
 *		VarI4FromStr		[OLEAUT32.64]
2599
 */
2600
HRESULT WINAPI VarI4FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, LONG* plOut)
2601 2602 2603 2604
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

2605
	TRACE("( %p, 0x%08lx, 0x%08lx, %p ), stub\n", strIn, lcid, dwFlags, plOut );
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
	
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	/* Check range of value.
     */
    dValue = round( dValue );
	if( dValue < I4_MIN || dValue > I4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*plOut = (LONG) dValue;

	return S_OK;
}

2637
/**********************************************************************
2638
 *              VarI4FromCy [OLEAUT32.62]
2639 2640
 * Convert currency to signed long
 */
2641
HRESULT WINAPI VarI4FromCy(CY cyIn, LONG* plOut) {
2642
   double t = round((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
2643 2644 2645 2646 2647 2648 2649
   
   if (t > I4_MAX || t < I4_MIN) return DISP_E_OVERFLOW;
   
   *plOut = (LONG)t;
   return S_OK;
}

2650
/******************************************************************************
2651
 *		VarR4FromUI1		[OLEAUT32.68]
2652
 */
2653
HRESULT WINAPI VarR4FromUI1(BYTE bIn, FLOAT* pfltOut)
2654
{
2655
	TRACE("( %X, %p ), stub\n", bIn, pfltOut );
2656 2657 2658 2659 2660 2661 2662

	*pfltOut = (FLOAT) bIn;

	return S_OK;
}

/******************************************************************************
2663
 *		VarR4FromI2		[OLEAUT32.69]
2664
 */
2665
HRESULT WINAPI VarR4FromI2(short sIn, FLOAT* pfltOut)
2666
{
2667
	TRACE("( %d, %p ), stub\n", sIn, pfltOut );
2668 2669 2670 2671 2672 2673 2674

	*pfltOut = (FLOAT) sIn;

	return S_OK;
}

/******************************************************************************
2675
 *		VarR4FromI4		[OLEAUT32.70]
2676
 */
2677
HRESULT WINAPI VarR4FromI4(LONG lIn, FLOAT* pfltOut)
2678
{
2679
	TRACE("( %lx, %p ), stub\n", lIn, pfltOut );
2680 2681 2682 2683 2684 2685 2686

	*pfltOut = (FLOAT) lIn;

	return S_OK;
}

/******************************************************************************
2687
 *		VarR4FromR8		[OLEAUT32.71]
2688
 */
2689
HRESULT WINAPI VarR4FromR8(double dblIn, FLOAT* pfltOut)
2690
{
2691
	TRACE("( %f, %p ), stub\n", dblIn, pfltOut );
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705

	/* Check range of value.
	 */
	if( dblIn < -(FLT_MAX) || dblIn > FLT_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pfltOut = (FLOAT) dblIn;

	return S_OK;
}

/******************************************************************************
2706
 *		VarR4FromDate		[OLEAUT32.73]
2707
 */
2708
HRESULT WINAPI VarR4FromDate(DATE dateIn, FLOAT* pfltOut)
2709
{
2710
	TRACE("( %f, %p ), stub\n", dateIn, pfltOut );
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724

	/* Check range of value.
	 */
	if( dateIn < -(FLT_MAX) || dateIn > FLT_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pfltOut = (FLOAT) dateIn;

	return S_OK;
}

/******************************************************************************
2725
 *		VarR4FromBool		[OLEAUT32.76]
2726
 */
2727
HRESULT WINAPI VarR4FromBool(VARIANT_BOOL boolIn, FLOAT* pfltOut)
2728
{
2729
	TRACE("( %d, %p ), stub\n", boolIn, pfltOut );
2730 2731 2732 2733 2734 2735 2736

	*pfltOut = (FLOAT) boolIn;

	return S_OK;
}

/******************************************************************************
2737
 *		VarR4FromI1		[OLEAUT32.213]
2738
 */
2739
HRESULT WINAPI VarR4FromI1(CHAR cIn, FLOAT* pfltOut)
2740
{
2741
	TRACE("( %c, %p ), stub\n", cIn, pfltOut );
2742 2743 2744 2745 2746 2747 2748

	*pfltOut = (FLOAT) cIn;

	return S_OK;
}

/******************************************************************************
2749
 *		VarR4FromUI2		[OLEAUT32.214]
2750
 */
2751
HRESULT WINAPI VarR4FromUI2(USHORT uiIn, FLOAT* pfltOut)
2752
{
2753
	TRACE("( %d, %p ), stub\n", uiIn, pfltOut );
2754 2755 2756 2757 2758 2759 2760

	*pfltOut = (FLOAT) uiIn;

	return S_OK;
}

/******************************************************************************
2761
 *		VarR4FromUI4		[OLEAUT32.215]
2762
 */
2763
HRESULT WINAPI VarR4FromUI4(ULONG ulIn, FLOAT* pfltOut)
2764
{
2765
	TRACE("( %ld, %p ), stub\n", ulIn, pfltOut );
2766 2767 2768 2769 2770 2771 2772

	*pfltOut = (FLOAT) ulIn;

	return S_OK;
}

/******************************************************************************
2773
 *		VarR4FromStr		[OLEAUT32.74]
2774
 */
2775
HRESULT WINAPI VarR4FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, FLOAT* pfltOut)
2776 2777 2778 2779
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

2780
	TRACE("( %p, %ld, %ld, %p ), stub\n", strIn, lcid, dwFlags, pfltOut );
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
	
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	/* Check range of value.
	 */
	if( dValue < -(FLT_MAX) || dValue > FLT_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pfltOut = (FLOAT) dValue;

	return S_OK;
}

2811
/**********************************************************************
2812
 *              VarR4FromCy [OLEAUT32.72]
2813 2814
 * Convert currency to float
 */
2815
HRESULT WINAPI VarR4FromCy(CY cyIn, FLOAT* pfltOut) {
2816
   *pfltOut = (FLOAT)((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
2817 2818 2819 2820
   
   return S_OK;
}

2821
/******************************************************************************
2822
 *		VarR8FromUI1		[OLEAUT32.68]
2823
 */
2824
HRESULT WINAPI VarR8FromUI1(BYTE bIn, double* pdblOut)
2825
{
2826
	TRACE("( %d, %p ), stub\n", bIn, pdblOut );
2827 2828 2829 2830 2831 2832 2833

	*pdblOut = (double) bIn;

	return S_OK;
}

/******************************************************************************
2834
 *		VarR8FromI2		[OLEAUT32.69]
2835
 */
2836
HRESULT WINAPI VarR8FromI2(short sIn, double* pdblOut)
2837
{
2838
	TRACE("( %d, %p ), stub\n", sIn, pdblOut );
2839 2840 2841 2842 2843 2844 2845

	*pdblOut = (double) sIn;

	return S_OK;
}

/******************************************************************************
2846
 *		VarR8FromI4		[OLEAUT32.70]
2847
 */
2848
HRESULT WINAPI VarR8FromI4(LONG lIn, double* pdblOut)
2849
{
2850
	TRACE("( %ld, %p ), stub\n", lIn, pdblOut );
2851 2852 2853 2854 2855 2856 2857

	*pdblOut = (double) lIn;

	return S_OK;
}

/******************************************************************************
2858
 *		VarR8FromR4		[OLEAUT32.81]
2859
 */
2860
HRESULT WINAPI VarR8FromR4(FLOAT fltIn, double* pdblOut)
2861
{
2862
	TRACE("( %f, %p ), stub\n", fltIn, pdblOut );
2863 2864 2865 2866 2867 2868 2869

	*pdblOut = (double) fltIn;

	return S_OK;
}

/******************************************************************************
2870
 *		VarR8FromDate		[OLEAUT32.83]
2871
 */
2872
HRESULT WINAPI VarR8FromDate(DATE dateIn, double* pdblOut)
2873
{
2874
	TRACE("( %f, %p ), stub\n", dateIn, pdblOut );
2875 2876 2877 2878 2879 2880 2881

	*pdblOut = (double) dateIn;

	return S_OK;
}

/******************************************************************************
2882
 *		VarR8FromBool		[OLEAUT32.86]
2883
 */
2884
HRESULT WINAPI VarR8FromBool(VARIANT_BOOL boolIn, double* pdblOut)
2885
{
2886
	TRACE("( %d, %p ), stub\n", boolIn, pdblOut );
2887 2888 2889 2890 2891 2892 2893

	*pdblOut = (double) boolIn;

	return S_OK;
}

/******************************************************************************
2894
 *		VarR8FromI1		[OLEAUT32.217]
2895
 */
2896
HRESULT WINAPI VarR8FromI1(CHAR cIn, double* pdblOut)
2897
{
2898
	TRACE("( %c, %p ), stub\n", cIn, pdblOut );
2899 2900 2901 2902 2903 2904 2905

	*pdblOut = (double) cIn;

	return S_OK;
}

/******************************************************************************
2906
 *		VarR8FromUI2		[OLEAUT32.218]
2907
 */
2908
HRESULT WINAPI VarR8FromUI2(USHORT uiIn, double* pdblOut)
2909
{
2910
	TRACE("( %d, %p ), stub\n", uiIn, pdblOut );
2911 2912 2913 2914 2915 2916 2917

	*pdblOut = (double) uiIn;

	return S_OK;
}

/******************************************************************************
2918
 *		VarR8FromUI4		[OLEAUT32.219]
2919
 */
2920
HRESULT WINAPI VarR8FromUI4(ULONG ulIn, double* pdblOut)
2921
{
2922
	TRACE("( %ld, %p ), stub\n", ulIn, pdblOut );
2923 2924 2925 2926 2927 2928 2929

	*pdblOut = (double) ulIn;

	return S_OK;
}

/******************************************************************************
2930
 *		VarR8FromStr		[OLEAUT32.84]
2931
 */
2932
HRESULT WINAPI VarR8FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, double* pdblOut)
2933 2934 2935 2936
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

2937
	TRACE("( %p, %ld, %ld, %p ), stub\n", strIn, lcid, dwFlags, pdblOut );
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
	
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	*pdblOut = dValue;

	return S_OK;
}

2961
/**********************************************************************
2962
 *              VarR8FromCy [OLEAUT32.82]
2963 2964
 * Convert currency to double
 */
2965
HRESULT WINAPI VarR8FromCy(CY cyIn, double* pdblOut) {
2966
   *pdblOut = (double)((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
2967 2968 2969 2970
   
   return S_OK;
}

2971
/******************************************************************************
2972
 *		VarDateFromUI1		[OLEAUT32.]
2973
 */
2974
HRESULT WINAPI VarDateFromUI1(BYTE bIn, DATE* pdateOut)
2975
{
2976
	TRACE("( %d, %p ), stub\n", bIn, pdateOut );
2977 2978 2979 2980 2981 2982 2983

	*pdateOut = (DATE) bIn;

	return S_OK;
}

/******************************************************************************
2984
 *		VarDateFromI2		[OLEAUT32.222]
2985
 */
2986
HRESULT WINAPI VarDateFromI2(short sIn, DATE* pdateOut)
2987
{
2988
	TRACE("( %d, %p ), stub\n", sIn, pdateOut );
2989 2990 2991 2992 2993 2994 2995

	*pdateOut = (DATE) sIn;

	return S_OK;
}

/******************************************************************************
2996
 *		VarDateFromI4		[OLEAUT32.90]
2997
 */
2998
HRESULT WINAPI VarDateFromI4(LONG lIn, DATE* pdateOut)
2999
{
3000
	TRACE("( %ld, %p ), stub\n", lIn, pdateOut );
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012

	if( lIn < DATE_MIN || lIn > DATE_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pdateOut = (DATE) lIn;

	return S_OK;
}

/******************************************************************************
3013
 *		VarDateFromR4		[OLEAUT32.91]
3014
 */
3015
HRESULT WINAPI VarDateFromR4(FLOAT fltIn, DATE* pdateOut)
3016
{
3017
    TRACE("( %f, %p ), stub\n", fltIn, pdateOut );
3018

3019
    if( ceil(fltIn) < DATE_MIN || floor(fltIn) > DATE_MAX )
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
	{
		return DISP_E_OVERFLOW;
	}

	*pdateOut = (DATE) fltIn;

	return S_OK;
}

/******************************************************************************
3030
 *		VarDateFromR8		[OLEAUT32.92]
3031
 */
3032
HRESULT WINAPI VarDateFromR8(double dblIn, DATE* pdateOut)
3033
{
3034
    TRACE("( %f, %p ), stub\n", dblIn, pdateOut );
3035

3036
	if( ceil(dblIn) < DATE_MIN || floor(dblIn) > DATE_MAX )
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
	{
		return DISP_E_OVERFLOW;
	}

	*pdateOut = (DATE) dblIn;

	return S_OK;
}

/******************************************************************************
3047
 *		VarDateFromStr		[OLEAUT32.94]
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
 * The string representing the date is composed of two parts, a date and time.
 *
 * The format of the time is has follows:
 * hh[:mm][:ss][AM|PM]
 * Whitespace can be inserted anywhere between these tokens.  A whitespace consists
 * of space and/or tab characters, which are ignored.
 *
 * The formats for the date part are has follows:
 * mm/[dd/][yy]yy 
 * [dd/]mm/[yy]yy
 * [yy]yy/mm/dd 
 * January dd[,] [yy]yy
 * dd January [yy]yy
 * [yy]yy January dd
 * Whitespace can be inserted anywhere between these tokens.
 *
 * The formats for the date and time string are has follows.
 * date[whitespace][time] 
 * [time][whitespace]date
 *
 * These are the only characters allowed in a string representing a date and time:
 * [A-Z] [a-z] [0-9] ':' '-' '/' ',' ' ' '\t'
 */
3071
HRESULT WINAPI VarDateFromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, DATE* pdateOut)
3072 3073
{
	HRESULT ret = S_OK;
3074 3075
    struct tm TM = { 0,0,0,0,0,0,0,0,0 };

3076
    TRACE("( %p, %lx, %lx, %p ), stub\n", strIn, lcid, dwFlags, pdateOut );
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088

    if( DateTimeStringToTm( strIn, lcid, &TM ) )
    {
        if( TmToDATE( &TM, pdateOut ) == FALSE )
        {
            ret = E_INVALIDARG;
        }
    }
    else
    {
        ret = DISP_E_TYPEMISMATCH;
    }
3089 3090 3091 3092 3093 3094


	return ret;
}

/******************************************************************************
3095
 *		VarDateFromI1		[OLEAUT32.221]
3096
 */
3097
HRESULT WINAPI VarDateFromI1(CHAR cIn, DATE* pdateOut)
3098
{
3099
	TRACE("( %c, %p ), stub\n", cIn, pdateOut );
3100 3101 3102 3103 3104 3105 3106

	*pdateOut = (DATE) cIn;

	return S_OK;
}

/******************************************************************************
3107
 *		VarDateFromUI2		[OLEAUT32.222]
3108
 */
3109
HRESULT WINAPI VarDateFromUI2(USHORT uiIn, DATE* pdateOut)
3110
{
3111
	TRACE("( %d, %p ), stub\n", uiIn, pdateOut );
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123

	if( uiIn > DATE_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pdateOut = (DATE) uiIn;

	return S_OK;
}

/******************************************************************************
3124
 *		VarDateFromUI4		[OLEAUT32.223]
3125
 */
3126
HRESULT WINAPI VarDateFromUI4(ULONG ulIn, DATE* pdateOut)
3127
{
3128
	TRACE("( %ld, %p ), stub\n", ulIn, pdateOut );
3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140

	if( ulIn < DATE_MIN || ulIn > DATE_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pdateOut = (DATE) ulIn;

	return S_OK;
}

/******************************************************************************
3141
 *		VarDateFromBool		[OLEAUT32.96]
3142
 */
3143
HRESULT WINAPI VarDateFromBool(VARIANT_BOOL boolIn, DATE* pdateOut)
3144
{
3145
	TRACE("( %d, %p ), stub\n", boolIn, pdateOut );
3146 3147 3148 3149 3150 3151

	*pdateOut = (DATE) boolIn;

	return S_OK;
}

3152
/**********************************************************************
3153
 *              VarDateFromCy [OLEAUT32.93]
3154 3155
 * Convert currency to date
 */
3156
HRESULT WINAPI VarDateFromCy(CY cyIn, DATE* pdateOut) {
3157
   *pdateOut = (DATE)((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
3158 3159 3160 3161

   if (*pdateOut > DATE_MAX || *pdateOut < DATE_MIN) return DISP_E_TYPEMISMATCH;
   return S_OK;
}
3162 3163

/******************************************************************************
3164
 *		VarBstrFromUI1		[OLEAUT32.108]
3165
 */
3166
HRESULT WINAPI VarBstrFromUI1(BYTE bVal, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3167
{
3168
	TRACE("( %d, %ld, %ld, %p ), stub\n", bVal, lcid, dwFlags, pbstrOut );
3169 3170 3171 3172 3173 3174 3175 3176
	sprintf( pBuffer, "%d", bVal );

	*pbstrOut =  StringDupAtoBstr( pBuffer );
	
	return S_OK;
}

/******************************************************************************
3177
 *		VarBstrFromI2		[OLEAUT32.109]
3178
 */
3179
HRESULT WINAPI VarBstrFromI2(short iVal, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3180
{
3181
	TRACE("( %d, %ld, %ld, %p ), stub\n", iVal, lcid, dwFlags, pbstrOut );
3182 3183 3184 3185 3186 3187 3188
	sprintf( pBuffer, "%d", iVal );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3189
 *		VarBstrFromI4		[OLEAUT32.110]
3190
 */
3191
HRESULT WINAPI VarBstrFromI4(LONG lIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3192
{
3193
	TRACE("( %ld, %ld, %ld, %p ), stub\n", lIn, lcid, dwFlags, pbstrOut );
3194 3195 3196 3197 3198 3199 3200 3201

	sprintf( pBuffer, "%ld", lIn );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3202
 *		VarBstrFromR4		[OLEAUT32.111]
3203
 */
3204
HRESULT WINAPI VarBstrFromR4(FLOAT fltIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3205
{
3206
	TRACE("( %f, %ld, %ld, %p ), stub\n", fltIn, lcid, dwFlags, pbstrOut );
3207 3208 3209 3210 3211 3212 3213 3214

	sprintf( pBuffer, "%.7g", fltIn );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3215
 *		VarBstrFromR8		[OLEAUT32.112]
3216
 */
3217
HRESULT WINAPI VarBstrFromR8(double dblIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3218
{
3219
	TRACE("( %f, %ld, %ld, %p ), stub\n", dblIn, lcid, dwFlags, pbstrOut );
3220 3221 3222 3223 3224 3225 3226

	sprintf( pBuffer, "%.15g", dblIn );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

3227 3228 3229 3230 3231 3232 3233 3234 3235
/******************************************************************************
 *    VarBstrFromCy   [OLEAUT32.113]
 */
HRESULT WINAPI VarBstrFromCy(CY cyIn, LCID lcid, ULONG dwFlags, BSTR *pbstrOut) {
				/* FIXME */
	return E_NOTIMPL;
}

 
3236
/******************************************************************************
3237
 *		VarBstrFromDate		[OLEAUT32.114]
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
 *
 * The date is implemented using an 8 byte floating-point number.
 * Days are represented by whole numbers increments starting with 0.00 has
 * being December 30 1899, midnight.
 * The hours are expressed as the fractional part of the number.
 * December 30 1899 at midnight = 0.00
 * January 1 1900 at midnight = 2.00
 * January 4 1900 at 6 AM = 5.25
 * January 4 1900 at noon = 5.50
 * December 29 1899 at midnight = -1.00
 * December 18 1899 at midnight = -12.00
 * December 18 1899 at 6AM = -12.25
 * December 18 1899 at 6PM = -12.75
 * December 19 1899 at midnight = -11.00
 * The tm structure is as follows:
 * struct tm {
 *		  int tm_sec;	   seconds after the minute - [0,59]
 *		  int tm_min;	   minutes after the hour - [0,59]
 *		  int tm_hour;	   hours since midnight - [0,23]
 *		  int tm_mday;	   day of the month - [1,31]
 *		  int tm_mon;	   months since January - [0,11]
3259
 *		  int tm_year;	   years
3260 3261 3262 3263 3264
 *		  int tm_wday;	   days since Sunday - [0,6]
 *		  int tm_yday;	   days since January 1 - [0,365]
 *		  int tm_isdst;    daylight savings time flag
 *		  };
 */
3265
HRESULT WINAPI VarBstrFromDate(DATE dateIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3266 3267 3268
{
		struct tm TM = {0,0,0,0,0,0,0,0,0};
	 
3269
    TRACE("( %f, %ld, %ld, %p ), stub\n", dateIn, lcid, dwFlags, pbstrOut );
3270

3271
    if( DateToTm( dateIn, lcid, &TM ) == FALSE )
3272
			{
3273
        return E_INVALIDARG;
3274 3275
		}

3276
    if( dwFlags & VAR_DATEVALUEONLY )
3277
			strftime( pBuffer, BUFFER_MAX, "%x", &TM );
3278
    else if( dwFlags & VAR_TIMEVALUEONLY )
3279 3280
			strftime( pBuffer, BUFFER_MAX, "%X", &TM );
		else
3281
        strftime( pBuffer, BUFFER_MAX, "%x %X", &TM );
3282 3283 3284 3285 3286 3287 3288

		*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3289
 *		VarBstrFromBool		[OLEAUT32.116]
3290
 */
3291
HRESULT WINAPI VarBstrFromBool(VARIANT_BOOL boolIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3292
{
3293
	TRACE("( %d, %ld, %ld, %p ), stub\n", boolIn, lcid, dwFlags, pbstrOut );
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309

	if( boolIn == VARIANT_FALSE )
	{
		sprintf( pBuffer, "False" );
	}
	else
	{
		sprintf( pBuffer, "True" );
	}

	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3310
 *		VarBstrFromI1		[OLEAUT32.229]
3311
 */
3312
HRESULT WINAPI VarBstrFromI1(CHAR cIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3313
{
3314
	TRACE("( %c, %ld, %ld, %p ), stub\n", cIn, lcid, dwFlags, pbstrOut );
3315 3316 3317 3318 3319 3320 3321
	sprintf( pBuffer, "%d", cIn );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3322
 *		VarBstrFromUI2		[OLEAUT32.230]
3323
 */
3324
HRESULT WINAPI VarBstrFromUI2(USHORT uiIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3325
{
3326
	TRACE("( %d, %ld, %ld, %p ), stub\n", uiIn, lcid, dwFlags, pbstrOut );
3327 3328 3329 3330 3331 3332 3333
	sprintf( pBuffer, "%d", uiIn );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3334
 *		VarBstrFromUI4		[OLEAUT32.231]
3335
 */
3336
HRESULT WINAPI VarBstrFromUI4(ULONG ulIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
3337
{
3338
	TRACE("( %ld, %ld, %ld, %p ), stub\n", ulIn, lcid, dwFlags, pbstrOut );
3339 3340 3341 3342 3343 3344 3345
	sprintf( pBuffer, "%ld", ulIn );
	*pbstrOut = StringDupAtoBstr( pBuffer );

	return S_OK;
}

/******************************************************************************
3346
 *		VarBoolFromUI1		[OLEAUT32.118]
3347
 */
3348
HRESULT WINAPI VarBoolFromUI1(BYTE bIn, VARIANT_BOOL* pboolOut)
3349
{
3350
	TRACE("( %d, %p ), stub\n", bIn, pboolOut );
3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364

	if( bIn == 0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3365
 *		VarBoolFromI2		[OLEAUT32.119]
3366
 */
3367
HRESULT WINAPI VarBoolFromI2(short sIn, VARIANT_BOOL* pboolOut)
3368
{
3369
	TRACE("( %d, %p ), stub\n", sIn, pboolOut );
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383

	if( sIn == 0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3384
 *		VarBoolFromI4		[OLEAUT32.120]
3385
 */
3386
HRESULT WINAPI VarBoolFromI4(LONG lIn, VARIANT_BOOL* pboolOut)
3387
{
3388
	TRACE("( %ld, %p ), stub\n", lIn, pboolOut );
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402

	if( lIn == 0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3403
 *		VarBoolFromR4		[OLEAUT32.121]
3404
 */
3405
HRESULT WINAPI VarBoolFromR4(FLOAT fltIn, VARIANT_BOOL* pboolOut)
3406
{
3407
	TRACE("( %f, %p ), stub\n", fltIn, pboolOut );
3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421

	if( fltIn == 0.0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3422
 *		VarBoolFromR8		[OLEAUT32.122]
3423
 */
3424
HRESULT WINAPI VarBoolFromR8(double dblIn, VARIANT_BOOL* pboolOut)
3425
{
3426
	TRACE("( %f, %p ), stub\n", dblIn, pboolOut );
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440

	if( dblIn == 0.0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3441
 *		VarBoolFromDate		[OLEAUT32.123]
3442
 */
3443
HRESULT WINAPI VarBoolFromDate(DATE dateIn, VARIANT_BOOL* pboolOut)
3444
{
3445
	TRACE("( %f, %p ), stub\n", dateIn, pboolOut );
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459

	if( dateIn == 0.0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3460
 *		VarBoolFromStr		[OLEAUT32.125]
3461
 */
3462
HRESULT WINAPI VarBoolFromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, VARIANT_BOOL* pboolOut)
3463 3464 3465 3466
{
	HRESULT ret = S_OK;
	char* pNewString = NULL;

3467
	TRACE("( %p, %ld, %ld, %p ), stub\n", strIn, lcid, dwFlags, pboolOut );
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490

    pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );

	if( pNewString == NULL || strlen( pNewString ) == 0 )
	{
		ret = DISP_E_TYPEMISMATCH;
	}

	if( ret == S_OK )
	{
		if( strncasecmp( pNewString, "True", strlen( pNewString ) ) == 0 )
		{
			*pboolOut = VARIANT_TRUE;
		}
		else if( strncasecmp( pNewString, "False", strlen( pNewString ) ) == 0 )
		{
			*pboolOut = VARIANT_FALSE;
		}
		else
		{
			/* Try converting the string to a floating point number.
			 */
			double dValue = 0.0;
3491
			HRESULT res = VarR8FromStr( strIn, lcid, dwFlags, &dValue );
3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
			if( res != S_OK )
			{
				ret = DISP_E_TYPEMISMATCH;
			}
			else if( dValue == 0.0 )
			{
				*pboolOut = VARIANT_FALSE;
			}
			else
			{
				*pboolOut = VARIANT_TRUE;
			}
		}
	}

	HeapFree( GetProcessHeap(), 0, pNewString );
	
	return ret;
}

/******************************************************************************
3513
 *		VarBoolFromI1		[OLEAUT32.233]
3514
 */
3515
HRESULT WINAPI VarBoolFromI1(CHAR cIn, VARIANT_BOOL* pboolOut)
3516
{
3517
	TRACE("( %c, %p ), stub\n", cIn, pboolOut );
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531

	if( cIn == 0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3532
 *		VarBoolFromUI2		[OLEAUT32.234]
3533
 */
3534
HRESULT WINAPI VarBoolFromUI2(USHORT uiIn, VARIANT_BOOL* pboolOut)
3535
{
3536
	TRACE("( %d, %p ), stub\n", uiIn, pboolOut );
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550

	if( uiIn == 0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

/******************************************************************************
3551
 *		VarBoolFromUI4		[OLEAUT32.235]
3552
 */
3553
HRESULT WINAPI VarBoolFromUI4(ULONG ulIn, VARIANT_BOOL* pboolOut)
3554
{
3555
	TRACE("( %ld, %p ), stub\n", ulIn, pboolOut );
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568

	if( ulIn == 0 )
	{
		*pboolOut = VARIANT_FALSE;
	}
	else
	{
		*pboolOut = VARIANT_TRUE;
	}

	return S_OK;
}

3569
/**********************************************************************
3570
 *              VarBoolFromCy [OLEAUT32.124]
3571 3572
 * Convert currency to boolean
 */
3573
HRESULT WINAPI VarBoolFromCy(CY cyIn, VARIANT_BOOL* pboolOut) {
3574
      if (cyIn.s.Hi || cyIn.s.Lo) *pboolOut = -1;
3575 3576 3577 3578 3579
      else *pboolOut = 0;
      
      return S_OK;
}

3580
/******************************************************************************
3581
 *		VarI1FromUI1		[OLEAUT32.244]
3582
 */
3583
HRESULT WINAPI VarI1FromUI1(BYTE bIn, CHAR* pcOut)
3584
{
3585
	TRACE("( %d, %p ), stub\n", bIn, pcOut );
3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599

	/* Check range of value.
	 */
	if( bIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) bIn;

	return S_OK;
}

/******************************************************************************
3600
 *		VarI1FromI2		[OLEAUT32.245]
3601
 */
3602
HRESULT WINAPI VarI1FromI2(short uiIn, CHAR* pcOut)
3603
{
3604
	TRACE("( %d, %p ), stub\n", uiIn, pcOut );
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616

	if( uiIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) uiIn;

	return S_OK;
}

/******************************************************************************
3617
 *		VarI1FromI4		[OLEAUT32.246]
3618
 */
3619
HRESULT WINAPI VarI1FromI4(LONG lIn, CHAR* pcOut)
3620
{
3621
	TRACE("( %ld, %p ), stub\n", lIn, pcOut );
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633

	if( lIn < CHAR_MIN || lIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) lIn;

	return S_OK;
}

/******************************************************************************
3634
 *		VarI1FromR4		[OLEAUT32.247]
3635
 */
3636
HRESULT WINAPI VarI1FromR4(FLOAT fltIn, CHAR* pcOut)
3637
{
3638
	TRACE("( %f, %p ), stub\n", fltIn, pcOut );
3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651

    fltIn = round( fltIn );
	if( fltIn < CHAR_MIN || fltIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) fltIn;

	return S_OK;
}

/******************************************************************************
3652
 *		VarI1FromR8		[OLEAUT32.248]
3653
 */
3654
HRESULT WINAPI VarI1FromR8(double dblIn, CHAR* pcOut)
3655
{
3656
	TRACE("( %f, %p ), stub\n", dblIn, pcOut );
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669

    dblIn = round( dblIn );
    if( dblIn < CHAR_MIN || dblIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) dblIn;

	return S_OK;
}

/******************************************************************************
3670
 *		VarI1FromDate		[OLEAUT32.249]
3671
 */
3672
HRESULT WINAPI VarI1FromDate(DATE dateIn, CHAR* pcOut)
3673
{
3674
	TRACE("( %f, %p ), stub\n", dateIn, pcOut );
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687

    dateIn = round( dateIn );
	if( dateIn < CHAR_MIN || dateIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) dateIn;

	return S_OK;
}

/******************************************************************************
3688
 *		VarI1FromStr		[OLEAUT32.251]
3689
 */
3690
HRESULT WINAPI VarI1FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, CHAR* pcOut)
3691 3692 3693 3694
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

3695
	TRACE("( %p, %ld, %ld, %p ), stub\n", strIn, lcid, dwFlags, pcOut );
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
  
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	/* Check range of value.
     */
    dValue = round( dValue );
	if( dValue < CHAR_MIN || dValue > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) dValue;

	return S_OK;
}

/******************************************************************************
3728
 *		VarI1FromBool		[OLEAUT32.253]
3729
 */
3730
HRESULT WINAPI VarI1FromBool(VARIANT_BOOL boolIn, CHAR* pcOut)
3731
{
3732
	TRACE("( %d, %p ), stub\n", boolIn, pcOut );
3733 3734 3735 3736 3737 3738 3739

	*pcOut = (CHAR) boolIn;

	return S_OK;
}

/******************************************************************************
3740
 *		VarI1FromUI2		[OLEAUT32.254]
3741
 */
3742
HRESULT WINAPI VarI1FromUI2(USHORT uiIn, CHAR* pcOut)
3743
{
3744
	TRACE("( %d, %p ), stub\n", uiIn, pcOut );
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756

	if( uiIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) uiIn;

	return S_OK;
}

/******************************************************************************
3757
 *		VarI1FromUI4		[OLEAUT32.255]
3758
 */
3759
HRESULT WINAPI VarI1FromUI4(ULONG ulIn, CHAR* pcOut)
3760
{
3761
	TRACE("( %ld, %p ), stub\n", ulIn, pcOut );
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772

	if( ulIn > CHAR_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pcOut = (CHAR) ulIn;

	return S_OK;
}

3773
/**********************************************************************
3774
 *              VarI1FromCy [OLEAUT32.250]
3775 3776
 * Convert currency to signed char
 */
3777
HRESULT WINAPI VarI1FromCy(CY cyIn, CHAR* pcOut) {
3778
   double t = round((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
3779 3780 3781 3782 3783 3784 3785
   
   if (t > CHAR_MAX || t < CHAR_MIN) return DISP_E_OVERFLOW;
   
   *pcOut = (CHAR)t;
   return S_OK;
}

3786
/******************************************************************************
3787
 *		VarUI2FromUI1		[OLEAUT32.257]
3788
 */
3789
HRESULT WINAPI VarUI2FromUI1(BYTE bIn, USHORT* puiOut)
3790
{
3791
	TRACE("( %d, %p ), stub\n", bIn, puiOut );
3792 3793 3794 3795 3796 3797 3798

	*puiOut = (USHORT) bIn;

	return S_OK;
}

/******************************************************************************
3799
 *		VarUI2FromI2		[OLEAUT32.258]
3800
 */
3801
HRESULT WINAPI VarUI2FromI2(short uiIn, USHORT* puiOut)
3802
{
3803
	TRACE("( %d, %p ), stub\n", uiIn, puiOut );
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815

	if( uiIn < UI2_MIN )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) uiIn;

	return S_OK;
}

/******************************************************************************
3816
 *		VarUI2FromI4		[OLEAUT32.259]
3817
 */
3818
HRESULT WINAPI VarUI2FromI4(LONG lIn, USHORT* puiOut)
3819
{
3820
	TRACE("( %ld, %p ), stub\n", lIn, puiOut );
3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832

	if( lIn < UI2_MIN || lIn > UI2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) lIn;

	return S_OK;
}

/******************************************************************************
3833
 *		VarUI2FromR4		[OLEAUT32.260]
3834
 */
3835
HRESULT WINAPI VarUI2FromR4(FLOAT fltIn, USHORT* puiOut)
3836
{
3837
	TRACE("( %f, %p ), stub\n", fltIn, puiOut );
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850

    fltIn = round( fltIn );
	if( fltIn < UI2_MIN || fltIn > UI2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) fltIn;

	return S_OK;
}

/******************************************************************************
3851
 *		VarUI2FromR8		[OLEAUT32.261]
3852
 */
3853
HRESULT WINAPI VarUI2FromR8(double dblIn, USHORT* puiOut)
3854
{
3855
	TRACE("( %f, %p ), stub\n", dblIn, puiOut );
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868

    dblIn = round( dblIn );
    if( dblIn < UI2_MIN || dblIn > UI2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) dblIn;

	return S_OK;
}

/******************************************************************************
3869
 *		VarUI2FromDate		[OLEAUT32.262]
3870
 */
3871
HRESULT WINAPI VarUI2FromDate(DATE dateIn, USHORT* puiOut)
3872
{
3873
	TRACE("( %f, %p ), stub\n", dateIn, puiOut );
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886

    dateIn = round( dateIn );
	if( dateIn < UI2_MIN || dateIn > UI2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) dateIn;

	return S_OK;
}

/******************************************************************************
3887
 *		VarUI2FromStr		[OLEAUT32.264]
3888
 */
3889
HRESULT WINAPI VarUI2FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, USHORT* puiOut)
3890 3891 3892 3893
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

3894
	TRACE("( %p, %ld, %ld, %p ), stub\n", strIn, lcid, dwFlags, puiOut );
3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
  
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	/* Check range of value.
     */
    dValue = round( dValue );
	if( dValue < UI2_MIN || dValue > UI2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) dValue;

	return S_OK;
}

/******************************************************************************
3927
 *		VarUI2FromBool		[OLEAUT32.266]
3928
 */
3929
HRESULT WINAPI VarUI2FromBool(VARIANT_BOOL boolIn, USHORT* puiOut)
3930
{
3931
	TRACE("( %d, %p ), stub\n", boolIn, puiOut );
3932 3933 3934 3935 3936 3937 3938

	*puiOut = (USHORT) boolIn;

	return S_OK;
}

/******************************************************************************
3939
 *		VarUI2FromI1		[OLEAUT32.267]
3940
 */
3941
HRESULT WINAPI VarUI2FromI1(CHAR cIn, USHORT* puiOut)
3942
{
3943
	TRACE("( %c, %p ), stub\n", cIn, puiOut );
3944 3945 3946 3947 3948 3949 3950

	*puiOut = (USHORT) cIn;

	return S_OK;
}

/******************************************************************************
3951
 *		VarUI2FromUI4		[OLEAUT32.268]
3952
 */
3953
HRESULT WINAPI VarUI2FromUI4(ULONG ulIn, USHORT* puiOut)
3954
{
3955
	TRACE("( %ld, %p ), stub\n", ulIn, puiOut );
3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967

	if( ulIn < UI2_MIN || ulIn > UI2_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*puiOut = (USHORT) ulIn;

	return S_OK;
}

/******************************************************************************
3968
 *		VarUI4FromStr		[OLEAUT32.277]
3969
 */
3970
HRESULT WINAPI VarUI4FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, ULONG* pulOut)
3971 3972 3973 3974
{
	double dValue = 0.0;
	LPSTR pNewString = NULL;

3975
	TRACE("( %p, %ld, %ld, %p ), stub\n", strIn, lcid, dwFlags, pulOut );
3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006

	/* Check if we have a valid argument
	 */
	pNewString = HEAP_strdupWtoA( GetProcessHeap(), 0, strIn );
	RemoveCharacterFromString( pNewString, "," );
	if( IsValidRealString( pNewString ) == FALSE )
	{
		return DISP_E_TYPEMISMATCH;
	}

	/* Convert the valid string to a floating point number.
	 */
	dValue = atof( pNewString );
  
	/* We don't need the string anymore so free it.
	 */
	HeapFree( GetProcessHeap(), 0, pNewString );

	/* Check range of value.
     */
    dValue = round( dValue );
	if( dValue < UI4_MIN || dValue > UI4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pulOut = (ULONG) dValue;

	return S_OK;
}

4007
/**********************************************************************
4008
 *              VarUI2FromCy [OLEAUT32.263]
4009 4010
 * Convert currency to unsigned short
 */
4011
HRESULT WINAPI VarUI2FromCy(CY cyIn, USHORT* pusOut) {
4012
   double t = round((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
4013 4014 4015 4016 4017 4018 4019 4020
   
   if (t > UI2_MAX || t < UI2_MIN) return DISP_E_OVERFLOW;
      
   *pusOut = (USHORT)t;
   
   return S_OK;
}

4021
/******************************************************************************
4022
 *		VarUI4FromUI1		[OLEAUT32.270]
4023
 */
4024
HRESULT WINAPI VarUI4FromUI1(BYTE bIn, ULONG* pulOut)
4025
{
4026
	TRACE("( %d, %p ), stub\n", bIn, pulOut );
4027 4028 4029 4030 4031 4032 4033

	*pulOut = (USHORT) bIn;

	return S_OK;
}

/******************************************************************************
4034
 *		VarUI4FromI2		[OLEAUT32.271]
4035
 */
4036
HRESULT WINAPI VarUI4FromI2(short uiIn, ULONG* pulOut)
4037
{
4038
	TRACE("( %d, %p ), stub\n", uiIn, pulOut );
4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050

	if( uiIn < UI4_MIN )
	{
		return DISP_E_OVERFLOW;
	}

	*pulOut = (ULONG) uiIn;

	return S_OK;
}

/******************************************************************************
4051
 *		VarUI4FromI4		[OLEAUT32.272]
4052
 */
4053
HRESULT WINAPI VarUI4FromI4(LONG lIn, ULONG* pulOut)
4054
{
4055
	TRACE("( %ld, %p ), stub\n", lIn, pulOut );
4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067

	if( lIn < UI4_MIN )
	{
		return DISP_E_OVERFLOW;
	}

	*pulOut = (ULONG) lIn;

	return S_OK;
}

/******************************************************************************
4068
 *		VarUI4FromR4		[OLEAUT32.273]
4069
 */
4070
HRESULT WINAPI VarUI4FromR4(FLOAT fltIn, ULONG* pulOut)
4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
{
    fltIn = round( fltIn );
    if( fltIn < UI4_MIN || fltIn > UI4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pulOut = (ULONG) fltIn;

	return S_OK;
}

/******************************************************************************
4084
 *		VarUI4FromR8		[OLEAUT32.274]
4085
 */
4086
HRESULT WINAPI VarUI4FromR8(double dblIn, ULONG* pulOut)
4087
{
4088
	TRACE("( %f, %p ), stub\n", dblIn, pulOut );
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101

    dblIn = round( dblIn );
	if( dblIn < UI4_MIN || dblIn > UI4_MAX )
	{
		return DISP_E_OVERFLOW;
    }

	*pulOut = (ULONG) dblIn;

	return S_OK;
}

/******************************************************************************
4102
 *		VarUI4FromDate		[OLEAUT32.275]
4103
 */
4104
HRESULT WINAPI VarUI4FromDate(DATE dateIn, ULONG* pulOut)
4105
{
4106
	TRACE("( %f, %p ), stub\n", dateIn, pulOut );
4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119

    dateIn = round( dateIn );
    if( dateIn < UI4_MIN || dateIn > UI4_MAX )
	{
		return DISP_E_OVERFLOW;
	}

	*pulOut = (ULONG) dateIn;

	return S_OK;
}

/******************************************************************************
4120
 *		VarUI4FromBool		[OLEAUT32.279]
4121
 */
4122
HRESULT WINAPI VarUI4FromBool(VARIANT_BOOL boolIn, ULONG* pulOut)
4123
{
4124
	TRACE("( %d, %p ), stub\n", boolIn, pulOut );
4125 4126 4127 4128 4129 4130 4131

	*pulOut = (ULONG) boolIn;

	return S_OK;
}

/******************************************************************************
4132
 *		VarUI4FromI1		[OLEAUT32.280]
4133
 */
4134
HRESULT WINAPI VarUI4FromI1(CHAR cIn, ULONG* pulOut)
4135
{
4136
	TRACE("( %c, %p ), stub\n", cIn, pulOut );
4137 4138 4139 4140 4141 4142 4143

	*pulOut = (ULONG) cIn;

	return S_OK;
}

/******************************************************************************
4144
 *		VarUI4FromUI2		[OLEAUT32.281]
4145
 */
4146
HRESULT WINAPI VarUI4FromUI2(USHORT uiIn, ULONG* pulOut)
4147
{
4148
	TRACE("( %d, %p ), stub\n", uiIn, pulOut );
4149 4150 4151 4152 4153 4154

	*pulOut = (ULONG) uiIn;

	return S_OK;
}

4155
/**********************************************************************
4156
 *              VarUI4FromCy [OLEAUT32.276]
4157 4158
 * Convert currency to unsigned long
 */
4159
HRESULT WINAPI VarUI4FromCy(CY cyIn, ULONG* pulOut) {
4160
   double t = round((((double)cyIn.s.Hi * 4294967296.0) + (double)cyIn.s.Lo) / 10000);
4161 4162 4163 4164 4165 4166 4167 4168 4169
   
   if (t > UI4_MAX || t < UI4_MIN) return DISP_E_OVERFLOW;

   *pulOut = (ULONG)t;

   return S_OK;
}

/**********************************************************************
4170
 *              VarCyFromUI1 [OLEAUT32.98]
4171 4172
 * Convert unsigned char to currency
 */
4173
HRESULT WINAPI VarCyFromUI1(BYTE bIn, CY* pcyOut) {
4174 4175
   pcyOut->s.Hi = 0;
   pcyOut->s.Lo = ((ULONG)bIn) * 10000;
4176 4177 4178 4179 4180
   
   return S_OK;
}

/**********************************************************************
4181
 *              VarCyFromI2 [OLEAUT32.99]
4182 4183
 * Convert signed short to currency
 */
4184
HRESULT WINAPI VarCyFromI2(short sIn, CY* pcyOut) {
4185 4186 4187
   if (sIn < 0) pcyOut->s.Hi = -1;
   else pcyOut->s.Hi = 0;
   pcyOut->s.Lo = ((ULONG)sIn) * 10000;
4188 4189 4190 4191 4192
   
   return S_OK;
}

/**********************************************************************
4193
 *              VarCyFromI4 [OLEAUT32.100]
4194 4195
 * Convert signed long to currency
 */
4196
HRESULT WINAPI VarCyFromI4(LONG lIn, CY* pcyOut) {
4197
      double t = (double)lIn * (double)10000;
4198 4199 4200
      pcyOut->s.Hi = (LONG)(t / (double)4294967296.0);
      pcyOut->s.Lo = (ULONG)fmod(t, (double)4294967296.0);
      if (lIn < 0) pcyOut->s.Hi--;
4201 4202 4203 4204 4205
   
      return S_OK;
}

/**********************************************************************
4206
 *              VarCyFromR4 [OLEAUT32.101]
4207 4208
 * Convert float to currency
 */
4209
HRESULT WINAPI VarCyFromR4(FLOAT fltIn, CY* pcyOut) {
4210
   double t = round((double)fltIn * (double)10000);
4211 4212 4213
   pcyOut->s.Hi = (LONG)(t / (double)4294967296.0);
   pcyOut->s.Lo = (ULONG)fmod(t, (double)4294967296.0);
   if (fltIn < 0) pcyOut->s.Hi--;
4214 4215 4216 4217 4218
   
   return S_OK;
}

/**********************************************************************
4219
 *              VarCyFromR8 [OLEAUT32.102]
4220 4221
 * Convert double to currency
 */
4222
HRESULT WINAPI VarCyFromR8(double dblIn, CY* pcyOut) {
4223
   double t = round(dblIn * (double)10000);
4224 4225 4226
   pcyOut->s.Hi = (LONG)(t / (double)4294967296.0);
   pcyOut->s.Lo = (ULONG)fmod(t, (double)4294967296.0);
   if (dblIn < 0) pcyOut->s.Hi--;
4227 4228 4229 4230 4231

   return S_OK;
}

/**********************************************************************
4232
 *              VarCyFromDate [OLEAUT32.103]
4233 4234
 * Convert date to currency
 */
4235
HRESULT WINAPI VarCyFromDate(DATE dateIn, CY* pcyOut) {
4236
   double t = round((double)dateIn * (double)10000);
4237 4238 4239
   pcyOut->s.Hi = (LONG)(t / (double)4294967296.0);
   pcyOut->s.Lo = (ULONG)fmod(t, (double)4294967296.0);
   if (dateIn < 0) pcyOut->s.Hi--;
4240 4241 4242 4243

   return S_OK;
}

4244
/**********************************************************************
4245
 *              VarCyFromStr [OLEAUT32.104]
4246 4247 4248 4249 4250 4251 4252
 */
HRESULT WINAPI VarCyFromStr(OLECHAR *strIn, LCID lcid, ULONG dwFlags, CY *pcyOut) {
				/* FIXME */
		return E_NOTIMPL;
}

 
4253
/**********************************************************************
4254
 *              VarCyFromBool [OLEAUT32.106]
4255 4256
 * Convert boolean to currency
 */
4257
HRESULT WINAPI VarCyFromBool(VARIANT_BOOL boolIn, CY* pcyOut) {
4258 4259 4260
   if (boolIn < 0) pcyOut->s.Hi = -1;
   else pcyOut->s.Hi = 0;
   pcyOut->s.Lo = (ULONG)boolIn * (ULONG)10000;
4261 4262 4263 4264 4265
   
   return S_OK;
}

/**********************************************************************
4266
 *              VarCyFromI1 [OLEAUT32.225]
4267 4268
 * Convert signed char to currency
 */
4269
HRESULT WINAPI VarCyFromI1(CHAR cIn, CY* pcyOut) {
4270 4271 4272
   if (cIn < 0) pcyOut->s.Hi = -1;
   else pcyOut->s.Hi = 0;
   pcyOut->s.Lo = (ULONG)cIn * (ULONG)10000;
4273 4274 4275 4276 4277
   
   return S_OK;
}

/**********************************************************************
4278
 *              VarCyFromUI2 [OLEAUT32.226]
4279 4280
 * Convert unsigned short to currency
 */
4281
HRESULT WINAPI VarCyFromUI2(USHORT usIn, CY* pcyOut) {
4282 4283
   pcyOut->s.Hi = 0;
   pcyOut->s.Lo = (ULONG)usIn * (ULONG)10000;
4284 4285 4286 4287 4288
   
   return S_OK;
}

/**********************************************************************
4289
 *              VarCyFromUI4 [OLEAUT32.227]
4290 4291
 * Convert unsigned long to currency
 */
4292
HRESULT WINAPI VarCyFromUI4(ULONG ulIn, CY* pcyOut) {
4293
   double t = (double)ulIn * (double)10000;
4294 4295
   pcyOut->s.Hi = (LONG)(t / (double)4294967296.0);
   pcyOut->s.Lo = (ULONG)fmod(t, (double)4294967296.0);
4296 4297 4298
      
   return S_OK;
}
4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310


/**********************************************************************
 *              DosDateTimeToVariantTime [OLEAUT32.14]
 * Convert dos representation of time to the date and time representation
 * stored in a variant.
 */
INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
                                    DATE *pvtime)
{
    struct tm t;

4311
    TRACE("( 0x%x, 0x%x, 0x%p ), stub\n", wDosDate, wDosTime, pvtime );
4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323
    
    t.tm_sec = (wDosTime & 0x001f) * 2;
    t.tm_min = (wDosTime & 0x07e0) >> 5;
    t.tm_hour = (wDosTime & 0xf800) >> 11;
    
    t.tm_mday = (wDosDate & 0x001f);
    t.tm_mon = (wDosDate & 0x01e0) >> 5;
    t.tm_year = ((wDosDate & 0xfe00) >> 9) + 1980;

    return TmToDATE( &t, pvtime );
}