//-------------------------------------------------------- // //-------------------------------------------------------- Bool TLString::ReadNextLetter(const TString& String,u32& CharIndex, TChar& Char) { // step over whitespace s32 NonWhitespaceIndex = String.GetCharIndexNonWhitespace( CharIndex ); if ( NonWhitespaceIndex == -1 ) return FALSE; // move char past whitespace CharIndex = (u32)NonWhitespaceIndex; const TChar& NextChar = String.GetCharAt(CharIndex); // is next char a letter? if ( TLString::IsCharLetter( NextChar ) ) { Char = NextChar; // move string past this letter for next thing CharIndex++; return TRUE; } else { // not a char, could be a number or summink return FALSE; } }
//-------------------------------------------------------- // //-------------------------------------------------------- SyncBool TLFile::ImportBinaryData(const TString& DataString,TBinary& BinaryData,TRef DataType) { /* // work out the type of data TRefRef BinaryDataType = BinaryData.GetDataTypeHint(); // check for conflicting type hints if ( DataType.IsValid() && BinaryDataType.IsValid() && DataType != BinaryDataType ) { TDebugString Debug_String; Debug_String << "Data import type hint mismatch; Tried to import as " << DataType << " but binary says it's " << BinaryDataType; TLDebug_Break( Debug_String ); // fall through to use the data type embedded in the binary data DataType = BinaryDataType; } else if ( BinaryDataType.IsValid() && !DataType.IsValid() ) { // use the type specified in the binary DataType = BinaryDataType; } */ // import the data based on the type u32 CharIndex = 0; switch ( DataType.GetData() ) { case TLBinary_TypeRef(float): { float f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, &f, 1 ) ) return SyncFalse; BinaryData.Write( f ); return SyncTrue; } case TLBinary_TypeRef(float2): { float2 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; BinaryData.Write( f ); return SyncTrue; } case TLBinary_TypeRef(float3): { float3 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; BinaryData.Write( f ); return SyncTrue; } case TLBinary_TypeRef(float4): { float4 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; BinaryData.Write( f ); return SyncTrue; } case TLBinary_TypeRef(TQuaternion): { float4 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; // convert to normalised quaternion TLMaths::TQuaternion Quat( f ); Quat.Normalise(); BinaryData.Write( Quat ); return SyncTrue; } case TLBinary_TypeRef(TEuler): { float3 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; // convert to Euler type TLMaths::TEuler Euler( f ); BinaryData.Write( Euler ); return SyncTrue; } case TLBinary_TypeRef(TAxisAngle): { float4 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; // convert to normalised quaternion TLMaths::TAxisAngle AxisAngle( f ); BinaryData.Write( AxisAngle ); return SyncTrue; } case TLBinary_TypeRef(TRef): { TRef Ref( DataString ); BinaryData.Write( Ref ); return SyncTrue; } case TLBinary_TypeRef_String: { // do string cleanup, convert "\n" to a linefeed etc if ( TLString::IsStringDirty( DataString ) ) { TString OutputString = DataString; TLString::CleanString( OutputString ); BinaryData.WriteString( OutputString ); } else { // already clean, just write the original BinaryData.WriteString( DataString ); } return SyncTrue; } case TLBinary_TypeRef(TColour): { float4 f; if ( !TLString::ReadNextFloatArray( DataString, CharIndex, f.GetData(), f.GetSize() ) ) return SyncFalse; // check range // gr: use TLDebug_CheckInRange() ? if ( f.x > 1.0f || f.x < 0.0f || f.y > 1.0f || f.y < 0.0f || f.z > 1.0f || f.z < 0.0f || f.w > 1.0f || f.w < 0.0f ) { if ( !TLDebug_Break( TString("Colour float type has components out of range (0..1); %.3f,%.3f,%.3f,%.3f", f.x, f.y, f.z, f.w) ) ) return SyncFalse; } TColour Colour( f ); BinaryData.Write( Colour ); return SyncTrue; } case TLBinary_TypeRef(TColour24): { Type3<s32> Colours; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.x ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.y ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.z ) ) return SyncFalse; // check range // gr: use TLDebug_CheckInRange() ? if ( Colours.x > 255 || Colours.x < 0 || Colours.y > 255 || Colours.y < 0 || Colours.z > 255 || Colours.z < 0 ) { if ( !TLDebug_Break( TString("Colour24 type has components out of range (0..255); %d,%d,%d", Colours.x, Colours.y, Colours.z ) ) ) return SyncFalse; } TColour24 Colour( Colours.x, Colours.y, Colours.z ); BinaryData.Write( Colour ); return SyncTrue; } case TLBinary_TypeRef(TColour32): { Type4<s32> Colours; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.x ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.y ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.z ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.w ) ) return SyncFalse; // check range // gr: use TLDebug_CheckInRange() ? if ( Colours.x > 255 || Colours.x < 0 || Colours.y > 255 || Colours.y < 0 || Colours.z > 255 || Colours.z < 0 || Colours.w > 255 || Colours.w < 0 ) { if ( !TLDebug_Break( TString("Colour32 type has components out of range (0..255); %d,%d,%d,%d", Colours.x, Colours.y, Colours.z, Colours.w ) ) ) return SyncFalse; } TColour32 Colour( Colours.x, Colours.y, Colours.z, Colours.w ); BinaryData.Write( Colour ); return SyncTrue; } case TLBinary_TypeRef(TColour64): { Type4<s32> Colours; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.x ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.y ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.z ) ) return SyncFalse; if ( !TLString::ReadNextInteger( DataString, CharIndex, Colours.w ) ) return SyncFalse; // check range // gr: use TLDebug_CheckInRange() ? if ( Colours.x > 65535 || Colours.x < 0 || Colours.y > 65535 || Colours.y < 0 || Colours.z > 65535 || Colours.z < 0 || Colours.w > 65535 || Colours.w < 0 ) { if ( !TLDebug_Break( TString("Colour64 type has components out of range (0..65535); %d,%d,%d,%d", Colours.x, Colours.y, Colours.z, Colours.w ) ) ) return SyncFalse; } TColour64 Colour( Colours.x, Colours.y, Colours.z, Colours.w ); BinaryData.Write( Colour ); return SyncTrue; } case TLBinary_TypeRef(u8): return ImportBinaryDataIntegerInRange<u8>( BinaryData, DataString ); case TLBinary_TypeRef(s8): return ImportBinaryDataIntegerInRange<s8>( BinaryData, DataString ); case TLBinary_TypeRef(u16): return ImportBinaryDataIntegerInRange<u16>( BinaryData, DataString ); case TLBinary_TypeRef(s16): return ImportBinaryDataIntegerInRange<s16>( BinaryData, DataString ); case TLBinary_TypeRef(u32): return ImportBinaryDataIntegerInRange<u32>( BinaryData, DataString ); case TLBinary_TypeRef(s32): return ImportBinaryDataIntegerInRange<s32>( BinaryData, DataString ); case TLBinary_TypeRef(Bool): { // read first char, we can work out true/false/0/1 from that if ( DataString.GetLength() == 0 ) return SyncFalse; const TChar& BoolChar = DataString.GetCharAt(0); if ( BoolChar == 't' || BoolChar == 'T' || BoolChar == '1' ) { BinaryData.Write( (Bool)TRUE ); return SyncTrue; } else if ( BoolChar == 'f' || BoolChar == 'F' || BoolChar == '0' ) { BinaryData.Write( (Bool)FALSE ); return SyncTrue; } else { TLDebug_Break("Bool data is not True,False,0 or 1"); return SyncFalse; } } default: break; }; TDebugString Debug_String; Debug_String << "Unsupported/todo data type " << DataType << ". Data string: [" << DataString << "]"; TLDebug_Break( Debug_String ); return SyncFalse; }