Skip to content

Commit

Permalink
Fixes newline problems on Windows for command logs generated in Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasmalacofilho committed Sep 4, 2013
1 parent 251a948 commit 656fe91
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
28 changes: 14 additions & 14 deletions src/sim/Simulator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ class Simulator {
public var profiling:Null<String>;
public var log:Array<String>;

public var newline:String;
public var screenSize:Int;

public var underlyingInput:haxe.io.Input;

public var online:Bool;

private function new( _underlyingInput:haxe.io.Input, _newline, _screenSize ) {
newline = _newline;
private function new( _underlyingInput:haxe.io.Input, _screenSize ) {
screenSize = _screenSize;
underlyingInput = _underlyingInput;
online = false;
Expand All @@ -34,7 +32,7 @@ class Simulator {
, ADijkstra
, sim.state.heapArity
, sim.state.heapReserve )
: new SimulatorState( this, newline
: new SimulatorState( this, baseNewline
, ADijkstra
, 3 // optimal b*log(N,b)
, 16 ); // reasonable considering
Expand Down Expand Up @@ -62,9 +60,9 @@ class Simulator {

public static inline var SHORTNAME = "Vijka";
public static inline var FULLNAME = "Vijka - Demand model for highway tolls on regional road networks";
public static inline var COPYRIGHT = "Copyright 2013, Jonas Malaco and Arthur Szász, Elebeta Consultoria";
public static inline var COPYRIGHT = "Copyright 2013, Jonas Malaco Filho and Arthur Campora Szász, Elebeta Consultoria";
public static inline var LICENSE = "Licensed under the BSD 3-clause license: http://opensource.org/licenses/BSD-3-Clause";
public static inline var VERSION = "1.1.0";
public static inline var VERSION = "1.1.1";
public static inline var BUILD = utils.GitVersion.get( 8 );
public static inline var PLATFORM = #if neko
"Neko";
Expand All @@ -87,14 +85,14 @@ class Simulator {
}

public static function println( s:String, ?err=false ) {
print( s+"\n", err );
print( s+baseNewline, err );
}

public static function printHL( s:String, ?err=false ) {
println( StringTools.rpad( "", s, sim != null ? sim.screenSize : 80 ) );
}

public function getArgs( inp:Input ):Array<String> {
public function getArgs( inp:Input, newline:String ):Array<String> {
var reader = new format.csv.Reader( inp, newline, " ", "'" );
var args = reader.readRecord();
if ( args.length>0 && args[0].length>0 && args[0].charCodeAt(0)=="#".code )
Expand All @@ -105,8 +103,9 @@ class Simulator {

public function strArgs( args:Array<String> ):String {
var buf = new haxe.io.BytesOutput();
new format.csv.Writer( buf, newline, " ", "'" ).writeRecord( args );
return buf.getBytes().toString();
new format.csv.Writer( buf, "\n", " ", "'" ).writeRecord( args );
var bufStr = buf.getBytes().toString();
return bufStr.substr( 0, bufStr.length - 1 );
}

private static var stdin = Sys.stdin();
Expand Down Expand Up @@ -146,11 +145,12 @@ class Simulator {
}

public static var sim:Simulator;
public static var baseNewline:String;

private static function main() {

var initialNewline = ( PLATFORM == "Java" && Sys.systemName() == "Windows" ) ? "\r\n" : "\n";
sim = new Simulator( stdin, initialNewline, 80 );
baseNewline = ( PLATFORM == "Java" && Sys.systemName() == "Windows" ) ? "\r\n" : "\n";
sim = new Simulator( stdin, 80 );

if ( Sys.args().length > 0 ) {
println( ":: "+Sys.args().join( " " ) );
Expand All @@ -167,8 +167,8 @@ class Simulator {

while ( true ) {
try {
print( "> " ); stdout.flush();
var r = sim.getArgs( stdin );
print( "> " );
var r = sim.getArgs( stdin, baseNewline );
sim.run( r, false, true, true );
}
catch ( e:haxe.io.Eof ) {
Expand Down
62 changes: 32 additions & 30 deletions src/sim/SimulatorAPI.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import sim.col.LinkTypeSpeedMap;
import sim.uq.Search;
import sim.uq.Update;

import sim.Simulator.baseNewline;
import sim.Simulator.print;
import sim.Simulator.printHL;
import sim.Simulator.println;
Expand Down Expand Up @@ -98,22 +99,22 @@ class SimulatorAPI extends mcli.CommandLine {
var nodes = sim.state.nodes; // just a shortcut
if ( nodes == null ) throw "No nodes";
var fout = _writeFile( path, false );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.newline );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.state.newline );
var first = true;
if ( filter == null ) {
for ( n in nodes ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonNode( n ) );
}
}
else {
var q = Search.prepare( filter, "id" );
for ( n in q.execute( sim, nodes, null ) ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonNode( n ) );
}
}
fout.writeString( sim.newline+"] }"+sim.newline );
fout.writeString( sim.state.newline+"] }"+sim.state.newline );
fout.close();
}

Expand Down Expand Up @@ -236,23 +237,23 @@ class SimulatorAPI extends mcli.CommandLine {
var links = sim.state.links; // just a shortcut
if ( links == null ) throw "No links";
var fout = _writeFile( path, false );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.newline );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.state.newline );
var first = true;
if ( filter == null ) {
for ( k in links ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonLink( k, false ) );
}
}
else {
var aliases = sim.state.aliases;
var q = Search.prepare( filter, "id" );
for ( k in q.execute( sim, links, aliases ) ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonLink( k, false ) );
}
}
fout.writeString( sim.newline+"] }"+sim.newline );
fout.writeString( sim.state.newline+"] }"+sim.state.newline );
fout.close();
}

Expand All @@ -266,23 +267,23 @@ class SimulatorAPI extends mcli.CommandLine {
var links = sim.state.links; // just a shortcut
if ( links == null ) throw "No links";
var fout = _writeFile( path, false );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.newline );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.state.newline );
var first = true;
if ( filter == null ) {
for ( k in links ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonLink( k, true ) );
}
}
else {
var aliases = sim.state.aliases;
var q = Search.prepare( filter, "id" );
for ( k in q.execute( sim, links, aliases ) ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonLink( k, true ) );
}
}
fout.writeString( sim.newline+"] }"+sim.newline );
fout.writeString( sim.state.newline+"] }"+sim.state.newline );
fout.close();
}

Expand Down Expand Up @@ -925,14 +926,14 @@ class SimulatorAPI extends mcli.CommandLine {
var st = sim.state.coldStorage; // just a shortcut
if ( st == null ) throw "No cold storage";
var fout = _writeFile( path, false );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.newline );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.state.newline );
var first = true;
for ( box in st )
for ( v in box.volumes() ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonVolume( v, '"key":"${box.key}"' ) );
}
fout.writeString( sim.newline+"] }"+sim.newline );
fout.writeString( sim.state.newline+"] }"+sim.state.newline );
fout.close();
}

Expand Down Expand Up @@ -974,13 +975,13 @@ class SimulatorAPI extends mcli.CommandLine {
var volumes = sim.state.volumes; // just a shortcut
if ( volumes == null ) throw "No volumes";
var fout = _writeFile( path, false );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.newline );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.state.newline );
var first = true;
for ( v in volumes ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
fout.writeString( _geojsonVolume( v, null ) );
}
fout.writeString( sim.newline+"] }"+sim.newline );
fout.writeString( sim.state.newline+"] }"+sim.state.newline );
fout.close();
}

Expand Down Expand Up @@ -1009,10 +1010,10 @@ class SimulatorAPI extends mcli.CommandLine {
if ( volumes == null ) throw "No volumes";
if ( refVolumes == null ) throw "No reference volumes";
var fout = _writeFile( path, false );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.newline );
fout.writeString( '{"type":"FeatureCollection","features":['+sim.state.newline );
var first = true;
for ( v in volumes ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
var ref = refVolumes.get( v.linkId );
if ( ref != null ) {
var v_ = v.copy();
Expand All @@ -1025,13 +1026,13 @@ class SimulatorAPI extends mcli.CommandLine {
}
for ( ref in refVolumes ) {
if ( !volumes.exists( ref.linkId ) ) {
if ( first ) first = false; else fout.writeString( ","+sim.newline+"\t" );
if ( first ) first = false; else fout.writeString( ","+sim.state.newline+"\t" );
var v_ = LinkVolume.make( ref.linkId, 0, 0, 0, 0 );
v_.sub( ref );
fout.writeString( _geojsonVolume( v_, null ) );
}
}
fout.writeString( sim.newline+"] }"+sim.newline );
fout.writeString( sim.state.newline+"] }"+sim.state.newline );
fout.close();
}

Expand Down Expand Up @@ -1263,7 +1264,7 @@ class SimulatorAPI extends mcli.CommandLine {
print( "Saving the current command log" );
if ( !reading ) {
var fout = _writeFile( path, false );
fout.writeString( sim.log.join("") );
fout.writeString( sim.log.join(sim.state.newline)+sim.state.newline );
fout.close();
}
println( "\rSaving the current command log... Done" );
Expand All @@ -1281,13 +1282,13 @@ class SimulatorAPI extends mcli.CommandLine {
if ( !reading )
println( "" );

var finp = _readFile( path, false );
var finp = _readFile( path, true );
var eof = false;
while ( !eof ) {
try {
var r = sim.getArgs( finp );
var r = sim.getArgs( finp, sim.state.newline );
if ( r.length != 0 ) {
print( ":: "+sim.strArgs(r) );
print( ":: "+sim.strArgs(r)+baseNewline );
sim.run( r, true, true, false );
}
}
Expand All @@ -1312,7 +1313,7 @@ class SimulatorAPI extends mcli.CommandLine {
public function showLog() {
println( "Showing the current log" );
printHL( "-" );
println( " :: "+sim.log.join( " :: " ) ); // log already has newlines
println( " :: "+sim.log.join( baseNewline+" :: " ) ); // log already has newlines
}


Expand Down Expand Up @@ -1357,7 +1358,8 @@ class SimulatorAPI extends mcli.CommandLine {
// ADVANCED -----------------------------------------------------------------

/**
[ADVANCED] Set the newline sequence for all file output
[ADVANCED] Set the newline sequence for all file output; options are NL,
CRNL and NLCR
**/
public function setNewline( sequence:String ) {
switch ( sequence ) {
Expand Down Expand Up @@ -1474,7 +1476,7 @@ class SimulatorAPI extends mcli.CommandLine {
[HACK] Windows hack for problems with newlines
**/
public function windows() {
sim.newline = "\r\n";
sim.state.newline = "\r\n";
}


Expand Down Expand Up @@ -1677,7 +1679,7 @@ class SimulatorAPI extends mcli.CommandLine {

private function _writeEtt( cl:Class<Dynamic>, fields:Array<ETTField>, outputPath:String ):ETTWriter {
var fout = _writeFile( outputPath, true );
var finfo = new format.ett.Data.FileInfo( sim.newline, ETTEncoding.UTF8, "\t", "\""
var finfo = new format.ett.Data.FileInfo( sim.state.newline, ETTEncoding.UTF8, "\t", "\""
, Type.getClassName( cl ), fields );
var w = new ETTWriter( finfo );
w.prepare( fout );
Expand Down

0 comments on commit 656fe91

Please sign in to comment.