Example #1
0
        private IEnumerable <IInputRecord> GetRecords(int limit = 0)
        {
            var inputParse = JsonInput.Factory().TryParse(Input, limit);

            if (inputParse.Records != null)
            {
                InputFormat      = InputFormat.JSON;
                RecordsArrayPath = inputParse.RecordsArrayPath;
                return(inputParse.Records);
            }

            inputParse = XmlInput.Factory().TryParse(Input, limit);
            if (inputParse.Records != null)
            {
                InputFormat      = InputFormat.XML;
                RecordsArrayPath = inputParse.RecordsArrayPath;
                return(inputParse.Records);
            }

            (IEnumerable <IInputRecord> Records, string RecordsArrayPath)csvRecords = (null, null),
            tsvRecords = (null, null);
            int csvMeanFieldCount = 0,
                tsvMeanFieldCount = 0;

            Parallel.Invoke(
                () =>
            {
                csvRecords = CsvInput.Factory().TryParse(Input, limit);
                if (csvRecords.Records != null)
                {
                    csvMeanFieldCount = GetMeanTokenCount(csvRecords.Records);
                }
            },

                () =>
            {
                tsvRecords = TsvInput.Factory().TryParse(Input, limit);
                if (tsvRecords.Records != null)
                {
                    tsvMeanFieldCount = GetMeanTokenCount(tsvRecords.Records);
                }
            }
                );

            if (csvMeanFieldCount > 0 && csvMeanFieldCount >= tsvMeanFieldCount)
            {
                InputFormat      = InputFormat.CSV;
                RecordsArrayPath = csvRecords.RecordsArrayPath;
                return(csvRecords.Records);
            }

            if (tsvMeanFieldCount > 0 && tsvMeanFieldCount > csvMeanFieldCount)
            {
                InputFormat      = InputFormat.TSV;
                RecordsArrayPath = tsvRecords.RecordsArrayPath;
                return(tsvRecords.Records);
            }

            throw new UnsupportedFormatException("Unable to detect input format.");
        }
Example #2
0
        public int Process(Stream input, Stream output, Encoding encoding, int limit = 0)
        {
            Debug.WriteLine($"Processing input...");
            var processTimer = new Stopwatch();

            processTimer.Start();

            Input inputProcessor;

            switch (InputFormat)
            {
            case InputFormat.JSON:
                inputProcessor = JsonInput.Factory();
                break;

            case InputFormat.XML:
                inputProcessor = XmlInput.Factory();
                break;

            case InputFormat.CSV:
                inputProcessor = CsvInput.Factory();
                break;

            case InputFormat.TSV:
                inputProcessor = TsvInput.Factory();
                break;

            default:
                throw new NotImplementedException($"Formating from {InputFormat} not implemented yet");
            }

            var processed = Process(
                inputProcessor.TryParse(input, encoding, RecordsArrayPath, limit),
                output,
                encoding);

            processTimer.Stop();
            Debug.WriteLine($"done in {(int)processTimer.Elapsed.TotalMilliseconds}ms");

            return(processed);
        }