Data Files and Scanners

DataFile | Scanner

 

DataFile
class : File
  • DESCRIPTION
    • In console-based Slag you can use class File to read and write data, but in Plasmacore you must use class DataFile since not all gaming platforms support the standard filesystem that File is geared towards.
    • When loading a DataFile, if you've saved a DataFile of the same name then it loads the saved data. If there's no saved version then the system attempts to load a file of the same name that you placed in your "data" resource directory. In either case paths on DataFile objects are ignored.
    • Examples:
        local DataFile save("slot1.sav") 
        if (save.exists)
          forEach (line in LineReader(save))
            local Scanner scanner(line)
            ...
          endForEach
        endIf
        ...
        local var writer = DataFile("slot1.sav").create_writer
        writer.println( hero.stats )
        ...
        writer.close
      
  • PROPERTIES
    • filepath : String
      • The filepath of this file.
    • exists : Logical
    • size : Int32
    • is_gamestate : Logical
    • data : String
  • METHODS
    • init(String init:filepath)
    • exists().Logical
      • Returns whether or not this file already exists.
    • size().Int32
      • Returns the byte size of this file. Throws a FileError if the file does not exist.
    • create_reader().Reader<<Char>>
      • Returns a Reader<<Char>> that returns characters from this data file.
    • create_writer().TextWriter
      • Returns a TextWriter that can write and print data to this file.
    • create_appender().TextWriter
      • Like write() but appends data to the file, if it exists, rather than overwriting it.
    • is_directory().Logical
      • Returns false - directory structures are not supported by DataFile.
    • absolute_path().String
      • Returns the absolute path denoted by this File object.
    • to_String().String
      • Returns the original filepath of this File object.
    • absolute_filepath().String
      • Same as 'filepath' for DataFile objects.
    • directory_listing().String[]
      • Throws an UnsupportedMethodError.
    • init_object()
    • hash_code().Int32
    • create_duplicate().Object
    • op==(Object other).Logical {multimethod}
    • op<>(Object other).Logical {multimethod}
    • type_name().String : native
    • runtime_type().RuntimeType : native
    • runtime_properties().RuntimeProperties
    • runtime_methods().RuntimeMethods
    • to_List().Char[]
    • to_bytes().Byte[]
    • init(String _path,String _filename)
    • path().String
    • filename().String
    • directory_listing(Int32 flags).String[]
    • extension().String

  

Scanner
class : Object
  • DESCRIPTION
    • Wraps an existing character reader (or readable object) and parses various types of input from it.
    • This is similar to Java's Scanner except it will not skip input looking for tokens but rather throw an error if asked to scan an input that is not immediately available. It is line-based and will not handle multi-line patterns.
    • It is often useful to have a LineReader read in lines from a file and then use a Scanner to parse each line.
    • Plasmacore Example:
        # Say file contains: 
        #   width: 800; height:400;
      
        forEach (line in LineReader(DataFile("config.txt")))
          local Scanner scanner(line)
          while (scanner.next_is_id)
            local String name = scanner.scan_id
            scanner.must_consume(":")
      
            local Int32 value = scanner.scan_Int32
            println( "$ = $" (name,value) )
              # prints:
              #  width = 800
              #  height = 400
            scanner.must_consume(";")
          endWhile
        endForEach
      
  • PROPERTIES
    • src : RewindableReader<<Char>>
      • Internal use - the input source for this scanner.
    • number_pattern : Pattern
      • Contains the pattern to use for scanning an Int32.
    • ws_pattern : Pattern
      • Contains the pattern to use for scanning whitespace.
    • real_pattern : Pattern
      • Contains the pattern to use for scanning a Real64.
    • string_pattern : Pattern
      • Contains the pattern to use for scanning a String.
    • id_pattern : Pattern
      • Contains the pattern to use for scanning an identifier.
    • discard_eols : Logical
      • Discard EOL characters while discarding whitespace? EOL characters may also be detected and discarded with consume_eol() and consume_eols().
  • METHODS
    • init(Readable<<Char>> input)
      • Initializes this Scanner to read from the given readable character data.
    • init([Reader<<Char>> input])
      • Initializes this Scanner to read from the given reader.
    • init(RewindableReader<<Char>> init:src)
      • Initializes this Scanner to read from the given reader.
    • consume_ws().Logical
      • Internal use - discards any whitespace that comes next in the input; called before every scan() and peek() except peek_line() and scan_line(). Returns "true" if any whitespace was consumed.
      • If the "discard_eols" property is "true", then eols are discarded at the same time.
    • consume_eol().Logical
      • Attempts to read and discard a single EOL (13 10 or just 10) from the input. Returns "true" on success. Must consume_ws on our own to prevent a possible infinite recursion.
    • consume_eols().Logical
      • Attempts to read and discard one or more EOL characters. Returns "true" on success.
    • has_another().Logical
      • Returns "true" if scanner reader has another value that can be previewed with peek() or read with read().
    • consume(Char ch).Logical
      • Attempts to read and discard the character 'ch'. Returns "true" on success.
    • consume(String st).Logical
      • Attempts to read and discard the string 'st'. Returns "true" on success.
    • must_consume(Char ch[,String mesg])
      • Throws a NoNextValueError if the character 'ch' cannot be consumed.
    • must_consume(String st[,String mesg])
      • Throws a NoNextValueError if the string 'st' cannot be consumed.
    • next_is(String st).Logical
      • Returns true if the next input matches the given string. consume(String) may be used to read past the string.
    • next_is(Pattern p).Logical
      • Returns true if the next input matches the given pattern.
    • peek(Pattern p).String
      • Returns the value that will be returned from the next call to read(Pattern). If there is no next value then a NoNextValueError is thrown.
    • scan(Pattern p).String
      • Returns the substring of pending input that matches the given pattern or else throws a NoNextValueError.
    • peek_line().String
      • Returns the next string that will be returned from scan_line(). If there is no next value then a NoNextValueError is thrown. Does not discard leading whitespace like most scan methods.
    • scan_line().String
      • Returns the next line of input up to a LF or CRLF. CRLF characters are discarded. Does not discard leading whitespace like most scan methods.
    • peek_Char().Char
      • Returns the character that will be returned from the next call to scan_Char(). If there is no next value then a NoNextValueError is thrown.
    • scan_Char().Char
      • Returns the next character of input.
    • next_is_Int32().Logical
      • Returns "true" if the pending input can be interpreted as an Int32 value.
      • Note: if the next input were "3.14" then next_is_Int32() would return "true" and scan_Int32() would return "3"
      • Note: the internal system is currently unable to determine which integers will fit in an Int32 and which require an Int64; it is up to the programmer to anticipate this and use the correct call.
    • peek_Int32().Int32
      • Returns the Int32 that will be returned from the next call to scan_Int32(). If there is no next value then a NoNextValueError is thrown.
      • Note: if the next input were "3.14" then peek_Int32() would return "3"
      • Note: the internal system is currently unable to determine which integers will fit in an Int32 and which require an Int64; it is up to the programmer to anticipate this and use the correct call.
    • scan_Int32().Int32
      • Attempts to parse and return an Int32 value from the pending input. Throws a NoNextValueError on failure.
      • Returns the Int32 that will be returned from the next call to scan_Int32(). If there is no next value then a NoNextValueError is thrown.
      • Note: if the next input were "3.14" then scan_Int32() would return "3"
      • Note: the internal system is currently unable to determine which integers will fit in an Int32 and which require an Int64; it is up to the programmer to anticipate this and use the correct call.
    • next_is_Int64().Logical
      • Returns "true" if the pending input can be interpreted as an Int64 value.
      • Note: if the next input were "3.14" then next_is_Int64() would return "true" and scan_Int64() would return "3"
      • Note: the internal system is currently unable to determine which integers will fit in an Int32 and which require an Int64; it is up to the programmer to anticipate this and use the correct call.
    • peek_Int64().Int64
      • Returns the Int64 that will be returned from the next call to scan_Int64(). If there is no next value then a NoNextValueError is thrown.
      • Note: if the next input were "3.14" then peek_Int64() would return "3"
      • Note: the internal system is currently unable to determine which integers will fit in an Int32 and which require an Int64; it is up to the programmer to anticipate this and use the correct call.
    • scan_Int64().Int64
      • Attempts to parse and return an Int64 value from the pending input. Throws a NoNextValueError on failure.
      • Returns the Int64 that will be returned from the next call to scan_Int64(). If there is no next value then a NoNextValueError is thrown.
      • Note: if the next input were "3.14" then scan_Int64() would return "3"
      • Note: the internal system is currently unable to determine which integers will fit in an Int32 and which require an Int64; it is up to the programmer to anticipate this and use the correct call.
    • next_is_Real64().Logical
      • Returns "true" if the pending input can be interpreted as a Real64 value.
    • peek_Real64().Real64
      • Returns the Real64 that will be returned from the next call to scan_Real64(). If there is no next value then a NoNextValueError is thrown.
    • scan_Real64().Real64
      • Attempts to parse and return a Real64 value from the pending input. Throws a NoNextValueError on failure.
    • next_is_String().Logical
      • Returns "true" if the pending input can be interpreted as a quoted string value of the form:
      •   //'"' any* '"'// ) : Pattern
        
        
    • scan_String().String
      • Attempts to parse and return a String value from the pending input. Throws a NoNextValueError on failure.
    • peek_String().String
      • Returns the String that will be returned from the next call to scan_String(). If there is no next value then a NoNextValueError is thrown.
    • next_is_id().Logical
      • Returns "true" if the pending input can be interpreted as an id value.
    • scan_id().String
      • Attempts to parse and return an id value from the pending input. Throws a NoNextValueError on failure.
    • peek_id().String
      • Returns the id that will be returned from the next call to scan_id(). If there is no next value then a NoNextValueError is thrown.
    • init_object()
    • hash_code().Int32
    • create_duplicate().Object
    • to_String().String
    • op==(Object other).Logical {multimethod}
    • op<>(Object other).Logical {multimethod}
    • type_name().String : native
    • runtime_type().RuntimeType : native
    • runtime_properties().RuntimeProperties
    • runtime_methods().RuntimeMethods