-
Notifications
You must be signed in to change notification settings - Fork 2
/
Converter.cs
198 lines (159 loc) · 4.68 KB
/
Converter.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (C) 2007 Philipp Garcia (phil@gotraxx.org)
*
* This file is part of SGFConvert.
*
* SGFConvert is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* HouseBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using GoTraxx;
namespace SGFConvert
{
class Converter : ErrorManagement
{
protected string InputFileName = string.Empty;
protected string OutputFileName = string.Empty;
protected int BoardSize = 0;
protected int DefaultBoardSize = 0;
protected bool RemoveWhiteSpace = false;
protected bool ConvertToSGFCoords = false;
protected bool ConvertToGoCoords = false;
protected bool Pretty = false;
public bool ParseOptions(string[] args)
{
foreach (string lArg in args)
{
string lOption = lArg;
string lParameter = string.Empty;
int lPos = lOption.IndexOf(':');
if (lPos > 0)
{
lParameter = lArg.Substring(lPos + 1);
lOption = lArg.Substring(0, lPos);
}
if ((lOption[0] != '-') && (lOption[0] != '/'))
return false;
lOption = lOption.Substring(1);
switch (lOption)
{
case "rws": RemoveWhiteSpace = true; break;
case "2s": ConvertToSGFCoords = true; break;
case "2g": ConvertToGoCoords = true; break;
case "pretty": Pretty = true; break;
case "o": OutputFileName = lParameter.Trim('"'); break;
case "i": InputFileName = lParameter.Trim('"'); break;
case "b": DefaultBoardSize = Convert.ToInt32(lParameter); break;
default: return false;
}
}
return true;
}
public bool Process()
{
BoardSize = DefaultBoardSize;
ReadFile lInput = new ReadFile();
if (string.IsNullOrEmpty(InputFileName))
lInput.OpenConsole();
else
lInput.OpenFile(InputFileName);
if (lInput.IsError())
return SetErrorMessage(lInput);
SGFTokens lTokens = SGFScanner.Tokenize(lInput);
if (lTokens.IsError())
return SetErrorMessage(lTokens);
if (RemoveWhiteSpace)
lTokens.RemoveAllWhiteSpace();
lTokens.ProcessProperties(ProcessProperty);
if (Pretty)
lTokens.Pretty();
SaveFile lOutput = new SaveFile();
try
{
if (string.IsNullOrEmpty(OutputFileName))
lOutput.Create();
else
lOutput.Create(OutputFileName);
if (lOutput.IsOk())
lTokens.ToFile(lOutput);
if (lOutput.IsError())
return SetErrorMessage(lOutput);
}
finally
{
lOutput.Close();
}
return true;
}
public void ProcessProperty(Property property)
{
property.Name = property.Name.Trim();
property.Value = property.Value.Trim();
if (property.Name == "SZ")
{
Int32.TryParse(property.Value, out BoardSize);
}
else
{
if (!ConvertToSGFCoords && !ConvertToGoCoords)
return;
SGFPropertyInfo lSGFPropertyInfo = SGFInfo.GetSGFPropertyInfo(property.Name);
if (lSGFPropertyInfo == null)
return;
if ((lSGFPropertyInfo.PropertyValueType != SGFPropertyValueType.Stone) &&
(lSGFPropertyInfo.PropertyValueType != SGFPropertyValueType.Point) &&
(lSGFPropertyInfo.PropertyValueType != SGFPropertyValueType.Move))
return;
string lValue = property.Value;
string lValue1 = lValue;
string lValue2 = string.Empty;
int lPos = lValue.IndexOf(':');
if (lPos > 0)
{
lValue1 = lValue.Substring(0, lPos - 1);
lValue2 = lValue.Substring(lPos + 1);
}
else if (lPos == 0)
{
lValue1 = string.Empty;
lValue2 = lValue1.Substring(1);
}
Coords lCoord = new Coords(lValue1, BoardSize);
if (ConvertToSGFCoords)
property.Value = lCoord.ToSGFCoords(BoardSize);
else
if (ConvertToGoCoords)
property.Value = lCoord.ToGoCoords();
if (lValue2 != string.Empty)
property.Value = property.Value + ":" + lValue2;
if (lSGFPropertyInfo.List)
{
for (int i = 0; i < property.AdditionalValues.Count; i++)
{
string lAddtValue = property.AdditionalValues[i];
lCoord = new Coords(lAddtValue, BoardSize);
if (ConvertToSGFCoords)
lAddtValue = lCoord.ToSGFCoords(BoardSize);
else
if (ConvertToGoCoords)
lAddtValue = lCoord.ToGoCoords();
property.AdditionalValues[i] = lAddtValue;
}
}
}
}
}
}