4DIS - XLF

From Unify Community Wiki
Jump to: navigation, search

X-Language-File

4D IS - Back


XLF is a class to read and write files.


You need ...

Coming Soon !

Coming Soon !

... and ...

XLF.js

<javascript>

class XLF { function XLF ( ) { data = new Array ( ); }

function XLF ( url : String ) { data = new Array ( );

LoadFile ( url ); }

public var data : Array;

public function FromString ( s : String ) { Clear ( );

var seperator : char = "\n" [ 0 ]; var list : String [ ] = s.Split ( seperator );

for ( var i : int = 0; i < list.length; i++ ) { data.Push ( XLFVar ( list [ i ] ) ); } }

public function ToString ( ) { var rs : String = "";

for ( var i : int = 0; i < data.length - 1; i++ ) rs += data [ i ].ToString ( ) + "\n";

rs += data [ i ].ToString ( );

return rs; }

public function Clear ( ) { while ( data.length > 0 ) data.RemoveAt ( 0 ); }

public function LoadFile ( url : String ) { if ( IO.ExistsFile ( url ) ) FromString ( IO.ReadFile ( url ) ); }

public function LoadFile ( url : String, key : int ) { if ( IO.ExistsFile ( url ) ) FromString ( FileEncryption.Encrypt ( IO.ReadFile ( url ), key ) ); }

public function SaveFile ( url : String ) { IO.WriteFile ( url, ToString ( ) ); }

public function SaveFile ( url : String, key : int ) { IO.WriteFile ( url, FileEncryption.Encrypt ( ToString ( ), key ) ); }

public function SetElement ( name : String, value : Vector3 ) { RemoveElement ( name + "_x" ); RemoveElement ( name + "_y" ); RemoveElement ( name + "_z" );

AddElement ( name + "_x", value.x ); AddElement ( name + "_y", value.y ); AddElement ( name + "_z", value.z ); }

public function SetElement ( name : String, value : Quaternion ) { RemoveElement ( name + "_x" ); RemoveElement ( name + "_y" ); RemoveElement ( name + "_z" ); RemoveElement ( name + "_w" );

AddElement ( name + "_x", value.x ); AddElement ( name + "_y", value.y ); AddElement ( name + "_z", value.z ); AddElement ( name + "_w", value.w ); }

public function ReadVector3 ( name : String ) { var nv3 : Vector3 = new Vector3 ( 0, 0, 0 );

if ( ExistsElement ( name + "_x" ) ) nv3.x = data [ FindElement ( name + "_x" ) ].value;

if ( ExistsElement ( name + "_y" ) ) nv3.y = data [ FindElement ( name + "_y" ) ].value;

if ( ExistsElement ( name + "_z" ) ) nv3.z = data [ FindElement ( name + "_z" ) ].value;

return nv3; }

public function ReadQuaternion ( name : String ) { var nv4 : Quaternion = new Quaternion ( 0, 0, 0, 0 );

if ( ExistsElement ( name + "_x" ) ) nv4.x = data [ FindElement ( name + "_x" ) ].value;

if ( ExistsElement ( name + "_y" ) ) nv4.y = data [ FindElement ( name + "_y" ) ].value;

if ( ExistsElement ( name + "_z" ) ) nv4.z = data [ FindElement ( name + "_z" ) ].value;

if ( ExistsElement ( name + "_w" ) ) nv4.w = data [ FindElement ( name + "_w" ) ].value;

return nv4; }

public function AddXLF ( name : String, xlf : XLF ) { for ( var e : XLFVar in xlf.data ) { RemoveElement ( name + "_" + e.name );

AddElement ( name + "_" + e.name, e.value ); } }

public function GetXLF ( name : String ) { var xlf : XLF = new XLF ( );

for ( var e : XLFVar in data ) if ( name == e.name.Substring ( 0, name.length ) ) xlf.AddElement ( e.name.Substring ( name.length, e.name.length ), e.value ); }

public function SetElement ( name : String, value : Object ) { RemoveElement ( name );

AddElement ( name, value ); }

public function ReadElement ( name : String ) { if ( ExistsElement ( name ) ) return data [ FindElement ( name ) ].value; else return null; }

public function ExistsElement ( name : String ) { for ( var i : int = 0; i < data.length; i++ ) if ( data [ i ].name == name ) return true;

return false; }

public function FindElement ( name : String ) { for ( var i : int = 0; i < data.length; i++ ) if ( data [ i ].name == name ) return i;

return 0; }

public function AddElement ( name : String, value : String ) { if ( ! ExistsElement ( name ) ) data.Push ( XLFVar ( name, "string", value ) ); else Debug.LogError ( "Element does already exists" ); }

public function AddElement ( name : String, value : int ) { if ( ! ExistsElement ( name ) ) data.Push ( XLFVar ( name, "int", value ) ); else Debug.LogError ( "Element does already exists" ); }

public function AddElement ( name : String, value : float ) { if ( ! ExistsElement ( name ) ) data.Push ( XLFVar ( name, "float", value ) ); else Debug.LogError ( "Element does already exists" ); }

public function AddElement ( name : String, value : boolean ) { if ( ! ExistsElement ( name ) ) data.Push ( XLFVar ( name, "bool", value ) ); else Debug.LogError ( "Element does already exists" ); }

public function RemoveElement ( name : String ) { for ( var i : int = 0; i < data.length; i++ ) if ( data [ i ].name == name ) { data.RemoveAt ( i ); break; } } }

class XLFVar { function XLFVar ( ) { }

function XLFVar ( n : String, t : String, v : Object ) { name = n; type = t; value = v; }

function XLFVar ( s : String ) { FromString ( s ); }

public var name : String = ""; public var type : String = ""; public var value : Object;

public function FromString ( s : String ) { var list : Array = XLFList.Compile ( s );

if ( list.length < 3 ) return null;

name = list [ 0 ]; type = list [ 1 ];

if ( type == "string" ) value = list [ 2 ]; else if ( type == "int" ) value = XLFStream.StringToInt ( list [ 2 ] ); else if ( type == "float" ) value = XLFStream.StringToFloat ( list [ 2 ] ); else if ( type == "bool" ) value = XLFStream.StringToBool ( list [ 2 ] ); }

public function ToString ( ) { var list : Array = new Array ( );

list.length = 3;

list [ 0 ] = name; list [ 1 ] = type;

if ( type == "string" ) list [ 2 ] = value; else if ( type == "int" ) list [ 2 ] = XLFStream.IntToString ( value ); else if ( type == "float" ) list [ 2 ] = XLFStream.FloatToString ( value ); else if ( type == "bool" ) list [ 2 ] = XLFStream.BoolToString ( value );

return XLFList.Generate ( list ); } }

class XLFStream { public static function IntToString ( s : int ) { return s.ToString ( ); }

public static function StringToInt ( s : String ) { var rv : int = 0; var factor : int = 1;

for ( var i : int = s.length - 1; i >= 0; i-- ) { for ( var c : int = 0; c < 10; c++ ) if ( c.ToString ( ) == s [ i ] ) { rv += c * factor; factor *= 10; } }

if ( s.IndexOf ( "-" ) != -1 ) rv *= -1;

return rv; }

public static function FloatToString ( s : float ) { var edit : String = s.ToString ( );

if ( edit.IndexOf ( "," ) == -1 ) if ( edit.IndexOf ( "." ) == -1 ) edit += ".0";

return edit; }

public static function StringToFloat ( s : String ) { if ( s.IndexOf ( "," ) == -1 ) if ( s.IndexOf ( "." ) == -1 ) return StringToInt ( s );

var rv : float = 0; var factor : float = 1.0; var ff : int = 0;

if ( s.IndexOf ( "," ) != -1 ) ff = s.IndexOf ( "," ); else ff = s.IndexOf ( "." );

ff = s.length - ff - 1;

for ( var f : int = 0; f < ff; f++ ) factor /= 10;

for ( var i : int = s.length - 1; i >= 0; i-- ) { for ( var c : int = 0; c < 9; c++ ) if ( c.ToString ( ) == s [ i ] ) { rv += c * factor; factor *= 10; } }

if ( s.IndexOf ( "-" ) != -1 ) rv *= -1;

return rv; }

public static function BoolToString ( s : boolean ) { if ( s ) return "true"; else return "false"; }

public static function StringToBool ( s : String ) { if ( s == "true" ) return true; else return false; } }

class XLFList { public static function Compile ( s : String ) { var list : Array = new Array ( ); var index : boolean = false; var temp : String = "";

for ( var i : int = 0; i < s.length; i++ ) { if ( index == false ) { if ( s [ i ] == "<" ) index = true; else if ( s [ i ] == " " ) index = true; else if ( s [ i ] == ">" ) break; } else if ( index == true ) { temp += s [ i ];

if ( i < s.length ) { if ( s [ i + 1 ] == " " ) { index = false;

list.Push ( temp ); temp = ""; } else if ( s [ i + 1 ] == ">" ) { index = false;

list.Push ( temp ); temp = ""; } } } }

for ( var b : int = 0; b < list.length; b++ ) list [ b ] = XLFString.PlaceSpecial ( list [ b ] );

return list; }

public static function Generate ( s : Array ) { if ( s.length == 0 ) return "<>";

var rs : String = "<";

for ( var i : int = 0; i < s.length - 1; i++ ) { rs += XLFString.ReplaceSpecial ( s [ i ] ) + " "; }

var le : String= s [ s.length - 1 ];

rs += XLFString.ReplaceSpecial ( le );

rs += ">";

return rs; } }

class XLFString { public static function ReplaceSpecial ( s : String ) { var edit : String = s;

while ( edit.IndexOf ( "[" ) != -1 ) edit = edit.Replace ( "[", "temp:squareBrackedOpen" );

while ( edit.IndexOf ( "]" ) != -1 ) edit = edit.Replace ( "]", "temp:squareBrackedClose" );

while ( edit.IndexOf ( "temp:squareBrackedClose" ) != -1 ) edit = edit.Replace ( "temp:squareBrackedClose", "[squareBrackedClose]" );

while ( edit.IndexOf ( "temp:squareBrackedOpen" ) != -1 ) edit = edit.Replace ( "temp:squareBrackedOpen", "[squareBrackedOpen]" );

while ( edit.IndexOf ( "<" ) != -1 ) edit = edit.Replace ( "<", "[brackedOpen]" );

while ( edit.IndexOf ( " " ) != -1 ) edit = edit.Replace ( " ", "[space]" );

while ( edit.IndexOf ( ">" ) != -1 ) edit = edit.Replace ( ">", "[brackedClose]" );

while ( edit.IndexOf ( "\n" ) != -1 ) edit = edit.Replace ( "\n", "[nextLine]" );

return edit; }

public static function PlaceSpecial ( s : String ) { var edit : String = s;

while ( edit.IndexOf ( "[squareBrackedOpen]" ) != -1 ) edit = edit.Replace ( "[squareBrackedOpen]", "[" );

while ( edit.IndexOf ( "[squareBrackedClose]" ) != -1 ) edit = edit.Replace ( "[squareBrackedClose]", "]" );

while ( edit.IndexOf ( "[brackedOpen]" ) != -1 ) edit = edit.Replace ( "[brackedOpen]", "<" );

while ( edit.IndexOf ( "[space]" ) != -1 ) edit = edit.Replace ( "[space]", " " );

while ( edit.IndexOf ( "[brackedClose]" ) != -1 ) edit = edit.Replace ( "[brackedClose]", ">" );

while ( edit.IndexOf ( "[nextLine]" ) != -1 ) edit = edit.Replace ( "[nextLine]", "\n" );

return edit; } }

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox