Problem translating c code to pascal

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem translating c code to pascal

Felipe Monteiro de Carvalho
Hello,

I'm converting apache 1.3 headers to pascal, and I found something I
don't know how to convert. It's these lines:

static const handler_rec hw_handlers[] = {
     { "hw-app", hw_handle_req },
     { NULL }
};

Where handler_req is declared as:

  handler_t = function (param1: Prequest_rec): Integer; cdecl;

  handler_rec = record
    content_type: PChar; { MUST be all lower case }
    handler: handler_t;
  end;

And hw_handle_req is a function of type handler_t

I don't understand what exactly that constant is. I looks like a
dynamic array with 2 elements, but the second is NULL. How can a
structure be NULL ?

Also, the static modifier here plays any role? Looks redundant to me.

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Problem translating c code to pascal

Marc Santhoff
Am Samstag, den 23.09.2006, 12:39 -0300 schrieb Felipe Monteiro de
Carvalho:

> Hello,
>
> I'm converting apache 1.3 headers to pascal, and I found something I
> don't know how to convert. It's these lines:
>
> static const handler_rec hw_handlers[] = {
>      { "hw-app", hw_handle_req },
>      { NULL }
> };
>
> Where handler_req is declared as:
>
>   handler_t = function (param1: Prequest_rec): Integer; cdecl;
>
>   handler_rec = record
>     content_type: PChar; { MUST be all lower case }
>     handler: handler_t;
>   end;
>
> And hw_handle_req is a function of type handler_t
>
> I don't understand what exactly that constant is. I looks like a
> dynamic array with 2 elements, but the second is NULL. How can a
> structure be NULL ?

Typically such structure arrays are iterated by the part using them.
This way one can add new items simply by writing the name and associated
handler into the code, if needed.

Think of hardware detection, one driver may know different models it
supports. When detection is active it simply iterates over the model
describing struct and checks presence until the item read from the
struct is NULL.

I think, a NIL pointer in pascal will do, although I have no idea what
this is good for in apache:

const
        first_handler_rec = (("hw_app"), (@hw_handle_rec));
type
        Phandler_rec = ^handler_rec
var
        hw_handlers: array of [0..1] Phandler_rec = (
                first_handler_rec,
                NIL
        );

That should be close to the pascal syntax for this construction - not
checked, only typed away.

Maybe you can put the constant value into the array directly, omitting
to have it separated and maybe you can omit the array size (0..1) too,
you'll have to try.

HTH,
Marc


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Problem translating c code to pascal

Felipe Monteiro de Carvalho
This is what almost works:

var
  hw_handlers: array[0..1] of handler_rec =
  (
    (content_type: 'hw_app'; handler: @DefaultHandler),
    nil
  );

But the compiler will complain on the nil:

mod_hello.lpr(108,5) Fatal: Syntax error, "(" expected but "NIL" found

On 9/23/06, Marc Santhoff <[hidden email]> wrote:
> Think of hardware detection, one driver may know different models it
> supports. When detection is active it simply iterates over the model
> describing struct and checks presence until the item read from the
> struct is NULL.

I still don't know how what is the inner working of this. Does that c
statement really means a static array with size two? A record cannot
be null. When you access a static record you go directly to it's
memory position of Initial_Pos + X * Record_Size, where X is the
record number. Then you cannot have a null record. The best you can
have is record whose content is null.

Or NULL could represent that the array is of size 1 not 2.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Problem translating c code to pascal

Marc Santhoff
Am Sonntag, den 24.09.2006, 11:37 -0300 schrieb Felipe Monteiro de
Carvalho:

> This is what almost works:
>
> var
>   hw_handlers: array[0..1] of handler_rec =
>   (
>     (content_type: 'hw_app'; handler: @DefaultHandler),
>     nil
>   );
>
> But the compiler will complain on the nil:
>
> mod_hello.lpr(108,5) Fatal: Syntax error, "(" expected but "NIL" found

That's why I suggested making it an array of pointers. A pointer can be
NIL. The main question is if apache can still access it in the right
way ...

Have you tried something stupid, like:

 hw_handlers: array[0..1] of handler_rec =
 (
   (content_type: 'hw_app'; handler: @DefaultHandler),
   (,)
 )

> On 9/23/06, Marc Santhoff <[hidden email]> wrote:
> > Think of hardware detection, one driver may know different models it
> > supports. When detection is active it simply iterates over the model
> > describing struct and checks presence until the item read from the
> > struct is NULL.
>
> I still don't know how what is the inner working of this. Does that c
> statement really means a static array with size two?

Yes it does.

>  A record cannot
> be null. When you access a static record you go directly to it's
> memory position of Initial_Pos + X * Record_Size, where X is the
> record number. Then you cannot have a null record. The best you can
> have is record whose content is null.

There is the problem, pascal is sort of stronger typed than C.

> Or NULL could represent that the array is of size 1 not 2.

It is an element that can have a NULL value, however the compiler
interprets it. Mostly variables in C do not have anything attached but
can be viewed as a block of memory. You can do with it what you like.
Pascal is lacking some common base type compatible with any record. One
solution for a few cases would be to use an array of byte and do the
housekeeping yourself.

How is the source using this array defined, can you get any hints from
that C code? Is it happy with a record having a NIL string and a NIL
pointer in it?

HTH,
Marc


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

RE: Problem translating c code to pascal

Jeff Pohlmeyer
In reply to this post by Felipe Monteiro de Carvalho
Maybe you can create the array dynamically at run-time,
in the unit's initialization section...

type
  p_handler_rec=^handler_rec;
  pp_handler_rec=^p_handler_rec;

var
  hw_handlers:pp_handler_rec;

initialization
  hw_handlers:=getmem(sizeof(p_handler_rec)*2);
  new(hw_handlers[0]);
  hw_handlers[0]^.content_type:='hw-app';
  hw_handlers[0]^.handler:=@hw_handle_req;
  hw_handlers[1]:=nil;
end.


 - Jeff
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RE: Problem translating c code to pascal

Felipe Monteiro de Carvalho
Ok, after a lot of headaches and making questions on ##c freenode
channel I found out what that is.

The structure is a NULL-terminated array / table. It was declared like this:

static const handler_rec hw_handlers[] = {
    { "hw-app", hw_handle_req },
    { NULL }
};

But that is just the short declaration for something like this:

static const handler_rec hw_handlers[] = {
    { "hw-app", hw_handle_req },
    { NULL, NULL }
};


So it´s an array of size 2, where the second element is filled with
NULLs, but it will be interpreted as an array of size 1, because the
last element marks the end of the array.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RE: Problem translating c code to pascal

Marc Santhoff
Am Samstag, den 30.09.2006, 11:48 -0300 schrieb Felipe Monteiro de
Carvalho:

> Ok, after a lot of headaches and making questions on ##c freenode
> channel I found out what that is.
>
> The structure is a NULL-terminated array / table. It was declared like this:
>
> static const handler_rec hw_handlers[] = {
>     { "hw-app", hw_handle_req },
>     { NULL }
> };
>
> But that is just the short declaration for something like this:
>
> static const handler_rec hw_handlers[] = {
>     { "hw-app", hw_handle_req },
>     { NULL, NULL }
> };
>
>
> So it´s an array of size 2, where the second element is filled with
> NULLs, but it will be interpreted as an array of size 1, because the
> last element marks the end of the array.

Thanks for making the explanation public,
Marc


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal