-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
104 lines (88 loc) · 3.36 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright 2021 Filip Strajnar
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using Sylvan.Data.Csv;
using System.Collections.Generic;
namespace CsvCodeGen
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Tool usage:\nCsvCodeGen <namespace> <class> <csv file path>");
return;
}
var codegen_result = CodeGen(args[0], args[1], args[2]);
System.IO.File.WriteAllText($"{args[1]}.cs", codegen_result);
}
static string CodeGen(string namespace_name, string class_name, string csv_path)
{
List<string> properties = new List<string>();
List<string> constructing = new List<string>();
var csv = Sylvan.Data.Csv.CsvDataReader.Create(csv_path, new CsvDataReaderOptions { HasHeaders = false });
for (int i = 0; i < csv.FieldCount; i++)
{
string correct = "";
string tmp = csv.GetString(i);
if (!Char.IsLetter(tmp[0]) && !(tmp[0] == '_'))
{
correct += "_";
}
foreach (var item in tmp)
{
if (Char.IsLetter(item) || Char.IsNumber(item) || item == '_')
correct += item;
}
properties.Add($"public string {correct}_{i} {{ get; set; }}\n");
constructing.Add($"{correct}_{i} = reader.GetString({i}),\n");
}
string GeneratedDocument = $"using System.Collections.Generic;\nusing Sylvan.Data.Csv;\nnamespace {namespace_name}\n{{\n class {class_name}\n {{\n";
foreach (var property in properties)
{
GeneratedDocument += $" {property}";
}
GeneratedDocument += @" public static " + class_name + @" ReadRow(CsvDataReader reader)
{
return new " + class_name + @"
{
";
foreach (var part in constructing)
{
GeneratedDocument += $" {part}";
}
GeneratedDocument += @"
};
}";
GeneratedDocument += @"
public static List<" + class_name + @"> Read(CsvDataReader reader)
{
List<" + class_name + @"> entireDocument = new();
while (reader.Read())
{
entireDocument.Add(ReadRow(reader));
}
return entireDocument;
}
public static List<" + class_name + @"> ReadFile(string csv_path)
{
var csv = Sylvan.Data.Csv.CsvDataReader.Create(csv_path, new CsvDataReaderOptions { HasHeaders = true });
return Read(csv);
}
}
}";
return GeneratedDocument;
}
}
}