Esempio n. 1
0
        /// <summary>
        /// Header consists of:
        /// 1) |---------------------| <-- hyphen line
        /// 2) |    |    | 5     | 1 | <-- (optional) max width of field
        /// 3) | LN | FC | 1     | 2 | <-- field number
        /// 4) |    |    | line3 | c | <-- (optional) header field
        /// 5) |-----------------+---| <-- hyphen line
        ///     (a) optional line number
        ///           (b) optional field count for each row
        /// </summary>
        private void PrintHeader()
        {
            // (1)
            // print top line
            PrintHyphenLine(false);

            // (2)
            if (PrintFieldMaxWidth)
            {
                PrintLineNumberField("FS");
                PrintFieldCountField("");

                for (int i = 0; i < MaxFieldCount; ++i)
                {
                    OutStream.Write("|");
                    PrintField(MaxFieldLengths[i].ToString(), MaxFieldLengths[i], true);
                }
                OutStream.Write("|" + System.Environment.NewLine);
            }

            // (3)
            {
                PrintLineNumberField("LN");
                PrintFieldCountField("FC");

                // print field numbers
                for (int i = 0; i < MaxFieldCount; ++i)
                {
                    OutStream.Write('|');
                    PrintField((FieldOffset + i).ToString(), MaxFieldLengths[i], true);
                }
                OutStream.Write("|" + System.Environment.NewLine);
            }

            // (4) print headers (if we need them)
            if (Header == PrintHeaderType.PrintRow1 || Header == PrintHeaderType.PrintExternFile)
            {
                string[] fields = ParseClass.ParseLine(HeaderLine, TrimFields, Delimiter, PreserveQuotes);

                PrintLineNumberField("");
                PrintFieldCountField(fields.Length.ToString());

                for (int i = 0; i < MaxFieldCount; ++i)
                {
                    OutStream.Write("|");
                    if (i >= fields.Length)
                    {
                        PrintField(null, MaxFieldLengths[i], true);
                    }
                    else
                    {
                        PrintField(fields[i], MaxFieldLengths[i], true);
                    }
                }
                OutStream.Write("|" + System.Environment.NewLine);
            }

            // (5) print plus line between header & body
            PrintHyphenLine(true);
        }
Esempio n. 2
0
        private void PrintBody()
        {
            for (int rowcount = 0; rowcount < RawFile.Count; rowcount++)
            {
                string   line   = RawFile[rowcount];
                string[] fields = ParseClass.ParseLine(line, TrimFields, Delimiter, PreserveQuotes);

                PrintLineNumberField((StartSkipRows + rowcount + 1).ToString());
                PrintFieldCountField(fields.Length.ToString());

                for (int i = 0; i < MaxFieldCount; ++i)
                {
                    OutStream.Write("|");
                    if (i >= fields.Length)
                    {
                        PrintField(null, MaxFieldLengths[i], true);
                    }
                    else
                    {
                        PrintField(fields[i], MaxFieldLengths[i], true);
                    }
                }
                OutStream.Write("|" + System.Environment.NewLine);
            }
        }
Esempio n. 3
0
        private int SaveFieldCount(string line)
        {
            string[] fields = ParseClass.ParseLine(line, TrimFields, Delimiter, PreserveQuotes);
            if (fields.Length > MaxFieldCount)
            {
                MaxFieldCount = fields.Length;
            }

            SaveFieldLengths(fields);

            return(0);
        }
Esempio n. 4
0
        public int ParseArgs(string[] args)
        {
            for (int i = 0; i < args.Length;)
            {
                if (!args[i].StartsWith("-"))
                {
                    if (ReadStdin)
                    {
                        FilenameIn = args[i];
                        i++;
                        continue;
                    }
                    else
                    {
                        Console.Error.WriteLine("Expected input of |{0}|. (Did you give two files?)", args[i]);
                        return(1);
                    }
                }

                switch (args[i])
                {
                case "-l": PrintAlignment = PrintAlignmentType.Left; i++; break;

                case "-c": PrintAlignment = PrintAlignmentType.Center; i++; break;

                case "-r": PrintAlignment = PrintAlignmentType.Right; i++; break;

                case "-n": PrintLineNumber = true; i++; break;

                case "-f": PrintFieldCount = true; i++; break;

                case "-m": PrintFieldMaxWidth = true; i++; break;

                case "-all": PrintLineNumber = true; PrintFieldCount = true; PrintFieldMaxWidth = true; i++; break;

                case "-h":
                {
                    i++;
                    if (i < args.Length && !args[i].StartsWith("-"))
                    {
                        Header         = PrintHeaderType.PrintExternFile;
                        FilenameHeader = args[i];
                        i++;
                    }
                    else
                    {
                        Header = PrintHeaderType.PrintRow1;
                    }
                    break;
                }

                case "-t": TrimFields = true; i++; break;

                case "-q": PrintFieldGutters = false; i++; break;

                case "-N":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected a number after -N.");
                        return(1);
                    }
                    int count;
                    if (!Int32.TryParse(args[i], out count))
                    {
                        Console.Error.WriteLine("Expected a number after -N. Unable to parse to a number.");
                        return(1);
                    }
                    LineCountToPrint = count;
                    i++;
                    break;
                }

                case "-s":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected a number after -s.");
                        return(1);
                    }
                    int count;
                    if (!Int32.TryParse(args[i], out count) || count < 0)
                    {
                        Console.Error.WriteLine("Expected a positive number after -s. Unable to parse to a number.");
                        return(1);
                    }
                    StartSkipRows = SkipRows = count;
                    i++;
                    break;
                }

                case "-2": UseTwoPassMethod = true; i++; break;

                case "-d":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected a delimiter after -d.");
                        return(1);
                    }
                    char parsed;
                    int  n = ParseClass.ParseStringArg(args[i], out parsed);
                    if (n > 0)
                    {
                        return(n);
                    }
                    Delimiter = parsed;
                    i++;
                    break;
                }

                case "-w":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected a delimiter after -w.");
                        return(1);
                    }
                    char parsed;
                    int  n = ParseClass.ParseStringArg(args[i], out parsed);
                    if (n > 0)
                    {
                        return(n);
                    }
                    WhitespaceLetter = parsed;
                    i++;
                    break;
                }

                case "-e":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected a delimiter after -e.");
                        return(1);
                    }
                    char parsed;
                    int  n = ParseClass.ParseStringArg(args[i], out parsed);
                    if (n > 0)
                    {
                        return(n);
                    }
                    EmptyFieldDelimiter = parsed;
                    i++;
                    break;
                }

                case "-o":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected a delimiter after -d.");
                        return(1);
                    }
                    int n;
                    int ret = ParseClass.ParseIntArg(args[i], out n);
                    if (ret > 0)
                    {
                        return(ret);
                    }
                    FieldOffset = n;
                    i++;
                    break;
                }

                case "-Q": PreserveQuotes = true; i++; break;

                case "-y": DynamicFields = true; i++; break;

                case "-F":
                {
                    i++;
                    if (i >= args.Length)
                    {
                        Console.Error.WriteLine("Expected another input after -F.");
                        return(1);
                    }
                    FilenameOut = args[i];
                    WriteStdout = false;
                    i++;
                    break;
                }

                case "-v": Verbose = true; i++; break;

                default:
                {
                    Console.WriteLine("Expected input of |{0}|.", args[i]);
                    return(1);
                }
                }
            }

            return(0);
        }