-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
148 lines (128 loc) · 3.18 KB
/
Utils.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualBasic.FileIO;
using tp1;
namespace tpfinal
{
internal class Utils
{
private static string patron = "";
public static int lineCount;
public static void init_patron()
{
//patron = System.IO.Directory.GetCurrentDirectory();
//lineCount = File.ReadLines(@patron).Count();
}
public static void set_patron(string patron_parm)
{
patron = patron_parm;
//lineCount = File.ReadLines(@patron).Count();
}
public static string get_patron()
{
if (patron == null)
{
patron = ""; // System.IO.Directory.GetCurrentDirectory();
//lineCount = File.ReadLines(@patron).Count();
}
return patron;
}
public static string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
//string str = str_input.Replace(' ', '_');
foreach (char c in str)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_' || c == ' ')
{
sb.Append(c);
}
}
return sb.ToString();
}
public static IEnumerable<IList<string>> Parse(TextReader reader, char delimiter, char qualifier)
{
var inQuote = false;
var record = new List<string>();
var sb = new StringBuilder();
while (reader.Peek() != -1)
{
var readChar = (char)reader.Read();
if (readChar == '\n' || (readChar == '\r' && (char)reader.Peek() == '\n'))
{
// If it's a \r\n combo consume the \n part and throw it away.
if (readChar == '\r')
reader.Read();
if (inQuote)
{
if (readChar == '\r')
sb.Append('\r');
sb.Append('\n');
}
else
{
if (record.Count > 0 || sb.Length > 0)
{
record.Add(sb.ToString());
sb.Clear();
}
if (record.Count > 0)
yield return record;
record = new List<string>(record.Count);
}
}
else if (sb.Length == 0 && !inQuote)
{
if (readChar == qualifier)
inQuote = true;
else if (readChar == delimiter)
{
record.Add(sb.ToString());
sb.Clear();
}
else if (char.IsWhiteSpace(readChar))
{
// Ignore leading whitespace
}
else
sb.Append(readChar);
}
else if (readChar == delimiter)
{
if (inQuote)
sb.Append(delimiter);
else
{
record.Add(sb.ToString());
sb.Clear();
}
}
else if (readChar == qualifier)
{
if (inQuote)
{
if ((char)reader.Peek() == qualifier)
{
reader.Read();
sb.Append(qualifier);
}
else
inQuote = false;
}
else
sb.Append(readChar);
}
else
sb.Append(readChar);
}
if (record.Count > 0 || sb.Length > 0)
record.Add(sb.ToString());
if (record.Count > 0)
yield return record;
}
}
}