Page 1 of 1

C to PB conversion needed

Posted: Tue Oct 18, 2011 4:55 pm
by MushroomHead
Hi guys,

I have 2 "C" headers that need converting to PB. They are originally for DLL plugins for TrueBasic and I want make some DLLs for my own use ... I don't have much any C experience and would be glad if someone can convert them for me :-

Code: Select all

typedef struct
{
	long	refcnt;		/* Number of True BASIC strings pointing to this physical string. */
	long	len;		/* Length of the True BASIC string text. */
    char    text[4];	/* The start of the text of the string. Not limited to 4 chars. */
} string;

typedef struct
{
	string	*str;		/* A pointer to the actual string. */
	long	maxlen;		/* Unused information. */
} tb_sptr;

typedef union
{
	signed char	ch[8];
	char		b[8];
	double		d;
	struct
	{
		long		val, flag;	/* If flag is -1, then val is used to store the number as an integer. */
	} i;
	struct
	{
		unsigned long	low;
		long			high;
	} x;
	struct
	{
		short		sig3, sig2, sig1, exp;
	} e;
} tb_num;

#define MAX_DIMS	30

typedef struct
{
	long	base, extent;
} range;

typedef struct
{
	long	dimensions;
	long	array_tag;	/* TAG_NARR if numeric array, TAG_SARR if string array. */
	long	maxsize, maxlen;
	long	elements;
	range	range[MAX_DIMS];
} array;

#define TAG_NARR	-1
#define	TAG_SARR	-2

Code: Select all

// A simple Test Program ...
#define		DLLPREFIX	__declspec(dllexport)

/* CALL TB_PlaySound(soundfile,syncflag)
	soundfile is name of a .WAV file
	syncflag is 0 for asynchronous sound, 1 for synchronous sound.
*/

DLLPREFIX void
TB_PlaySound(char *ptrs)
{
	tb_sptr	*soundfile;
	tb_num	*syncflag;

	DWORD	 soundtype = SND_FILENAME;
	char	*csoundfile;

	/* Get flag and adjust soundtype if necessary */
	syncflag = *(tb_num **) ptrs;
	if (syncflag->i.flag != -1) return;
	if (syncflag->i.val) soundtype = soundtype & SND_SYNC; else soundtype = soundtype & SND_ASYNC;

	/* Advance to next TB arg */
	ptrs += 8;

	/* Get TB string with sound file name */
	soundfile = *(tb_sptr **) ptrs;

	/* Convert TB string to C string */
	csoundfile = (char *) calloc(soundfile->str->len+1,1);
	if (csoundfile == NULL) return;
	memcpy(csoundfile,soundfile->str->text,soundfile->str->len);

	/* Play the sound using the Windows API */
	(void) PlaySound(csoundfile,NULL,soundtype);

	/* Free the C string */
	free(csoundfile);
}

Thanks for your help.

Re: C to PB conversion needed

Posted: Tue Oct 18, 2011 6:34 pm
by Tenaja
Loosely defined, in C, typedef's are just structures. Convert that, and you are well on your way. If you have problems, post your progress.

Re: C to PB conversion needed

Posted: Tue Oct 18, 2011 7:31 pm
by Psychophanta
My approach is:

For the 1st one:

Code: Select all

Structure _string_
  refcnt.l;      /* Number of True BASIC strings pointing to this physical string. */
  len.l;      /* Length of the True BASIC string text. */
  text.a[4];   /* The start of the text of the string. Not limited to 4 chars. */
EndStructure

Structure tb_sptr
  *str._string_;      /* A pointer to the actual string. */
  maxlen.l;      /* Unused information. */
EndStructure

Structure _i_
  val.l:flag.l;   /* If flag is -1, then val is used to store the number as an integer. */
EndStructure
Structure x
  low.l;unsigned
  high.l;
EndStructure
Structure e
  sig3.w:sig2.w:sig1.w:exp.w;
EndStructure
Structure tb_num
  StructureUnion
    ch.b[8];
    b.a[8];
    d.d;
    i._i_;
    x.x;
    e.e;
  EndStructureUnion
EndStructure

Macro MAX_DIMS:30:EndMacro

Structure range
  base.l:extent.l;
EndStructure

Structure _Array_
  dimensions.l;
  array_tag.l;   /* TAG_NARR if numeric array, TAG_SARR if string array. */
  maxsize.l:maxlen.l;
  elements.l;
  range.range[MAX_DIMS];
EndStructure

Macro TAG_NARR:-1:EndMacro
Macro TAG_SARR:-2:EndMacro
and for the 2nd:

Code: Select all

;// A simple Test Program ...
Macro DLLPREFIX:__declspec(dllexport):EndMacro

; /* CALL TB_PlaySound(soundfile,syncflag)
;    soundfile is name of a .WAV file
;    syncflag is 0 For asynchronous sound, 1 For synchronous sound.
; */

ProcedureCDLL TB_PlaySound(*ptrs.a)
  Protected *soundfile.tb_sptr,*syncflag.tb_num,soundtype.l = SND_FILENAME,*csoundfile.a;

  ; /* Get flag And adjust soundtype If necessary */
  *syncflag=*ptrs;
  If *syncflag\i\flag<>-1:ProcedureReturn:EndIf;
  If *syncflag\i\val:soundtype & SND_SYNC:Else:soundtype & SND_ASYNC:EndIf;

  ; /* Advance To Next TB arg */
  *ptrs+8;

  ; /* Get TB string With sound file name */
  *soundfile=*ptrs;

  ; /* Convert TB string To C string */
   *csoundfile = AllocateMemory(*soundfile\str\len+1);
   If *csoundfile=#Null:ProcedureReturn:EndIf;
   CopyMemory(*soundfile\str\text,*csoundfile,*soundfile\str\len);

  ; /* Play the sound using the Windows API */
   PlaySound_(*csoundfile,#Null,soundtype);

  ; /* Free the C string */
   FreeMemory(*csoundfile);
 EndProcedure
...but however, i have to say that the very interesting thing would be a PB -> C conversor.

Re: C to PB conversion needed

Posted: Thu Oct 20, 2011 3:02 pm
by MushroomHead
Cheers Psychophanta, will try it later and see how it goes.