cvarutil.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000,2001 by the Free Pascal development team
  5. Interface and OS-dependent part of variant support
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$ifdef HASVARIANT}
  13. Resourcestring
  14. SNoWidestrings = 'No widestrings supported';
  15. SNoInterfaces = 'No interfaces supported';
  16. Procedure NoWidestrings;
  17. begin
  18. Raise Exception.Create(SNoWideStrings);
  19. end;
  20. Procedure NoInterfaces;
  21. begin
  22. Raise Exception.Create(SNoInterfaces);
  23. end;
  24. Constructor EVariantError.CreateCode (Code : longint);
  25. begin
  26. ErrCode:=Code;
  27. end;
  28. Procedure VariantTypeMismatch;
  29. begin
  30. Raise EVariantError.CreateCode(VAR_TYPEMISMATCH);
  31. end;
  32. Function ExceptionToVariantError (E : Exception): HResult;
  33. begin
  34. If E is EoutOfMemory then
  35. Result:=VAR_OUTOFMEMORY
  36. else
  37. Result:=VAR_EXCEPTION;
  38. end;
  39. { ---------------------------------------------------------------------
  40. OS-independent functions not present in Windows
  41. ---------------------------------------------------------------------}
  42. Function VariantToSmallInt(Const VargSrc : TVarData) : SmallInt;
  43. begin
  44. With VargSrc do
  45. Case (VType and VarTypeMask) of
  46. VarSmallInt: Result:=VSmallInt;
  47. VarShortInt: Result:=VShortInt;
  48. VarInteger : Result:=VInteger;
  49. VarSingle : Result:=Round(VSingle);
  50. VarDouble : Result:=Round(VDouble);
  51. VarCurrency: Result:=Round(VCurrency);
  52. VarDate : Result:=Round(VDate);
  53. VarOleStr : NoWideStrings;
  54. VarBoolean : Result:=SmallInt(VBoolean);
  55. VarByte : Result:=VByte;
  56. VarWord : Result:=VWord;
  57. VarLongWord : Result:=VLongWord;
  58. VarInt64 : Result:=VInt64;
  59. VarQword : Result:=VQWord;
  60. else
  61. VariantTypeMismatch;
  62. end;
  63. end;
  64. Function VariantToShortInt(Const VargSrc : TVarData) : ShortInt;
  65. begin
  66. With VargSrc do
  67. Case (VType and VarTypeMask) of
  68. VarSmallInt: Result:=VSmallInt;
  69. VarShortInt: Result:=VShortInt;
  70. VarInteger : Result:=VInteger;
  71. VarSingle : Result:=Round(VSingle);
  72. VarDouble : Result:=Round(VDouble);
  73. VarCurrency: Result:=Round(VCurrency);
  74. VarDate : Result:=Round(VDate);
  75. VarOleStr : NoWideStrings;
  76. VarBoolean : Result:=SmallInt(VBoolean);
  77. VarByte : Result:=VByte;
  78. VarWord : Result:=VWord;
  79. VarLongWord : Result:=VLongWord;
  80. VarInt64 : Result:=VInt64;
  81. VarQword : Result:=VQWord;
  82. else
  83. VariantTypeMismatch;
  84. end;
  85. end;
  86. Function VariantToLongint(Const VargSrc : TVarData) : Longint;
  87. begin
  88. With VargSrc do
  89. Case (VType and VarTypeMask) of
  90. VarSmallInt: Result:=VSmallInt;
  91. VarShortInt: Result:=VShortInt;
  92. VarInteger : Result:=VInteger;
  93. VarSingle : Result:=Round(VSingle);
  94. VarDouble : Result:=Round(VDouble);
  95. VarCurrency: Result:=Round(VCurrency);
  96. VarDate : Result:=Round(VDate);
  97. VarOleStr : NoWideStrings;
  98. VarBoolean : Result:=Longint(VBoolean);
  99. VarByte : Result:=VByte;
  100. VarWord : Result:=VWord;
  101. VarLongWord : Result:=VLongWord;
  102. VarInt64 : Result:=VInt64;
  103. VarQword : Result:=VQWord;
  104. else
  105. VariantTypeMismatch;
  106. end;
  107. end;
  108. Function VariantToCardinal(Const VargSrc : TVarData) : Cardinal;
  109. begin
  110. With VargSrc do
  111. Case (VType and VarTypeMask) of
  112. VarSmallInt: Result:=VSmallInt;
  113. VarShortInt: Result:=VShortInt;
  114. VarInteger : Result:=VInteger;
  115. VarSingle : Result:=Round(VSingle);
  116. VarDouble : Result:=Round(VDouble);
  117. VarCurrency: Result:=Round(VCurrency);
  118. VarDate : Result:=Round(VDate);
  119. VarOleStr : NoWideStrings;
  120. VarBoolean : Result:=Longint(VBoolean);
  121. VarByte : Result:=VByte;
  122. VarWord : Result:=VWord;
  123. VarLongWord : Result:=VLongWord;
  124. VarInt64 : Result:=VInt64;
  125. VarQword : Result:=VQWord;
  126. else
  127. VariantTypeMismatch;
  128. end;
  129. end;
  130. Function VariantToSingle(Const VargSrc : TVarData) : Single;
  131. begin
  132. With VargSrc do
  133. Case (VType and VarTypeMask) of
  134. VarSmallInt: Result:=VSmallInt;
  135. VarShortInt: Result:=VShortInt;
  136. VarInteger : Result:=VInteger;
  137. VarSingle : Result:=VSingle;
  138. VarDouble : Result:=VDouble;
  139. VarCurrency: Result:=VCurrency;
  140. VarDate : Result:=VDate;
  141. VarOleStr : NoWideStrings;
  142. VarBoolean : Result:=Longint(VBoolean);
  143. VarByte : Result:=VByte;
  144. VarWord : Result:=VWord;
  145. VarLongWord : Result:=VLongWord;
  146. VarInt64 : Result:=VInt64;
  147. VarQword : Result:=VQWord;
  148. else
  149. VariantTypeMismatch;
  150. end;
  151. end;
  152. Function VariantToDouble(Const VargSrc : TVarData) : Double;
  153. begin
  154. With VargSrc do
  155. Case (VType and VarTypeMask) of
  156. VarSmallInt: Result:=VSmallInt;
  157. VarShortInt: Result:=VShortInt;
  158. VarInteger : Result:=VInteger;
  159. VarSingle : Result:=VSingle;
  160. VarDouble : Result:=VDouble;
  161. VarCurrency: Result:=VCurrency;
  162. VarDate : Result:=VDate;
  163. VarOleStr : NoWideStrings;
  164. VarBoolean : Result:=Longint(VBoolean);
  165. VarByte : Result:=VByte;
  166. VarWord : Result:=VWord;
  167. VarLongWord : Result:=VLongWord;
  168. VarInt64 : Result:=VInt64;
  169. VarQword : Result:=VQWord;
  170. else
  171. VariantTypeMismatch;
  172. end;
  173. end;
  174. Function VariantToCurrency(Const VargSrc : TVarData) : Currency;
  175. begin
  176. Try
  177. With VargSrc do
  178. Case (VType and VarTypeMask) of
  179. VarSmallInt: Result:=VSmallInt;
  180. VarShortInt: Result:=VShortInt;
  181. VarInteger : Result:=VInteger;
  182. VarSingle : Result:=FloatToCurr(VSingle);
  183. VarDouble : Result:=FloatToCurr(VDouble);
  184. VarCurrency: Result:=VCurrency;
  185. VarDate : Result:=FloatToCurr(VDate);
  186. VarOleStr : NoWideStrings;
  187. VarBoolean : Result:=Longint(VBoolean);
  188. VarByte : Result:=VByte;
  189. VarWord : Result:=VWord;
  190. VarLongWord : Result:=VLongWord;
  191. VarInt64 : Result:=VInt64;
  192. VarQword : Result:=VQWord;
  193. else
  194. VariantTypeMismatch;
  195. end;
  196. except
  197. On EConvertError do
  198. VariantTypeMismatch;
  199. else
  200. Raise;
  201. end;
  202. end;
  203. Function VariantToDate(Const VargSrc : TVarData) : TDateTime;
  204. begin
  205. Try
  206. With VargSrc do
  207. Case (VType and VarTypeMask) of
  208. VarSmallInt: Result:=FloatToDateTime(VSmallInt);
  209. VarShortInt: Result:=FloatToDateTime(VShortInt);
  210. VarInteger : Result:=FloatToDateTime(VInteger);
  211. VarSingle : Result:=FloatToDateTime(VSingle);
  212. VarDouble : Result:=FloatToDateTime(VDouble);
  213. VarCurrency: Result:=FloatToDateTime(VCurrency);
  214. VarDate : Result:=VDate;
  215. VarOleStr : NoWideStrings;
  216. VarBoolean : Result:=FloatToDateTime(Longint(VBoolean));
  217. VarByte : Result:=FloatToDateTime(VByte);
  218. VarWord : Result:=FloatToDateTime(VWord);
  219. VarLongWord : Result:=FloatToDateTime(VLongWord);
  220. VarInt64 : Result:=FloatToDateTime(VInt64);
  221. VarQWord : Result:=FloatToDateTime(VQword);
  222. else
  223. VariantTypeMismatch;
  224. end;
  225. except
  226. On EConvertError do
  227. VariantTypeMismatch;
  228. else
  229. Raise;
  230. end;
  231. end;
  232. Function VariantToBoolean(Const VargSrc : TVarData) : Boolean;
  233. begin
  234. With VargSrc do
  235. Case (VType and VarTypeMask) of
  236. VarSmallInt: Result:=VSmallInt<>0;
  237. VarShortInt: Result:=VShortInt<>0;
  238. VarInteger : Result:=VInteger<>0;
  239. VarSingle : Result:=VSingle<>0;
  240. VarDouble : Result:=VDouble<>0;
  241. VarCurrency: Result:=VCurrency<>0;
  242. VarDate : Result:=VDate<>0;
  243. VarOleStr : NoWideStrings;
  244. VarBoolean : Result:=VBoolean;
  245. VarByte : Result:=VByte<>0;
  246. VarWord : Result:=VWord<>0;
  247. VarLongWord : Result:=VLongWord<>0;
  248. VarInt64 : Result:=Vint64<>0;
  249. VarQword : Result:=VQWord<>0;
  250. else
  251. VariantTypeMismatch;
  252. end;
  253. end;
  254. Function VariantToByte(Const VargSrc : TVarData) : Byte;
  255. begin
  256. Try
  257. With VargSrc do
  258. Case (VType and VarTypeMask) of
  259. VarSmallInt: Result:=VSmallInt;
  260. VarShortInt: Result:=VShortInt;
  261. VarInteger : Result:=VInteger;
  262. VarSingle : Result:=Round(VSingle);
  263. VarDouble : Result:=Round(VDouble);
  264. VarCurrency: Result:=Round(VCurrency);
  265. VarDate : Result:=Round(VDate);
  266. VarOleStr : NoWideStrings;
  267. VarBoolean : Result:=Longint(VBoolean);
  268. VarByte : Result:=VByte;
  269. VarWord : Result:=VWord;
  270. VarLongWord : Result:=VLongWord;
  271. VarInt64 : Result:=Vint64;
  272. VarQword : Result:=VQWord;
  273. else
  274. VariantTypeMismatch;
  275. end;
  276. except
  277. On EConvertError do
  278. VariantTypeMismatch;
  279. else
  280. Raise;
  281. end;
  282. end;
  283. Function VariantToInt64(Const VargSrc : TVarData) : Int64;
  284. begin
  285. Try
  286. With VargSrc do
  287. Case (VType and VarTypeMask) of
  288. VarSmallInt: Result:=VSmallInt;
  289. VarShortInt: Result:=VShortInt;
  290. VarInteger : Result:=VInteger;
  291. VarSingle : Result:=Trunc(VSingle);
  292. VarDouble : Result:=Trunc(VDouble);
  293. VarCurrency: Result:=VCurrency;
  294. VarDate : Result:=Trunc(VDate);
  295. VarOleStr : NoWideStrings;
  296. VarBoolean : Result:=Longint(VBoolean);
  297. VarByte : Result:=VByte;
  298. VarWord : Result:=VWord;
  299. VarLongWord : Result:=VLongWord;
  300. VarInt64 : Result:=VInt64;
  301. VarQword : Result:=VQWord;
  302. else
  303. VariantTypeMismatch;
  304. end;
  305. except
  306. On EConvertError do
  307. VariantTypeMismatch;
  308. else
  309. Raise;
  310. end;
  311. end;
  312. Function VariantToQWord(Const VargSrc : TVarData) : QWord;
  313. begin
  314. Try
  315. With VargSrc do
  316. Case (VType and VarTypeMask) of
  317. VarSmallInt: Result:=VSmallint;
  318. VarShortInt: Result:=VShortInt;
  319. VarInteger : Result:=VInteger;
  320. VarSingle : Result:=Trunc(VSingle);
  321. VarDouble : Result:=Trunc(VDouble);
  322. VarCurrency: Result:=VCurrency;
  323. VarDate : Result:=Trunc(VDate);
  324. VarOleStr : NoWideStrings;
  325. VarBoolean : Result:=Longint(VBoolean);
  326. VarByte : Result:=VByte;
  327. VarWord : Result:=VWord;
  328. VarLongWord : Result:=VLongWord;
  329. VarInt64 : Result:=VInt64;
  330. VarQword : Result:=VQWord;
  331. else
  332. VariantTypeMismatch;
  333. end;
  334. except
  335. On EConvertError do
  336. VariantTypeMismatch;
  337. else
  338. Raise;
  339. end;
  340. end;
  341. Function VariantToWideString(Const VargSrc : TVarData) : WideString;
  342. Const
  343. BS : Array[Boolean] of WideString = ('False','True');
  344. begin
  345. Try
  346. With VargSrc do
  347. Case (VType and VarTypeMask) of
  348. VarSmallInt : Result:=IntTostr(VSmallint);
  349. VarShortInt : Result:=IntToStr(VShortInt);
  350. VarInteger : Result:=IntToStr(VInteger);
  351. VarSingle : Result:=FloatToStr(VSingle);
  352. VarDouble : Result:=FloatToStr(VDouble);
  353. VarCurrency : Result:=IntToStr(VCurrency);
  354. VarDate : Result:=DateTimeToStr(VDate);
  355. VarOleStr : Result:=WideString(Pointer(VOleStr));
  356. VarBoolean : Result:=BS[VBoolean];
  357. VarByte : Result:=IntToStr(VByte);
  358. VarWord : Result:=IntToStr(VWord);
  359. VarLongWord : Result:=IntToStr(VLongWord);
  360. VarInt64 : Result:=IntToStr(VInt64);
  361. VarQword : Result:=IntToStr(VQWord);
  362. else
  363. VariantTypeMismatch;
  364. end;
  365. except
  366. On EConvertError do
  367. VariantTypeMismatch;
  368. else
  369. Raise;
  370. end;
  371. end;
  372. Function VariantToAnsiString(Const VargSrc : TVarData) : AnsiString;
  373. begin
  374. end;
  375. Function VariantToShortString(Const VargSrc : TVarData) : ShortString;
  376. begin
  377. end;
  378. { ---------------------------------------------------------------------
  379. Some debug routines
  380. ---------------------------------------------------------------------}
  381. Procedure DumpVariant(Const VArgSrc : TVarData);
  382. begin
  383. DumpVariant(Output,VArgSrc);
  384. end;
  385. (*
  386. tvardata = packed record
  387. vtype : tvartype;
  388. case integer of
  389. 0:(res1 : word;
  390. case integer of
  391. 0:
  392. (res2,res3 : word;
  393. case word of
  394. varsmallint : (vsmallint : smallint);
  395. varinteger : (vinteger : longint);
  396. varsingle : (vsingle : single);
  397. vardouble : (vdouble : double);
  398. varcurrency : (vcurrency : currency);
  399. vardate : (vdate : tdatetime);
  400. varolestr : (volestr : pwidechar);
  401. vardispatch : (vdispatch : pointer);
  402. varerror : (verror : dword);
  403. varboolean : (vboolean : wordbool);
  404. varunknown : (vunknown : pointer);
  405. // vardecimal : ( : );
  406. varshortint : (vshortint : shortint);
  407. varbyte : (vbyte : byte);
  408. varword : (vword : word);
  409. varlongword : (vlongword : dword);
  410. varint64 : (vint64 : int64);
  411. varqword : (vqword : qword);
  412. varword64 : (vword64 : qword);
  413. varstring : (vstring : pointer);
  414. varany : (vany : pointer);
  415. vararray : (varray : pvararray);
  416. varbyref : (vpointer : pointer);
  417. );
  418. 1:
  419. (vlongs : array[0..2] of longint);
  420. );
  421. 1:(vwords : array[0..6] of word);
  422. 2:(vbytes : array[0..13] of byte);
  423. end;
  424. *)
  425. Const
  426. VarTypeStrings : Array [varEmpty..varqword] of string = (
  427. 'empty', 'null', 'smallint', 'integer', 'single', 'double',
  428. 'currency', 'date', 'olestr', 'dispatch', 'error', 'boolean',
  429. 'variant', 'unknown', 'unknown','decimal', 'shortint', 'byte', 'word',
  430. 'longword', 'int64', 'qword');
  431. Procedure DumpVariant(Var F : Text; Const VArgSrc : TVarData);
  432. begin
  433. If VArgSrc.vType in [varEmpty..varqword] then
  434. Writeln(F,'Variant has type : ',VarTypeStrings[VArgSrc.vType])
  435. else if (VArgSrc.vType=VarArray) Then
  436. begin
  437. Write(F,'Variant is array.');
  438. exit;
  439. end
  440. else if (VargSrc.vType=VarByRef) then
  441. begin
  442. Writeln(F,'Variant is by reference.');
  443. exit;
  444. end
  445. else
  446. begin
  447. Writeln(F,'Variant has unknown type: ', VargSrc.vType);
  448. Exit;
  449. end;
  450. If VArgSrc.vType<>varEmpty then
  451. With VArgSrc do
  452. begin
  453. Write(F,'Value is: ') ;
  454. Case vtype of
  455. varnull : Write(F,'Null');
  456. varsmallint : Write(F,vsmallint);
  457. varinteger : Write(F,vinteger);
  458. varsingle : Write(F,vsingle);
  459. vardouble : Write(F,vdouble);
  460. varcurrency : Write(F,vcurrency) ;
  461. vardate : Write(F,vdate) ;
  462. varolestr : Write(F,'Not supported') ;
  463. vardispatch : Write(F,'Not suppordted') ;
  464. varerror : Write(F,'Error') ;
  465. varboolean : Write(F,vboolean) ;
  466. varvariant : Write(F,'Unsupported') ;
  467. varunknown : Write(F,'Unsupported') ;
  468. vardecimal : Write(F,'Unsupported') ;
  469. varshortint : Write(F,vshortint) ;
  470. varbyte : Write(F,vbyte) ;
  471. varword : Write(F,vword) ;
  472. varlongword : Write(F,vlongword) ;
  473. varint64 : Write(F,vint64) ;
  474. varqword : Write(F,vqword) ;
  475. end;
  476. Writeln(f);
  477. end;
  478. end;
  479. {$endif HASVARIANT}
  480. {
  481. $Log$
  482. Revision 1.5 2001-11-17 10:29:48 florian
  483. * make cycle for win32 fixed
  484. Revision 1.4 2001/11/15 22:33:14 michael
  485. + Real/Boolean support added, Start of string support
  486. Revision 1.3 2001/11/14 23:00:17 michael
  487. + First working variant support
  488. Revision 1.2 2001/08/19 21:02:02 florian
  489. * fixed and added a lot of stuff to get the Jedi DX( headers
  490. compiled
  491. }