BBString *bbStringFromLong( BBInt64 n ){ char buf[64],*p=buf+64; int neg=n<0; if( neg ){ n=-n;if( n<0 ) return bbStringFromBytes( "-9223372036854775808",20 ); } do{ *--p=n%10+'0'; }while(n/=10); if( neg ) *--p='-'; return bbStringFromBytes( p,buf+64-p ); }
BBString *bbStringFromInt( int n ){ char buf[64],*p=buf+64; int neg=n<0; if( neg ){ n=-n;if( n<0 ) return bbStringFromBytes( "-2147483648",11 ); } do{ *--p=n%10+'0'; }while(n/=10); if( neg ) *--p='-'; return bbStringFromBytes( p,buf+64-p ); }
/* Converts a Lua value to a BMax object (Userdata -> BBObject*, String -> BBString*, Table -> lua_tobmaxarray(..)) */ BBObject *lua_tobmaxobject(lua_State *state, int index) { /* check the type at the index */ switch(lua_type(state, index)) { case LUA_TNONE: luaL_error(state, ERRORSTR("@lua_tobmaxobject: Invalid index (%d)"), index); return &bbNullObject; case LUA_TNIL: /* return null for nil */ return &bbNullObject; case LUA_TSTRING: /* if it's a string, return a string (since a BBString is an object) */ { size_t length; const char *sbuf = lua_tolstring(state, -1, &length); return (BBObject*)bbStringFromBytes(sbuf, length); } case LUA_TTABLE: /* pass off to lua_tobmaxarray */ return (BBObject*)lua_tobmaxarray(state, index); /* get an object out of the userdata */ case LUA_TUSERDATA: if ( lua_isbmaxobject(state, index) != 0 ) return *(BBObject**)lua_touserdata(state, index); /* and the best default ever: an error */ default: luaL_error(state, ERRORSTR("@lua_tobmaxobject: Value at index (%d) is not an object."), index); return &bbNullObject; } }
BBString *bbReadStdin(){ #define BUF_SIZE 256 int sz=0; char *str=0; BBString *t; for(;;){ int t_sz; char buf[BUF_SIZE],*p; fgets( buf,BUF_SIZE,stdin ); buf[BUF_SIZE-1]=0; if( p=strchr( buf,'\n' ) ){ t_sz=p-buf; if( t_sz && isspace(buf[t_sz-1]) ) --t_sz; }else{ t_sz=strlen( buf ); } str=(char*)bbMemExtend( str,sz,sz+t_sz ); bbMemCopy( str+sz,buf,t_sz ); sz+=t_sz; if( t_sz<BUF_SIZE-1 ) break; } if( sz ) t=bbStringFromBytes( str,sz ); else t=&bbEmptyString; bbMemFree( str ); return t; }
BBString *bbStringFromArray( BBArray *arr ){ int n; void *p; if( arr->dims!=1 ) return &bbEmptyString; n=arr->scales[0]; p=BBARRAYDATA(arr,arr->dims); switch( arr->type[0] ){ case 'b':return bbStringFromBytes( p,n ); case 's':return bbStringFromShorts( p,n ); case 'i':return bbStringFromInts( p,n ); } return &bbEmptyString; }
BBString* bmx_dtime_format(time_t time, BBString* fmt, BBString* tz) { #define BUFSIZE 512 char buf[BUFSIZE]; char* tzo; //printf("tzset tz=\"%s\" tz#%p, empty#%p, null#%p\n", bbTmpCString(tz), tz, &bbEmptyString, &bbNullObject); if (tz && tz->length > 0) { tzo = getenv("TZ"); c_dtime_settz(bbTmpCString(tz)); } struct tm* tm = localtime(&time); int count = strftime(buf, BUFSIZE, bbTmpCString(fmt), tm); if (tz && tz->length > 0) { c_dtime_settz(tzo); } if (count == 0) return &bbEmptyString; return bbStringFromBytes(buf, count); }
BBString *bbStringFromCString( const char *p ){ return p ? bbStringFromBytes( p,strlen(p) ) : &bbEmptyString; }
void bbStartup( int argc,char *argv[],void *dummy1,void *dummy2 ){ int i,k; BBString **p; //Start up GC and create bbAppFile, bbAppDir and bbLaunchDir #if _WIN32 char *ebp; OSVERSIONINFO os={ sizeof(os) }; //asm( "movl %%ebp,%0;":"=r"(ebp) );//::"%ebp" ); //bbGCStackTop=ebp+28; // BaH bbThreadStartup(); bbGCStartup(); if( GetVersionEx( &os ) ){ if( os.dwPlatformId==VER_PLATFORM_WIN32_NT ){ _bbusew=1; } } if( _bbusew ){ int e=0; wchar_t buf[MAX_PATH]; _wgetcwd( buf,MAX_PATH ); for( i=0;buf[i];++i ){ if( buf[i]=='\\' ) buf[i]='/'; } bbLaunchDir=bbStringFromWString( buf ); GetModuleFileNameW( GetModuleHandleW(0),buf,MAX_PATH ); for( i=0;buf[i];++i ){ if( buf[i]=='\\' ) buf[i]='/'; if( buf[i]=='/' ) e=i; } bbAppFile=bbStringFromWString( buf ); if( e ){ if( buf[e-1]==':' ) ++e; bbAppDir=bbStringFromShorts( buf,e ); }else{ bbAppDir=&bbEmptyString; } _wchdir( bbTmpWString( bbAppDir ) ); }else{ int e=0; char buf[MAX_PATH]; _getcwd( buf,MAX_PATH ); for( i=0;buf[i];++i ){ if( buf[i]=='\\' ) buf[i]='/'; } bbLaunchDir=bbStringFromCString( buf ); GetModuleFileNameA( GetModuleHandleA(0),buf,MAX_PATH ); for( i=0;buf[i];++i ){ if( buf[i]=='\\' ) buf[i]='/'; if( buf[i]=='/' ) e=i; } bbAppFile=bbStringFromCString( buf ); if( e ){ if( buf[e-1]==':' ) ++e; bbAppDir=bbStringFromBytes( buf,e ); }else{ bbAppDir=&bbEmptyString; } _chdir( bbTmpCString( bbAppDir ) ); } #elif __linux char *ebp; char buf[PATH_MAX]; char lnk[PATH_MAX]; pid_t pid; // asm( "movl %%ebp,%0;":"=r"(ebp) );//::"%ebp" ); bbGCStackTop=ebp+28; // BaH bbThreadStartup(); bbGCStartup(); getcwd( buf,PATH_MAX ); bbLaunchDir=bbStringFromUTF8String( buf ); pid=getpid(); sprintf( lnk,"/proc/%i/exe",pid ); i=readlink( lnk,buf,PATH_MAX ); if( i>0 ){ char *p; buf[i]=0; bbAppFile=bbStringFromUTF8String( buf ); p=strrchr( buf,'/' ); if( p ){ *p=0; bbAppDir=bbStringFromUTF8String( buf ); }else{ bbAppDir=&bbEmptyString; } }else{ bbAppFile=&bbEmptyString; bbAppDir=&bbEmptyString; } chdir( bbTmpUTF8String( bbAppDir ) ); #elif __APPLE__ CFURLRef url; char buf[PATH_MAX],*e; //#if BB_ARGP // bbGCStackTop=bbArgp(0); //#else bbGCStackTop=&argc; //#endif // BaH bbThreadStartup(); bbGCStartup(); getcwd( buf,PATH_MAX ); bbLaunchDir=bbStringFromUTF8String( buf ); url=CFBundleCopyExecutableURL( CFBundleGetMainBundle() ); CFURLGetFileSystemRepresentation( url,true,(UInt8*)buf,PATH_MAX ); CFRelease( url ); bbAppFile=bbStringFromUTF8String( buf ); if( e=strstr( buf,".app/Contents/MacOS/" ) ){ *e=0; } if( e=strrchr( buf,'/' ) ){ *e=0; bbAppDir=bbStringFromUTF8String( buf ); }else{ bbAppDir=&bbEmptyString; } chdir( bbTmpUTF8String( bbAppDir ) ); #endif BBINCREFS( bbLaunchDir ); BBINCREFS( bbAppDir ); BBINCREFS( bbAppFile ); bbAppTitle=bbStringFromCString( "BlitzMax Application" ); BBINCREFS( bbAppTitle ); bbAppArgs=bbArrayNew1D( "$",argc ); BBINCREFS( bbAppArgs ); p=(BBString**)BBARRAYDATA( bbAppArgs,1 ); for( k=0;k<argc;++k ){ BBString *arg=bbStringFromCString( argv[k] ); BBINCREFS( arg ); *p++=arg; } startup(); }