home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / delphi / kolekce / d6 / rxlibsetup.exe / {app} / units / DATACONV.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-02-19  |  14.5 KB  |  526 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 2001,2002 SGB Software          }
  6. {         Copyright (c) 1997, 1998 Fedor Koshevnikov,   }
  7. {                        Igor Pavluk and Serge Korolev  }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11.  
  12.  
  13. unit DataConv;
  14.  
  15. interface
  16.  
  17. {$I RX.INC}
  18.  
  19. uses SysUtils, {$IFDEF WIN32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  20.   Messages, Classes, Graphics, Controls, Forms, Dialogs, DateUtil;
  21.  
  22. type
  23.  
  24.   TDataType = (dtString, dtInteger, dtFloat, dtDateTime, dtDate,
  25.     dtTime, dtBoolean);
  26.  
  27.   TTimeFormat = (tfHHMMSS, tfHMMSS, tfHHMM, tfHMM);
  28.  
  29. { TDateTimeFormat }
  30.  
  31.   TDateTimeFormat = class(TPersistent)
  32.   private
  33.     FAMString: string[7];
  34.     FPMString: string[7];
  35.     FDateOrder: TDateOrder;
  36.     FTimeFormat: TTimeFormat;
  37.     FTimeSeparator: Char;
  38.     FDateSeparator: Char;
  39.     FLongDate: Boolean;
  40.     FFourDigitYear: Boolean;
  41.     FLeadingZero: Boolean;
  42.     function GetAMString: string;
  43.     procedure SetAMString(const Value: string);
  44.     function GetPMString: string;
  45.     procedure SetPMString(const Value: string);
  46.   protected
  47.     function GetDateMask: string; virtual;
  48.     function GetTimeMask: string; virtual;
  49.     function GetMask: string; virtual;
  50.   public
  51.     constructor Create;
  52.     destructor Destroy; override;
  53.     procedure Assign(Source: TPersistent); override;
  54.     procedure ResetDefault; virtual;
  55.     property DateMask: string read GetDateMask;
  56.     property TimeMask: string read GetTimeMask;
  57.     property Mask: string read GetMask;
  58.   published
  59.     property AMString: string read GetAMString write SetAMString;
  60.     property PMString: string read GetPMString write SetPMString;
  61.     property DateOrder: TDateOrder read FDateOrder write FDateOrder;
  62.     property TimeFormat: TTimeFormat read FTimeFormat write FTimeFormat;
  63.     property TimeSeparator: Char read FTimeSeparator write FTimeSeparator;
  64.     property DateSeparator: Char read FDateSeparator write FDateSeparator;
  65.     property LongDate: Boolean read FLongDate write FLongDate default False;
  66.     property FourDigitYear: Boolean read FFourDigitYear write FFourDigitYear default True;
  67.     property LeadingZero: Boolean read FLeadingZero write FLeadingZero default False;
  68.   end;
  69.  
  70. { TConverter }
  71.  
  72.   TConverter = class(TComponent)
  73.   private
  74.     { Private declarations }
  75.     FData: String;
  76.     FTextValues: array[Boolean] of string[15];
  77.     FDataType: TDataType;
  78.     FDateTimeFormat: TDateTimeFormat;
  79.     FFloatFormat: TFloatFormat;
  80.     FPrecision, FDigits: Integer;
  81.     FRaiseOnError: Boolean;
  82.     FOnChange: TNotifyEvent;
  83.     procedure SetDataType(Value: TDataType);
  84.     procedure SetDateTimeFormat(Value: TDateTimeFormat);
  85.     function GetDateTimeFormat: TDateTimeFormat;
  86.     function GetString: string;
  87.     procedure SetString(const Value: string);
  88.     function GetDateTime: TDateTime;
  89.     function GetBoolValues(Index: Integer): string;
  90.     procedure SetBoolValues(Index: Integer; const Value: string);
  91.     procedure CheckDataType;
  92.     function BoolToStr(Value: Boolean): string;
  93.     function FloatToString(Value: Double): string;
  94.     function DateTimeToString(Value: TDateTime): string;
  95.   protected
  96.     { Protected declarations }
  97.     procedure Change; dynamic;
  98.     function GetAsBoolean: Boolean; virtual;
  99.     function GetAsDateTime: TDateTime; virtual;
  100.     function GetAsDate: TDateTime; virtual;
  101.     function GetAsTime: TDateTime; virtual;
  102.     function GetAsFloat: Double; virtual;
  103.     function GetAsInteger: Longint; virtual;
  104.     function GetAsString: string; virtual;
  105.     procedure SetAsBoolean(Value: Boolean); virtual;
  106.     procedure SetAsDateTime(Value: TDateTime); virtual;
  107.     procedure SetAsDate(Value: TDateTime); virtual;
  108.     procedure SetAsTime(Value: TDateTime); virtual;
  109.     procedure SetAsFloat(Value: Double); virtual;
  110.     procedure SetAsInteger(Value: Longint); virtual;
  111.     procedure SetAsString(const Value: string); virtual;
  112.   public
  113.     { Public declarations }
  114.     constructor Create(AOwner: TComponent); override;
  115.     destructor Destroy; override;
  116.     procedure Clear;
  117.     function IsValidChar(Ch: Char): Boolean; virtual;
  118.     property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
  119.     property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
  120.     property AsDate: TDateTime read GetAsDate write SetAsDate;
  121.     property AsTime: TDateTime read GetAsTime write SetAsTime;
  122.     property AsFloat: Double read GetAsFloat write SetAsFloat;
  123.     property AsInteger: Longint read GetAsInteger write SetAsInteger;
  124.     property AsString: string read GetAsString write SetAsString;
  125.   published
  126.     { Published declarations }
  127.     property DataType: TDataType read FDataType write SetDataType default dtString;
  128.     property DateTimeFormat: TDateTimeFormat read GetDateTimeFormat write SetDateTimeFormat;
  129.     property Digits: Integer read FDigits write FDigits default 2;
  130.     property DisplayFalse: string index 0 read GetBoolValues write SetBoolValues;
  131.     property DisplayTrue: string index 1 read GetBoolValues write SetBoolValues;
  132.     property FloatFormat: TFloatFormat read FFloatFormat write FFloatFormat default ffGeneral;
  133.     property Precision: Integer read FPrecision write FPrecision default 15;
  134.     property RaiseOnError: Boolean read FRaiseOnError write FRaiseOnError default False;
  135.     property Text: string read GetString write SetAsString;
  136.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  137.   end;
  138.  
  139. implementation
  140.  
  141. { TDateTimeFormat }
  142.  
  143. constructor TDateTimeFormat.Create;
  144. begin
  145.   inherited Create;
  146.   ResetDefault;
  147. end;
  148.  
  149. destructor TDateTimeFormat.Destroy;
  150. begin
  151.   inherited Destroy;
  152. end;
  153.  
  154. procedure TDateTimeFormat.ResetDefault;
  155. begin
  156.   FAMString := TimeAMString;
  157.   FPMString := TimePMString;
  158.   FTimeSeparator := SysUtils.TimeSeparator;
  159.   FDateSeparator := SysUtils.DateSeparator;
  160.   FDateOrder := doDMY;
  161.   FTimeFormat := tfHHMMSS;
  162.   FLongDate := False;
  163.   FFourDigitYear := True;
  164.   FLeadingZero := False;
  165. end;
  166.  
  167. procedure TDateTimeFormat.Assign(Source: TPersistent);
  168. begin
  169.   if Source is TDateTimeFormat then begin
  170.     FAMString := TDateTimeFormat(Source).AMString;
  171.     FPMString := TDateTimeFormat(Source).PMString;
  172.     FDateOrder := TDateTimeFormat(Source).DateOrder;
  173.     FTimeFormat := TDateTimeFormat(Source).TimeFormat;
  174.     FTimeSeparator := TDateTimeFormat(Source).TimeSeparator;
  175.     FDateSeparator := TDateTimeFormat(Source).DateSeparator;
  176.     FLongDate := TDateTimeFormat(Source).LongDate;
  177.     FFourDigitYear := TDateTimeFormat(Source).FourDigitYear;
  178.     FLeadingZero := TDateTimeFormat(Source).LeadingZero;
  179.     Exit;
  180.   end;
  181.   inherited Assign(Source);
  182. end;
  183.  
  184. function TDateTimeFormat.GetAMString: string;
  185. begin
  186.   Result := FAMString;
  187. end;
  188.  
  189. procedure TDateTimeFormat.SetAMString(const Value: string);
  190. begin
  191.   if Value = '' then FAMString := TimeAMString
  192.   else FAMString := Value;
  193. end;
  194.  
  195. function TDateTimeFormat.GetPMString: string;
  196. begin
  197.   Result := FPMString;
  198. end;
  199.  
  200. procedure TDateTimeFormat.SetPMString(const Value: string);
  201. begin
  202.   if Value = '' then FPMString := TimePMString
  203.   else FPMString := Value;
  204. end;
  205.  
  206. function TDateTimeFormat.GetDateMask: string;
  207. var
  208.   S: array[1..3] of string[7];
  209.   Separator: string[3];
  210. begin
  211.   Result := '';
  212.   if LeadingZero then begin
  213.     S[1] := 'dd';
  214.     S[2] := 'mm';
  215.   end
  216.   else begin
  217.     S[1] := 'd';
  218.     S[2] := 'm';
  219.   end;
  220.   if LongDate then begin
  221.     S[2] := 'mmmm';
  222.     Separator := ' ';
  223.   end
  224.   else Separator := '"' + DateSeparator + '"';
  225.   if FourDigitYear then S[3] := 'yyyy'
  226.   else S[3] := 'yy';
  227.   case DateOrder of
  228.     doDMY: Result := S[1] + Separator + S[2] + Separator + S[3];
  229.     doMDY: Result := S[2] + Separator + S[1] + Separator + S[3];
  230.     doYMD: Result := S[3] + Separator + S[2] + Separator + S[1];
  231.   end;
  232. end;
  233.  
  234. function TDateTimeFormat.GetTimeMask: string;
  235. var
  236.   S: array[1..3] of string[7];
  237.   Separator: string[3];
  238.   AMPM: string[16];
  239. begin
  240.   Separator := '"' + TimeSeparator + '"';
  241.   AMPM := ' ' + AMString + '/' + PMString;
  242.   if LeadingZero then begin
  243.     S[1] := 'hh';
  244.     S[2] := 'nn';
  245.     S[3] := 'ss';
  246.   end
  247.   else begin
  248.     S[1] := 'h';
  249.     S[2] := 'n';
  250.     S[3] := 's';
  251.   end;
  252.   case TimeFormat of
  253.     tfHHMMSS: Result := S[1] + Separator + S[2] + Separator + S[3];
  254.     tfHMMSS: Result := S[1] + Separator + S[2] + Separator + S[3] + AMPM;
  255.     tfHHMM: Result := S[1] + Separator + S[2];
  256.     tfHMM: Result := S[1] + Separator + S[2] + AMPM;
  257.   end;
  258. end;
  259.  
  260. function TDateTimeFormat.GetMask: string;
  261. begin
  262.   Result := GetDateMask + ' ' + GetTimeMask;
  263. end;
  264.  
  265. { TConverter }
  266.  
  267. constructor TConverter.Create(AOwner: TComponent);
  268. begin
  269.   inherited Create(AOwner);
  270.   FData := EmptyStr;
  271.   FDataType := dtString;
  272.   FPrecision := 15;
  273.   FDigits := 2;
  274.   FDateTimeFormat := TDateTimeFormat.Create;
  275.   FTextValues[False] := 'False';
  276.   FTextValues[True] := 'True';
  277.   FRaiseOnError := False;
  278. end;
  279.  
  280. destructor TConverter.Destroy;
  281. begin
  282.   FDataType := dtString;
  283.   //if (FData <> nil) and (FData^ <> '') then Dispose(FData);
  284.   FDateTimeFormat.Free;
  285.   inherited Destroy;
  286. end;
  287.  
  288. procedure TConverter.Clear;
  289. begin
  290.   //if (FData <> nil) and (FData^ <> '') then Dispose(FData);
  291.   FData := EmptyStr;
  292.   Change;
  293. end;
  294.  
  295. procedure TConverter.Change;
  296. begin
  297.   if Assigned(FOnChange) then FOnChange(Self);
  298. end;
  299.  
  300. function TConverter.GetString: string;
  301. begin
  302.   Result := FData;
  303. end;
  304.  
  305. procedure TConverter.SetString(const Value: string);
  306. begin
  307.   FData := Value;
  308. end;
  309.  
  310. function TConverter.GetDateTimeFormat: TDateTimeFormat;
  311. begin
  312.   Result := FDateTimeFormat;
  313. end;
  314.  
  315. procedure TConverter.SetDateTimeFormat(Value: TDateTimeFormat);
  316. begin
  317.   FDateTimeFormat.Assign(Value);
  318. end;
  319.  
  320. function TConverter.GetBoolValues(Index: Integer): string;
  321. begin
  322.   Result := FTextValues[Boolean(Index)];
  323. end;
  324.  
  325. procedure TConverter.SetBoolValues(Index: Integer; const Value: string);
  326. begin
  327.   FTextValues[Boolean(Index)] := Value;
  328. end;
  329.  
  330. function TConverter.BoolToStr(Value: Boolean): string;
  331. begin
  332.   Result := GetBoolValues(Integer(Value));
  333. end;
  334.  
  335. function TConverter.FloatToString(Value: Double): string;
  336. begin
  337.   Result := FloatToStrF(Value, FloatFormat, Precision, Digits);
  338. end;
  339.  
  340. function TConverter.DateTimeToString(Value: TDateTime): string;
  341. begin
  342.   case FDataType of
  343.     dtDate: Result := FormatDateTime(DateTimeFormat.DateMask, Value);
  344.     dtTime: Result := FormatDateTime(DateTimeFormat.TimeMask, Value);
  345.     else Result := FormatDateTime(DateTimeFormat.Mask, Value);
  346.   end;
  347. end;
  348.  
  349. procedure TConverter.SetDataType(Value: TDataType);
  350. begin
  351.   if Value <> FDataType then begin
  352.     FDataType := Value;
  353.     try
  354.       CheckDataType;
  355.       Change;
  356.     except
  357.       Clear;
  358.       if RaiseOnError then raise;
  359.     end;
  360.   end;
  361. end;
  362.  
  363. function TConverter.IsValidChar(Ch: Char): Boolean;
  364. begin
  365.   case FDataType of
  366.     dtString: Result := True;
  367.     dtInteger: Result := Ch in ['+', '-', '0'..'9'];
  368.     dtFloat: Result := Ch in [DecimalSeparator, '+', '-', '0'..'9', 'E', 'e'];
  369.     dtDateTime, dtDate, dtTime: Result := True;
  370.     dtBoolean: Result := True;
  371.     else Result := False;
  372.   end;
  373. end;
  374.  
  375. procedure TConverter.CheckDataType;
  376. begin
  377.   case FDataType of
  378.     dtInteger, dtFloat: StrToFloat(GetString);
  379.     dtDateTime, dtDate, dtTime: GetDateTime;
  380.   end;
  381. end;
  382.  
  383. function TConverter.GetAsBoolean: Boolean;
  384. var
  385.   S: string[15];
  386. begin
  387.   S := GetString;
  388.   Result := (Length(S) > 0) and ((S[1] in ['T', 't', 'Y', 'y']) or
  389.     (S = FTextValues[True]));
  390. end;
  391.  
  392. function TConverter.GetDateTime: TDateTime;
  393. var
  394.   S: string;
  395.   I: Integer;
  396.   DateS, TimeS: set of Char;
  397. begin
  398.   S := GetString;
  399.   DateS := ['/', '.'] + [DateTimeFormat.DateSeparator] - 
  400.     [DateTimeFormat.TimeSeparator];
  401.   TimeS := [':', '-'] - [DateTimeFormat.DateSeparator] + 
  402.     [DateTimeFormat.TimeSeparator];
  403.   for I := 1 to Length(S) do begin
  404.     if S[I] in DateS then S[I] := DateSeparator
  405.     else if S[I] in TimeS then S[I] := TimeSeparator;
  406.   end;
  407.   Result := StrToDateTime(S);
  408. end;
  409.  
  410. function TConverter.GetAsDateTime: TDateTime;
  411. begin
  412.   try
  413.     Result := GetDateTime;
  414.   except
  415.     Result := NullDate;
  416.   end;
  417. end;
  418.  
  419. function TConverter.GetAsDate: TDateTime;
  420. var
  421.   Year, Month, Day: Word;
  422. begin
  423.   try
  424.     Result := GetAsDateTime;
  425.     DecodeDate(Result, Year, Month, Day);
  426.     Result := EncodeDate(Year, Month, Day);
  427.   except
  428.     Result := NullDate;
  429.   end;
  430. end;
  431.  
  432. function TConverter.GetAsTime: TDateTime;
  433. var
  434.   Hour, Min, Sec, MSec: Word;
  435. begin
  436.   try
  437.     Result := GetAsDateTime;
  438.     DecodeTime(Result, Hour, Min, Sec, MSec);
  439.     Result := EncodeTime(Hour, Min, Sec, MSec);
  440.   except
  441.     Result := NullDate;
  442.   end;
  443. end;
  444.  
  445. function TConverter.GetAsFloat: Double;
  446. begin
  447.   try
  448.     case FDataType of
  449.       dtDateTime: Result := GetAsDateTime;
  450.       dtDate: Result := GetAsDate;
  451.       dtTime: Result := GetAsTime;
  452.       else Result := StrToFloat(GetString);
  453.     end;
  454.   except
  455.     Result := 0.0;
  456.   end;
  457. end;
  458.  
  459. function TConverter.GetAsInteger: Longint;
  460. begin
  461.   Result := Round(GetAsFloat);
  462. end;
  463.  
  464. function TConverter.GetAsString: string;
  465. begin
  466.   case FDataType of
  467.     dtString: Result := GetString;
  468.     dtInteger: Result := IntToStr(GetAsInteger);
  469.     dtFloat: Result := FloatToString(GetAsFloat);
  470.     dtDateTime: Result := DateTimeToString(GetAsDateTime);
  471.     dtDate: Result := DateTimeToString(GetAsDate);
  472.     dtTime: Result := DateTimeToString(GetAsTime);
  473.     dtBoolean: Result := BoolToStr(GetAsBoolean);
  474.   end;
  475. end;
  476.  
  477. procedure TConverter.SetAsBoolean(Value: Boolean);
  478. begin
  479.   SetAsString(BoolToStr(Value));
  480. end;
  481.  
  482. procedure TConverter.SetAsDateTime(Value: TDateTime);
  483. begin
  484.   SetAsString(DateTimeToStr(Value));
  485. end;
  486.  
  487. procedure TConverter.SetAsDate(Value: TDateTime);
  488. begin
  489.   SetAsDateTime(Value);
  490. end;
  491.  
  492. procedure TConverter.SetAsTime(Value: TDateTime);
  493. begin
  494.   SetAsDateTime(Value);
  495. end;
  496.  
  497. procedure TConverter.SetAsFloat(Value: Double);
  498. begin
  499.   if FDataType in [dtDateTime, dtDate, dtTime] then
  500.     SetAsDateTime(Value)
  501.   else SetAsString(FloatToStr(Value));
  502. end;
  503.  
  504. procedure TConverter.SetAsInteger(Value: Longint);
  505. begin
  506.   if FDataType = dtInteger then SetAsString(IntToStr(Value))
  507.   else SetAsFloat(Value);
  508. end;
  509.  
  510. procedure TConverter.SetAsString(const Value: string);
  511. var
  512.   S: string;
  513. begin
  514.   S := GetString;
  515.   SetString(Value);
  516.   try
  517.     CheckDataType;
  518.     Change;
  519.   except
  520.     SetString(S);
  521.     if RaiseOnError then raise;
  522.   end;
  523. end;
  524.  
  525. end.
  526.