Convert C structure to purebasic

Just starting out? Need help? Post your questions and find answers here.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Convert C structure to purebasic

Post by LuckyLuke »

Hi,

I'm working on a wrapper for the libwebsockets library: (https://libwebsockets.org/)
But I have some issues with this C structure:

Code: Select all

/** struct lws_http_mount
 *
 * arguments for mounting something in a vhost's url namespace
 */
struct lws_http_mount {
	const struct lws_http_mount *mount_next;
	/**< pointer to next struct lws_http_mount */
	const char *mountpoint;
	/**< mountpoint in http pathspace, eg, "/" */
	const char *origin;
	/**< path to be mounted, eg, "/var/www/warmcat.com" */
	const char *def;
	/**< default target, eg, "index.html" */
	const char *protocol;
	/**<"protocol-name" to handle mount */

	const struct lws_protocol_vhost_options *cgienv;
	/**< optional linked-list of cgi options.  These are created
	 * as environment variables for the cgi process
	 */
	const struct lws_protocol_vhost_options *extra_mimetypes;
	/**< optional linked-list of mimetype mappings */
	const struct lws_protocol_vhost_options *interpret;
	/**< optional linked-list of files to be interpreted */

	int cgi_timeout;
	/**< seconds cgi is allowed to live, if cgi://mount type */
	int cache_max_age;
	/**< max-age for reuse of client cache of files, seconds */
	unsigned int auth_mask;
	/**< bits set here must be set for authorized client session */

	unsigned int cache_reusable:1; /**< set if client cache may reuse this */
	unsigned int cache_revalidate:1; /**< set if client cache should revalidate on use */
	unsigned int cache_intermediaries:1; /**< set if intermediaries are allowed to cache */

	unsigned char origin_protocol; /**< one of enum lws_mount_protocols */
	unsigned char mountpoint_len; /**< length of mountpoint string */

	const char *basic_auth_login_file;
	/**<NULL, or filepath to use to check basic auth logins against. (requires LWSAUTHM_DEFAULT) */

	/* Add new things just above here ---^
	 * This is part of the ABI, don't needlessly break compatibility
	 *
	 * The below is to ensure later library versions with new
	 * members added above will see 0 (default) even if the app
	 * was not built against the newer headers.
	 */

	void *_unused[2]; /**< dummy */
};
This purebasic structure is working fine, but I had to add some dummy fields.

Code: Select all

Structure lws_http_mount
  mount_next.i
  mountpoint.i
  origin.i                ;
  def.i                   ;
  protocol.i              ;
  cgienv.i                ;
  extra_mimetype.i        ;
  interpret.i             ;
  cgi_timeout.l           ;
  cache_max_age.l         ;
  auth_mask.l;
  cache_reusable.l
  origin_protocol.a      ; 
 
  mountpoint_len.a       ;
  dummya.w
  dummyb.w
  dummyc.w

  basic_auth_login_file.i   
EndStructure
This one is not working. The field mountpoint_len is not filled. What am I doing wrong ?

Code: Select all

Structure lws_http_mount Align 4
  mount_next.i
  mountpoint.i
  origin.i                ;
  def.i                   ;
  protocol.i              ;
  cgienv.i                ;
  extra_mimetype.i        ;
  interpret.i             ;
  cgi_timeout.l           ;
  cache_max_age.l         ;
  auth_mask.l             ;
  cache_reusable.a
  origin_protocol.a      ; 
 
  mountpoint_len.a      ;
  
  basic_auth_login_file.i   
EndStructure
Can someone help me to understand this please ?

Thanks !

LuckyLuke
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Convert C structure to purebasic

Post by spikey »

Did you try this at all?

Code: Select all

Structure lws_http_mount Align #PB_Structure_AlignC
PB's layouts aren't always the same as C's and this could cause, potentially, interoperability problems. This tells PB to lay the structure out the way C would do it - it would be my first attempt to fix this problem.

If that doesn't help - are we talking about the 32- or 64-bit version of the compiler? This structure would get laid out differently between the two versions...
infratec
Always Here
Always Here
Posts: 6871
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Convert C structure to purebasic

Post by infratec »

This fits the structure in C

Code: Select all

Structure lws_http_mount Align #PB_Structure_AlignC 
  *mount_next
  *mountpoint
  *origin
  *def
  *protocol
  *cgienv
  *extra_mimetype
  *interpret
  cgi_timeout.l
  cache_max_age.l
  auth_mask.l
  cache_reusable.a
  cache_revalidate.a
  cache_intermediaries.a
  Dummy.a
  origin_protocol.a
  mountpoint_len.a
  *basic_auth_login_file
  unused.i[2]
EndStructure

Debug "Size: " + Str(SizeOf(lws_http_mount))
Debug "auth_mask: " + Str(OffsetOf(lws_http_mount\auth_mask))
Debug "origin_protocol: " + Str(OffsetOf(lws_http_mount\origin_protocol))
Debug "basic_auth_login_file: " + Str(OffsetOf(lws_http_mount\basic_auth_login_file))
tested against:

Code: Select all

int main(void) {
	struct lws_http_mount test;

	printf("Structure size: %i\n", sizeof(test));
	printf("auth_mask: %i\n", offsetof(struct lws_http_mount, auth_mask));
	printf("cache_revalidate: %i\n", offsetof(struct lws_http_mount, cache_revalidate));
	printf("origin_protocol: %i\n", offsetof(struct lws_http_mount, origin_protocol));
	printf("basic_auth_login_file: %i\n", offsetof(struct lws_http_mount, basic_auth_login_file));
	exit(0);
}
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

[SOLVED] Re: Convert C structure to purebasic

Post by LuckyLuke »

Thanks for the help !

I'm very close to a working example now on windows ... (linux will follow).

LuckyLuke
Post Reply