Esempio n. 1
0
        public static Func <ISourceRow> FixedWidthReader(this Func <int> readNext, ISourceFileContext context, Interfaces.ILog logger)
        {
            if (context?.FileConfiguration == null)
            {
                var msg = Localization.GetLocalizationString("Could not get Source Configuration...");
                logger?.Fatal(msg);
                throw new ArgumentException(msg);
            }
            var locker   = new object();
            var rowCount = 0;

            logger?.Info(string.Format(Localization.GetLocalizationString("Loading data from {0}..."), context.FileConfiguration.Name));
            var lineReaders = context.FileConfiguration.Rows.Select(x => x.GetLineFunc(context.FileConfiguration)).ToList();

            return(() =>
            {
                lock (locker)
                {
                    var result = new List <ISourceField>();
                    foreach (var reader in lineReaders)
                    {
                        result.AddRange(reader(readNext));
                    }
                    return result.Count > 0 ? new SourceRow {
                        Context = context, Fields = result, LineNumber = ++rowCount
                    } : null;
                }
            });
        }
Esempio n. 2
0
        public static Func <int> BufferedRead(this ISourceFileContext r, Interfaces.ILog logger)
        {
            if (r?.Stream == null)
            {
                throw new ImporterArgumentOutOfRangeException(Localization.GetLocalizationString("Source stream cannot be null"));
            }
            var buff          = new char[60];
            var position      = 0;
            var length        = 0;
            var rd            = r.Stream;
            var readChars     = (long)0;
            var resultMessage = new Action(() =>
            {
                if (readChars > 0)
                {
                    logger?.Debug($"Loaded {readChars} characters");
                }
            });

            return(() =>
            {
                if (position >= length)
                {
                    if (rd == null || rd.EndOfStream)
                    {
                        resultMessage();
                        readChars = 0;
                        return -1;
                    }
                    length = rd.ReadBlock(buff, 0, 5);
                    readChars += length;
                    position = 0;
                    if (length == 0)
                    {
                        return -1;
                    }
                }

                return buff[position++];
            });
        }
Esempio n. 3
0
        public static Func <ISourceRow> CsvReader(this Func <int> readNext, ISourceFileContext context, Interfaces.ILog logger)
        {
            var fileConfig = context.FileConfiguration;

            if (fileConfig == null)
            {
                var msg = Localization.GetLocalizationString("Could not get Source Configuration...");
                logger?.Fatal(msg);
                throw new ArgumentException(msg);
            }

            var nullValue = string.IsNullOrWhiteSpace(fileConfig.NullValue) ? "" : fileConfig.NullValue;
            var delimiter = string.IsNullOrWhiteSpace(fileConfig.Delimiter) ? ',' : fileConfig.Delimiter[0];
            var qualifier = string.IsNullOrWhiteSpace(fileConfig.Qualifier) ? '\"' : fileConfig.Qualifier[0];

            var locker   = new object();
            var rowCount = 0;

            logger?.Info(string.Format(Localization.GetLocalizationString("Loading data from {0}..."), fileConfig.Name));
            return(() =>
            {
                lock (locker)
                {
                    var columns = new List <ISourceField>();
                    var hadQualifier = false;
                    Action <StringBuilder> enqueue = clmn =>
                    {
                        var value = clmn.ToString();
                        columns.Add((value.Length > 0 && (value != nullValue || hadQualifier)) ? new SourceField(value) : new SourceField(null));
                    };
                    int c;
                    while ((c = readNext()) >= 0 && (c == '\n' || c == '\r'))
                    {
                        ;
                    }
                    if (c < 0)
                    {
                        logger?.Info(Localization.GetLocalizationString("Loaded {0} line(s) from {1}.", rowCount, fileConfig.Name));
                        return null;
                    }
                    var isQualified = false;
                    var column = new StringBuilder();
                    while (c >= 0)
                    {
                        if (c == qualifier && (column.Length == 0 || hadQualifier))
                        {
                            hadQualifier = true;
                            isQualified = !isQualified;
                            if (isQualified && column.Length > 0)
                            {
                                column.Append((char)c);
                            }
                        }
                        else
                        if (c == delimiter && !isQualified)
                        {
                            enqueue(column);
                            column = new StringBuilder();
                        }
                        else
                        if ((c == '\n' || c == '\r') && !isQualified)
                        {
                            enqueue(column);
                            column = null;
                            break;
                        }
                        else
                        {
                            column.Append((char)c);
                        }
                        c = readNext();
                    }
                    if (c == -1 && column != null)
                    {
                        enqueue(column);
                    }

                    rowCount++;
                    return new SourceRow {
                        Context = context, Fields = columns, LineNumber = rowCount
                    };
                }
            });
        }