Exemplo n.º 1
0
        /// <summary>
        /// Asynchronously get next line and wait for a task (with a Tuple) to complete, ignoring tuples with nulls at end of each source.
        /// </summary>
        /// <param name="feeder">Feeder to read the line from (should be the 1st level LineFeeder, i.e. with LineFeederForSource as constituent feeders).</param>
        /// <returns>Tuple containing text line and a source number or null at end.</returns>
        internal static Tuple <ExternalLine, int> GetNextLineSynced(this ILineFeeder feeder)
        {
            var retVal = feeder.GetNextLineAsync().Result;

            // retVal here is null at the very end of the entire sequence produced by LineFeeder.
            // At end of each level of LineFeeder nesting (i.e. LineFeederForSource), it is a tuple containing null.
            // Note that this assumes a single level of nesting, where LineFeeder is fed by LineFeederForSource (and not by LineFeeders (recursively)).
            if (retVal == null)
            {
                return(null);
            }
            return(retVal.Item1 == null?GetNextLineSynced(feeder) : retVal);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs one time initialization of the intake source(s), e.g. open input file(s).
        /// This method is only called when the InitErrorOccurred property is accessed for the first time.
        /// </summary>
        /// <returns>true if initialization successful; false if not, e.g. file not found</returns>
        private bool InitIntake()
        {
            var skipRepeatedHeader  = _config.InputDataKind.CanHaveHeaderRow() && _config.HeadersInFirstInputRow && _config.InputHeadersRepeated;
            var x12SegmentDelimiter = _config.InputDataKind == KindOfTextData.X12 ? _config.DefaultX12SegmentDelimiter ?? "~" : null; //note that default delimiter (~) is unlikely to be used as it get overwritten by pos 106 of ISA segment

            // Note that in order for the exceptions to be caught in the try blocks below, CreateLineFeeder (or IntakeInitializer as applicable) processing
            // must be eagerly evaluated (no deferred execution).
            if (_intakeFromReader)
            {
                try
                {
                    //In case both IntakeReaders and InputFileNames are present, then IntakeReaders wins
                    _intakeReader = _config.IntakeReaders == null?LineFeederCreator.CreateLineFeeder(_config.InputFileNames.ToListOfStrings('|'), _config.InputDataKind, _config.AsyncIntake, skipRepeatedHeader, x12SegmentDelimiter, _config.XmlJsonIntakeSettings)
                                        : LineFeederCreator.CreateLineFeeder(_config.IntakeReaders(), _config.InputDataKind, _config.AsyncIntake, skipRepeatedHeader, x12SegmentDelimiter, _config.XmlJsonIntakeSettings);
                }
                catch (Exception ex)
                {
                    //something went wrong with input file(s) or text reader(s), there is not much we can do other than to report it
                    var errMsg = _config.IntakeReaders == null ? $"Attempt to access input file(s) '{_config.InputFileNames}' failed."
                                                          : "Attempt to open text reader(s) failed.";
                    _config.Logger.LogFatal(errMsg, ex);
                    return(false);
                }
                return(true);
            }
            else //intake from IntakeSupplier (or IntakeSupplierAsync) function
            {
                string errMsg;
                try
                {
                    errMsg = _config.IntakeInitializer(_globalCache);
                }
                catch (Exception ex)
                {
                    _config.Logger.LogFatal("Intake initializer threw exception.", ex);
                    return(false);
                }
                if (errMsg != null)
                {
                    _config.Logger.LogFatal("Intake initializer failed:\r\n" + errMsg);
                    return(false);
                }
                return(true);
            }
        }