Qore FixedLengthUtil Module Reference  1.5
FixedLengthUtil::FixedLengthWriter Class Reference

Writer for fixed-length line output data. More...

Inheritance diagram for FixedLengthUtil::FixedLengthWriter:

Public Member Methods

 constructor (Qore::OutputStream output, hash< auto > specs, *hash< auto > opts)
 Instantiates the FixedLengthWriter object. More...
 
 write (hash< auto > line)
 Renders a single line for a single input record hash to the output stream. More...
 
 write (list< auto > lines)
 iterates the input records and writes rendered versions of all input records to the output stream More...
 
 write (Qore::AbstractIterator lines)
 iterates the input records and writes rendered versions of all input records to the output stream More...
 
- Public Member Methods inherited from FixedLengthUtil::FixedLengthAbstractWriter
bool checkTransition (*string from, *string to)
 A verification function to be called for each line. This method can be overridden to achieve a begin-to-end validation of the whole input file. More...
 
 constructor (hash< auto > specs, *hash< auto > opts)
 creates the FixedLengthAbstractWriter object More...
 
string formatLine (hash< auto > line)
 Formats a single line from a hash describing the record type and its contents. More...
 
int linesCount ()
 get processed lines count
 
- Public Member Methods inherited from FixedLengthUtil::FixedLengthBase
 constructor (hash< auto > m_specs, *hash< auto > m_opts)
 Creates the object from the record specifications.
 
*hash< string, AbstractDataField > getRecordType ()
 Returns the description of the record type, if any.
 

Private Attributes

StreamWriter output
 the output stream for the fixed-length data
 
- Private Attributes inherited from FixedLengthUtil::FixedLengthBase
const GlobalOptionMap
 Translates from global options to data provider options.
 
*hash< auto > m_opts
 Global input or output options.
 
hash< auto > m_specs
 The record specifications.
 

Detailed Description

Writer for fixed-length line output data.

Example:
#!/usr/bin/env qore
%new-style
%enable-all-warnings
%require-types
%strict-args
%requires FixedLengthUtil
list<auto> data = (
{"type": "type1", "record": {"col1": 11111, "col2": "bb"}},
{"type": "type2", "record": {"col3": "c", "col4": "ddd", "col5": "31122014"}},
{"type": "type1", "record": {"col1": 22222, "col2": "gg"}},
);
hash<auto> specs = {
"type1": {
"col1": {"length": 5, "type": "int"},
"col2": {"length": 2, "type": "string"},
},
"type2": {
"col3": {"length": 1, "type": "string"},
"col4": {"length": 3, "type": "string"},
"col5": {"length": 8, "type": "date", "format": "DDMMYYYY", "timezone": "Europe/Prague"},
},
};
hash<auto> global_options = {
"eol": "\n",
};
FileOutputStream output("output.txt");
FixedLengthWriter w(output, specs, global_options);
w.write(data);

Member Function Documentation

◆ constructor()

FixedLengthUtil::FixedLengthWriter::constructor ( Qore::OutputStream  output,
hash< auto >  specs,
*hash< auto >  opts 
)

Instantiates the FixedLengthWriter object.

Parameters
outputthe OutputStream for the data generated
specsFixed-length line specification; see Specification Hash for more information
optsGlobal options; see Global Options for valid values

◆ write() [1/3]

FixedLengthUtil::FixedLengthWriter::write ( hash< auto >  line)
virtual

Renders a single line for a single input record hash to the output stream.

Example:
const Spec = (
"header": (
"flow_type": ("length": 3, "type": "string"),
"record_type": ("length": 3, "type": "int", "padding_char": "0"),
"number_of_records": ("length": 8, "type": "int", "padding_char": "0"),
),
);
hash hh = (
"type": "header",
"record": (
"flow_type": "WTS",
"record_type": "950",
"number_of_records": 1,
),
);
FixedLengthWriter w(output, Spec);
w.write(hh);
Parameters
lineA hash representing input data to be writen to the output stream; the hash must have the following keys:
  • "type": a string giving the record type (must be defined in Specification Hash given in the constructor)
  • "record": a hash giving the input record to be rendered (with keys as defined in the Record Description Hash for the record identified by the type argument)
Exceptions
INVALID-RECORDrecord name (type key in the record hash) not recognized
FIELD-INPUT-ERRORthe input value is too large to render into the output field
RECORD-TRANSITION-ERRORa record transition error occurred; an invalid record sequence was given in the input data

Implements FixedLengthUtil::FixedLengthAbstractWriter.

◆ write() [2/3]

FixedLengthUtil::FixedLengthWriter::write ( list< auto >  lines)
virtual

iterates the input records and writes rendered versions of all input records to the output stream

Example:
FixedLengthWriter w(output, Spec);
w.write(i);
Parameters
linesA list of input records; each list value must be a hash with the following keys:
  • "type": a string giving the record type (must be defined in Specification Hash given in the constructor)
  • "record": a hash giving the input record to be rendered (with keys as defined in the Record Description Hash for the record identified by the type argument)
Exceptions
INVALID-RECORDrecord name (type key in the record hash) not recognized
FIELD-INPUT-ERRORthe input value is too large to render into the output field
RECORD-TRANSITION-ERRORa record transition error occurred; an invalid record sequence was given in the input data

Implements FixedLengthUtil::FixedLengthAbstractWriter.

◆ write() [3/3]

FixedLengthUtil::FixedLengthWriter::write ( Qore::AbstractIterator  lines)
virtual

iterates the input records and writes rendered versions of all input records to the output stream

Example:
FixedLengthWriter w(output, Spec);
w.write(i);
Parameters
linesAn iterator to stream input records; each iterator value must be a hash with the following keys:
  • "type": a string giving the record type (must be defined in Specification Hash given in the constructor)
  • "record": a hash giving the input record to be rendered (with keys as defined in the Record Description Hash for the record identified by the type argument)
Exceptions
INVALID-RECORDrecord name (type key in the record hash) not recognized
FIELD-INPUT-ERRORthe input value is too large to render into the output field
RECORD-TRANSITION-ERRORa record transition error occurred; an invalid record sequence was given in the input data

Implements FixedLengthUtil::FixedLengthAbstractWriter.