Subject Re: [firebird-support] udf firebird delphi
Author Marc Collin
Martijn Tonies wrote:
> Hi Marc,
>
> > > I'm not an expert on writing UDFs at all... but:
> > > > i created this function:
> > > >
> > > > function MixStr(Str:PChar):PChar;
> > >
> > > What's the declaration you're using? cdecl?
> > >
> > > > var
> > > > i, j : Integer;
> > > > c1 : Char;
> > > > begin
> > > > if (Str = nil) then
> > > > result := nil
> > > > else
> > > > begin
> > > > for i:=Length(Str) downto 1 do
> > >
> > > What does Length(Str) return?
> > for allo, that return 4... it's ok
>
> I've been testing it with other values - it works OK. It
> seems Delphi is converting it to it's native string type.
>
> > What is the length of
> > > a PChar in Delphi? According to the Help:
> > > function Length(S): Integer;
> > > S is a string- or array-valued expression.
> > >
> > > > begin
> > > > j:=Random(i)+1;
> > > > c1:=Str[i];
> > > > Str[i]:=Str[j];
> > > > Str[j]:=c1;
> > > > end;
> > > > result := Str;
> > > > end;
> > > > end;
> > > >
> > > > under firebirdi declared:
> > > >
> > > > Declare external function f_MixStr cstring(64)
> > > > returns cstring(64) free_it
> > > > entry_point 'MixStr' module_name 'brudflib2.dll';
> > >
> > > As you're not grabbing any memory, should you use
> > > FREE_IT?
> > >
> > have remove it...
> >
> >
> > function MixStr(Str:PChar):PChar;cdecl; export;
> > var
> > i, j : Integer;
> > c1 : Char;
> > begin
> > if (Str = nil) then
> > result := nil
> > else
> > begin
> > for i:=Length(Str) downto 1 do
> > begin
> > j:=Random(i)+1;
> > c1:=Str[i];
> > Str[i]:=Str[j];
> > Str[j]:=c1;
> > end;
> > result := Str;
> > end;
> > end;
> >
> > drop external function f_MixStr;
> > Declare external function f_MixStr cstring(64)
> > returns cstring(64)
> > entry_point 'MixStr' module_name 'brudflib2.dll';
> >
> > select f_MixStr('allo') from rdb$database;
> >
> >
> > sometime that return a, sometime al, sometime la, sometime lla, sometime
> > llao...
> > return not all letter
>
> Well, I've been testing your routine in plain delphi:
>
> procedure TForm1.Button1Click(Sender: TObject);
> var
> i, j : Integer;
> c1 : Char;
> str: PChar;
> begin
> GetMem(str, Length('test_string'));
> StrPCopy(str, 'test_string');
>
> <insert rest here>
>
> But it returns all sorts of strings, but never with all characters.
>
> What should your routine do?

mix the character of the string...

imagine you type: world
that can return lorwd, ordwl...

that mix lettre form the word

IMO, it's syntax may be alright,
> but the routine itself isn't.


in delphi i use:

Function TForm1.MixStr(Str:String):String;
var
i, j : Integer;
c1 : Char;
Begin
For i:=Length(Str) downto 1 Do
Begin
j:=Random(i)+1;
c1:=Str[i];
Str[i]:=Str[j];
Str[j]:=c1;
End;
result := Str;
End;

but can't use that for udf...

> With regards,
>
> Martijn Tonies
> Database Workbench - developer tool for InterBase, Firebird, MySQL & MS SQL
> Server.
> Upscene Productions
> http://www.upscene.com
>

http://pages.infinit.net/borland