Ejemplo n.º 1
0
        // Detect comma-separated/tab-separated based on the paste content.
        private void PasteCsvClick(object sender, EventArgs e)
        {
            int validCSV, validTSV;
            List <List <string> > csv, tsv;

            // TODO: Look at getting CSV directly from the clipboard.
            // It's more work than it sounds: there doesn't seem to be a consensus on what the exact
            // format of that data is.
            // Excel in particular writes it in UTF-8 (or the windows code page, according to some)
            // with a null terminator.

            // It's plain text: use guesswork to figure out if it's TSV or CSV.
            // Tab-separated is rarer, so if it works with tabs, it's probably that.
            string text = Clipboard.GetText();

            csv = CsvUtilities.ParseCSV(',', text, out validCSV);
            tsv = CsvUtilities.ParseCSV('\t', text, out validTSV);

            Paste(validTSV >= validCSV ? tsv : csv, null);
        }
Ejemplo n.º 2
0
        static WordListEntries FromDelimitedText(string text)
        {
            if (text != null)
            {
                int validCSV, validTSV;
                var csv     = CsvUtilities.ParseCSV(',', text, out validCSV);
                var tsv     = CsvUtilities.ParseCSV('\t', text, out validTSV);
                var lines   = validTSV >= validCSV ? tsv : csv;
                var entries = new List <WordListEntry>();

                foreach (var line in lines)
                {
                    if (line.Count >= 2)
                    {
                        entries.Add(new WordListEntry(null, line[0], line[1]));
                    }
                }

                return(new WordListEntries(null, entries));
            }

            return(null);
        }