//----------------------------------------------------------------// STLString MOAILuaState::GetStackTrace ( int level ) { int firstpart = 1; /* still before eventual `...' */ lua_Debug ar; lua_State* L = this->mState; STLString out; out.append ( "stack traceback:" ); while ( lua_getstack ( L, level++, &ar )) { if ( level > LEVELS1 && firstpart ) { if ( !lua_getstack ( L, level + LEVELS2, &ar )) { level--; } else { // too many levels out.append ( "\n\t..." ); /* too many levels */ // find last levels */ while ( lua_getstack ( L, level + LEVELS2, &ar )) level++; } firstpart = 0; continue; } out.append ( "\n\t" ); lua_getinfo ( L, "Snl", &ar ); out.append ( ar.short_src ); if ( ar.currentline > 0 ) { out.write ( ":%d", ar.currentline ); } if ( *ar.namewhat != '\0' ) { out.write ( " in function '%s'", ar.name ); } else { if ( *ar.what == 'm' ) { out.write ( " in main chunk" ); } else if ( *ar.what == 'C' || *ar.what == 't' ) { out.write ( " ?" ); } else { out.write ( " in function <%s:%d>", ar.short_src, ar.linedefined ); } } } out.append ( "\n" ); return out; }
//----------------------------------------------------------------// bool USFileSys::Copy ( cc8* path, cc8* newPath ) { zl_stat fileStat; if ( !USFileSys::GetFileStat ( path, fileStat )) return false; if ( !fileStat.mExists ) return false; if ( fileStat.mIsDir ) { bool result = true; STLString cwd = USFileSys::GetCurrentPath (); STLString toPath = USFileSys::GetAbsoluteDirPath ( newPath ); USFileSys::AffirmPath ( toPath ); USFileSys::SetCurrentPath ( path ); ZLDIR* itr = zl_dir_open (); if ( itr ) { while ( zl_dir_read_entry ( itr )) { cc8* entry = zl_dir_entry_name ( itr ); if ( strcmp ( entry, "." ) == 0 ) continue; if ( strcmp ( entry, ".." ) == 0 ) continue; STLString destEntry = toPath; destEntry.append ( entry ); if ( !USFileSys::Copy ( entry, destEntry )) { result = false; break; } } zl_dir_close ( itr ); } USFileSys::SetCurrentPath ( cwd ); return result; } else { USFileStream infile; if ( infile.OpenRead ( path )) { USFileStream outfile; if ( outfile.OpenWrite ( newPath )) { outfile.WriteStream ( infile ); return true; } } } return false; }
//----------------------------------------------------------------// static STLString _escapeString ( cc8* str ) { u32 len = ( u32 )strlen ( str ); STLString outStr; outStr.reserve ( len * 2 ); for ( u32 i = 0; i < len; ++i ) { char c = str [ i ]; if ( c == '\\' ) { outStr.append ( "\\\\" ); } else { outStr.push_back ( c ); } } return outStr; }
//----------------------------------------------------------------// STLString ZLCgt::ReadUnicodeAsASCII ( ZLStream& stream ) { // TODO: change this to convert to UTF8! // Also: GOLD now has native UTF8 support. Look into it. STLString result = ""; static const u32 BUFFER_SIZE = 1024; char buffer [ BUFFER_SIZE ]; u16 utf = 1; while ( utf ) { for ( u32 i = 0; ( i < BUFFER_SIZE ) && utf; ++i ) { utf = stream.Read < u16 >( 0 ); buffer [ i ] = ( char )( utf & 0x00ff ); } result.append ( buffer ); } return result; }
//----------------------------------------------------------------// STLString USStream::ReadToken ( cc8* delimiters ) { STLString str; if ( this->IsAtEnd ()) return str; char stackBuffer [ LOCAL_BUFFER ]; USMemStream memStream; memStream.SetChunkSize ( LOCAL_BUFFER ); memStream.SetGuestBuffer ( stackBuffer, LOCAL_BUFFER ); char c = 0; size_t size = 0; do { c = this->Read < char >( 0 ); if ( delimiters && c ) { bool isDelimiter = false; for ( size_t i = 0; delimiters [ i ]; ++i ) { if ( delimiters [ i ] == c ) { isDelimiter = true; break; } } if ( isDelimiter ) { if ( size ) { c = 0; } else { continue; } } } memStream.Write < char >( c ); size++; } while ( c ); if ( size ) { str.reserve ( size + 1 ); memStream.Seek ( 0, SEEK_SET ); while ( size > 0 ) { char buffer [ LOCAL_BUFFER ]; size_t readSize = size; if ( LOCAL_BUFFER < readSize ) { readSize = LOCAL_BUFFER; } memStream.ReadBytes ( buffer, readSize ); str.append ( buffer, readSize ); size -= readSize; } } return str; }
//----------------------------------------------------------------// u32 USLuaSerializer::WriteTable ( USStream& stream, USLuaState& state, int idx, u32 tab ) { STLString indent; for ( u32 i = 0; i < tab; ++i ) { indent.append ( "\t" ); } u32 count = 0; u32 itr = state.PushTableItr ( idx ); while ( state.TableItrNext ( itr )) { if ( count == 0 ) { stream.Print ( "\n" ); } switch ( lua_type ( state, -2 )) { case LUA_TSTRING: { stream.Print ( "%s[ \"%s\" ] = ", indent.c_str (), lua_tostring ( state, -2 )); break; } case LUA_TNUMBER: { stream.Print ( "%s[ %s ]\t= ", indent.c_str (), lua_tostring ( state, -2 )); break; } }; switch ( lua_type ( state, -1 )) { case LUA_TBOOLEAN: { int value = lua_toboolean ( state, -1 ); cc8* str = ( value ) ? "true": "false"; stream.Print ( "%s,\n", str ); break; } case LUA_TTABLE: { uintptr tableID = ( uintptr )lua_topointer ( state, -1 ); if ( this->mTableMap.contains ( tableID )) { stream.Print ( "objects [ 0x%08X ],\n", tableID ); } else { stream.Print ( "{" ); if ( this->WriteTable ( stream, state, -1, tab + 1 )) { stream.Print ( "%s},\n", indent.c_str ()); } else { stream.Print ( "},\n" ); } } break; } case LUA_TSTRING: { STLString str = _escapeString ( lua_tostring ( state, -1 )); stream.Print ( "\"%s\",\n", str.c_str ()); break; } case LUA_TNUMBER: { stream.Print ( "%s,\n", lua_tostring ( state, -1 )); break; } case LUA_TLIGHTUSERDATA: { stream.Print ( "%p,\n", lua_touserdata ( state, -1 )); break; } }; ++count; } return count; }