/** * General Trainsmart package. */ package com.trainsmart { /** * Management of the pointer to the patch. */ public class Pointer { // Some useful strings: private static const lineFeed:String = '\n'; private static const carriageReturn:String = '\r'; private static const equal:String = '='; private static const dot:String = '.'; private static const space:String = ' '; private static const bytes:String = 'B'; private static const kilobytes:String = 'KB'; private static const megabytes:String = 'MB'; private static const kilo:Number = 1024; private static const URLField:String = 'patch'; private static const sizeField:String = 'size'; private static const majorField:String = 'major'; private static const minorField:String = 'minor'; private static const revisionField:String = 'revision'; private static const requiredField:String = 'required'; /** * URL for the patch. */ private static var URL:String = null; /** * Size of the patch, in bytes. */ private static var size:int = -1; /** * New version (major number). */ private static var major:int = -1; /** * New version (minor number). */ private static var minor:int = -1; /** * New version (revision number). */ private static var revision:int = -1; /** * Whether this patch is compulsory or not (since version 0.9.5). */ private static var required:Boolean = false; /** * Main constructor. */ function Pointer (pointerContent:String) { var exception:Error = new Error (); if (pointerContent.length < 1) { exception.name = 'The pointer to the patch seems empty'; throw exception; } var lines:Array = pointerContent.substring (0, pointerContent.length - 1).split (lineFeed); var pair:Array = null; var i:String = null; var fieldNames:Array = [URLField, sizeField, majorField, minorField, revisionField, requiredField]; var foundValues:Array = [false, false, false, false, false, false]; if (lines.length != fieldNames.length) { exception.name = 'Wrong number of lines (' + lines.length + ') in the pointer to the patch'; throw exception; } for each (var j:String in lines) { i = j; while (lineFeed == i.charAt (i.length - 1) || carriageReturn == i.charAt (i.length - 1)) i = i.substring (0, i.length - 1); pair = i.split (equal); if (pair.length != 2) { exception.name = 'Wrong line ("' + i + '") in the pointer to the patch'; throw exception; } if (-1 == fieldNames.indexOf (pair [0].toString ().toLowerCase ())) { exception.name = 'Wrong field name ("' + pair [0].toString () + '") in the pointer to the patch'; throw exception; } if (foundValues [fieldNames.indexOf (pair [0].toString ().toLowerCase ())]) { exception.name = 'The field "' + pair [0].toString () + '" is repeated in the pointer to the patch'; throw exception; } foundValues [fieldNames.indexOf (pair [0].toString ().toLowerCase ())] = true; switch (pair [0].toString ().toLowerCase ()) { case URLField: URL = pair [1]; break; case sizeField: size = pair [1]; break; case majorField: major = pair [1]; break; case minorField: minor = pair [1]; break; case revisionField: revision = pair [1]; break; case requiredField: switch (pair [1]) { case '0': required = false; break; case '1': required = true; break; default: exception.name = 'Wrong value ("' + pair [1].toString () + '") in field "required"'; throw exception; break; } break; } } } /** * Get the URL for the patch. */ public function getURL (): String { return URL; } /** * Get the size of the patch, nicely formatted (e.g. "2.34 MB"). */ public function getSize (): String { var result:String = null; if (size < kilo) result = size + space + bytes; else { if (size < kilo * kilo) { result = (size / kilo).toString (); if (-1 != result.indexOf (dot)) result = result.substring (0, result.indexOf (dot) + 2); result += space + kilobytes; } else { result = (size / (kilo * kilo)).toString (); if (-1 != result.indexOf (dot)) result = result.substring (0, result.indexOf (dot) + 2); result += space + megabytes; } } return result; } /** * Get the major version. */ public function getMajor (): int { return major; } /** * Get the minor version. */ public function getMinor (): int { return minor; } /** * Get the revision version. */ public function getRevision (): int { return revision; } /** * Get the "required" property. */ public function getRequired (): Boolean { return required; } /** * Get a software version, nicely formatted (e.g. "2.3.14"). */ public static function formatVersion (major:int, minor:int, revision:int): String { return major + dot + minor + dot + revision; } } } // EOF