void DString_SubString( DString *self, DString *sub, daoint from, daoint n ) { daoint i, size = self->size; if( from >= size ){ DString_Reset( sub, 0 ); return; } if( n < 0 || n > size ) n = size; if( from+n > size ) n = size-from; DString_Reset( sub, n ); memcpy( sub->chars, self->chars + from, n * sizeof(char) ); }
int DaoStream_ReadStdin( DaoStream *self, DString *data, int count ) { DString_Reset( data, 0 ); if( count >= 0 ){ DString_Reset( data, count ); DString_Reset( data, fread( data->chars, 1, count, stdin ) ); }else if( count == -1 ){ DaoFile_ReadLine( stdin, data ); }else{ DaoFile_ReadAll( stdin, data, 0 ); } fseek( stdin, 0, SEEK_END ); return data->size; }
static int DaoFileStream_Read( DaoStream *stream, DString *data, int count ) { DaoFileStream *self = (DaoFileStream*) stream; DString_Reset( data, 0 ); if( DaoFileStream_AtEnd( stream ) ) return -1; if( count >= 0 ){ DString_Reset( data, count ); DString_Reset( data, fread( data->chars, 1, count, self->file ) ); }else if( count == -1 ){ DaoFile_ReadLine( self->file, data ); }else{ DaoFile_ReadAll( self->file, data, 0 ); } return data->size; }
daoint DaoStream_Read( DaoStream *self, DString *output, daoint count ) { DString_Reset( output, 0 ); if( self->Read == NULL ) return 0; if( count == -1 ){ return self->Read( self, output, -1 ); }else if( count <= -2 ){ return self->Read( self, output, -2 ); }else if( count <= 0x7fffffff ){ return self->Read( self, output, count ); } DString_Reserve( output, count ); count = DaoStream_ReadBytes( self, output->chars, count ); if( count > 0 ) DString_Reset( output, count ); return count; }
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 ); }
int DaoStream_ReadLine( DaoStream *self, DString *line ) { DString_Reset( line, 0 ); if( self->Read == NULL ) return 0; if( self->AtEnd && self->AtEnd( self ) ) return 0; return self->Read( self, line, -1 ) >= 0; }
void DaoxDataFrame_AddLabels( DaoxDataFrame *self, int dim, DMap *labels ) { DString *lab; DNode *it; if( labels->keytype != D_STRING && labels->keytype != D_VALUE ) return; if( labels->valtype != 0 && labels->valtype != D_VALUE ) return; lab = DString_New(1); DaoxDataFrame_AddLabelGroup( self, dim ); for(it=DMap_First(labels); it; it=DMap_Next(labels,it)){ DString *lab2 = it->key.pString; daoint idx = it->value.pInt; if( labels->keytype == D_VALUE ){ if( it->key.pValue->type != DAO_STRING ) continue; lab2 = it->key.pValue->xString.data; } if( labels->valtype == D_VALUE ){ if( it->value.pValue->type != DAO_INTEGER ) continue; idx = it->value.pValue->xInteger.value; } if( idx < 0 ) continue; DString_Reset( lab, 0 ); DString_Append( lab, lab2 ); DaoxDataFrame_AddLabel( self, dim, lab->mbs, idx ); } DString_Delete( lab ); }
static char* DaoReadLine( const char *s, DString *buffer ) { int ch; char *line; DString_Reset( buffer, 0 ); #ifdef DAO_WITH_THREAD if( ! DThread_IsMain() ){ printf( "%s", s ); fflush( stdout ); while( (ch = getchar()) != '\n' ) DString_AppendWChar( buffer, ch ); return DString_GetData( buffer ); } #endif readingline = 1; count = 0; #ifdef DAO_USE_READLINE line = readline( s ); DString_SetChars( buffer, line ); free( line ); #endif readingline = 0; return DString_GetData( buffer ); }
void DaoxDataColumn_SetCell( DaoxDataColumn *self, daoint i, DaoValue *value ) { if( value == NULL ){ complex16 zero = {0.0,0.0}; switch( self->type->tid ){ default : GC_DecRC( self->cells->data.values[i] ); self->cells->data.values[i] = value; break; case DAO_INTEGER : self->cells->data.daoints[i] = 0; break; case DAO_FLOAT : self->cells->data.floats[i] = 0.0; break; case DAO_DOUBLE : self->cells->data.doubles[i] = 0.0; break; case DAO_COMPLEX : self->cells->data.complexes[i] = zero; break; case DAO_STRING : DString_Reset( & self->cells->data.strings[i], 0 ); break; } return; } switch( self->type->tid ){ default : GC_ShiftRC( value, self->cells->data.values[i] ); self->cells->data.values[i] = value; break; case DAO_INTEGER : self->cells->data.daoints[i] = DaoValue_GetInteger( value ); break; case DAO_FLOAT : self->cells->data.floats[i] = DaoValue_GetFloat( value ); break; case DAO_DOUBLE : self->cells->data.doubles[i] = DaoValue_GetDouble( value ); break; case DAO_COMPLEX : self->cells->data.complexes[i] = DaoValue_GetComplex( value ); break; case DAO_STRING : DaoValue_GetString( value, & self->cells->data.strings[i] ); break; } }
static int DaoStringStream_Read( DaoStream *stream, DString *data, int count ) { DaoStringStream *self = (DaoStringStream*) stream; DString_Reset( data, 0 ); if( self->offset >= self->base.buffer->size ) return -1; if( count >= 0 ){ DString_SubString( self->base.buffer, data, self->offset, count ); self->offset += data->size; }else if( count == -1 ){ int delim = '\n'; daoint pos = DString_FindChar( self->base.buffer, delim, self->offset ); if( self->offset == 0 && (pos == DAO_NULLPOS || pos == self->base.buffer->size-1) ){ DString_Append( data, self->base.buffer ); self->offset = self->base.buffer->size; }else if( pos == DAO_NULLPOS ){ DString_SubString( self->base.buffer, data, pos, -1 ); self->offset = self->base.buffer->size; }else{ DString_SubString( self->base.buffer, data, pos, pos - self->offset + 1 ); self->offset = pos + 1; } if( self->base.mode & DAO_STREAM_AUTOCONV ) DString_ToUTF8( data ); return self->offset < self->base.buffer->size; }else{ if( self->offset == 0 ){ DString_Assign( data, self->base.buffer ); }else{ DString_SubString( self->base.buffer, data, self->offset, -1 ); } self->offset += data->size; } return data->size; }
void DString_SetChars( DString *self, const char *chs ) { if( self->chars && self->chars == chs ) return; if( chs == NULL ){ DString_Clear( self ); return; } DString_Reset( self, strlen( chs ) ); memcpy( self->chars, chs, self->size*sizeof(char) ); }
void DaoRoutine_MakeName( DaoRoutine *self, DString *name, int max1, int max2, int max3 ) { DString *hostName = self->routHost ? self->routHost->name : NULL; DaoType *routType = self->routType; int M = (routType->attrib & DAO_TYPE_SELF) != 0; int N = routType->nested->size; int i; DString_Reset( name, 0 ); /* For builtin containers, whose methods may have no routHost set: */ if( hostName == NULL && M ) hostName = routType->nested->items.pType[0]->aux->xType.name; /* // Mixin classes have methods converted from constructors of component classes. // These methods still have the DAO_ROUT_INITOR flag. So checking the names is // the better way to use here. */ if( hostName && ! DString_EQ( self->routName, hostName ) ){ if( hostName->size + self->routName->size < (max1-2) ){ DString_Append( name, hostName ); DString_AppendChars( name, "::" ); DString_Append( name, self->routName ); }else{ DString_PartialAppend( name, hostName, max1/2-1 ); DString_AppendChars( name, "::" ); DString_PartialAppend( name, self->routName, max1/2-1 ); } }else{ DString_PartialAppend( name, self->routName, max1 ); } if( max3 == 0 ){ DString_AppendChars( name, "()" ); return; } DString_AppendChars( name, "( " ); if( N == M+1 ){ DaoType *type = routType->nested->items.pType[M]; DString_PartialAppend( name, type->name, 2*max2 ); }else{ for(i=M; i<N; ++i){ DaoType *type = routType->nested->items.pType[i]; if( i > M ) DString_AppendChars( name, ", " ); if( i < M + max3 ){ DString_PartialAppend( name, type->name, max2 ); }else{ DString_AppendChars( name, "~~" ); break; } } } DString_AppendChars( name, " )" ); }
int DaoRegex_ChangeExt( DaoRegex *self, DString *input, DString *output, DString *target, int index, daoint *start2, daoint *end2 ) { daoint start = start2 ? (daoint) *start2 : 0; daoint end = end2 ? (daoint) *end2 : 0; daoint i, n=0, p1=start, p2=end, p3, last; DaoValue *value = NULL; DaoString matched = {DAO_STRING,0,0,0,0,NULL}; DList *array = DList_New( DAO_DATA_VALUE ); DString *tmp = DString_New(); DString *tmp2 = DString_New(); DString_Reset( output, 0 ); if( self == NULL || input->size == 0 ) goto DoNothing; matched.value = tmp; Dao_ParseTarget( target, array, (DaoValue*) & matched ); if( end == 0 ) end = p2 = DString_Size( input ); n = last = 0; while( DaoRegex_Match( self, input, & p1, & p2 ) ){ n += 1; if( index ==0 || n == index ){ DString_SubString( input, tmp2, last, p1 - last ); DString_Append( output, tmp2 ); DString_Clear( tmp ); for(i=0; i<array->size; i++){ value = array->items.pValue[i]; if( value->type == DAO_INTEGER ){ if( DaoRegex_SubMatch( self, value->xInteger.value, & p1, & p3 ) ){ DString_SubString( input, tmp2, p1, p3 - p1 ); DString_Append( tmp, tmp2 ); } }else{ DString_Append( tmp, value->xString.value ); } } DString_Append( output, tmp ); last = p2; } if( start2 ) *start2 = p1; if( end2 ) *end2 = p2; p1 = p2; p2 = end; if( index && n == index ) break; } DString_SubString( input, tmp2, last, end - last ); DString_Append( output, tmp2 ); DoNothing: DString_Delete( tmp ); DString_Delete( tmp2 ); DList_Delete( array ); return n; }
void DaoxDataFrame_GetLabel( DaoxDataFrame *self, int dim, int group, daoint i, DString *lab ) { DMap *labels = self->labels[dim]->items.pMap[group]; DNode *it; DString_Reset( lab, 0 ); for(it=DMap_First(labels); it; it=DMap_Next(labels,it)){ if( it->value.pInt == i ){ DString_Append( lab, it->key.pString ); break; } } }
int DaoxFont_Open( DaoxFont *self, const char *file ) { FILE *fin = fopen( file, "r" ); DaoxFont_ResetGlyphs( self ); DString_Reset( self->buffer, 0 ); if( fin == NULL ) return 0; DaoFile_ReadAll( fin, self->buffer, 1 ); return DaoxFont_Init( self, self->buffer ); }
int DaoFile_ReadLine( FILE *fin, DString *line ) { char buf[IO_BUF_SIZE]; DString_Reset( line, 0 ); DString_ToMBS( line ); if( feof( fin ) ) return 0; do{ buf[IO_BUF_SIZE - 1] = 1; if( !fgets( buf, IO_BUF_SIZE, fin ) ) break; DString_AppendMBS( line, buf ); } while( buf[IO_BUF_SIZE - 1] != 1 ); return 1; }
int DaoXmlParser_ParseEscapedChar( DaoXmlParser *self, DString *escape ) { DString_Reset( escape, 0 ); if( (self->source + 2) > self->end ) return 1; if( *self->source != '&' ) return 1; self->source += 1; while( self->source < self->end && *self->source != ';' ){ DString_AppendChar( escape, *self->source ); self->source += 1; } self->source += 1; return 0; }
int DaoXmlParser_ParseIdentifier( DaoXmlParser *self, DString *string ) { char ch = *self->source; DString_Reset( string, 0 ); if( !isalpha( ch ) && ch != ':' && ch != '_' ) return 1; while( isalnum( ch ) || ch == ':' || ch == '_' || ch == '-' || ch == '.' ){ DString_AppendChar( string, *self->source ); self->source += 1; if( self->source >= self->end ) break; ch = *self->source; } return 0; }
int DaoFile_ReadAll( FILE *fin, DString *all, int close ) { char buf[IO_BUF_SIZE]; DString_Reset( all, 0 ); DString_ToMBS( all ); if( fin == NULL ) return 0; while(1){ size_t count = fread( buf, 1, IO_BUF_SIZE, fin ); if( count ==0 ) break; DString_AppendDataMBS( all, buf, count ); } if( close ) fclose( fin ); return 1; }
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 ); }
int DaoFile_ReadAll( FILE *fin, DString *output, int close ) { char buf[IO_BUF_SIZE]; DString_Reset( output, 0 ); if( fin == NULL ) return 0; while(1){ // TODO: read directly into output; size_t count = fread( buf, 1, IO_BUF_SIZE, fin ); if( count ==0 ) break; DString_AppendBytes( output, buf, count ); } if( close ) fclose( fin ); return 1; }
static void ParseKeyValueString( DaoProcess *proc, DaoMap *mulmap, DaoMap *map, const char *data ) { const char *end = data + strlen( data ); DaoValue *vk = (DaoValue*) DaoProcess_NewString( proc, NULL, 0 ); DaoValue *vv = (DaoValue*) DaoProcess_NewString( proc, NULL, 0 ); DString *key = DaoString_Get( DaoValue_CastString( vk ) ); DString *value = DaoString_Get( DaoValue_CastString( vv ) ); DString *buffer = key; buffer->size = 0; for(; data < end; ++data) { if( buffer->size >= buffer->bufSize ) DString_Reserve( buffer, 1.5*buffer->size + 8 ); if( *data == '=' ) { buffer->chars[ buffer->size ] = 0; buffer = value; buffer->size = 0; } else if( *data == '&' || *data == ';' ) { buffer->chars[ buffer->size ] = 0; InsertKeyValue( proc, mulmap, map, vk, vv ); DString_Reset( key, 0 ); /* also detaching shared memory; */ DString_Reset( value, 0 ); /* also detaching shared memory; */ buffer = key; } else if( *data != ' ' ) { if( *data == '%' ) { char a = tolower( data[1] ); char b = tolower( data[2] ); buffer->chars[ buffer->size ++ ] = (char) ((HEXTOI(a) << 4) | HEXTOI(b)); data += 2; } else if( *data == '+' ) { buffer->chars[ buffer->size ++ ] = ' '; } else { buffer->chars[ buffer->size ++ ] = *data; } } } if( key->size ) InsertKeyValue( proc, mulmap, map, vk, vv ); }
int DaoXmlParser_ParseQuotedString( DaoXmlParser *self, DString *string ) { DNode *it; char quote; DString_Reset( string, 0 ); if( (self->source + 2) > self->end ) return 1; if( self->source[0] != '\'' && self->source[0] != '"' ) return 1; quote = self->source[0]; self->source += 1; while( self->source < self->end && *self->source != quote ){ if( DaoXmlParser_ParseFormatedChar( self, string ) ) return 1; } self->source += 1; return 0; }
int DaoFile_ReadLine( FILE *fin, DString *line ) { int ch; DString_Reset( line, 0 ); if( feof( fin ) ) return 0; while( (ch = fgetc(fin)) != EOF ){ if( line->size == line->bufSize ) DString_Reserve( line, 5 + 1.2*line->size ); line->chars[ line->size ++ ] = ch; line->chars[ line->size ] = '\0'; if( ch == '\n' ) break; } return 1; }
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 ); }
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 ); } }
void DaoCGI_SendFile( DaoProcess *proc, DaoValue *p[], int N ) { DString *mbs; DString *file = DaoValue_TryGetString( p[0] ); DString *mime = DaoValue_TryGetString( p[1] ); DString *notfound = DaoValue_TryGetString( p[2] ); char buf[IO_BUF_SIZE]; FILE *fin = fopen( DString_GetData( file ), "r" ); if( fin == NULL ) { printf( "%s", DString_GetData( notfound ) ); return; } mbs = DString_New(); printf( "Content-Type: %s\n\n", DString_GetData( mime ) ); while(1) { size_t count = fread( buf, 1, IO_BUF_SIZE, fin ); if( count ==0 ) break; DString_Reset( mbs, 0 ); DString_AppendBytes( mbs, buf, count ); DaoFile_WriteString( stdout, mbs ); } fclose( fin ); DString_Delete( mbs ); }
static void FRAME_PRINT( DaoProcess *proc, DaoValue *p[], int n ) { DaoxDataFrame *self = (DaoxDataFrame*) p[0]; DaoxDataFrame *original = self->original; DaoStream *stream = proc->stdioStream; DaoStream *sstream = DaoStream_New(); DaoValue valueBuffer, *nulls[3] = {NULL,NULL,NULL}; DVector *rlabwidth = DVector_New( sizeof(int) ); DVector *clabwidth = DVector_New( sizeof(int) ); DVector *decimals = DVector_New( sizeof(int) ); DVector *scifmts = DVector_New( sizeof(int) ); DVector *aligns = DVector_New( sizeof(int) ); DString *label = DString_New(1); daoint d, g, i, j, k, s, N, M, K, J = 1; int idwidth, maxwidth = 16, maxdec = 3; char idfmt[16]; char fmt[16]; char buf[512]; sstream->attribs |= DAO_IO_STRING; memset( &valueBuffer, 0, sizeof(DaoValue) ); if( stream == NULL ) stream = proc->vmSpace->stdioStream; if( self->original == NULL ){ DaoxDataFrame_PrepareSlices( self ); DaoDataFrame_MakeSlice( self, proc, nulls, 3, self->slices ); original = self; } N = self->slices->items.pVector[0]->data.daoints[1]; M = self->slices->items.pVector[1]->data.daoints[1]; K = self->slices->items.pVector[2]->data.daoints[1]; DString_Reset( label, 10 + 4*sizeof(void*) + log10(1+N+M+K) ); sprintf( label->mbs, "\nDataFrame[%p]", self ); DaoStream_WriteMBS( stream, label->mbs ); if( original != self ){ sprintf( label->mbs, " (Slices from DataFrame[%p])", original ); DaoStream_WriteMBS( stream, label->mbs ); } sprintf( label->mbs, "\nDimensions: Rows=%" DAO_INT_FORMAT ";", N ); DaoStream_WriteMBS( stream, label->mbs ); sprintf( label->mbs, " Cols=%" DAO_INT_FORMAT ";", M ); DaoStream_WriteMBS( stream, label->mbs ); sprintf( label->mbs, " Deps=%" DAO_INT_FORMAT ";\n", K ); DaoStream_WriteMBS( stream, label->mbs ); idwidth = 1 + (int)log10(N+1); for(i=0; i<N; ++i){ daoint ii = DaoSlice_GetIndex( self->slices->items.pVector[0], i ); int width = 1 + (int)log10(ii+1); if( width > idwidth ) idwidth = width; } sprintf( idfmt, "%%%i%s:", idwidth, DAO_INT_FORMAT ); if( M == 1 ){ maxwidth = 64; maxdec = 24; }else if( M == 2 ){ maxwidth = 40; maxdec = 12; }else if( M <= 4 ){ maxwidth = 24; maxdec = 6; } for(g=0; g<original->labels[DAOX_DF_ROW]->size; ++g){ int width = 0; for(i=0; i<N; ++i){ daoint ii = DaoSlice_GetIndex( self->slices->items.pVector[0], i ); DaoxDataFrame_GetLabel( original, DAOX_DF_ROW, g, ii, label ); if( label->size > width ) width = label->size; if( width > maxwidth ) break; } if( width > maxwidth ) width = maxwidth; DVector_PushInt( rlabwidth, width ); } for(j=0; j<M; ++j){ int w, datatype, max = 0, min = 0, dec = 0; daoint width, jj = DaoSlice_GetIndex( self->slices->items.pVector[2], j ); DaoxDataColumn *col = (DaoxDataColumn*) original->columns->items.pVoid[jj]; DVector *cells = col->cells; datatype = DaoType_GetDataType( col->type ); width = DaoxDataColumn_GetPrintWidth( col, 16 ); for(i=0; i<N && i<1000; ++i){ daoint v, ii = DaoSlice_GetIndex( self->slices->items.pVector[0], i ); complex16 com; switch( datatype ){ case DAO_INTEGER : v = cells->data.daoints[ii]; w = log10( fabs(v) + 1E-32 ) + (v < 0); if( w > max ) max = w; break; case DAO_FLOAT : CheckPrintWidth( cells->data.floats[ii], & max, & min, & dec ); break; case DAO_DOUBLE : CheckPrintWidth( cells->data.doubles[ii], & max, & min, & dec ); break; case DAO_COMPLEX : com = cells->data.complexes[ii]; CheckPrintWidth( com.real, & max, & min, & dec ); CheckPrintWidth( com.imag, & max, & min, & dec ); break; case DAO_STRING : if( cells->data.strings[i].size > max ) max = cells->data.strings[i].size; break; default : break; } } if( dec > maxdec ) dec = maxdec; if( col->type->tid == DAO_COMPLEX ){ max *= 2; min *= 2; } if( datatype == 0 ){ width = maxwidth; DVector_PushInt( aligns, 1 ); DVector_PushInt( scifmts, 0 ); DVector_PushInt( decimals, 0 ); }else if( datatype == DAO_STRING ){ width = max; DVector_PushInt( aligns, 1 ); DVector_PushInt( scifmts, 0 ); DVector_PushInt( decimals, 0 ); }else if( max >= maxwidth || min <= -dec ){ width = 16; DVector_PushInt( aligns, 0 ); DVector_PushInt( scifmts, 1 ); DVector_PushInt( decimals, dec ); }else{ width = max + dec + 1; if( col->type->tid == DAO_COMPLEX ) width += dec + 6; DVector_PushInt( aligns, 0 ); DVector_PushInt( scifmts, 0 ); DVector_PushInt( decimals, dec ); } for(g=0; g<original->labels[DAOX_DF_COL]->size; ++g){ DaoxDataFrame_GetLabel( original, DAOX_DF_COL, g, jj, label ); if( label->size > width ) width = label->size; if( width > maxwidth ) break; } if( width > maxwidth ) width = maxwidth; DVector_PushInt( clabwidth, width ); } for(k=0; k<K; ++k){ daoint kk = DaoSlice_GetIndex( self->slices->items.pVector[2], k ); DaoStream_WriteMBS( stream, "Depth: " ); DaoStream_WriteInt( stream, kk ); DaoStream_WriteMBS( stream, ";" ); if( original->labels[DAOX_DF_DEP]->size ) DaoStream_WriteMBS( stream, "\nLabels:" ); for(g=0; g<original->labels[DAOX_DF_DEP]->size; ++g){ DaoxDataFrame_GetLabel( original, DAOX_DF_DEP, g, kk, label ); DaoStream_WriteMBS( stream, " " ); DaoStream_WriteString( stream, label ); DaoStream_WriteMBS( stream, ";" ); } DaoStream_WriteMBS( stream, "\n" ); for(j=0; j<M; j=J){ int width2, width = idwidth+1; for(i=0; i<rlabwidth->size; ++i) width += rlabwidth->data.ints[i] + 1; width += 1; J = j; width2 = width; for(J=j; J<M; ++J){ daoint jj = DaoSlice_GetIndex( self->slices->items.pVector[1], J ); width2 += clabwidth->data.ints[J] + 2; if( width2 > 80 ){ width2 -= clabwidth->data.ints[J] + 2; break; } } if( J == j ) J += 1; sprintf( buf, "from %" DAO_INT_FORMAT " to %" DAO_INT_FORMAT ":\n", j, J-1 ); DaoStream_WriteMBS( stream, j == 0 ? "| Columns " : "> Columns " ); DaoStream_WriteMBS( stream, buf ); for(g=0; g<original->labels[DAOX_DF_COL]->size; ++g){ sprintf( fmt, "%%-%is", width ); sprintf( buf, fmt, j == 0 ? "|" : ">" ); DaoStream_WriteMBS( stream, buf ); for(s=j; s<J; ++s){ daoint jj = DaoSlice_GetIndex( self->slices->items.pVector[1], s ); int width = clabwidth->data.ints[s]; int align = aligns->data.ints[s]; if( align ){ sprintf( fmt, "%%-%is", width ); }else{ sprintf( fmt, "%%%is", width ); } DaoxDataFrame_GetLabel( original, DAOX_DF_COL, g, jj, label ); if( label->size > width ) DString_Reset( label, width ); snprintf( buf, width+1, fmt, label->mbs ); DaoStream_WriteMBS( stream, " " ); DaoStream_WriteMBS( stream, buf ); } DaoStream_WriteMBS( stream, "\n" ); } printf( j == 0 ? "|" : ">" ); while( --width2 ) printf( "-" ); DaoStream_WriteMBS( stream, J < M ? ">" : "|" ); DaoStream_WriteMBS( stream, "\n" ); for(i=0; i<N; ++i){ daoint ii = DaoSlice_GetIndex( self->slices->items.pVector[0], i ); sprintf( buf, idfmt, ii ); DaoStream_WriteMBS( stream, buf ); for(g=0; g<original->labels[DAOX_DF_ROW]->size; ++g){ int width = rlabwidth->data.ints[g]; DaoxDataFrame_GetLabel( original, DAOX_DF_ROW, g, ii, label ); if( label->size > width ) DString_Reset( label, width ); if( g ) DaoStream_WriteMBS( stream, "," ); sprintf( fmt, "%%-%is", width ); snprintf( buf, width+1, fmt, label->mbs ); DaoStream_WriteMBS( stream, buf ); } DaoStream_WriteMBS( stream, ": " ); for(s=j; s<J; ++s){ int scifmt = scifmts->data.ints[s]; int dec = decimals->data.ints[s]; int width = clabwidth->data.ints[s]; daoint jj = DaoSlice_GetIndex( self->slices->items.pVector[2], s ); DaoxDataColumn *col = (DaoxDataColumn*) original->columns->items.pVoid[jj]; DaoValue *value = DaoxDataColumn_GetCell( col, i, & valueBuffer ); DaoStream_WriteMBS( stream, " " ); if( value == NULL ){ sprintf( fmt, "%%-%is", width ); snprintf( buf, width+1, fmt, " " ); }else if( value->type == DAO_INTEGER ){ sprintf( fmt, "%%%i%s", width, DAO_INT_FORMAT ); snprintf( buf, width+1, fmt, value->xInteger.value ); }else if( value->type == DAO_FLOAT || value->type == DAO_DOUBLE ){ double f = DaoValue_GetDouble( value ); if( scifmt ){ sprintf( fmt, "%%%iE", width ); }else{ sprintf( fmt, "%%%i.%if", width, dec ); } snprintf( buf, width+1, fmt, f ); }else if( value->type == DAO_COMPLEX ){ complex16 com = value->xComplex.value; char s = com.imag>=0 ? '+' : '-'; int w = width/2-2; int d = dec; if( scifmt ){ sprintf( fmt, "(%%%i.3E,%%%i.3E)", w, w ); }else{ sprintf( fmt, "(%%%i.%if,%%%i.%if)", w, d, w, d ); } snprintf( buf, width, fmt, com.real, com.imag ); }else{ DString_Reset( sstream->streamString, 0 ); DaoValue_Print( value, proc, sstream, NULL ); DString_Reset( label, 0 ); DString_Append( label, sstream->streamString ); if( label->size > width ) DString_Reset( label, width ); DString_ChangeMBS( label, "%t", "\\t", 0 ); DString_ChangeMBS( label, "%n", "\\n", 0 ); sprintf( fmt, "%%-%is", width ); snprintf( buf, width+1, fmt, label->mbs ); } DaoStream_WriteMBS( stream, buf ); } DaoStream_WriteMBS( stream, "\n" ); } DaoStream_WriteMBS( stream, "\n" ); } } DaoStream_Delete( sstream ); DVector_Delete( aligns ); DVector_Delete( scifmts ); DVector_Delete( decimals ); DVector_Delete( rlabwidth ); DVector_Delete( clabwidth ); DString_Delete( label ); }
int main( int argc, char **argv ) { int restart = 0; int i, k, idsrc, vmods = 0; DString *opts = NULL, *args = NULL; /*mtrace(); */ for(i=1; i<argc; i++){ if( strcmp( argv[i], "-r" ) ==0 || strcmp( argv[i], "--restart" ) ==0 ){ restart = i; break; } } if( restart ) DaoRestartRun( argv, argc, restart ); vmSpace = DaoInit( argv[0] ); idsrc = -1; for(i=1; i<argc; i++){ if( strcmp( argv[i], "-e" ) ==0 || strcmp( argv[i], "--eval" ) ==0 ) break; /* also allows execution of script files without suffix .dao */ if( argv[i][0] != '-' ){ idsrc = i; break; } } #ifdef DAO_WITH_STATIC_MODULES /* // For single file deployment. // Identify the script argument, such that the arguments before the script // can be passed in as virtual machine arguments. // Example: ./script --restart script.dao ... */ args = DString_Copy( vmSpace->daoBinFile ); DString_Erase( args, 0, vmSpace->daoBinPath->size ); DString_AppendChars( args, ".dao" ); idsrc = 1; for(i=1; i<argc; i++){ if( strcmp( argv[i], args->chars ) == 0 ){ idsrc = i; break; } } vmods = DaoVmSpace_AddVirtualModules( vmSpace, dao_virtual_modules ); DString_Reset( args, 0 ); #endif k = idsrc; if( k < 0 ) k = argc; if( opts == NULL ) opts = DString_New(); if( args == NULL ) args = DString_New(); for(i=1; i<k; i++ ){ DString_AppendChars( opts, argv[i] ); DString_AppendChar( opts, '\1' ); } if( idsrc >= 0 ){ #ifdef DAO_WITH_STATIC_MODULES idsrc += 1; #endif for(i=idsrc; i<argc; i++ ){ DString_AppendChars( args, argv[i] ); DString_AppendChar( args, '\1' ); } } DaoVmSpace_ParseOptions( vmSpace, DString_GetData( opts ) ); #ifdef DAO_WITH_STATIC_MODULES if( vmods ){ DString_InsertChars( args, "/@/\1", 0, 0, 0 ); DString_InsertChars( args, dao_virtual_modules[0].name, 3, 0, 0 ); /* set the path for the virtual files: */ DaoVmSpace_SetPath( vmSpace, "/@/" ); }else #endif if( idsrc < 0 && argc == 1 ){ DString_AppendChar( opts, '\1' ); DString_AppendChars( opts, "-vi" ); DaoVmSpace_ParseOptions( vmSpace, DString_GetData( opts ) ); } #ifdef DAO_USE_READLINE DaoVmSpace_ReadLine( vmSpace, DaoReadLine ); DaoVmSpace_AddHistory( vmSpace, (AddHistory) add_history ); read_history( NULL ); #endif signal( SIGINT, DaoSignalHandler ); signal( SIGSEGV, DaoStackTrace ); /* Start execution. */ k = DaoVmSpace_RunMain( vmSpace, DString_GetData( args ) ); #ifdef DAO_USE_READLINE write_history( NULL ); #endif DString_Delete( args ); DString_Delete( opts ); DaoQuit(); return k; }
void DaoProfiler_Report( DaoProfiler *self0, DaoStream *stream ) { DaoComplex com = {DAO_COMPLEX,0,0,0,1,{0.0,0.0}}; DaoxProfiler *self = (DaoxProfiler*) self0; DMap *summary = DMap_New( DAO_DATA_VALUE, 0 ); DMap *summary2 = DMap_New( DAO_DATA_VALUE, 0 ); DString *name1 = DString_New(); DString *name2 = DString_New(); DNode *it, *it2; int count, max = 20; char buf1[32]; char buf2[24]; char buf[120]; for(it=DMap_First(self->profile); it; it=DMap_Next(self->profile,it)){ DaoRoutine *callee = (DaoRoutine*) it->key.pValue; com.value = DaoProfiler_Sum( it->value.pMap ); com.value.real = - com.value.real; com.value.imag = - com.value.imag; DMap_Insert( summary, & com, it ); } DaoStream_WriteChars( stream, "\n" ); DaoStream_WriteChars( stream, delimiter ); DaoStream_WriteChars( stream, delimiter2 ); snprintf( buf, sizeof(buf), header_format, "Routine", "#Calls", "CPU Time" ); DaoStream_WriteChars( stream, buf ); DaoStream_WriteChars( stream, delimiter2 ); for(count=max,it=DMap_First(summary); it; it=DMap_Next(summary,it),--count){ DNode *it2 = (DNode*) it->value.pVoid; dao_complex data = it->key.pValue->xComplex.value; DaoRoutine *callee = (DaoRoutine*) it2->key.pValue; DaoRoutine_MakeName( callee, name1, 28, 10, 2 ); snprintf( buf, sizeof(buf), row_format, name1->chars, (int) -data.imag, -data.real ); DaoStream_WriteChars( stream, buf ); if( count == 0 ) break; } DaoStream_WriteChars( stream, "\n" ); DaoStream_WriteChars( stream, delimiter3 ); snprintf( buf, sizeof(buf), header_format2, "Routine", "Caller", "#Calls", "CPU Time" ); DaoStream_WriteChars( stream, buf ); DaoStream_WriteChars( stream, delimiter3 ); for(count=max,it=DMap_First(summary); it; it=DMap_Next(summary,it),--count){ DNode *it2 = (DNode*) it->value.pVoid; DaoRoutine *callee = (DaoRoutine*) it2->key.pValue; DMap *profile = it2->value.pMap; DaoRoutine_MakeName( callee, name1, 30, 0, 0 ); DMap_Reset( summary2 ); for(it2=DMap_First(profile); it2; it2=DMap_Next(profile,it2)){ DaoRoutine *caller = (DaoRoutine*) it2->key.pValue; DaoComplex *data = (DaoComplex*) it2->value.pValue; com.value.real = - data->value.real; com.value.imag = - data->value.imag; DMap_Insert( summary2, & com, caller ); } for(it2=DMap_First(summary2); it2; it2=DMap_Next(summary2,it2)){ DaoRoutine *caller = (DaoRoutine*) it2->value.pValue; dao_complex data = it2->key.pValue->xComplex.value; DString_Reset( name2, 0 ); if( caller ) DaoRoutine_MakeName( caller, name2, 22, 0, 0 ); snprintf( buf, sizeof(buf), row_format2, name1->chars, name2->chars, (int) -data.imag, -data.real ); DaoStream_WriteChars( stream, buf ); DString_Reset( name1, 0 ); } if( count == 0 ) break; } DString_Delete( name1 ); DString_Delete( name2 ); }