Used to parse fixed width text files into usable Euphoria sequences.
Copyright (c) 2009 by Jeremy Cowgar <jeremy@cowgar.com>
Source code is free to use for any purpose. If you make bug fixes or enhancements, please submit them back to the author. This is not required, but appriciated.
Create a sequence containing the fixed width specification. This is a sequence of individual field specifications
{ field width, conversion routine_id(), optional paramaters passed to conversion routine }
You can then pass this sequence of field definitions to a variety of functions, the two major ones being parse_file and parse_line.
include fixed.e sequence spec = { { 5, CVT_NUMBER, 0 }, -- 0 = default value { 15, CVT_TRIMMED }, { 15, CVT_TRIMMED } } enum F_ID, F_FIRST_NAME, F_LAST_NAME object data = fixed:parse_file(spec, "people.txt") if atom(data) then puts(1, "Could not parse people.txt\n") end if ? { length(data), data[1][F_ID] }
See the file t_fixed.e in the current directory for more example uses.
To test fixed.e to ensure it is functioning properly on your system, execute eutest from the directory you installed fixed.e. This should execute the unit test t_fixed.e which tests most/all aspects of fixed.e.
include fixed.e public enum ERROR_OPENCould not open a file for parsing
include fixed.e public enum ERROR_TO_SMALLLine was too small
include fixed.e public constant CVT_STRING = - 1Convert field to a string value (noop)
include fixed.e public constant CVT_TRIMMED = routine_id("to_trimmed_string")
include fixed.e public constant CVT_NUMBER = routine_id("to_number")Covert field to a number (integer or atom)
include fixed.e public constant CVT_DATETIME = routine_id("dt:parse")Convert field to a datetime value
include fixed.e public function parse_line(sequence layout, sequence line)Parse a fixed width string (single line)
An atom on failure, indicating the error code or a sequence of items according to the layout specification.
include fixed.e public function parse_file(sequence layout, sequence filename)Parse a fixed width file
An atom on failure indicating the error code or a sequence of sequences representing the fixed width data of every line according to the layout specification.