示例#1
0
static void UT_CastToString( DaoProcess *proc, DaoValue *p[], int n )
{
	DaoxUserType *self = (DaoxUserType*) p[0];
	DString *res = DaoProcess_PutChars( proc, "" );
	DString_Reserve( res, 50 );
	res->size = sprintf( res->chars, "UserType.{%" DAO_I64 "}", self->value );
}
示例#2
0
文件: daoStream.c 项目: daokoder/dao
static void DaoIO_Read( DaoProcess *proc, DaoValue *p[], int N )
{
	DaoStream *self = proc->stdioStream;
	DString *ds = DaoProcess_PutChars( proc, "" );
	int ch, size, amount = -1; /* amount=-2: all; amount=-1: line; amount>=0: bytes; */
	char buf[IO_BUF_SIZE];

	if( self == NULL ) self = proc->vmSpace->stdioStream;
	if( N > 0 ){
		self = (DaoStream*) p[0];
		amount = -2;
	}
	if( DaoIO_CheckMode( self, proc, DAO_STREAM_READABLE ) == 0 ) return;
	if( N > 1 ){
		if( p[1]->type == DAO_INTEGER ){
			amount = p[1]->xInteger.value;
			if( amount < 0 ){
				DaoProcess_RaiseError( proc, NULL, "cannot read negative amount!" );
				return;
			}
		}else{
			amount = - 1 - p[1]->xEnum.value;
		}
	}
	DString_Reset( ds, 0 );
	self->Read( self, ds, amount );
	if( self->mode & DAO_STREAM_AUTOCONV ) DString_ToUTF8( ds );
}
示例#3
0
void DaoCGI_RandomString( DaoProcess *proc, DaoValue *p[], int N )
{
    int len = DaoValue_TryGetInteger( p[0] );
    int alnum = DaoValue_TryGetInteger( p[1] );
    int i;
    DString *res = DaoProcess_PutChars( proc, "" );
    if( alnum ) {
        for(i=0; i<len; i++)
            DString_AppendChar( res, alnumChars[ (int)(62 * (rand()/(RAND_MAX+1.0)) ) ] );
    } else {
        for(i=0; i<len; i++)
            DString_AppendChar( res, (char)(255 * (rand()/(RAND_MAX+1.0))) );
    }
}
示例#4
0
static void ZIP_Compress( DaoProcess *proc, DaoValue *p[], int N )
{
	DString *source = p[0]->xString.value;
	DString *res = DaoProcess_PutChars( proc, "" );
	unsigned int resLen = source->size;
	int block = 100000;
	daoint size = source->size;

	DString_Reserve( res, size );
	size = 1 + size / block;
	if( size > 9 ) size = 9;
	BZ2_bzBuffToBuffCompress( res->chars, & resLen, source->chars, source->size, size, 0, 30 );
	DString_Reset( res, resLen );
}
示例#5
0
static void ZIP_Decompress( DaoProcess *proc, DaoValue *p[], int N )
{
	DString *source = p[0]->xString.value;
	DString *res = DaoProcess_PutChars( proc, "" );
	unsigned int resLen;

	/* TODO: get decompressed size from the compressed data? */
	DString_Reserve( res, source->size );
	resLen = res->bufSize;
	while( resLen == res->bufSize ){
		DString_Reserve( res, 2*resLen );
		resLen = res->bufSize;
		BZ2_bzBuffToBuffDecompress( res->chars, & resLen, source->chars, source->size, 0, 0 );
	}
	DString_Reset( res, resLen );
}
示例#6
0
static void DaoIO_ReadFile( DaoProcess *proc, DaoValue *p[], int N )
{
	DString *res = DaoProcess_PutChars( proc, "" );
	daoint silent = p[1]->xBoolean.value;
	if( DString_Size( p[0]->xString.value ) ==0 ){
		char buf[4096];
		while(1){
			size_t count = fread( buf, 1, sizeof( buf ), stdin );
			if( count ==0 ) break;
			DString_AppendBytes( res, buf, count );
		}
	}else{
		FILE *fin = DaoIO_OpenFile( proc, p[0]->xString.value, "r", silent );
		struct stat info;
		if( fin == NULL ) return;
		fstat( fileno( fin ), &info );
		DString_Reserve( res, info.st_size );
		DString_Reset( res, fread( res->chars, 1, info.st_size, fin ) );
		fclose( fin );
	}
}
示例#7
0
static void DaoSTD_Version( DaoProcess *proc, DaoValue *p[], int N )
{
	DaoProcess_PutChars( proc, p[0]->xBoolean.value ? dao_version : DAO_VERSION );
}
示例#8
0
static void DaoSTD_About( DaoProcess *proc, DaoValue *p[], int N )
{
	DString *str = DaoProcess_PutChars( proc, "" );
	Dao_AboutVars( proc, p, N, str );
}