cvarutil.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. Resourcestring
  2. SNoWidestrings = 'No widestrings supported';
  3. SNoInterfaces = 'No interfaces supported';
  4. Procedure NoWidestrings;
  5. begin
  6. Raise Exception.Create(SNoWideStrings);
  7. end;
  8. Procedure NoInterfaces;
  9. begin
  10. Raise Exception.Create(SNoInterfaces);
  11. end;
  12. Constructor EVariantError.CreateCode (Code : longint);
  13. begin
  14. ErrCode:=Code;
  15. end;
  16. Procedure VariantTypeMismatch;
  17. begin
  18. Raise EVariantError.CreateCode(VAR_TYPEMISMATCH);
  19. end;
  20. Function ExceptionToVariantError (E : Exception): HResult;
  21. begin
  22. If E is EoutOfMemory then
  23. Result:=VAR_OUTOFMEMORY
  24. else
  25. Result:=VAR_EXCEPTION;
  26. end;
  27. { ---------------------------------------------------------------------
  28. OS-independent functions not present in Windows
  29. ---------------------------------------------------------------------}
  30. Function VariantToSmallInt(Const VargSrc : TVarData) : SmallInt;
  31. begin
  32. With VargSrc do
  33. Case (VType and VarTypeMask) of
  34. VarSmallInt: Result:=VSmallInt;
  35. VarInteger : Result:=VInteger;
  36. VarSingle : Result:=Round(VSingle);
  37. VarDouble : Result:=Round(VDouble);
  38. VarCurrency: Result:=Round(VCurrency);
  39. VarDate : Result:=Round(VDate);
  40. VarOleStr : NoWideStrings;
  41. VarBoolean : Result:=SmallInt(VBoolean);
  42. VarByte : Result:=VByte;
  43. else
  44. VariantTypeMismatch;
  45. end;
  46. end;
  47. Function VariantToLongint(Const VargSrc : TVarData) : Longint;
  48. begin
  49. With VargSrc do
  50. Case (VType and VarTypeMask) of
  51. VarSmallInt: Result:=VSmallInt;
  52. VarInteger : Result:=VInteger;
  53. VarSingle : Result:=Round(VSingle);
  54. VarDouble : Result:=Round(VDouble);
  55. VarCurrency: Result:=Round(VCurrency);
  56. VarDate : Result:=Round(VDate);
  57. VarOleStr : NoWideStrings;
  58. VarBoolean : Result:=Longint(VBoolean);
  59. VarByte : Result:=VByte;
  60. else
  61. VariantTypeMismatch;
  62. end;
  63. end;
  64. Function VariantToSingle(Const VargSrc : TVarData) : Single;
  65. begin
  66. With VargSrc do
  67. Case (VType and VarTypeMask) of
  68. VarSmallInt: Result:=VSmallInt;
  69. VarInteger : Result:=VInteger;
  70. VarSingle : Result:=VSingle;
  71. VarDouble : Result:=VDouble;
  72. VarCurrency: Result:=VCurrency;
  73. VarDate : Result:=VDate;
  74. VarOleStr : NoWideStrings;
  75. VarBoolean : Result:=Longint(VBoolean);
  76. VarByte : Result:=VByte;
  77. else
  78. VariantTypeMismatch;
  79. end;
  80. end;
  81. Function VariantToDouble(Const VargSrc : TVarData) : Double;
  82. begin
  83. With VargSrc do
  84. Case (VType and VarTypeMask) of
  85. VarSmallInt: Result:=VSmallInt;
  86. VarInteger : Result:=VInteger;
  87. VarSingle : Result:=VSingle;
  88. VarDouble : Result:=VDouble;
  89. VarCurrency: Result:=VCurrency;
  90. VarDate : Result:=VDate;
  91. VarOleStr : NoWideStrings;
  92. VarBoolean : Result:=Longint(VBoolean);
  93. VarByte : Result:=VByte;
  94. else
  95. VariantTypeMismatch;
  96. end;
  97. end;
  98. Function VariantToCurrency(Const VargSrc : TVarData) : Currency;
  99. begin
  100. Try
  101. With VargSrc do
  102. Case (VType and VarTypeMask) of
  103. VarSmallInt: Result:=VSmallInt;
  104. VarInteger : Result:=VInteger;
  105. VarSingle : Result:=FloatToCurr(VSingle);
  106. VarDouble : Result:=FloatToCurr(VDouble);
  107. VarCurrency: Result:=VCurrency;
  108. VarDate : Result:=FloatToCurr(VDate);
  109. VarOleStr : NoWideStrings;
  110. VarBoolean : Result:=Longint(VBoolean);
  111. VarByte : Result:=VByte;
  112. else
  113. VariantTypeMismatch;
  114. end;
  115. except
  116. On EConvertError do
  117. VariantTypeMismatch;
  118. else
  119. Raise;
  120. end;
  121. end;
  122. Function VariantToDate(Const VargSrc : TVarData) : TDateTime;
  123. begin
  124. Try
  125. With VargSrc do
  126. Case (VType and VarTypeMask) of
  127. VarSmallInt: Result:=FloatToDateTime(VSmallInt);
  128. VarInteger : Result:=FloatToDateTime(VInteger);
  129. VarSingle : Result:=FloatToDateTime(VSingle);
  130. VarDouble : Result:=FloatToDateTime(VDouble);
  131. VarCurrency: Result:=FloatToDateTime(VCurrency);
  132. VarDate : Result:=VDate;
  133. VarOleStr : NoWideStrings;
  134. VarBoolean : Result:=FloatToDateTime(Longint(VBoolean));
  135. VarByte : Result:=FloatToDateTime(VByte);
  136. else
  137. VariantTypeMismatch;
  138. end;
  139. except
  140. On EConvertError do
  141. VariantTypeMismatch;
  142. else
  143. Raise;
  144. end;
  145. end;
  146. Function VariantToBoolean(Const VargSrc : TVarData) : Boolean;
  147. begin
  148. With VargSrc do
  149. Case (VType and VarTypeMask) of
  150. VarSmallInt: Result:=VSmallInt<>0;
  151. VarInteger : Result:=VInteger<>0;
  152. VarSingle : Result:=VSingle<>0;
  153. VarDouble : Result:=VDouble<>0;
  154. VarCurrency: Result:=VCurrency<>0;
  155. VarDate : Result:=VDate<>0;
  156. VarOleStr : NoWideStrings;
  157. VarBoolean : Result:=VBoolean;
  158. VarByte : Result:=VByte<>0;
  159. else
  160. VariantTypeMismatch;
  161. end;
  162. end;
  163. Function VariantToByte(Const VargSrc : TVarData) : Byte;
  164. begin
  165. Try
  166. With VargSrc do
  167. Case (VType and VarTypeMask) of
  168. VarSmallInt: Result:=VSmallInt;
  169. VarInteger : Result:=VInteger;
  170. VarSingle : Result:=Round(VSingle);
  171. VarDouble : Result:=Round(VDouble);
  172. VarCurrency: Result:=Round(VCurrency);
  173. VarDate : Result:=Round(VDate);
  174. VarOleStr : NoWideStrings;
  175. VarBoolean : Result:=Longint(VBoolean);
  176. VarByte : Result:=VByte;
  177. else
  178. VariantTypeMismatch;
  179. end;
  180. except
  181. On EConvertError do
  182. VariantTypeMismatch;
  183. else
  184. Raise;
  185. end;
  186. end;