jan_flanders

Untitled

Jun 7th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import haxe.io.Bytes;
  2. import haxe.io.BytesInput;
  3. import haxe.io.BytesOutput;
  4. import haxe.Int32;
  5.  
  6. typedef Data =
  7. {
  8.     var version:Int;
  9.     var count:Int;
  10.     var entries:Array<Dynamic>;//if version 1: 32bit integer, if version 2: 64 bit integer
  11. }
  12. class Main
  13. {
  14.     public static function main()
  15.     {
  16.         var output = new BytesOutput();
  17.         output.writeByte(2);//version
  18.         output.writeByte(2);//count
  19.         output.writeDouble(10);//entry 1
  20.         output.writeDouble(20);//entry 2
  21.        
  22.         var input = new BytesInput(output.getBytes());
  23.        
  24.         var data:Data =
  25.         {
  26.             version : input.readByte(),
  27.             count : input.readByte(),
  28.             entries : new Array<Dynamic>()
  29.         }
  30.        
  31.         for(i in 0...data.count)
  32.         {
  33.             data.entries.push( switch(data.version)
  34.             {
  35.                 case 1: input.readInt32();
  36.                 case 2: input.readDouble();//characters 4-8 : Float should be haxe.Int32
  37.             });
  38.         }
  39.    
  40.  
  41.        
  42.         /* untyped works
  43.         for(i in 0...data.count)
  44.         {
  45.             data.entries.push( switch(data.version)
  46.             {
  47.                 case 1: untyped input.readInt32();
  48.                 case 2: untyped input.readDouble();
  49.             });
  50.         }
  51.         */
  52.        
  53.        
  54.         /* cast works
  55.         for(i in 0...data.count)
  56.         {
  57.             data.entries.push( switch(data.version)
  58.             {
  59.                 case 1: cast input.readInt32();
  60.                 case 2: cast input.readDouble();
  61.             });
  62.         }
  63.         */
  64.        
  65.        
  66.         /* switch outside push works
  67.         for(i in 0...data.count)
  68.         {
  69.             switch(data.version)
  70.             {
  71.                 case 1: data.entries.push( input.readInt32());
  72.                 case 2: data.entries.push( input.readDouble());
  73.             }
  74.         }
  75.         */
  76.        
  77.        
  78.        
  79.         /* switch outside loop works
  80.         switch(data.version)
  81.         {
  82.             case 1:
  83.                 for(i in 0...data.count)
  84.                 {
  85.                     data.entries.push(input.readInt32());
  86.                 }
  87.                
  88.             case 2:
  89.                 for(i in 0...data.count)
  90.                 {
  91.                     data.entries.push(input.readDouble());
  92.                 }
  93.         }
  94.         */
  95.        
  96.        
  97.         for(e in data.entries)
  98.         {
  99.             trace(e);
  100.         }
  101.        
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment