Exemple #1
0
        public static string Line(IEnumerable <string> data)
        {
            if (null == data)
            {
                throw new ArgumentNullException("data");
            }

            var buffer = new StringBuilder();
            var i      = 0;

            foreach (var item in data)
            {
                if (0 != i)
                {
                    buffer.Append(',');
                }

#if NET20
                buffer.Append(CsvStringExtensionMethods.FormatCommaSeparatedValue(item));
#else
                buffer.Append(item.FormatCommaSeparatedValue());
#endif
                i++;
            }

            if (0 == i)
            {
                throw new ArgumentOutOfRangeException("data");
            }

            return(buffer.ToString());
        }
        public virtual void Save(Lexicon lexicon)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, string.Empty);
            if (null == lexicon)
            {
                throw new ArgumentNullException("lexicon");
            }

            using (var writers = new StreamWriterDictionary("CANONICAL,SYNONYMS")
            {
                Access = FileAccess.Write,
                Mode = FileMode.Create,
                Share = FileShare.None
            })
            {
#if NET20
                if (0 == IEnumerableExtensionMethods.Count(lexicon))
#else
                if (!lexicon.Any())
#endif
                {
                    writers.Item(Location.FullName).WriteLine(string.Empty);
                    return;
                }

#if NET20
                var items = new SortedList <string, LexicalItem>();
                foreach (var item in lexicon)
                {
                    items.Add(item.CanonicalForm, item);
                }

                foreach (var item in items)
                {
                    var synonyms = new SortedList <string, string>();
                    foreach (var synonym in item.Value.Synonyms)
                    {
                        synonyms.Add(synonym, synonym);
                    }

                    writers
                    .Item(Location.FullName)
                    .WriteLine(StringExtensionMethods.FormatWith(
                                   "{0},{1}",
                                   CsvStringExtensionMethods.FormatCommaSeparatedValue(item.Value.CanonicalForm),
                                   CsvStringExtensionMethods.FormatCommaSeparatedValue(IEnumerableExtensionMethods.Concat(synonyms.Values, ';'))));
                }
#else
                foreach (var item in lexicon.OrderBy(x => x.CanonicalForm))
                {
                    writers
                    .Item(Location.FullName)
                    .WriteLine("{0},{1}".FormatWith(
                                   item.CanonicalForm.FormatCommaSeparatedValue(),
                                   item.Synonyms.OrderBy(x => x).Concat(';').FormatCommaSeparatedValue()));
                }
#endif
            }
        }
Exemple #3
0
        public static void Save(FileInfo file,
                                DataTable table)
        {
            if (null == file)
            {
                throw new ArgumentNullException("file");
            }

            if (null == table)
            {
                throw new ArgumentNullException("table");
            }

            var exists = file.Exists;

            using (var stream = file.Open(exists ? FileMode.Append : FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
            {
                using (var writer = new StreamWriter(stream))
                {
                    var buffer = new StringBuilder();
                    var index  = 0;
                    if (!exists)
                    {
                        foreach (DataColumn column in table.Columns)
                        {
                            if (0 != index)
                            {
                                buffer.Append(',');
                            }

                            index++;
#if NET20
                            buffer.Append(CsvStringExtensionMethods.FormatCommaSeparatedValue(column.ColumnName));
#else
                            buffer.Append(column.ColumnName.FormatCommaSeparatedValue());
#endif
                        }
                    }

                    writer.WriteLine(buffer.ToString());
#if NET20 || NET35
                    buffer = new StringBuilder();
#else
                    buffer.Clear();
#endif
                    index = 0;
                    foreach (DataRow row in table.Rows)
                    {
                        foreach (var item in row.ItemArray)
                        {
                            if (0 != index)
                            {
                                buffer.Append(',');
                            }

                            index++;
#if NET20
                            buffer.Append(CsvStringExtensionMethods.FormatCommaSeparatedValue(item.ToString()));
#else
                            buffer.Append(item.ToString().FormatCommaSeparatedValue());
#endif
                        }

                        writer.WriteLine(buffer.ToString());
                    }
                }
            }
        }