home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d6 / RX275D6.ZIP / Units / DATACONV.PAS < prev    next >
Pascal/Delphi Source File  |  1999-10-12  |  15KB  |  522 lines

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