static void DaoIO_ReadFile( DaoProcess *proc, DaoValue *p[], int N ) { DString *res = DaoProcess_PutMBString( proc, "" ); daoint silent = p[1]->xInteger.value; if( proc->vmSpace->options & DAO_OPTION_SAFE ){ DaoProcess_RaiseException( proc, DAO_ERROR, "not permitted" ); return; } if( DString_Size( p[0]->xString.data ) ==0 ){ char buf[1024]; while(1){ size_t count = fread( buf, 1, sizeof( buf ), stdin ); if( count ==0 ) break; DString_AppendDataMBS( res, buf, count ); } }else{ FILE *fin = DaoIO_OpenFile( proc, p[0]->xString.data, "r", silent ); struct stat info; if( fin == NULL ) return; fstat( fileno( fin ), &info ); DString_Resize( res, info.st_size ); DString_Resize( res, fread( res->mbs, 1, res->size, fin ) ); fclose( fin ); } }
static void DaoBUF_GetString( DaoProcess *proc, DaoValue *p[], int N ) { Dao_Buffer *self = (Dao_Buffer*) p[0]; DString *str = DaoProcess_PutMBString( proc, "" ); if( p[1]->xEnum.value == 0 ){ DString_Resize( str, self->size ); memcpy( str->mbs, self->buffer.pVoid, self->size ); }else{ DString_ToWCS( str ); DString_Resize( str, self->size / sizeof( wchar_t ) ); memcpy( str->wcs, self->buffer.pVoid, str->size * sizeof( wchar_t ) ); } }
/* Real copying, no implicit sharing here. For thread safty. */ DString* DString_DeepCopy( DString *self ) { int share = self->sharing; DString *copy = DString_New(); DString_SetSharing( copy, share ); DString_Resize( copy, self->size ); memcpy( copy->chars, self->chars, self->size *sizeof(char) ); return copy; }
void DString_Reset( DString *self, daoint size ) { if( size <= self->bufSize ){ DString_Detach( self, self->bufSize ); self->size = size; self->chars[size] = '\0'; return; } DString_Resize( self, size ); }
void DString_SetWords( DString *self, const wchar_t *bytes, int count ) { int i; wchar_t *data; DString_ToWCS( self ); DString_Resize( self, count ); data = self->wcs; for( i=0; i<count; i++ ) data[i] = bytes[i]; }
void DaoStream_WriteString( DaoStream *self, DString *val ) { int i; if( val->mbs ){ const char *data = val->mbs; if( self->redirect && self->redirect->StdioWrite ){ DString *mbs = DString_New(1); DString_SetDataMBS( mbs, data, val->size ); self->redirect->StdioWrite( self->redirect, mbs ); DString_Delete( mbs ); }else if( self->file ){ if( self->format ){ fprintf( self->file, self->format, data ); }else{ DaoFile_WriteString( self->file, val ); } }else if( self->attribs & DAO_IO_STRING ){ DString_AppendDataMBS( self->streamString, data, val->size ); }else{ if( self->format ){ printf( self->format, data ); }else{ DaoFile_WriteString( stdout, val ); } } }else{ const wchar_t *data = val->wcs; if( self->redirect && self->redirect->StdioWrite ){ DString *mbs = DString_New(1); DString_SetWords( mbs, data, val->size ); self->redirect->StdioWrite( self->redirect, mbs ); DString_Delete( mbs ); }else if( self->file ){ if( self->format ){ fprintf( self->file, self->format, data ); }else{ DaoFile_WriteString( self->file, val ); } }else if( self->attribs & DAO_IO_STRING ){ DString *wcs = self->streamString; int size = 0; DString_ToWCS( self->streamString ); size = wcs->size; DString_Resize( self->streamString, wcs->size + val->size ); memcpy( wcs->wcs + size, val->wcs, val->size * sizeof(wchar_t) ); }else{ if( self->format ){ printf( self->format, data ); }else{ DaoFile_WriteString( stdout, val ); } } } }
static void DaoIO_Read( DaoProcess *proc, DaoValue *p[], int N ) { DaoStream *self = proc->stdioStream; DString *ds = DaoProcess_PutMBString( proc, "" ); int count = 0; if( self == NULL ) self = proc->vmSpace->stdioStream; if( N >0 ) self = & p[0]->xStream; if( N >1 ) count = p[1]->xInteger.value; if( (self->attribs & (DAO_IO_FILE | DAO_IO_PIPE)) && self->file == NULL ){ DaoProcess_RaiseException( proc, DAO_ERROR, "stream is not open!" ); return; } if( ( self->mode & DAO_IO_READ ) == 0 ){ DaoProcess_RaiseException( proc, DAO_ERROR, "stream is not readable" ); return; } if( self->file == NULL && self->redirect && self->redirect->StdioRead ){ self->redirect->StdioRead( self->redirect, ds, count ); }else if( count ){ FILE *fd = stdin; DString_Clear( ds ); if( self->file ) fd = self->file; if( count >0 ){ DString_Resize( ds, count ); DString_Resize( ds, fread( ds->mbs, 1, count, fd ) ); }else{ struct stat info; fstat( fileno( fd ), &info ); DString_Resize( ds, info.st_size - ftell( fd )/2 ); DString_Resize( ds, fread( ds->mbs, 1, ds->size, fd ) ); } if( fd == stdin ) fseek( stdin, 0, SEEK_END ); }else{ DaoStream_ReadLine( self, ds ); } }
void DaoxShader_CompileShader( DaoxShader *self, int type, DArray *strings ) { daoint i, n = strings->size; uint_t shader = glCreateShader( type ); const GLchar **sources; GLint length, shader_ok; if( shader == 0 ){ fprintf(stderr, "Failed to create shader of type %i\n", type ); return; } sources = (const GLchar**) malloc( n*sizeof(GLchar*) ); for(i=0; i<n; ++i){ sources[i] = (const GLchar*) DString_GetMBS( strings->items.pString[i] ); if( i == 0 ) sources[i] += 2; /* skip //; */ } glShaderSource( shader, n, sources, NULL ); glCompileShader( shader ); free( sources ); glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok); if( !shader_ok ){ const char *log2; DString *log = DString_New(1); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length ); DString_Resize( log, length ); log2 = DString_GetMBS(log); glGetShaderInfoLog( shader, length, NULL, (char*)log2 ); fprintf(stderr, "Failed to compile shader!\nWith error message: %s", log2 ); glDeleteShader(shader); return; } switch( type ){ case GL_VERTEX_SHADER : if( self->vertexShader ) glDeleteShader( self->vertexShader ); self->vertexShader = shader; break; case GL_FRAGMENT_SHADER : if( self->fragmentShader ) glDeleteShader( self->fragmentShader ); self->fragmentShader = shader; break; } if( shader && self->program ) glAttachShader( self->program, shader ); }
void DString_Assign( DString *self, DString *chs ) { int *data1 = (int*)self->chars - self->sharing; int *data2 = (int*)chs->chars - chs->sharing; int assigned = 0; if( self == chs ) return; if( data1 == data2 ) return; if( data2 != dao_string ){ #ifdef DAO_WITH_THREAD DMutex_Lock( & mutex_string_sharing ); #endif if( self->aux ) self->aux->size = 0; if( self->sharing && chs->sharing ){ if( data1 != dao_string ){ data1[0] -= 1; if( data1[0] ==0 ) dao_free( data1 ); } memcpy( self, chs, sizeof(DString) - sizeof(DStringAux*) ); data2[0] += 1; assigned = 1; }else if( data1 == NULL && chs->sharing ){ memcpy( self, chs, sizeof(DString) - sizeof(DStringAux*) ); data2[0] += 1; assigned = 1; } #ifdef DAO_WITH_THREAD DMutex_Unlock( & mutex_string_sharing ); #endif if( assigned ) return; } if( self->chars == NULL ){ self->size = self->bufSize = chs->size; self->chars = (char*) dao_malloc( (chs->size + 1)*sizeof(char) ); memcpy( self->chars, chs->chars, chs->size*sizeof(char) ); self->chars[ self->size ] = 0; }else{ DString_Resize( self, chs->size ); memcpy( self->chars, chs->chars, chs->size*sizeof(char) ); } }
void DaoxShader_Finalize( DaoxShader *self ) { GLint length, program_ok; if( self->program == 0 ) return; int shaderAttribute = 0; glBindFragDataLocation( self->program, 0, "fragColor"); glLinkProgram( self->program ); glGetProgramiv( self->program, GL_LINK_STATUS, &program_ok ); if( !program_ok ){ const char *log2; DString *log = DString_New(1); glGetProgramiv( self->program, GL_INFO_LOG_LENGTH, &length ); DString_Resize( log, length ); log2 = DString_GetMBS(log); glGetProgramInfoLog( self->program, length, NULL, (char*)log2 ); fprintf(stderr, "Failed to link shader program with error message: %s\n", log2 ); glDeleteProgram(self->program); self->program = 0; } }
void DString_EncodeBase64( DString *self, DString *dest ) { const char base64_chars[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; daoint i, j = 0; DString_Resize( dest, ( self->size/3 )*4 + ( self->size%3? 4 : 0 ) ); for ( i = 0; i < self->size/3; i++ ){ uint8_t high = self->chars[i*3], mid = self->chars[i*3 + 1], low = self->chars[i*3 + 2]; uint_t triplet = ( (uint_t)high << 16 ) | ( (uint_t)mid << 8 ) | low; dest->chars[j++] = base64_chars[( triplet & ( 63 << 18 ) ) >> 18]; dest->chars[j++] = base64_chars[( triplet & ( 63 << 12 ) ) >> 12]; dest->chars[j++] = base64_chars[( triplet & ( 63 << 6 ) ) >> 6]; dest->chars[j++] = base64_chars[triplet & 63]; } if ( self->size%3 == 1 ){ uint_t triplet = ( (uint_t)self->chars[self->size - 1] ) << 16; dest->chars[j++] = base64_chars[( triplet & ( 63 << 18 ) ) >> 18]; dest->chars[j++] = base64_chars[( triplet & ( 63 << 12 ) ) >> 12]; dest->chars[j++] = '='; dest->chars[j] = '='; }
void DString_Erase( DString *self, daoint start, daoint n ) { daoint i, rest; if( start >= self->size ) return; if( n < 0 ) n = self->size; if( n + start > self->size ) n = self->size - start; rest = self->size - start - n; if( rest ==0 ){ DString_Resize( self, start ); return; } DString_Detach( self, self->size ); memmove( self->chars + start, self->chars + start + n, rest * sizeof(char) ); self->chars[start+rest] = 0; self->size -= n; if( self->size < 0.5*self->bufSize && self->size+5 < self->bufSize ){ self->bufSize = (daoint)(0.6 * self->bufSize) + 1; DString_Realloc( self, self->bufSize ); } }