 bool answer = getDelimiterAsChar(MyOptions.Delimiter, out Separator);
 private static bool getDelimiterAsChar(string inString, out char Separator)
        {

            if (inString == "\t" || inString=="\\t")
            {
                Separator = '\t';
                return true;
            }
            else
            {
                Separator = inString[0];
       
                return false;
            }

        }
        
        
        if (line.Contains(Separator.ToString()) == false)
            {
                switch (Separator)
                {
                    case '\t':
                        if (line.Contains("|") == true)
                        {
                            MyOptions.Delimiter = "|";
                            Separator = '|';
                        }
                        else
                        {
                            MyOptions.Delimiter = ",";
                            Separator = ',';
                        }
                        break;
                    case ',':
                        if (line.Contains("|") == true)
                        {
                            MyOptions.Delimiter = "|";
                            Separator = '|';
                        }
                        else
                        {
                            MyOptions.Delimiter = "\t";
                            Separator = '\t';
                        }
                        break;
                    default:
                        System.Windows.Forms.MessageBox.Show("ERROR");
                        break;
                }
                
                
                                ////i.e. if it's the last line then force assign a datatype
                //if (i > (MyObject.RowCount - 10) && MyObject.ColumnTypes[q] == DataTypes.unknown)
                //{                     
                //    MyObject.ColumnTypes[q] = DataTypes.varchar;
                //}
                
                
                    private string[] getFields(string line, string SeparatorString, int[] columnWidths)
        {


            string[] f2 = new string[7];
     
            string pattern = @"^(?<Door>\d{1,})";
            string Door = getMatch(line, pattern);

            string Address = getMatch(line.Substring(3), @"(?<Add1>[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)");

            string Address2 = getMatch(line.Substring(9), @"(?<Add2>\s\s[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)\s+(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})");



            pattern = @"(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2}))";
            string PostCode = getMatch(line, pattern);

            string UPRN = getMatch(line, @"\s(?<UPRN>\d{10})\s");

            string RRN = getMatch(line, @"(?<RRN>\d{4}-\d{4}-\d{4}-\d{4}-\d{4})");

            string Completed = getMatch(line, @"(?<Completed>\d\d[\/]\d\d[\/]\d\d\d\d)");

            f2[0] = Door;
            f2[1] = Address;
            f2[2] = Address2;
            f2[3] = PostCode;
            f2[4] = UPRN;
            f2[5] = RRN;
            f2[6] = Completed;
            return f2;

        }  
                
                
        private string[] getFields(string line, string SeparatorString, int[] columnWidths)
        {

            //int[] cw = new int[7];
            //cw[0] = 3;
            //cw[1] = 15;
            //cw[2] = 15;
            //cw[3] = 10;
            //cw[4] = 24;
            //cw[5] = 10;

            //string[] fields = Regex.Split(line, @"\s{2,}");
            

            ////f2[0] = fields[0].Substring(0, Math.Min(cw[0],fields[0].Length));
            ////f2[1] = fields[1].Substring(0, Math.Min(cw[1], fields[1].Length));
            ////f2[2] = fields[2].Substring(0, Math.Min(cw[2], fields[2].Length));
            ////f2[3] = fields[3].Substring(0, Math.Min(cw[3], fields[3].Length));
            ////f2[4] = fields[4].Substring(0, Math.Min(cw[4], fields[4].Length));
            ////f2[5] = fields[5].Substring(0, Math.Min(cw[5], fields[5].Length));
            //Regex r = new Regex(@"\s{2,}");
            //line = r.Replace(line, " ");
            string[] f2 = new string[7];
            //f2[0] = line.Substring(0, 3);
            //if (f2[0].Contains(" ") == true)
            //{
            //    f2[0] = DGen.StringTool.LeftOf(f2[0], " ");
            //}
            //f2[1] = line.Substring(4, 10);
            //f2[2] = line.Substring(14, 10);
            //f2[3] = line.Substring(24, 10);
            //f2[4] = line.Substring(Math.Min(line.Length-24,34), 24);
            //f2[5] = line.Substring(Math.Min(line.Length-10,57), 10);

            string pattern = @"^(?<Door>\d{1,})";
            string Door = getMatch(line,pattern);

            string Address = getMatch(line.Substring(3), @"(?<Add1>[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)");

            string Address2 = getMatch(line.Substring(9), @"(?<Add2>\s\s[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)\s+(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})"); 



            pattern = @"(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2}))";
            string PostCode = getMatch(line, pattern);

            string UPRN = getMatch(line, @"\s(?<UPRN>\d{10})\s");

            string RRN = getMatch(line, @"(?<RRN>\d{4}-\d{4}-\d{4}-\d{4}-\d{4})");

            string Completed = getMatch(line, @"(?<Completed>\d\d[\/]\d\d[\/]\d\d\d\d)");
             
            f2[0] = Door;
            f2[1] = Address;
            f2[2] = Address2;
            f2[3] = PostCode;
            f2[4] = UPRN;
            f2[5] = RRN;
            f2[6] = Completed;
            return f2;
            
        }

            string Address = getMatch(line.Substring(3), @"(?<Add1>[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)");

            string Address2 = getMatch(line.Substring(9), @"(?<Add2>\s\s[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)\s+(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})");



            pattern = @"(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2}))";
            string PostCode = getMatch(line, pattern);

            string UPRN = getMatch(line, 

             = getMatch(line, 
             
             
                       // @"^(?<Door>\d{1,})\s+(?<Add1>[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)";
            //\s+(?<Add2>[a-zA-z]+\s+[a-zA-z]+|[a-zA-z]+)\s+(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2}))\s+(?<UPRN>\d{10})\s+(?<RRN>\d{4}-\d{4}-\d{4}-\d{4}-\d{4})\s+(?<Completed>\d\d[\/]\d\d[\/]\d\d\d\d)";
            string pattern = "abc";
            string Door = getMatch(line, pattern);
            
            
                    private static string prepareInputText(string input, string Separator)
        {
            input = input.Replace("'", "''").TrimEnd();
            input = input.Replace(Separator, GlobalSeparator);
            return input;
        }
        
        
               private static string prepareInputText(string input)
        {
            input = input.Replace("'", "''").TrimEnd();
            


            input = DGen.RemoveNulls(input);
            return input;
        }
        
        
        
        
    public class PrepWork
    {

        public string endResult;
        public StringBuilder PreparedText = new StringBuilder();
        public const string GlobalDelimiter = "||||";
        public string Delimiter;


        public PrepWork(string input, string delimiter)
        {
            endResult = "";
            Delimiter = delimiter;
           
            string[] Lines = input.Split('\n');

            foreach (string Line in Lines)
            {
                if (Line.Trim().Length > 0)
                {
                    string tmpLine = Line;
                    confirmDelimiter(Line, Delimiter);
                    if (Delimiter == ",")
                    {
                        tmpLine = removeRogueCommas(tmpLine);
                        
                    }
                   
                    tmpLine = tmpLine.Replace(Delimiter, GlobalDelimiter);
                    PreparedText.AppendLine(tmpLine);
                }
            }
           
            

        }
        private static string removeRogueCommas(string line)
        {
            line = line.Replace(@""",""", "\"87712261XXX787712261XXX\"");
            line = line.Replace(",", " ");
            line = line.Replace("\"87712261XXX787712261XXX\"", "\",\"");
            return line;
        }

        private void confirmDelimiter(string line, string delimiter)
        {
            // We're happy with the delimiter if it occurs in the header. If it doesn't, then the below applies.
            // (We are trying to guess the delimiter if it applies)
            //TODO : A setting to stop this applying for when you're only after one column.
            if (line.Contains(delimiter) == false)
            {
                string[] possibleDelimiters = new string[8];
                possibleDelimiters[0] = "\t";
                possibleDelimiters[1] = "|";
                possibleDelimiters[2] = ",";
                possibleDelimiters[3] = "/";
                possibleDelimiters[4] = "    ";
                possibleDelimiters[5] = "   ";
                possibleDelimiters[6] = "  ";
                possibleDelimiters[7] = " ";
                int[] occurencesOfDelimiter = new int[8];
                int bestGuessSoFar = 0;
                string bestGuess = delimiter;
                for (int i = 0; i < possibleDelimiters.Length; i++)
                {
                    //occurencesOfDelimiter[i] = DGen.StringToolCharOccurs(line, possibleDelimiters[i]);
                    occurencesOfDelimiter[i] = DGen.StringOccurs(line, possibleDelimiters[i]);
                    if (occurencesOfDelimiter[i] > bestGuessSoFar)
                    {
                        bestGuessSoFar = occurencesOfDelimiter[i];
                        bestGuess = possibleDelimiters[i];
                    }
                }
                if (bestGuessSoFar > 0)
                {
                    Delimiter = bestGuess;                    
                }
            }
        }


    }

private string[] getFields(string line, bool isHeaderRow)
        {
            //string line2 = line.Replace("\t", "    ");
            //return getFields(line2, @"[ ]{3,}");
            //return Regex.Split(line, GlobalSeparator);
            string[] tmpArray = Regex.Split(line, GlobalSeparator);
            int items = tmpArray.Length;
            if (tmpArray[items - 1].Length < 1)
            {
                items--;
            }
            return (string[])DGen.DGen.ResizeArray(tmpArray, items);
        }
        private string[] getFields(string line, char SeparatorCharacter)
        {

            string[] fields = line.Split(SeparatorCharacter);
            return fields;
        }
        private string[] getFields(string line, string SeparatorString)
        {

            string[] fields = Regex.Split(line, SeparatorString);
            return fields;
        }
        private string[] getFields(string line, int[] columnWidths)
        {
            int pos = 0;
            int columns = columnWidths.Count();
            string[] fields = new string[columns + 1];
            for (int i = 0; i < columns; i++)
            {
                fields[i] = line.Substring(pos, columnWidths[i]);
                pos += columnWidths[i];
            }
            fields[columns] = line.Substring(pos);
            //string[] fields = Regex.Split(line, SeparatorString);

            return fields;
        }




        private string[] getFields(string line, string SeparatorString, int maxCols)
        {
            
            string[] fields = Regex.Split(line, SeparatorString);
            int elements = fields.Length;
            if (elements > maxCols)
            {
                int diff = elements - maxCols;
                for (int i = 0; i < diff; i++)
                {
                    fields[maxCols] += fields[maxCols+i];
                }
                
            }
      
            string[] fields2 = new string[maxCols];
            fields2 = (string[])DGen.DGen.ResizeArray(fields, maxCols);
            return fields2;
        }
        private string[] getFields(string line, string SeparatorString, int[] columnWidths)
        {

           
            string[] f2 = new string[7];
           

            string pattern = @"^(?<Door>\d{1,})";
            string Door = getMatch(line,pattern);
            

            string Address = getMatch(line.Substring(3), @"(?<Add1>[a-zA-z]+\s{1,2}[a-zA-z]+|[a-zA-z]+)");

            string Address2 = getMatch(DGen.StringTool.RightOf(line, Address), @"(?<Add2>[a-zA-z]+\s{1,2}[a-zA-z]+\s{2,}|[a-zA-Z]{3,}|[a-zA-Z]{2,}\s+[a-zA-Z]+\s+[a-zA-Z]{3,})");



            pattern = @"(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[\s]{0,3}(\d[A-Z]{2}))";
         //   pattern = @"(?<PostCode>(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2}){2})";

            //line = Regex.Replace(line, @"[A-Z][A-Z][0-9](\s{2})[0-9][A-Z][A-Z]",@" ");

            string PostCode = getMatch(DGen.StringTool.RightOf(line,Address2), pattern);

            string UPRN = getMatch(line, @"\s(?<UPRN>\d{9,10}|\d{8,10}\.)");

            string RRN = getMatch(line, @"(?<RRN>\d{4}-\d{4}-\d{4}-\d{4}-\d{4})");

            string Completed = getMatch(line, @"(?<Completed>\d\d[\/]\d\d[\/]\d\d\d\d)");
             
            f2[0] = Door;
            f2[1] = Address;
            f2[2] = Address2;
            f2[3] = PostCode;
            f2[4] = UPRN;
            f2[5] = RRN;
            f2[6] = Completed;
            return f2;
            
        }

        private static string getMatch(string line, string pattern)
        {

            Regex r2 = new Regex(pattern,RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
            
            Match m = r2.Match(line);
            
            return m.Value;
        }


            


        private string[] getFields(string line, string SeparatorString, bool useTemplate)
        {


            string[] f2 = new string[7];

  

            string DoorP = @"(?<Door>\d{1,})";
            string Add1P = @"(?<Add1>[a-zA-z\,]{2,}\s+[a-zA-z]+|[a-zA-z]{3,})";
            string Add2P= @"(?<Add2>[a-zA-z\,]+\s+[a-zA-z]+|[a-zA-z]+)?";
           // string PostCodeP = @"(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2}))??";
             string PostCodeP = @"(?<PostCode>(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]*(\d[A-Z]{2}))*";
            string UPRNP = @"(?<UPRN>\d{9,10})";
            string RRNP = @"(?<RRN>\d{4}-\d{4}-\d{4}-\d{4}-\d{4})";
            string CompletedP= @"(?<Completed>\d\d[\/]\d\d[\/]\d\d\d\d)*";

            string fullPattern = DoorP + @"\s+" + Add1P + @"\s+" + Add2P + @"\s+";
            fullPattern += PostCodeP + @"\s+" + UPRNP + @"\s+" + RRNP + @"\s+" + CompletedP;
           // Regex r9 = new Regex(fullPattern, RegexOptions.ExplicitCapture);

            MatchCollection myMatchCollection = Regex.Matches(line, fullPattern,RegexOptions.ExplicitCapture);
            int i=0;
            foreach (Match myMatch in myMatchCollection)
            {
                try {

                f2[0] = myMatch.Groups["Door"].Value;
                f2[1] = myMatch.Groups["Add1"].Value;
                f2[2] = myMatch.Groups["Add2"].Value;
                f2[3] = myMatch.Groups["PostCode"].Value;
                f2[4] = myMatch.Groups["UPRN"].Value;
                f2[5] = myMatch.Groups["RRN"].Value;
                f2[6] = myMatch.Groups["Completed"].Value;   
                }
                catch (Exception ex)
                {
                    //moo;
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
            

            return f2;

        }









        
  

        


        private static char getDelimiterAsCharacter(string inString)
        {
            if (inString == null)
            {
                inString = "\t";
            }
            char rtnValue;
            if (inString == "\t" || inString == "\\t")
            {
                rtnValue = '\t';
            }
            else
            {
                rtnValue = inString[0];
            }
            return rtnValue;
        }

        private static string removeRogueCommas(string line)
        {
            line = line.Replace(@""",""", "\"87712261XXX787712261XXX\"");
            line = line.Replace(",", " ");
            line = line.Replace("\"87712261XXX787712261XXX\"", "\",\"");
            return line;
        }



        private void confirmDelimiter(string line)
        {
            // We're happy with the delimiter if it occurs in the header. If it doesn't, then the below applies.
            // (We are trying to guess the delimiter if it applies)
            //TODO : A setting to stop this applying for when you're only after one column.
            if (line.Contains(Separator.ToString()) == false)
            {
                char[] possibleDelimiters = new char[4];
                possibleDelimiters[0] = '\t';
                possibleDelimiters[1] = '|';
                possibleDelimiters[2] = ',';
                possibleDelimiters[3] = '/';
                int[] occurencesOfDelimiter = new int[4];
                int bestGuessSoFar = 0;
                char bestGuess = Separator;
                for (int i = 0; i < possibleDelimiters.Length; i++)
                {
                    occurencesOfDelimiter[i] = DGen.CharOccurs(line, possibleDelimiters[i]);
                    if (occurencesOfDelimiter[i] > bestGuessSoFar)
                    {
                        bestGuessSoFar = occurencesOfDelimiter[i];
                        bestGuess = possibleDelimiters[i];
                    }
                }
                if (bestGuessSoFar > 0)
                {
                    Separator = bestGuess;
                }
            }
        }
        
        private void fetchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = @"C:\Dump\EPC";
            wget WG = new wget(filePath);
            foreach (string Line in inStr.Lines)
            {
                string tmp = "8399-5775-9220-2926-8083";

                string RRN = "";
                RRN = Line.Trim();
                if (RRN.Length == tmp.Length)
                {
                    WG.fetchReport(RRN);
                    outStr.Text += WG.DiagnosticMessages;
                }
                

            }
            
            
          

        }
        
        
          private CheckedListBox getCLB()
        {

            CheckedListBox clbX = new CheckedListBox();
            clbX.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Right)));
            clbX.CheckOnClick = true;
            clbX.FormattingEnabled = true;
            clbX.Items.AddRange(new object[] {
            "table3",
            "table4"});
            clbX.Location = new System.Drawing.Point(479, 226);
            clbX.Name = "checkedListBox1";
            clbX.Size = new System.Drawing.Size(157, 229);
            clbX.TabIndex = 3;
            return clbX;

        }
        
        
        
        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.CurrentValue == CheckState.Checked)
            {
                MessageBox.Show(e.Index.ToString());
            }


            if (e.NewValue == CheckState.Unchecked)
            {
                MessageBox.Show(e.Index.ToString());
            }
            else
            {
                //TODO : Implement this.
                MessageBox.Show("fail/error");
            }
        }
private void refreshCheckBox()
        {
            
            checkedListBox1.CheckOnClick = true;
            checkedListBox1.Enabled = true;
        
            
            CheckedListBox.ObjectCollection clb1 = new CheckedListBox.ObjectCollection(checkedListBox1);
            clb1.Clear();
            foreach (DataColumn dc in ourDataTable.Columns)
            { clb1.Add(dc.ColumnName); }
          
            checkedListBox1.Show();
            checkedListBox1.Refresh();
        }
        
        
        //= new int[possibleDelimiters.Length];

               // confirmDelimiter(Line, Delimiter);



                //int bestGuessSoFar = 0;
                //string bestGuess = delimiter;
                //for (int i = 0; i < possibleDelimiters.Length; i++)
                //{
                //    occurencesOfDelimiter[i] = DGen.DText.StringOccurs(line, possibleDelimiters[i]);
                //    if (i >= 5 && bestGuessSoFar == 0)
                //    {
                //        bestGuessSoFar = occurencesOfDelimiter[i];
                //        bestGuess = possibleDelimiters[i];
                //    }
                //    if (i < 5 && occurencesOfDelimiter[i] > bestGuessSoFar)
                //    {
                //        bestGuessSoFar = occurencesOfDelimiter[i];
                //        bestGuess = possibleDelimiters[i];
                //    }
                //}
                //if (bestGuessSoFar > 0)
                //{
                //    if (bestGuess==@"\," )
                //    {
                //        bestGuess=bestGuess.Replace(@"\","");
                //    }
                //    Delimiter = bestGuess;
                //}




        /// <summary>
        /// Produces an output of the form "AND something in ('1','2','3','4','5','1','2'). INPUT MUST BE LINE BREAK SEPARATED.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string string2csv(string input)
        {
            int DesiredWidth = 3; //in columns, not characters
            var csvString = new StringBuilder();
            csvString.AppendLine("WHERE 1=1");
            csvString.AppendLine(" AND something IN \n\t(");
            string[] tmpString = input.Split('\n');
            // csvString.Append("\t\t");

            int i = 0;
            var termLengths = new int[tmpString.Length];
            int LongestLine = 0;

            foreach (string linus in tmpString)
            {
                if (linus.Trim().Length > LongestLine)
                {
                    LongestLine = linus.Trim().Length;
                }
                i++;
            }
            i = 0;
            LongestLine += 2;
            foreach (string line in tmpString)
            {
                if (i%DesiredWidth == 0)
                {
                    csvString.AppendLine();
                    //csvString.Append("\t\t");
                }
                string theTerm = "'" + line.Trim() + "'";
                csvString.Append("\t\t");
                csvString.Append(theTerm.PadRight(LongestLine, ' '));
                csvString.Append(",");


                i++;
            }
            csvString.Remove(csvString.Length - 1, 1);

            csvString.AppendLine();
            csvString.Append("\t)");
            return csvString.ToString();
        }