-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverBase.cs
167 lines (165 loc) · 5.48 KB
/
ConverBase.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
using System.Text;
namespace TemplateConverTools
{
internal class ConverBase : IDisposable
{
public readonly BigEndianBinaryReader BigEndianReader;
public readonly Dictionary<int, string> _sharedIDDic = new();
private readonly MemoryStream data;
private readonly List<string> fileNameList = new();
private readonly UTF8Encoding utf8WithBom = new(true);
private readonly string savePath = "./csvTemp";
private const string tabName = "mydb_sharedStr_internaltbl";
private StreamWriter? writer;
private FileStream? fs;
public Action<string> onError;
public int SharedStrCount = 0;
public ConverBase(string filePath,string? savePath = null)
{
var templateFile = new FileInfo(filePath);
if (!templateFile.Exists)
{
throw new FileNotFoundException(filePath);
}
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
data = new MemoryStream(Utility.DecompressData(File.ReadAllBytes(templateFile.FullName)));
BigEndianReader = new BigEndianBinaryReader(data);
onError += Console.WriteLine;
if(savePath is not null)
{
this.savePath = savePath;
}
Directory.CreateDirectory(this.savePath);
}
public bool ReadFile()
{
var bytes = Utility.UTFConverBigEndianBytes(tabName);
BigEndianReader.ReadBytes(bytes.Length);
ReadSharedStringSize();
if (!ReadSharedStr())
{
return false;
}
while (true)
{
ReadFileName();
ReadTableSize(out int col, out byte row);
WriteTable(ref row, ref col);
writer?.Close();
writer?.Dispose();
fs?.Close();
fs?.Dispose();
if (data.Position == data.Length)
{
break;
}
}
return true;
}
private bool ReadSharedStringSize()
{
SharedStrCount = BigEndianReader.ReadInt32();
Special();
return true;
}
public virtual bool ReadSharedStr()
{
return true;
}
public virtual void Special()
{
}
private void ReadFileName()
{
fileNameList.Add(BigEndianReader.GetReadString());
fs = new FileStream($"{savePath}/{fileNameList[^1]}.csv", FileMode.Create, FileAccess.Write);
writer = new StreamWriter(fs, utf8WithBom);
}
private void ReadTableSize(out int col, out byte row)
{
col = BigEndianReader.ReadInt32();
row = BigEndianReader.ReadByte();
}
private void WriteTable(ref byte row, ref int col)
{
if (writer is null)
{
return;
}
string[] rowNameS = new string[row];
//write row
for (int i = 0; i < row; rowNameS[i] = BigEndianReader.GetReadString(), writer.Write(rowNameS[i], i++))
{
if (i > 0)
{
writer.Write(',');
}
}
//write col
for (int i = 0; i < col; i++)
{
writer.Write('\n');
var subLen = BigEndianReader.ReadInt16();
var endLen = data.Position + subLen;
for (int j = 0; j < rowNameS.Length; j++)
{
var spl = rowNameS[j].Split('|');
if (spl.Length>=2)
{
switch(spl[1])
{
case Utility.fType:
{
writer.Write(BigEndianReader.ReadFloat());
break;
}
case Utility.dType:
{
writer.Write(WriteDecimal());
break;
}
case Utility.sType:
default:
{
writer.Write(WriteString());
break;
}
}
}
else
{
writer.Write(WriteString());
}
if (data.Position >= endLen)
{
break;
}
writer.Write(',');
}
}
}
public virtual string WriteString()
{
if (BigEndianReader.ReadBoolean())
{
return _sharedIDDic[BigEndianReader.ReadInt32()];
}
else
{
return BigEndianReader.GetReadString();
}
}
public virtual string WriteDecimal()
{
return BigEndianReader.ReadInt32().ToString();
}
public void Dispose()
{
writer?.Close();
writer?.Dispose();
fs?.Close();
fs?.Dispose();
GC.SuppressFinalize(this);
}
}
}