Exemplo n.º 1
0
        private void btnExeInputForm_Click(object sender, EventArgs e)
        {
            string        caption          = "Confirm";
            string        showEditHeaders  = "Edit Headers: ";
            string        showElementIndex = "Columns: ";
            string        showLineAmount   = "Total lines: ";
            string        showNewFilePath  = "Save to: ";
            var           buttons          = MessageBoxButtons.OKCancel;
            var           icon             = MessageBoxIcon.Information;
            List <string> elementList;

            inputEditHeaders  = radioEditHeaders.Checked;
            inputLineAmount   = string.IsNullOrEmpty(txtLineAmount.Text) ? "0" : txtLineAmount.Text;
            inputElementIndex = string.IsNullOrEmpty(txtElementIndex.Text) ? "0" : txtElementIndex.Text;

            DialogResult result = MessageBox.Show(
                showNewFilePath + newFilePath + "\n" +
                showEditHeaders + inputEditHeaders + "\n" +
                showLineAmount + inputLineAmount + "\n" +
                showElementIndex + inputElementIndex,
                caption, buttons, icon
                );

            if (result == DialogResult.OK)
            {
                lineAmount   = int.Parse(inputLineAmount);
                elementIndex = new List <int>();
                elementList  = new List <string>(ProcessLines.SplitValues(inputElementIndex, ','));

                for (int i = 0; i < elementList.Count; i++)
                {
                    elementIndex.Add(int.Parse(elementList[i]));
                }

                Close();
            }
        }
        public static void Main(string[] args)
        {
            // Console splash
            Console.SetWindowSize(145, 30);
            Console.SetWindowPosition(0, 0);
            Console.WriteLine("Console program for producing generic tabular data.");
            Console.WriteLine("\nSelect file to read from or create new:");
            Console.Write(">>> Press any key to continue...");
            Console.ReadKey(true);

            // GUI read data
            FileReadGUI inputReadForm = new FileReadGUI();

            // Display original quoted lines
            string[] rawLines = File.ReadAllLines(inputReadForm.OldFilePath + inputReadForm.OldFileName);
            Console.WriteLine(">>> Input file: {0}\n", inputReadForm.OldFileName);
            Console.WriteLine(">>> Raw lines:");
            for (int i = 0; i < rawLines.Length; i++)
            {
                Console.WriteLine(rawLines[i]);
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(">>> Press any key to continue...\n");
            Console.ReadKey(true);

            // Remove quotation
            string strToRemove = "\"";
            string headLine    = ProcessLines.RemoveString(rawLines[0], strToRemove);
            string recordLine  = ProcessLines.RemoveString(rawLines[1], strToRemove);

            // Split values
            char          valueDelimiter = ',';
            List <string> headList       = new List <string>();
            List <string> recordList     = new List <string>();

            headList   = ProcessLines.SplitValues(headLine, valueDelimiter);
            recordList = ProcessLines.SplitValues(recordLine, valueDelimiter);

            // GUI edit and write data file
            FileWriteGUI inputWriteForm = new FileWriteGUI();

            inputWriteForm.CallbackGUI();

            // Header edit
            UpdateLine updateHead = new UpdateLine(headList);

            if (inputWriteForm.InputEditHeaders)
            {
                updateHead.ModifySingleLine(inputWriteForm.ElementIndex);
            }

            // Row edit
            UpdateLine updateLine = new UpdateLine(recordList, inputWriteForm.LineAmount);

            if (inputWriteForm.LineAmount < 2)
            {
                updateLine.ModifySingleLine(inputWriteForm.ElementIndex);
            }
            else
            {
                updateLine.ModifyMultiLine(inputWriteForm.ElementIndex);
            }

            // Longest field indexes for column padding
            List <int> longestColumnField = new List <int>();

            longestColumnField = ProcessLines.CompareFields(updateHead, updateLine);

            // Concatenate Values
            string[] dataSet = new string[] { };
            dataSet = ProcessLines.AppendValues(updateHead, updateLine);

            // Write data to new file
            bool           overwrite     = true;
            CreateTextFile objNewTxtFile = new CreateTextFile(inputWriteForm.NewFilePath, inputWriteForm.NewFileName);

            objNewTxtFile.CreateFile(dataSet, overwrite);

            // Results
            Console.WriteLine("\n>>> Begin unique tabular data:\n");
            int counter        = 0;
            int totalStrLength = 0;
            int rowDivider     = 0;

            foreach (string s in updateHead.StrList)
            {
                totalStrLength += s.Length;
            }

            for (int i = 0; i < longestColumnField.Count; i++)
            {
                // Padding: 4 = amount of characters padding the column
                rowDivider += int.Parse(longestColumnField[i].ToString()) + 4;
            }

            foreach (string s in updateHead.StrList)
            {
                // Padding: "| "
                Console.Write("| {0," + longestColumnField[counter] + "} |", s);
                counter++;
            }

            Console.Write(Environment.NewLine);
            Console.WriteLine(new String('-', rowDivider));

            if (inputWriteForm.LineAmount < 2)
            {
                counter = 0;

                foreach (string s in updateLine.StrList)
                {
                    Console.Write("| {0," + longestColumnField[counter] + "} |", s);
                    counter++;
                }
                Console.Write(Environment.NewLine);
            }
            else
            {
                int count = updateLine.StrListArr.Count;

                for (int i = 0; i < count; i++)
                {
                    counter = 0;

                    foreach (string s in updateLine.StrListArr[i])
                    {
                        Console.Write("| {0," + longestColumnField[counter] + "} |", s);
                        counter++;
                    }
                    Console.Write(Environment.NewLine);
                }
            }

            Console.WriteLine("\n>>> Press any key to exit.");
            Console.ReadKey();
        }