XLF

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(By 4D Interactive Studio)
(By 4D Interactive Studio)
Line 62: Line 62:
 
 
 
return readedString;
 
return readedString;
 +
}
 +
}
 +
 +
</javascript>
 +
 +
----
 +
 +
XLF Classes.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;
 
}
 
}
 
}
 
}
  
 
</javascript>
 
</javascript>

Revision as of 16:39, 21 October 2011

X-Language-File

By 4D Interactive Studio

XLF is a class to read and write files.


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 Classes.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; } }

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox