Code refactoring

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1943 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
jujibo
2011-09-14 08:29:32 +00:00
parent fc1c449cd2
commit e28a883a60
4 changed files with 95 additions and 92 deletions

View File

@ -0,0 +1,82 @@
unit jcontrolutils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
function ReplaceChar(const s: string; ch1: char; ch2: char): string;
function CountChar(const s: string; ch: char): integer;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
function NormalizeDate(const Value: string; theValue: TDateTime): string;
function NormalizeDateSeparator(const s: string): string;
implementation
function ReplaceChar(const s: string; ch1: char; ch2: char): string;
var
i: integer;
begin
Result := s;
for i := 1 to length(Result) do
if Result[i] = ch1 then
Result[i] := ch2;
end;
function CountChar(const s: string; ch: char): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to length(s) do
if s[i] = ch then
Inc(Result);
end;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
begin
Assert(Assigned(Strings));
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.DelimitedText := Input;
end;
function NormalizeDate(const Value: string; theValue: TDateTime): string;
var
texto: string;
i: integer;
d, m, y: word;
begin
if theValue = 0 then
DecodeDate(Now, y, m, d)
else
decodedate(theValue, y, m, d);
// normalize date
texto := Value;
texto:= NormalizeDateSeparator(texto);
//texto := replacechar(texto, '.', DateSeparator);
//texto := replacechar(texto, '-', DateSeparator);
//texto := replacechar(texto, '/', DateSeparator);
i := countchar(texto, DateSeparator);
case i of
1: texto := texto + DateSeparator + IntToStr(y);
0: texto := texto + DateSeparator + IntToStr(m) + DateSeparator + IntToStr(y);
end;
Result := texto;
end;
function NormalizeDateSeparator(const s: string): string;
var
i: integer;
begin
Result := s;
for i := 1 to length(Result) do
if Result[i] in ['.', ',', '/', '-'] then // valid date separators
Result[i] := DateSeparator;
end;
end.

View File

@ -22,7 +22,8 @@ unit jdbgridutils;
interface
uses
Classes, SysUtils, Grids, Dialogs, StdCtrls, LCLType, DBGrids, Controls, DB;
Classes, SysUtils, Grids, Dialogs, LCLType, DBGrids, Controls, DB,
jcontrolutils;
type
@ -102,10 +103,6 @@ var
integerDbGridControl: TJDbGridIntegerCtrl;
currencyDbGridControl: TJDbGridCurrencyCtrl;
function replacechar(const s: string; ch1: char; ch2: char): string;
function countchar(const s: string; ch: char): integer;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
implementation
uses
@ -503,34 +500,6 @@ begin
Result := CellEditor;
end;
function replacechar(const s: string; ch1: char; ch2: char): string;
var
i: integer;
begin
Result := s;
for i := 1 to length(Result) do
if Result[i] = ch1 then
Result[i] := ch2;
end;
function countchar(const s: string; ch: char): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to length(s) do
if s[i] = ch then
Inc(Result);
end;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
begin
Assert(Assigned(Strings));
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.DelimitedText := Input;
end;
procedure CreateResources;
begin
dateDbGridControl := TJDbGridDateCtrl.Create;

View File

@ -22,7 +22,7 @@ unit jdbutils;
interface
uses
Classes, StdCtrls, SysUtils, DBCtrls;
Classes, SysUtils, DBCtrls, jcontrolutils;
type
@ -71,7 +71,6 @@ type
procedure setFormat(const AValue: string);
procedure OnKeyPress(Sender: TObject; var key: char);
function IsValidDate(const Value: string): boolean;
function NormalizeDate(const Value: string): string;
public
function isNull: boolean;
property format: string read getFormat write setFormat;
@ -83,9 +82,6 @@ var
integerDbControl: TJDBIntegerCtrl;
currencyDbControl: TJDBCurrencyCtrl;
function replacechar(const s: string; ch1: char; ch2: char): string;
function countchar(const s: string; ch: char): integer;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
implementation
@ -225,7 +221,7 @@ procedure TJDBDateCtrl.myEditOnEditingDone(Sender: TObject);
var
bufCaption: string;
begin
bufCaption := NormalizeDate(myEdit.Caption);
bufCaption := NormalizeDate(myEdit.Caption, theValue);
if Length(myEdit.Caption) = 0 then
theValue := 0
else
@ -270,30 +266,6 @@ begin
Result := True;
end;
function TJDBDateCtrl.NormalizeDate(const Value: string): string;
var
texto: string;
i: integer;
d, m, y: word;
begin
if theValue = 0 then
DecodeDate(Now, y, m, d)
else
decodedate(theValue, y, m, d);
// normalize date
texto := Value;
texto := replacechar(texto, '.', DateSeparator);
texto := replacechar(texto, '-', DateSeparator);
texto := replacechar(texto, '/', DateSeparator);
i := countchar(texto, DateSeparator);
case i of
1: texto := texto + DateSeparator + IntToStr(y);
0: texto := texto + DateSeparator + IntToStr(m) + DateSeparator + IntToStr(y);
end;
Result := texto;
end;
function TJDBDateCtrl.isNull: boolean;
begin
Result := theValue = 0;
@ -309,34 +281,6 @@ begin
myEdit.SelectAll;
end;
function replacechar(const s: string; ch1: char; ch2: char): string;
var
i: integer;
begin
Result := s;
for i := 1 to length(Result) do
if Result[i] = ch1 then
Result[i] := ch2;
end;
function countchar(const s: string; ch: char): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to length(s) do
if s[i] = ch then
Inc(Result);
end;
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
begin
Assert(Assigned(Strings));
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.DelimitedText := Input;
end;
procedure CreateResources;
begin
dateDbControl := TJDBDateCtrl.Create;

View File

@ -40,7 +40,7 @@
<PackageName Value="LCL"/>
</Item3>
</RequiredPackages>
<Units Count="2">
<Units Count="3">
<Unit0>
<Filename Value="testmemdataset.lpr"/>
<IsPartOfProject Value="True"/>
@ -53,6 +53,11 @@
<ResourceBaseClass Value="Form"/>
<UnitName Value="main"/>
</Unit1>
<Unit2>
<Filename Value="jcontrolutils.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="jcontrolutils"/>
</Unit2>
</Units>
</ProjectOptions>
<CompilerOptions>
@ -72,6 +77,9 @@
</Options>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>