What Monkey type corresponds to FILE *?

Monkey Forums/Monkey Programming/What Monkey type corresponds to FILE *?

Canardian(Posted 2011) [#1]
I see that Monkey uses FILE * in various files, but how do you get a FILE * variable back to Monkey?
I tried with Field f:Object, and it works, it creates a file, writes a correct Int value into it, and closes it, but Monkey still crashes with a NULL pointer exception when returning the FILE * to Monkey as Object.

Since it works, maybe I should rather remove the NULL pointer crashing from Monkey? Maybe it's using somekind of GC which causes it to crash, although there's no need for GC here.

My fileio.cpp looks like this:
// fileio.cpp

Object* openfile(String s, String mode)
{
	FILE *f=NULL;
	f=fopen(s.ToCString<char>(),mode.ToCString<char>());
	return (Object *)f;
}

void closefile(Object *f)
{
	if((FILE *)f)fclose((FILE *)f);
}

void writefileint(Object *f,int i)
{
	fwrite(&i,sizeof(i),1,(FILE *)f);
}

int readfileint(Object *f)
{
	int i=0;
	fread(&i,sizeof(i),1,(FILE *)f);
	return i;
}


And the Monkey file looks like this:
Import mojo

#If TARGET="html5" Then
	Import "fileio.js"		' not yet implemented!
#Else
	Import "fileio.cpp"  	' tell the compiler to include code from fileio.cpp
#Endif

Extern
	' the C++ function mytestfunc1 is renamed as MyFunc1 in Monkey
	Function OpenFile:Object(s:String,mode:String)="openfile"
	Function CloseFile:Void(f:Object)="closefile"
	Function WriteFileInt:Void(f:Object,i:Int)="writefileint"
	Function ReadFileInt:Int(f:Object)="readfileint"

Public

Class Game Extends App
	Field i:Int
	Field f:Object
	Method OnCreate()
		i=123
		f=OpenFile("testfile.bin","wb")	' call the c++ function here
		WriteFileInt(f,i)
		CloseFile(f)
		SetUpdateRate 60	' this sets the update rate in FPS
	End
	Method OnRender()
		Cls 30,30,30		' Cls is needed, else nothing will be drawn
		DrawText "Hello World",0,0
		DrawText "i="+i,0,20
	End
End

Function Main()
	New Game
End



JIM(Posted 2011) [#2]
You could try using ints in monkey and doing the casting in C++. Sine you know, pointers are ints.


Brucey(Posted 2011) [#3]
Sine you know, pointers are ints

Only on 32-bit builds?


Canardian(Posted 2011) [#4]
@JIM: Wow you're awesome, thanks a lot!
Now it works and doesn't crash anymore:
// fileio.cpp

int openfile(String s, String mode)
{
	FILE *f=NULL;
	f=fopen(s.ToCString<char>(),mode.ToCString<char>());
	return (int)f;
}

void closefile(int f)
{
	if((FILE *)f)fclose((FILE *)f);
}

void writefileint(int f,int i)
{
	fwrite(&i,sizeof(i),1,(FILE *)f);
}

int readfileint(int f)
{
	int i=0;
	fread(&i,sizeof(i),1,(FILE *)f);
	return i;
}


Import mojo

#If TARGET="html5" Then
	Import "fileio.js"		' not yet implemented!
#Else
	Import "fileio.cpp"  	' tell the compiler to include code from fileio.cpp
#Endif

Extern
	Function OpenFile:Int(s:String,mode:String)="openfile"
	Function CloseFile:Void(f:Int)="closefile"
	Function WriteFileInt:Void(f:Int,i:Int)="writefileint"
	Function ReadFileInt:Int(f:Int)="readfileint"

Public

Class Game Extends App
	Field i:Int
	Field f:Int
	Method OnCreate()
		i=123
		f=OpenFile("testfile.bin","wb")	' call the c++ function here
		WriteFileInt(f,i)
		CloseFile(f)
		SetUpdateRate 60	' this sets the update rate in FPS
	End
	Method OnRender()
		Cls 30,30,30		' Cls is needed, else nothing will be drawn
		DrawText "Hello World",0,0
		DrawText "i="+i,0,20
	End
End

Function Main()
	New Game
End



Brucey(Posted 2011) [#5]
I guess 32-bits is the future after all ! :-)


JIM(Posted 2011) [#6]
Well yeah, forgot to mention that :) But as long as it works, 32-bit is the future :D


Canardian(Posted 2011) [#7]
I think 64-bit will never work completely, as long 32-bit supporting hardware still exists. Developers are lazy and don't follow the newest technology if the old technology still exists and works. Maybe when all hardware is 128-bit, then developers start slowly to develop 64-bit programs :)

Well, looking at the mirror, I still use 32-bit Windows XP, and have no plans to upgrade to 32-bit or 64-bit Windows 7 until 2020 or later. I tried it a few times, and it was slower than XP with OpenGL apps.


Brucey(Posted 2011) [#8]
Ah... I forget all those people who use Windows... never mind! :-)


Canardian(Posted 2011) [#9]
Yeah, I'm trying to, and I have already 5 Linuxes (Debian, openSUSE, Ubuntu, Mint, Debian64), 1 Android and 1 BSD in my GRUB loader. But Monkey was again released first on Windows and Mac, so where's my Linux and BSD version of Monkey? :)


Brucey(Posted 2011) [#10]
so where's my Linux and BSD version of Monkey?

I guess you'll have to wait for Mark to sort that out.
I could, but there's not much point if Mark is going to do it anyway.