XLF

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(By 4D Interactive Studio)
(Blanked the page)
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
  
== '''X-Language-File''' ==
 
 
== By 4D Interactive Studio ==
 
 
 
 
XLF is a class to read and write files.
 
 
----
 
 
'''Using :'''
 
 
<javascript>
 
 
var file : XLF = new XLF ( "Test.txt" );
 
 
</javascript>
 
 
----
 
 
IO.js
 
 
<javascript>
 
 
import System.IO;
 
 
class IO
 
{
 
// ------------------------------ File ------------------------------ //
 
 
public static function ExistsFile ( url : String )
 
{
 
var info : FileInfo = new FileInfo ( url );
 
 
return info.Exists;
 
}
 
 
public static function RemoveFile ( url : String )
 
{
 
var info : FileInfo = new FileInfo ( url );
 
 
if ( info.Exists )
 
info.Delete ( );
 
}
 
 
public static function WriteFile ( url : String, string : String )
 
{
 
var writer : StreamWriter;
 
var info : FileInfo = new FileInfo ( url );
 
 
if ( info.Exists )
 
info.Delete ( );
 
 
writer = info.CreateText ( );
 
 
writer.Write ( string );
 
writer.Close ( );
 
}
 
 
public static function ReadFile ( url : String )
 
{
 
var info : FileInfo = new FileInfo ( url );
 
 
if ( ! info.Exists )
 
return "";
 
 
var reader : StreamReader = File.OpenText ( url );
 
var readedString : String = reader.ReadToEnd ( ); 
 
 
reader.Close ( );
 
 
return readedString;
 
}
 
}
 
 
</javascript>
 
 
XLF.js
 
 
<javascript>
 
 
class XLFVar
 
{
 
// List : <...name... ...type... ...value...>
 
 
/*
 
Types :
 
 
"string" = String
 
"int" = int
 
"float" = float
 
"bool" = boolean
 
*/
 
 
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
 
{
 
// Int ...
 
 
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;
 
}
 
 
// Float ...
 
 
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;
 
}
 
 
// Bool ...
 
 
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]" );
 
 
// Part B ...
 
 
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]", "]" );
 
 
// Part B ...
 
 
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;
 
}
 
}
 
 
class XLF
 
{
 
function XLF ( )
 
{
 
data = new Array ( );
 
}
 
 
function XLF ( url : String )
 
{
 
data = new Array ( );
 
 
LoadFile ( url );
 
}
 
 
// -------------------------------------------------- Data -------------------------------------------------- //
 
 
public var data : Array;
 
 
// -------------------------------------------------- IO -------------------------------------------------- //
 
 
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 );
 
}
 
 
// -------------------------------------------------- Basic functions -------------------------------------------------- //
 
 
// Save and Load
 
 
public function LoadFile ( url : String )
 
{
 
if ( IO.ExistsFile ( url ) )
 
FromString ( IO.ReadFile ( url ) );
 
}
 
 
public function SaveFile ( url : String )
 
{
 
IO.WriteFile ( url, ToString ( ) );
 
}
 
 
// Data
 
 
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;
 
}
 
 
// -------------------------------------------------- Functions -------------------------------------------------- //
 
 
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;
 
}
 
}
 
}
 
 
</javascript>
 

Latest revision as of 18:59, 21 October 2011

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox