Ejemplo n.º 1
0
        /// <summary>
        /// Uses the Microsoft Text Field Parser to parse the FixedWidth file, to give a baseline
        /// against an established class library for parsing.
        /// </summary>
        private static void _TextFieldParserFixedWidth()
        {
            string[] fields;
            string   s;

            using (VB.TextFieldParser tfp = new VB.TextFieldParser(PerformanceTests.FW_DATA_FILE))
            {
                tfp.TextFieldType = VB.FieldType.FixedWidth;
                tfp.SetFieldWidths(new int[PerformanceTests.NUMBER_OF_COLUMNS_IN_DATA] {
                    5, 5, 1, 28, 42, 15, 13, 9, 9, 1, 13, 14, 13, 6
                });
                tfp.CommentTokens             = new string[] { "#" };
                tfp.HasFieldsEnclosedInQuotes = true;

                while (!tfp.EndOfData)
                {
                    fields = tfp.ReadFields();

                    for (int i = 0; i < fields.Length; ++i)
                    {
                        s = fields[i] as string;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses a big text blob into rows and columns, using the settings
        /// </summary>
        /// <param name="text">Big blob of text</param>
        /// <returns>Parsed data</returns>
        public List <string[]> Parse(string text)
        {
            // The actual _parsing_ .NET can handle. Well, VisualBasic anyway...
            using (var reader = new StringReader(text))
                using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader))
                {
                    var errors = new StringBuilder();
                    if (this.CommentCharacter != default(char))
                    {
                        parser.CommentTokens = new[] { this.CommentCharacter.ToString() }
                    }
                    ;
                    parser.SetDelimiters(this.Separator.ToString());
                    parser.HasFieldsEnclosedInQuotes = this.TextQualifier != default(char);

                    if (this.FieldWidths != null)
                    {
                        parser.TextFieldType = FieldType.FixedWidth;
                        try
                        {
                            parser.SetFieldWidths(this.FieldWidths.ToArray());
                        }
                        catch (Exception e)
                        {
                            errors.AppendLine(e.Message);
                        }
                    }

                    var ret = new List <string[]>();
                    while (!parser.EndOfData)
                    {
                        try
                        {
                            ret.Add(parser.ReadFields());
                        }
                        catch (MalformedLineException e)
                        {
                            errors.AppendFormat("Error on line {0}: {1}\n", e.LineNumber, e.Message);
                        }
                    }
                    if (errors.Length > 0)
                    {
                        MessageBox.Show(errors.ToString(), "Errors");
                    }
                    return(ret);
                }
        }
Ejemplo n.º 3
0
        public static TextFieldParser CreateTextFieldParser(string path, string extension)
        {
            TextFieldParser tfp;
            try
            {
                tfp = new TextFieldParser(path);
            }
            catch (FileNotFoundException e)
            {
                Console.Out.WriteLine(Properties.Settings.Default.fileOpenError);
                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                switch (extension)
                {
                    case ".txt":
                        int[] format = { 3, 2, 10, 20, 20, 6, 6, 9 };
                        tfp.TextFieldType = FieldType.FixedWidth;
                        tfp.SetFieldWidths(format);
                        break;
                    case ".csv":
                        tfp.TextFieldType = FieldType.Delimited;
                        tfp.SetDelimiters(",");
                        break;
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(Properties.Settings.Default.fileFormatError);
                Console.Out.WriteLine(e);
            }

            return tfp;
        }
Ejemplo n.º 4
0
 public void SetFieldWidhtsTest()
 {
     using (StringReader reader = new StringReader ("abcd" + Constants.vbNewLine + "efgh" + Constants.vbNewLine))
     using (TextFieldParser t = new TextFieldParser (reader)) {
         t.TextFieldType = FieldType.Delimited;
         t.SetFieldWidths (new int [] { 1, 3, 2 });
         Assert.AreEqual ("1;3;2", Helper.Join (t.FieldWidths, ";"), "#01");
         Assert.AreEqual (FieldType.Delimited, t.TextFieldType, "#02");
     }
 }
Ejemplo n.º 5
0
        public void FixedTest1()
        {
            using (StringReader reader = new StringReader ("abcdef" + Constants.vbNewLine + "1234" + Constants.vbNewLine + "ghijklmno" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                t.SetFieldWidths (new int [] {1, 3, 2});
                t.TextFieldType = FieldType.FixedWidth;
                Assert.AreEqual ("a;bcd;ef", Strings.Join (t.ReadFields (), ";"), "#01");
                try {
                    Assert.AreEqual ("1;234", Strings.Join (t.ReadFields (), ";"), "#02");
                    Assert.Fail ("#E3 - Expected 'MalformedLineException'");
                } catch (MalformedLineException ex) {
                    Assert.AreEqual ("Line 2 cannot be parsed using the current FieldWidths.", ex.Message, "#E1");
                    Assert.AreEqual (2, ex.LineNumber, "#E2");
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#E4 - Expected 'MalformedLineException'");
                }
                Assert.AreEqual ("g;hij;kl", Strings.Join (t.ReadFields (), ";"), "#03");
            }

            using (StringReader reader = new StringReader ("abcdef" + Constants.vbNewLine + "1234" + Constants.vbNewLine + "ghijklmno" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                t.SetFieldWidths (new int [] {});
                t.TextFieldType = FieldType.FixedWidth;
                try {
                    Assert.AreEqual ("a;bcd;ef", Strings.Join (t.ReadFields (), ";"), "#11");
                    Assert.Fail ("#E12 - Expected 'InvalidOperationException'");
                } catch (InvalidOperationException ex) {
                    Assert.AreEqual ("Unable to read fixed width fields because FieldWidths is Nothing or empty.", ex.Message, "#E11");
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#E13 - Expected 'InvalidOperationException'");
                }
            }

            using (StringReader reader = new StringReader (" bcdef" + Constants.vbNewLine + "1 234" + Constants.vbNewLine + "gh klmno" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                t.SetFieldWidths (new int [] { 1, 3, 2 });
                t.TextFieldType = FieldType.FixedWidth;
                Assert.AreEqual (";bcd;ef", Strings.Join (t.ReadFields (), ";"), "#21");
                try {
                    Assert.AreEqual ("1;234", Strings.Join (t.ReadFields (), ";"), "#22");
                    Assert.Fail ("#E23 - Expected 'MalformedLineException'");
                } catch (MalformedLineException ex) {
                    Assert.AreEqual ("Line 2 cannot be parsed using the current FieldWidths.", ex.Message, "#E21");
                    Assert.AreEqual (2, ex.LineNumber, "#E22");
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#E24 - Expected 'MalformedLineException'");
                }
                Assert.AreEqual ("g;h k;lm", Strings.Join (t.ReadFields (), ";"), "#23");
            }
        }
 public static TextFieldParser OpenTextFieldParser(string file, params int[] fieldWidths)
 {
     TextFieldParser parser2 = new TextFieldParser(file);
     parser2.SetFieldWidths(fieldWidths);
     parser2.TextFieldType = FieldType.FixedWidth;
     return parser2;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Uses the Microsoft Text Field Parser to parse the FixedWidth file, to give a baseline
        /// against an established class library for parsing.
        /// </summary>
        private static void _TextFieldParserFixedWidth()
        {
            string[] fields;
            string s;

            using (VB.TextFieldParser tfp = new VB.TextFieldParser(PerformanceTests.FW_DATA_FILE))
            {
                tfp.TextFieldType = VB.FieldType.FixedWidth;
                tfp.SetFieldWidths(new int[PerformanceTests.NUMBER_OF_COLUMNS_IN_DATA] { 5, 5, 1, 28, 42, 15, 13, 9, 9, 1, 13, 14, 13, 6 });
                tfp.CommentTokens = new string[] { "#" };
                tfp.HasFieldsEnclosedInQuotes = true;

                while (!tfp.EndOfData)
                {
                    fields = tfp.ReadFields();

                    for (int i = 0; i < fields.Length; ++i)
                        s = fields[i] as string;
                }
            }
        }