Example #1
0
static void fileext_free_file( value o ) {
	fio *f = val_file(o);
	if(f != NULL)
		f->close();
	delete f;
	val_gc(o, NULL);
}
Example #2
0
/**
	file_write : 'file -> s:string -> p:int -> l:int -> int
	<doc>
	Write up to [l] chars of string [s] starting at position [p]. 
	Returns the number of chars written which is >= 0.
	</doc>
**/
static value file_write( value o, value s, value pp, value n ) {
	int p, len;
	fio *f;
	val_check_kind(o,k_file);
	val_check(s,string);
	val_check(pp,int);
	val_check(n,int);
	f = val_file(o);
	p = val_int(pp);
	len = val_int(n);
	if( p < 0 || len < 0 || p > val_strlen(s) || p + len > val_strlen(s) )
		neko_error();
	while( len > 0 ) {
		int d;
		POSIX_LABEL(file_write_again);
		d = (int)fwrite(val_string(s)+p,1,len,f->io);
		if( d <= 0 ) {
			HANDLE_FINTR(f->io,file_write_again);
			file_error("file_write",f);
		}
		p += d;
		len -= d;
	}
	return n;
}
Example #3
0
static void free_stdfile( value v )
{
        // Delete, but do not close...
	fio *file =  val_file(v);
	delete file;
	val_gc(v,NULL);
}
Example #4
0
/**
	file_close : 'file -> void
	<doc>Close an file. Any other operations on this file will fail</doc> 
**/
static value file_close( value o ) {	
	fio *f;
	val_check_kind(o,k_file);
	f = val_file(o);
	f->close();
	return alloc_bool(true);
}
Example #5
0
static void free_file( value v )
{
	fio *file =  val_file(v);
	file->close();
	delete file;
	free_abstract(v);
}
Example #6
0
/**
	file_read : 'file -> s:string -> p:int -> l:int -> int
	<doc>
	Read up to [l] chars into the string [s] starting at position [p].
	Returns the number of chars readed which is > 0 (or 0 if l == 0).
	</doc>
**/
static value file_read( value o, value s, value pp, value n ) {
	fio *f;
	int p;
	int len;
	val_check_kind(o,k_file);
	val_check(s,string);
	val_check(pp,int);
	val_check(n,int);
	f = val_file(o);
	p = val_int(pp);
	len = val_int(n);
	if( p < 0 || len < 0 || p > val_strlen(s) || p + len > val_strlen(s) )
		neko_error();
	while( len > 0 ) {
		int d;
		POSIX_LABEL(file_read_again);
		d = (int)fread((char*)val_string(s)+p,1,len,f->io);
		if( d <= 0 ) {
			int size = val_int(n) - len;
			HANDLE_FINTR(f->io,file_read_again);
			if( size == 0 )
				file_error("file_read",f);
			return alloc_int(size);
		}
		p += d;
		len -= d;
	}
	return n;
}
Example #7
0
static void free_file( value v )
{
	fio *file =  val_file(v);
	file->close();
	delete file;
	val_gc(v,NULL);
}
Example #8
0
/**
	file_read : 'file -> s:string -> p:int -> l:int -> int
	<doc>
	Read up to [l] chars into the string [s] starting at position [p].
	Returns the number of chars readed which is > 0 (or 0 if l == 0).
	</doc>
**/
static value file_read( value o, value s, value pp, value n ) {
	fio *f;
	int p;
	int len;
	int buf_len;
	val_check_kind(o,k_file);
	val_check(s,buffer);
	buffer buf = val_to_buffer(s);
	buf_len = buffer_size(buf);
	val_check(pp,int);
	val_check(n,int);
	f = val_file(o);
	p = val_int(pp);
	len = val_int(n);
	if( p < 0 || len < 0 || p > buf_len || p + len > buf_len )
		return alloc_null();
	gc_enter_blocking();
	while( len > 0 ) {
		int d;
		POSIX_LABEL(file_read_again);
		d = (int)fread(buffer_data(buf)+p,1,len,f->io);
		if( d <= 0 ) {
			int size = val_int(n) - len;
			HANDLE_FINTR(f->io,file_read_again);
			if( size == 0 )
				file_error("file_read",f);
			return alloc_int(size);
		}
		p += d;
		len -= d;
	}
	gc_exit_blocking();
	return n;
}
Example #9
0
static void free_stdfile( value v )
{
        // Delete, but do not close...
	fio *file =  val_file(v);
	delete file;
	free_abstract(v);
}
Example #10
0
/**
	file_close : 'file -> void
	<doc>Close an file. Any other operations on this file will fail</doc> 
**/
static value file_close( value o ) {	
	fio *f;
	val_check_kind(o,k_file);
	f = val_file(o);
	fclose(f->io);
	val_kind(o) = NULL;
	return val_null;
}
Example #11
0
/**
	file_flush : 'file -> void
	<doc>Flush the file buffer</doc>
**/
static value file_flush( value o ) {
	fio *f;
	val_check_kind(o,k_file);
	f = val_file(o);
	if( fflush( f->io ) != 0 )
		file_error("file_flush",f);
	return val_true;
}
Example #12
0
/**
	file_flush : 'file -> void
	<doc>Flush the file buffer</doc>
**/
static value file_flush( value o ) {
	fio *f;
	val_check_kind(o,k_file);
	f = val_file(o);
	gc_enter_blocking();
	if( fflush( f->io ) != 0 )
		file_error("file_flush",f);
	gc_exit_blocking();
	return alloc_bool(true);
}
Example #13
0
/**
	file_tell : 'file -> int
	<doc>Return the current position in the file</doc>
**/
static value file_tell( value o ) {
	int p;
	fio *f;
	val_check_kind(o,k_file);
	f = val_file(o);
	p = ftell(f->io);
	if( p == -1 )
		file_error("file_tell",f);
	return alloc_int(p);
}
Example #14
0
/**
	file_seek : 'file -> pos:int -> mode:int -> void
	<doc>Use [fseek] to move the file pointer.</doc>
**/
static value file_seek( value o, value pos, value kind ) {
	fio *f;
	val_check_kind(o,k_file);
	val_check(pos,int);
	val_check(kind,int);
	f = val_file(o);
	if( fseek(f->io,val_int(pos),val_int(kind)) != 0 )
		file_error("file_seek",f);
	return val_null;
}
Example #15
0
/**
	file_read_char : 'file -> int
	<doc>Read a char from the file. Exception on error</doc>
**/
static value file_read_char( value o ) {
	unsigned char cc;
	fio *f;
	val_check_kind(o,k_file);
	f = val_file(o);
	POSIX_LABEL(read_char_again);
	if( fread(&cc,1,1,f->io) != 1 ) {
		HANDLE_FINTR(f->io,read_char_again);
		file_error("file_read_char",f);
	}
	return alloc_int(cc);
}
Example #16
0
/**
        file_close : 'file -> void
        <doc>Close an file. Any other operations on this file will fail</doc>
**/
static value tmpfile_close( value o ) {
        fio *f;
	vkind k_file = kind_import("file");
        val_check_kind(o,k_file);
        f = val_file(o);
        fclose(f->io);

	// no longer needs garbage collection
	val_gc(o,NULL);
        val_kind(o) = NULL;
	return val_true;
}
Example #17
0
/**
	file_seek : 'file -> pos:int -> mode:int -> void
	<doc>Use [fseek] to move the file pointer.</doc>
**/
static value file_seek( value o, value pos, value kind ) {
	fio *f;
	val_check_kind(o,k_file);
	val_check(pos,int);
	val_check(kind,int);
	f = val_file(o);
	gc_enter_blocking();
	if( fseek(f->io,val_int(pos),val_int(kind)) != 0 )
		file_error("file_seek",f);
	gc_exit_blocking();
	return alloc_bool(true);
}
Example #18
0
/**
	file_write_char : 'file -> c:int -> void
	<doc>Write the char [c]. Error if [c] outside of the range 0..255</doc>
**/
static value file_write_char( value o, value c ) {
	unsigned char cc;
	fio *f;
	val_check(c,int);
	val_check_kind(o,k_file);
	if( val_int(c) < 0 || val_int(c) > 255 )
		neko_error();
	cc = (char)val_int(c);
	f = val_file(o);
	POSIX_LABEL(write_char_again);
	if( fwrite(&cc,1,1,f->io) != 1 ) {
		HANDLE_FINTR(f->io,write_char_again);
		file_error("file_write_char",f);
	}
	return val_null;
}
Example #19
0
/**
	file_write_char : 'file -> c:int -> void
	<doc>Write the char [c]. Error if [c] outside of the range 0..255</doc>
**/
static value file_write_char( value o, value c ) {
	unsigned char cc;
	fio *f;
	val_check(c,int);
	val_check_kind(o,k_file);
	if( val_int(c) < 0 || val_int(c) > 255 )
		return alloc_null();
	cc = (char)val_int(c);
	f = val_file(o);
	gc_enter_blocking();
	POSIX_LABEL(write_char_again);
	if( fwrite(&cc,1,1,f->io) != 1 ) {
		HANDLE_FINTR(f->io,write_char_again);
		file_error("file_write_char",f);
	}
	gc_exit_blocking();
	return alloc_bool(true);
}
Example #20
0
static void fileext_free_stdio( value o ) {
	fio *f = val_file(o);
	delete f;
	val_gc(o, NULL);
}
Example #21
0
static void cleanup( value o ) {
        fio *f = val_file(o);
        fclose(f->io);
}
Example #22
0
/**
	file_eof : 'file -> bool
	<doc>Tell if we have reached the end of the file</doc>
**/
static value file_eof( value o ) {
	val_check_kind(o,k_file);
	return alloc_bool( feof(val_file(o)->io) );
}
Example #23
0
/**
	file_name : 'file -> string
	<doc>Return the name of the file which was opened</doc>
**/
static value file_name( value o ) {
	val_check_kind(o,k_file);
	return alloc_filename(val_file(o)->name.c_str());
}