Beispiel #1
0
        /// <summary>
        /// Gets a high level representation of the first table in the file.
        /// </summary>
        /// <param name="ignoreErrors">If true, the reader will not throw an exception when it encounters unexpected data.</param>
        /// <returns></returns>
        public Table BuildTable(bool ignoreErrors = false)
        {
            var tableNameDefinitions = TpsFile.GetTableNameRecords();

            var tableDefinitions = TpsFile.GetTableDefinitions(ignoreErrors: ignoreErrors);

            var firstTableDefinition = tableDefinitions.First();

            var dataRecords = GatherDataRecords(firstTableDefinition.Key, firstTableDefinition.Value, ignoreErrors);
            var memoRecords = GatherMemoRecords(firstTableDefinition.Key, firstTableDefinition.Value, ignoreErrors);

            IEnumerable <(int recordNumber, IReadOnlyDictionary <string, TpsObject> nameValuePairs)> unifiedRecords = Enumerable.Concat(dataRecords, memoRecords)
                                                                                                                      .GroupBy(numberNVPairs => numberNVPairs.recordNumber)
                                                                                                                      .Select(groupedNumberNVPairs => (
                                                                                                                                  recordNumber: groupedNumberNVPairs.Key,
                                                                                                                                  nameValuePairs: (IReadOnlyDictionary <string, TpsObject>)groupedNumberNVPairs
                                                                                                                                  .SelectMany(pair => pair.nameValuePairs)
                                                                                                                                  .ToDictionary(kv => kv.Key, kv => kv.Value)));

            var rows = unifiedRecords.Select(r => new Row(r.recordNumber, r.nameValuePairs));

            string tableName = tableNameDefinitions
                               .First(n => n.TableNumber == firstTableDefinition.Key).Header.Name;

            var table = new Table(tableName, rows);

            return(table);
        }
Beispiel #2
0
        public void ShouldDecryptFile()
        {
            using (var fsEncrypted = new FileStream("Resources/encrypted-a.tps", FileMode.Open))
                using (var fsUnencrypted = new FileStream("Resources/not-encrypted.tps", FileMode.Open))
                {
                    var encryptedFile = new TpsFile(fsEncrypted, new Key("a"));
                    var decryptedFile = new TpsFile(fsUnencrypted);

                    var encryptedDefinitions = encryptedFile.GetTableDefinitions(ignoreErrors: false);
                    var decryptedDefinitions = decryptedFile.GetTableDefinitions(ignoreErrors: false);

                    Assert.AreEqual(decryptedDefinitions.Count, encryptedDefinitions.Count);

                    // Note that record IDs may differ.
                    var encryptedRecords = encryptedFile.GetDataRecords(table: 2, tableDefinition: encryptedDefinitions[2], ignoreErrors: false);
                    var decryptedRecords = decryptedFile.GetDataRecords(table: 1, tableDefinition: decryptedDefinitions[1], ignoreErrors: false);

                    Assert.AreEqual(decryptedRecords.Count(), encryptedRecords.Count());

                    var zip = decryptedRecords.Zip(encryptedRecords, (d, e) => (dec: d, enc: e));

                    foreach (var(dec, enc) in zip)
                    {
                        CollectionAssert.AreEqual(dec.Values, enc.Values);
                    }
                }
        }
Beispiel #3
0
        internal IEnumerable <T> DeserializeInternal <T>(bool ignoreErrors, CancellationToken ct) where T : class, new()
        {
            var firstTableDefinition = TpsFile.GetTableDefinitions(ignoreErrors).First();

            var dataRecords = TpsFile.GetDataRecords(firstTableDefinition.Key, firstTableDefinition.Value, ignoreErrors)
                              .ToDictionary(r => r.RecordNumber, r => r.Values);

            var memoRecords = GatherMemoRecords(firstTableDefinition.Key, firstTableDefinition.Value, ignoreErrors);

            var columns = BuildColumnLookup(firstTableDefinition.Value.Fields.Select(f => f.Name.ToUpperInvariant()));

            foreach (var dataRecord in dataRecords)
            {
                int recordNumber = dataRecord.Key;
                var values       = dataRecord.Value;

                T target = new T();

                var members = DeserializerContext.GetModelMembers <T>(target);

                foreach (var member in members)
                {
                    if (member.IsRecordNumber)
                    {
                        member.SetMember(target, recordNumber);
                    }
                    else
                    {
                        string requestedField = member.FieldAttribute.FieldName.ToUpperInvariant();

                        if (columns.TryGetValue(requestedField, out int index))
                        {
                            object interpretedValue = member.FieldAttribute.InterpretValue(member.MemberInfo, values[index]);
                            member.SetMember(target, interpretedValue);
                        }
                        else if (memoRecords.TryGetValue(recordNumber, out var fieldValueDictionary) &&
                                 fieldValueDictionary.ToDictionary(fv => fv.Key.ToUpperInvariant(), fv => fv.Value).TryGetValue(requestedField, out TpsObject value))
                        {
                            object interpretedValue = member.FieldAttribute.InterpretValue(member.MemberInfo, value);
                            member.SetMember(target, interpretedValue);
                        }
                        else if (member.FieldAttribute.IsRequired)
                        {
                            throw new TpsParserException($"The required field '{requestedField}' could not be found in record {recordNumber}.");
                        }
                    }

                    ct.ThrowIfCancellationRequested();
                }

                yield return(target);

                ct.ThrowIfCancellationRequested();
            }
        }
Beispiel #4
0
        public void ShouldReadFromFile()
        {
            using (var stream = new FileStream("Resources/table-with-time.tps", FileMode.Open))
            {
                var file = new TpsFile(stream);

                var tableDef = file.GetTableDefinitions(false).First().Value;
                var record   = file.GetDataRecords(1, tableDef, false).First();

                var valuePairs = record.GetFieldValuePairs();

                Assert.AreEqual(new TimeSpan(0, 6, 23, 15, 0), valuePairs["ClockIn"].Value);
                Assert.AreEqual(new TimeSpan(0, 12, 59, 59, 0), valuePairs["ClockOut"].Value);
            }
        }
Beispiel #5
0
        public static int Print(Options options)
        {
            FileStream file;

            try
            {
                file = File.Open(options.InputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File '{0}' was not found.", options.InputFile);
                return(1);
            }
            catch (IOException)
            {
                Console.WriteLine("Unable to open file '{0}'.", options.InputFile);
                return(1);
            }

            try
            {
                var tps = new TpsFile(file);

                var definitions = tps.GetTableDefinitions();
                var map         = tps.GetTableNames().ToDictionary(n => n.TableNumber, n => new
                {
                    n.Name,
                    Definition = definitions.First(d => d.TableNumber == n.TableNumber)
                });

                foreach (var table in map)
                {
                    Console.WriteLine("Table ID: {0} ({1})", table.Key, table.Value.Name);
                    Console.WriteLine();

                    foreach (var field in table.Value.Definition.Fields)
                    {
                        Console.WriteLine("{0} ({1})", field.FieldName, field.Type);
                    }
                }
            }
            finally
            {
                file.Dispose();
            }

            return(0);
        }
        public static int Print(Options options)
        {
            FileStream file;
            try
            {
                file = File.Open(options.InputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File '{0}' was not found.", options.InputFile);
                return 1;
            }
            catch (IOException)
            {
                Console.WriteLine("Unable to open file '{0}'.", options.InputFile);
                return 1;
            }

            try
            {
                var tps = new TpsFile(file);

                var definitions = tps.GetTableDefinitions();
                var map = tps.GetTableNames().ToDictionary(n => n.TableNumber, n => new
                {
                    n.Name,
                    Definition = definitions.First(d => d.TableNumber == n.TableNumber)
                });

                foreach (var table in map)
                {
                    Console.WriteLine("Table ID: {0} ({1})", table.Key, table.Value.Name);
                    Console.WriteLine();

                    foreach (var field in table.Value.Definition.Fields)
                    {
                        Console.WriteLine("{0} ({1})", field.FieldName, field.Type);
                    }
                }
            }
            finally
            {
                file.Dispose();
            }

            return 0;
        }
Beispiel #7
0
        /// <summary>
        /// Gets a high level representation of the first table in the file.
        /// </summary>
        /// <param name="ignoreErrors">If true, the reader will not throw an exception when it encounters unexpected data.</param>
        /// <returns></returns>
        public Table BuildTable(bool ignoreErrors = false)
        {
            var tableNameDefinitions = TpsFile.GetTableNameRecords();

            var tableDefinitions = TpsFile.GetTableDefinitions(ignoreErrors: ignoreErrors);

            var firstTableDefinition = tableDefinitions.First();

            var dataRecords = GatherDataRecords(firstTableDefinition.Key, firstTableDefinition.Value, ignoreErrors);
            var memoRecords = GatherMemoRecords(firstTableDefinition.Key, firstTableDefinition.Value, ignoreErrors);

            var unifiedRecords = new Dictionary <int, Dictionary <string, TpsObject> >();

            foreach (var dataKvp in dataRecords)
            {
                unifiedRecords.Add(dataKvp.Key, dataKvp.Value.ToDictionary(pair => pair.Key, pair => pair.Value));
            }

            foreach (var memoRecord in memoRecords)
            {
                int recordNumber = memoRecord.Key;

                var dataNameValues = dataRecords[recordNumber];

                foreach (var memoNameValue in memoRecord.Value)
                {
                    unifiedRecords[recordNumber].Add(memoNameValue.Key, memoNameValue.Value);
                }
            }

            var rows = unifiedRecords.Select(r => new Row(DeserializerContext, r.Key, r.Value));

            string tableName = tableNameDefinitions
                               .First(n => n.TableNumber == firstTableDefinition.Key).Header.Name;

            var table = new Table(tableName, rows);

            return(table);
        }
Beispiel #8
0
        public static int Export(Options options)
        {
            FileStream file;

            try
            {
                file = File.Open(options.InputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File '{0}' was not found.", options.InputFile);
                return(1);
            }
            catch (IOException)
            {
                Console.WriteLine("Unable to open file '{0}'.", options.InputFile);
                return(1);
            }

            try
            {
                var tps = new TpsFile(file);

                var definitions = tps.GetTableDefinitions();
                if (definitions.Count > 1)
                {
                    Console.WriteLine("Unable to export TPS files with multiple tables to CSV.");
                    return(1);
                }

                using (var writer = File.CreateText(options.OutputFile))
                    using (var csv = new CsvWriter(writer))
                    {
                        foreach (var field in definitions[0].Fields)
                        {
                            var name = field.FieldName.Substring(field.FieldName.IndexOf(":") + 1);

                            if (field.IsArray())
                            {
                                for (var i = 0; i < field.Elements; i++)
                                {
                                    csv.WriteField(name + "[" + i + "]");
                                }
                                continue;
                            }

                            csv.WriteField(name);
                        }
                        csv.NextRecord();

                        foreach (var record in tps.GetDataRecords(definitions[0]))
                        {
                            foreach (var value in record.Values)
                            {
                                var array = value as IEnumerable <object>;
                                if (array == null)
                                {
                                    csv.WriteField(value);
                                }
                                else
                                {
                                    foreach (var arrayValue in array)
                                    {
                                        csv.WriteField(arrayValue);
                                    }
                                }
                            }
                            csv.NextRecord();
                        }
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex);
                return(1);
            }
            finally
            {
                file.Dispose();
            }

            return(0);
        }
        public static int Export(Options options)
        {
            FileStream file;
            try
            {
                file = File.Open(options.InputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File '{0}' was not found.", options.InputFile);
                return 1;
            }
            catch (IOException)
            {
                Console.WriteLine("Unable to open file '{0}'.", options.InputFile);
                return 1;
            }

            try
            {
                var tps = new TpsFile(file);

                var definitions = tps.GetTableDefinitions();
                if (definitions.Count > 1)
                {
                    Console.WriteLine("Unable to export TPS files with multiple tables to CSV.");
                    return 1;
                }

                using (var writer = File.CreateText(options.OutputFile))
                using (var csv = new CsvWriter(writer))
                {
                    foreach (var field in definitions[0].Fields)
                    {
                        var name = field.FieldName.Substring(field.FieldName.IndexOf(":") + 1);

                        if (field.IsArray())
                        {
                            for (var i = 0; i < field.Elements; i++)
                            {
                                csv.WriteField(name + "[" + i + "]");
                            }
                            continue;
                        }

                        csv.WriteField(name);
                    }
                    csv.NextRecord();

                    foreach (var record in tps.GetDataRecords(definitions[0]))
                    {
                        foreach (var value in record.Values)
                        {
                            var array = value as IEnumerable<object>;
                            if (array == null)
                                csv.WriteField(value);
                            else
                            {
                                foreach (var arrayValue in array)
                                    csv.WriteField(arrayValue);
                            }
                        }
                        csv.NextRecord();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex);
                return 1;
            }
            finally
            {
                file.Dispose();
            }

            return 0;
        }