C header conversion, need clarification

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
aleofjax
Posts: 117
Joined: Oct 18, 2007 8:10

C header conversion, need clarification

Postby aleofjax » Dec 29, 2007 4:28

Alright, I'm starting to work on converting some c headers with swig, and I think I'm doing pretty well with the manual touch-ups, but I ran into a couple of snags. One of the header files makes swig throw some syntax errors. I think it has to do with some functions being defined in the .h instead of just declared. Giving swig this header:

Code: Select all

/* Copyright (c) 2007 Scott Lembcke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
typedef struct cpVect{
   float x,y;
} cpVect;

#define cpvzero ((cpVect){0.0f, 0.0f})

static inline cpVect
cpv(const float x, const float y)
{
   return (cpVect){x, y};
}

static inline cpVect
cpvadd(const cpVect v1, const cpVect v2)
{
   return cpv(v1.x + v2.x, v1.y + v2.y);
}

static inline cpVect
cpvneg(const cpVect v)
{
   return cpv(-v.x, -v.y);
}

static inline cpVect
cpvsub(const cpVect v1, const cpVect v2)
{
   return cpv(v1.x - v2.x, v1.y - v2.y);
}

static inline cpVect
cpvmult(const cpVect v, const float s)
{
   return cpv(v.x*s, v.y*s);
}

static inline float
cpvdot(const cpVect v1, const cpVect v2)
{
   return v1.x*v2.x + v1.y*v2.y;
}

static inline float
cpvcross(const cpVect v1, const cpVect v2)
{
   return v1.x*v2.y - v1.y*v2.x;
}

static inline cpVect
cpvperp(const cpVect v)
{
   return cpv(-v.y, v.x);
}

static inline cpVect
cpvproject(const cpVect v1, const cpVect v2)
{
   return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2));
}

static inline cpVect
cpvrotate(const cpVect v1, const cpVect v2)
{
   return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
}

static inline cpVect
cpvunrotate(const cpVect v1, const cpVect v2)
{
   return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
}

extern inline float cpvlength(const cpVect v);
extern inline float cpvlengthsq(const cpVect v);
extern inline cpVect cpvnormalize(const cpVect v);
extern inline cpVect cpvforangle(const float a);
extern inline float cpvtoangle(const cpVect v);
extern char *cpvstr(const cpVect v);


gives me this fb header:

Code: Select all

''
''
'' cpVect -- header translated with help of SWIG FB wrapper
''
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
''         be included in other distributions without authorization.
''
''
#ifndef __cpVect_bi__
#define __cpVect_bi__

type cpVect
   x as single
   y as single
end type

type cpVect as any

declare function cpvdot cdecl alias "cpvdot" (byval v1 as cpVect, byval v2 as cpVect) as single
declare function cpvcross cdecl alias "cpvcross" (byval v1 as cpVect, byval v2 as cpVect) as single
declare function cpvlength cdecl alias "cpvlength" (byval v as cpVect) as single
declare function cpvlengthsq cdecl alias "cpvlengthsq" (byval v as cpVect) as single
declare function cpvtoangle cdecl alias "cpvtoangle" (byval v as cpVect) as single
declare function cpvstr cdecl alias "cpvstr" (byval v as cpVect) as zstring ptr

#endif


I know I have to change "type cpVect as any" to "type cpVect as any ptr"
I'm not sure what to do with "#define cpvzero ((cpVect){0.0f, 0.0f})". It looks like its defining something with a casted type. How do I handle that?

As far as the function definitions go, I don't mind recoding them, if that's what I have to do, but I'm not sure how to handle those return values. Any help?

Also, another translation seems a bit odd to me.
.h:

Code: Select all

typedef int (*cpHashSetEqlFunc)(void *ptr, void *elt);
typedef void *(*cpHashSetTransFunc)(void *ptr, void *data);
typedef void (*cpHashSetIterFunc)(void *elt, void *data);
typedef int (*cpHashSetRejectFunc)(void *elt, void *data);

to .bi:

Code: Select all

type cpHashSetEqlFunc as function cdecl(byval as any ptr, byval as any ptr) as integer
type cpHashSetTransFunc as sub cdecl(byval as any ptr, byval as any ptr)
type cpHashSetIterFunc as sub cdecl(byval as any ptr, byval as any ptr)
type cpHashSetRejectFunc as function cdecl(byval as any ptr, byval as any ptr) as integer


That looks like some pretty spooky pointer referencing going on there. Did swig get this one right?

Thanks for any help you can offer!
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Dec 29, 2007 7:11

Remove the 'type cpVect as any ptr', since that type is defined as a 'type' in freebasic.

As for the macro, here's what you want:

Code: Select all

#define cpvzero (type<cpVect>(0, 0))


The crazy ptr stuff at the bottom looks ok, I think.
aleofjax
Posts: 117
Joined: Oct 18, 2007 8:10

Postby aleofjax » Dec 29, 2007 15:26

Alright, cool. Thanks!

What about those function Definitions? Like I said, I don't mind rewriting them myself, but... Well, for example:

Code: Select all

Static inline cpVect
cpv(Const float x, Const float y)
{
        Return (cpVect){x, y};
}


What exactly is that returning? It looks like a cpVect Object initialized with (x,y), right? So that should be like:

Code: Select all

Function cpv Cdecl Alias "cpv" (Byval x As Single, y As Single) as cpVect
   Return New cpVect( x, y )
End Function


Did I get that right? Should that work?
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Postby VirusScanner » Dec 30, 2007 4:47

New returns a pointer. You want

Code: Select all

private function cpv( byval x as const single, byval y as const single ) as cpVect
  return type<cpVect>( x, y )
end function
aleofjax
Posts: 117
Joined: Oct 18, 2007 8:10

Postby aleofjax » Dec 30, 2007 6:36

Ah, I thought "New" might give me problems. Thanks.

One more thing (I think!): Should all functions defined as static in the c header be defined as private in the translation?

Edit:
Oh no! My C header has a void pointer called "ptr"! It tried to translate and of course "Ptr As Any Ptr" is no good. I know I can rename the pointer, but does that mean I'll have to track down every instance of it in the c source, then recompile everything? <panic>
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Dec 30, 2007 9:10

I think you're right about static/private

A better idea might be to change 'ptr' in the C file to something better, then run swig over that...

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 1 guest