static void DaoStream_ReadLines( DaoStream *self, DaoList *list, DaoProcess *proc, int count, int chop ) { DaoValue *res; DaoString *line; DaoVmCode *sect = DaoProcess_InitCodeSection( proc, 1 ); daoint i = 0; if( sect == NULL ){ line = DaoString_New(); while( (count == 0 || (i++) < count) && DaoStream_ReadLine( self, line->value ) ){ if( line->value->size == 0 && self->AtEnd != NULL && self->AtEnd( self ) ) break; if( chop ) DString_Chop( line->value, 0 ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; if( sect->b ){ DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; DString tmp2 = DString_WrapChars( "" ); tmp.value = & tmp2; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); } while( (count == 0 || (i++) < count) && DaoStream_ReadLine( self, line->value ) ){ if( line->value->size == 0 && self->AtEnd != NULL && self->AtEnd( self ) ) break; if( chop ) DString_Chop( line->value, 0 ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_PopFrame( proc ); } }
static void DaoIO_ReadLines( DaoProcess *proc, DaoValue *p[], int N ) { DString *fname; DaoValue *res; DaoString *line; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoList *list = DaoProcess_PutList( proc ); int chop = p[1]->xInteger.value; char buf[IO_BUF_SIZE]; FILE *fin; if( proc->vmSpace->options & DAO_OPTION_SAFE ){ DaoProcess_RaiseException( proc, DAO_ERROR, "not permitted" ); return; } fin = DaoIO_OpenFile( proc, p[0]->xString.data, "r", 0 ); if( fin == NULL ) return; if( sect == NULL || DaoProcess_PushSectionFrame( proc ) == NULL ){ line = DaoString_New(1); while( DaoFile_ReadLine( fin, line->data ) ){ if( chop ) DString_Chop( line->data ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; tmp.data = p[0]->xString.data; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); DaoProcess_AcquireCV( proc ); while( DaoFile_ReadLine( fin, line->data ) ){ if( chop ) DString_Chop( line->data ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); } fclose( fin ); }
static void DaoIO_ReadLines2( DaoProcess *proc, DaoValue *p[], int N ) { DaoValue *res; DaoString *line; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoList *list = DaoProcess_PutList( proc ); DaoStream *self = & p[0]->xStream; daoint i = 0, count = p[1]->xInteger.value; int chop = p[2]->xInteger.value; if( sect == NULL || DaoProcess_PushSectionFrame( proc ) == NULL ){ line = DaoString_New(1); while( (i++) < count && DaoStream_ReadLine( self, line->data ) ){ if( chop ) DString_Chop( line->data ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; DString tmp2 = DString_WrapMBS( "" ); tmp.data = & tmp2; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); DaoProcess_AcquireCV( proc ); while( (i++) < count && DaoStream_ReadLine( self, line->data ) ){ if( chop ) DString_Chop( line->data ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); } }
static void SYS_Ctimef( DaoProcess *proc, DaoValue *p[], int N ) { int i; int halfday = 0; const int size = p[1]->xString.data->size; const char *format = DString_GetMBS( p[1]->xString.data ); char buf[100]; char *p1 = buf+1; char *p2; DaoMap *sym = NULL; DaoString *ds = DaoString_New(1); DaoValue *key = (DaoValue*) ds; DString *S; struct tm *ctime; time_t t = (time_t)p[0]->xInteger.value; if( t == 0 ) t = time(NULL); ctime = gmtime( & t ); if( N > 1 ){ sym = (DaoMap*)p[2]; if( sym->items->size == 0 ) sym = NULL; } S = DaoProcess_PutMBString( proc, "" ); for( i=0; i+1<size; i++ ){ if( format[i] == '%' && ( format[i+1] == 'a' || format[i+1] == 'A' ) ){ halfday = 1; break; } } buf[0] = '0'; /* for padding */ for( i=0; i+1<size; i++ ){ p2 = p1; p1[0] = 0; if( format[i] == '%' ){ const char ch = format[i+1]; switch( ch ){ case 'Y' : sprintf( p1, "%i", ctime->tm_year+1900 ); break; case 'y' : sprintf( p1, "%i", ctime->tm_year+1900 ); p2 += 2; break; case 'M' : case 'm' : if( ! addStringFromMap( key, S, sym, "month", ctime->tm_mon ) ){ sprintf( p1, "%i", ctime->tm_mon+1 ); if( ch=='M' && p1[1]==0 ) p2 = buf; /* padding 0; */ }else p2 = NULL; break; case 'D' : case 'd' : if( ! addStringFromMap( key, S, sym, "date", ctime->tm_mday ) ){ sprintf( p1, "%i", ctime->tm_mday ); if( ch=='D' && p1[1]==0 ) p2 = buf; /* padding 0; */ }else p2 = NULL; break; case 'W' : case 'w' : if( ! addStringFromMap( key, S, sym, "week", ctime->tm_wday ) ) sprintf( p1, "%i", ctime->tm_wday+1 ); else p2 = NULL; break; case 'H' : case 'h' : if( halfday ) sprintf( p1, "%i", ctime->tm_hour %12 ); else sprintf( p1, "%i", ctime->tm_hour ); if( ch=='H' && p1[1]==0 ) p2 = buf; /* padding 0; */ break; case 'I' : case 'i' : sprintf( p1, "%i", ctime->tm_min ); if( ch=='I' && p1[1]==0 ) p2 = buf; /* padding 0; */ break; case 'S' : case 's' : sprintf( p1, "%i", ctime->tm_sec ); if( ch=='S' && p1[1]==0 ) p2 = buf; /* padding 0; */ break; case 'a' : if( ! addStringFromMap( key, S, sym, "halfday", 0 ) ){ if( ctime->tm_hour >= 12 ) strcpy( p1, "pm" ); else strcpy( p1, "am" ); }else p2 = NULL; break; case 'A' : if( ! addStringFromMap( key, S, sym, "halfday", 1 ) ){ if( ctime->tm_hour >= 12 ) strcpy( p1, "PM" ); else strcpy( p1, "AM" ); }else p2 = NULL; break; default : break; } if( p2 ) DString_AppendMBS( S, p2 ); i ++; }else{ DString_AppendChar( S, format[i] ); } } if( i+1 == size ) DString_AppendChar( S, format[i] ); DaoString_Delete( ds ); }