1 Fixed Width File Parser

1.1 Introduction

Used to parse fixed width text files into usable Euphoria sequences.

1.1.1 License

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.

1.1.2 Basic Use

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.

1.1.3 Example

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] }

1.1.4 See Also

See the file t_fixed.e in the current directory for more example uses.

1.1.5 Testing

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.

1.2 Reference

1.2.1 Error Constants

1.2.1.1 ERROR_OPEN

include fixed.e
public enum ERROR_OPEN
Could not open a file for parsing

1.2.1.2 ERROR_TO_SMALL

include fixed.e
public enum ERROR_TO_SMALL
Line was too small

1.2.2 Conversion Types

1.2.2.1 CVT_STRING

include fixed.e
public constant CVT_STRING = - 1
Convert field to a string value (noop)

1.2.2.2 CVT_TRIMMED

include fixed.e
public constant CVT_TRIMMED = routine_id("to_trimmed_string")

1.2.2.3 CVT_NUMBER

include fixed.e
public constant CVT_NUMBER = routine_id("to_number")
Covert field to a number (integer or atom)

Required Parameters:

1.2.2.4 CVT_DATETIME

include fixed.e
public constant CVT_DATETIME = routine_id("dt:parse")
Convert field to a datetime value

Required Parameters:

1.2.3 Parsing Functions

1.2.3.1 parse_line

include fixed.e
public function parse_line(sequence layout, sequence line)
Parse a fixed width string (single line)

Parameters:

  1. layout - fixed width layout
  2. line - line to parse

Returns:

An atom on failure, indicating the error code or a sequence of items according to the layout specification.

1.2.3.2 parse_file

include fixed.e
public function parse_file(sequence layout, sequence filename)
Parse a fixed width file

Parameters:

  1. layout - fixed width layout
  2. filename - filename to parse

Returns:

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.