/// <summary> /// result != null, arrays != null /// Uses CultureHelper.SystemCultureInfo. /// </summary> /// <returns></returns> public static List <string[]> ParseClipboardData() { IDataObject dataObj = Clipboard.GetDataObject(); if (dataObj is null) { return(new List <string[]>()); } object clipboardData = dataObj.GetData(DataFormats.CommaSeparatedValue); if (clipboardData != null) { string clipboardDataString = GetClipboardDataString(clipboardData); return(CsvHelper.ParseCsv(CultureHelper.SystemCultureInfo.TextInfo.ListSeparator, clipboardDataString)); } clipboardData = dataObj.GetData(DataFormats.Text); if (clipboardData != null) { string clipboardDataString = GetClipboardDataString(clipboardData); return(CsvHelper.ParseCsv("\t", clipboardDataString)); } return(new List <string[]>()); }
/// <summary> /// Uses CultureHelper.SystemCultureInfo /// </summary> /// <param name="data"></param> public static void SetClipboardData(List <string[]> data) { if (data is null || data.Count == 0) { return; } var sb1 = new StringBuilder(); var sb2 = new StringBuilder(); foreach (var row in data) { sb1.Append(CsvHelper.FormatForCsv(CultureHelper.SystemCultureInfo.TextInfo.ListSeparator, row)); sb1.Append(Environment.NewLine); sb2.Append(CsvHelper.FormatForCsv("\t", row)); sb2.Append(Environment.NewLine); } string clipboardData1 = sb1.ToString(); string clipboardData2 = sb2.ToString(); Clipboard.Clear(); if (!String.IsNullOrEmpty(clipboardData1)) { Clipboard.SetData(DataFormats.CommaSeparatedValue, clipboardData1); } if (!String.IsNullOrEmpty(clipboardData2)) { Clipboard.SetData(DataFormats.Text, clipboardData2); } }
public string?GetValue(string?fileName, string key, int column) { if (string.IsNullOrWhiteSpace(fileName) || column < 0) { return(null); } if (!_csvFilesCollection.TryGetValue(fileName !.ToUpperInvariant(), out CsvFile? csvFile)) { return(null); } if (csvFile.Data == null) { csvFile.Data = CsvHelper.LoadCsvFile(csvFile.FileFullName, true, null, UserFriendlyLogger, csvFile.IncludeFileNamesCollection); } if (!csvFile.Data.TryGetValue(key, out List <string?>?fileLine)) { return(null); } if (column >= fileLine.Count) { return(null); } return(fileLine[column]); }
/// <summary> /// Saves changed data to .csv file on disk. /// </summary> public void SaveData(string?fileName) { if (_csvDbDirectoryInfo is null) { return; } if (string.IsNullOrWhiteSpace(fileName)) { return; } string fileNameUpper = fileName !.ToUpperInvariant(); if (!_csvFilesCollection.TryGetValue(fileNameUpper, out CsvFile? csvFile)) { return; } if (csvFile.DataIsChangedByProgram) { _fileSystemWatcher.EnableRaisingEvents = false; try { var isNewCsvFile = csvFile.LastWriteTimeUtc == DateTime.MinValue; // If the file to be deleted does not exist, no exception is thrown. File.Delete(csvFile.FileFullName); // For 'a' to 'A' changes in files names to work. using (var writer = new StreamWriter(csvFile.FileFullName, false, new UTF8Encoding(true))) { foreach (var fileLine in csvFile.Data !.OrderBy(kvp => kvp.Key)) { writer.WriteLine(CsvHelper.FormatForCsv(",", fileLine.Value.ToArray())); } } csvFile.LastWriteTimeUtc = File.GetLastWriteTimeUtc(csvFile.FileFullName); if (isNewCsvFile) { CsvFileChanged?.Invoke(CsvFileChangeAction.Added, fileNameUpper); } else { CsvFileChanged?.Invoke(CsvFileChangeAction.Updated, fileNameUpper); } } catch (Exception ex) { _logger?.LogError(ex, Properties.Resources.CsvDb_CsvFileWritingError + " " + csvFile.FileFullName); } csvFile.DataIsChangedByProgram = false; var t = FileSystemWatcherEnableRaisingEventsAsync(); } }
public void SetValues(string?fileName, IEnumerable <string?> values) { if (string.IsNullOrWhiteSpace(fileName)) { return; } string fileNameUpper = fileName !.ToUpperInvariant(); if (!fileNameUpper.EndsWith(@".CSV")) { return; } if (!_csvFilesCollection.TryGetValue(fileNameUpper, out CsvFile? csvFile)) { csvFile = new CsvFile(); if (_csvDbDirectoryInfo is not null) { csvFile.FileFullName = Path.Combine(_csvDbDirectoryInfo.FullName, fileName); } csvFile.Data = new CaseInsensitiveDictionary <List <string?> >(); _csvFilesCollection.Add(fileNameUpper, csvFile); } var enumerator = values.GetEnumerator(); if (!enumerator.MoveNext()) { return; } string key = enumerator.Current ?? @""; if (csvFile.Data == null) { csvFile.Data = CsvHelper.LoadCsvFile(csvFile.FileFullName, true, null, UserFriendlyLogger, csvFile.IncludeFileNamesCollection); } if (!csvFile.Data.TryGetValue(key, out List <string?>?fileLine)) { fileLine = new List <string?> { key }; csvFile.Data.Add(key, fileLine); } else { fileLine.Clear(); fileLine.Add(key); } while (enumerator.MoveNext()) { fileLine.Add(enumerator.Current); } csvFile.DataIsChangedByProgram = true; }
public void SetValue(string?fileName, string key, int column, string?value) { if (string.IsNullOrWhiteSpace(fileName) || column < 0) { return; } string fileNameUpper = fileName !.ToUpperInvariant(); if (!fileNameUpper.EndsWith(@".CSV")) { return; } if (!_csvFilesCollection.TryGetValue(fileNameUpper, out CsvFile? csvFile)) { csvFile = new CsvFile(); if (_csvDbDirectoryInfo is not null) { csvFile.FileFullName = Path.Combine(_csvDbDirectoryInfo.FullName, fileName); } csvFile.Data = new CaseInsensitiveDictionary <List <string?> >(); _csvFilesCollection.Add(fileNameUpper, csvFile); } if (csvFile.Data == null) { csvFile.Data = CsvHelper.LoadCsvFile(csvFile.FileFullName, true, null, UserFriendlyLogger, csvFile.IncludeFileNamesCollection); } if (!csvFile.Data.TryGetValue(key, out List <string?>?fileLine)) { fileLine = new List <string?> { key }; csvFile.Data.Add(key, fileLine); } if (column >= fileLine.Count) { fileLine.Capacity = column + 1; fileLine.AddRange(Enumerable.Repeat <string?>(null, column + 1 - fileLine.Count)); } if (column > 0) { fileLine[column] = value; } csvFile.DataIsChangedByProgram = true; }
/// <summary> /// List.Count >= 1, List[0] is not null /// </summary> /// <param name="fileName"></param> /// <returns></returns> public CaseInsensitiveDictionary <List <string?> > GetValues(string?fileName) { if (string.IsNullOrWhiteSpace(fileName)) { return(new CaseInsensitiveDictionary <List <string?> >()); } if (!_csvFilesCollection.TryGetValue(fileName !.ToUpperInvariant(), out CsvFile? csvFile)) { return(new CaseInsensitiveDictionary <List <string?> >()); } if (csvFile.Data == null) { csvFile.Data = CsvHelper.LoadCsvFile(csvFile.FileFullName, true, null, UserFriendlyLogger, csvFile.IncludeFileNamesCollection); } return(csvFile.Data); }
/// <summary> /// Returns new uint key, starting from 1. /// </summary> /// <param name="fileName"></param> /// <returns></returns> public uint GetNewKey(string?fileName) { if (string.IsNullOrWhiteSpace(fileName)) { return(1); } if (!_csvFilesCollection.TryGetValue(fileName !.ToUpperInvariant(), out CsvFile? csvFile)) { return(1); } if (csvFile.Data == null) { csvFile.Data = CsvHelper.LoadCsvFile(csvFile.FileFullName, true, null, UserFriendlyLogger, csvFile.IncludeFileNamesCollection); } if (csvFile.Data.Count == 0) { return(1); } return(csvFile.Data.Keys.Max(k => new Any(k).ValueAsUInt32(false)) + 1); }
public bool DeleteValues(string?fileName, string key) { if (string.IsNullOrWhiteSpace(fileName)) { return(false); } if (!_csvFilesCollection.TryGetValue(fileName !.ToUpperInvariant(), out CsvFile? csvFile)) { return(false); } if (csvFile.Data == null) { csvFile.Data = CsvHelper.LoadCsvFile(csvFile.FileFullName, true, null, UserFriendlyLogger, csvFile.IncludeFileNamesCollection); } bool removed = csvFile.Data.Remove(key); if (removed) { csvFile.DataIsChangedByProgram = true; } return(removed); }
/// <summary> /// First column in file is Key and must be unique. /// Can contain include directives, defines and comments. /// If file does not exist returns empty result. /// All fields values are trimmed. /// result != null, lists != null /// </summary> /// <param name="fileFullName"></param> /// <param name="includeFiles"></param> /// <param name="defines"></param> /// <returns></returns> public static CaseInsensitiveDictionary <List <string> > ParseCsvFile(string fileFullName, bool includeFiles, Dictionary <Regex, string> defines = null) { var fileData = new CaseInsensitiveDictionary <List <string> >(); if (File.Exists(fileFullName)) { if (defines is null) { defines = new Dictionary <Regex, string>(); } string filePath = Path.GetDirectoryName(fileFullName); using (var reader = new StreamReader(fileFullName, true)) { string line = ""; string l; while ((l = reader.ReadLine()) != null) { l = l.Trim(); if (l.Length > 0 && l[l.Length - 1] == '\\') { line += l.Substring(0, l.Length - 1); continue; } else { line += l; } if (line == "") { continue; } if (includeFiles && StringHelper.StartsWithIgnoreCase(line, @"#include") && line.Length > 8) { var q1 = line.IndexOf('"', 8); if (q1 != -1 && q1 + 1 < line.Length) { var q2 = line.IndexOf('"', q1 + 1); if (q2 != -1 && q2 > q1 + 1) { var includeFileName = line.Substring(q1 + 1, q2 - q1 - 1); foreach (var kvp in ParseCsvFile(filePath + @"\" + includeFileName, false, defines)) { fileData[kvp.Key] = kvp.Value; } } } } else if (StringHelper.StartsWithIgnoreCase(line, @"#define") && line.Length > 7) { int q1 = 7; for (; q1 < line.Length; q1++) { char ch = line[q1]; if (Char.IsWhiteSpace(ch)) { continue; } else { break; } } if (q1 < line.Length) { int q2 = q1 + 1; for (; q2 < line.Length; q2++) { char ch = line[q2]; if (Char.IsWhiteSpace(ch)) { break; } else { continue; } } string define = line.Substring(q1, q2 - q1); string subst = @""; if (q2 < line.Length - 1) { subst = ReplaceDefines(line.Substring(q2 + 1).Trim(), defines); } defines[new Regex(@"\b" + define + @"\b", RegexOptions.IgnoreCase)] = subst; } } else if (line[0] == '#') { // Comment, skip } else { List <string> fields = CsvHelper.ParseCsvLine(@",", ReplaceDefines(line, defines)).ToList(); if (fields.Count > 0) { if (String.IsNullOrEmpty(fields[0])) { if (fields.Count > 1) { fileData[@""] = fields; } } else { fileData[fields[0]] = fields; } } } line = ""; } } } return(fileData); }