john30 / ebusd-configuration

ebusd configuration files
GNU General Public License v3.0
185 stars 294 forks source link

Make sure all rows match the header comma count #240

Closed kpvleeuwen closed 2 years ago

kpvleeuwen commented 2 years ago

This helps in editing and searching in github, see warning: image

kpvleeuwen commented 2 years ago

Also includes the file found in #235.

kpvleeuwen commented 2 years ago

For future reference; a C# script to fix this. Not the best language to do it in, but hey :)

fix_commas_in_csv.cs

``` cs var path = @"\path\to\ebusd-configuration\ebusd-2.1.x"; Parallel.ForEach(Directory.EnumerateFiles(path, "*.csv", SearchOption.AllDirectories), EvenOutCommas); static void EvenOutCommas(string file) { var wanted = GetCommacount(File.ReadLines(file).First()); var linesWithEvenCommas = new List(); int linesChanged = 0; foreach (string line in File.ReadLines(file)) { var commacount = GetCommacount(line); if (commacount < wanted) { linesChanged++; linesWithEvenCommas.Add(line + new string(',', wanted - commacount)); } else if (commacount > wanted) { linesChanged++; linesWithEvenCommas.Add(line.Substring(0, line.Length - (commacount - wanted))); } else linesWithEvenCommas.Add(line); } if (linesChanged > 0) { Console.WriteLine($"changed {linesChanged} in {file}"); File.WriteAllLines(file, linesWithEvenCommas); } } static int GetCommacount(string line) { int count = 0; for (int i = 0; i < line.Length; i++) { char c = line[i]; if (c == ',') count++; else if (c == '"') { do i++; while (line[i] != '"'); // find closest closing quote } } return count; } ```

kpvleeuwen commented 2 years ago

Closing for now to avoid conflicts with PRs with actual new features, such as #160