forked from braph/outlook2auerswald
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutlook2auerswald.cs
414 lines (343 loc) · 10.2 KB
/
outlook2auerswald.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* outlook2auerswald - convert exported outlook contacts to auerswald format
* Copyright (C) 2015 Benjamin Abendroth
*
* This program 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.
*
* This program 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.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class CSVRow : List<string>
{
}
class CSVFile : List<CSVRow>
{
private Dictionary<string, int> clIndex;
public void BuildIndex()
{
clIndex = new Dictionary<string, int>();
if (Count == 0)
throw new Exception("Kopfzeile nicht vorhanden");
for (int i = 0; i < this[0].Count; ++i)
clIndex.Add(this[0][i], i);
}
public string TryGetField(int row, string name)
{
try
{
return GetField(row, name);
}
catch (Exception)
{
return "";
}
}
public string GetField(int row, string name)
{
if (clIndex == null)
BuildIndex();
if (row >= Count)
throw new Exception("Zugriff auf nicht vorhandene Zeile: " + row);
try
{
return this[row][clIndex[name]];
}
catch (Exception)
{
throw new Exception("Zugriff auf nicht vorhandene Spalte: " + name);
}
}
public void SetField(int row, string name, string value)
{
GetField(row, name);
this[row][clIndex[name]] = value;
}
}
class CSVStreamReader : StreamReader
{
private int clDelimiter;
private int clEnclosure;
public CSVStreamReader(string file, Encoding enc, char delimiter, char enclosure) : base(file, enc)
{
clDelimiter = (int) delimiter;
clEnclosure = (int) enclosure;
}
public CSVRow ReadRow()
{
CSVRow row = new CSVRow();
int c;
string s = "";
bool inEnclosure = false;
while ((c = Read()) != -1)
{
if (inEnclosure)
{
if (c == clEnclosure)
{
inEnclosure = false;
}
else
{
s += (char) c;
}
}
else
{
if (c == clEnclosure)
{
inEnclosure = true;
}
else if (c == clDelimiter)
{
row.Add(s);
s = "";
}
else if (c == (int) '\n')
{
break;
}
}
}
if (inEnclosure)
throw new Exception("Unerwartetes Datei-Ende");
row.Add(s);
return row;
}
}
static class CSVFileReader
{
public static CSVFile Open(string file, Encoding enc, char delimiter, char enclosure)
{
CSVFile csvObj = new CSVFile();
CSVStreamReader csvsr = new CSVStreamReader(file, enc, delimiter, enclosure);
while (! csvsr.EndOfStream)
csvObj.Add(csvsr.ReadRow());
return csvObj;
}
}
class CSVFileWriter : StreamWriter
{
private char clDelimiter;
private char clEnclosure;
public CSVFileWriter(string file, Encoding enc, char delimiter, char enclosure) : base(file, false, enc)
{
clDelimiter = delimiter;
clEnclosure = enclosure;
}
private void WriteField(string field)
{
if (field == string.Empty)
return;
Write(clEnclosure);
foreach (char c in field)
{
if (c == clEnclosure)
Write('\\');
Write(c);
}
Write(clEnclosure);
}
private void WriteRow(CSVRow row)
{
if (row.Count == 0)
return;
WriteField(row[0]);
for (int i = 1; i < row.Count; ++i)
{
Write(clDelimiter);
WriteField(row[i]);
}
}
public void WriteCsv(CSVFile csvObj)
{
if (csvObj.Count == 0)
return;
WriteRow(csvObj[0]);
for (int i = 1; i < csvObj.Count; ++i)
{
Write('\n');
WriteRow(csvObj[i]);
}
Flush();
}
}
class CSVFileConverter
{
/*
{ "<Ausgabespalte>", "<Eingabespalte>" }
Wenn Eingabespaltenname leer ("") ist, wird die Spalte leer gelassen oder mit einer
speziellen Funktion befuellt.
*/
private static Dictionary<string, string> clConfig = new Dictionary<string, string>()
{
{ "Anrede", "Anrede" },
{ "Vorname", "Vorname" },
{ "Nachname", "Nachname" },
{ "Displayname", "" }, // = Vorname+Nachname
{ "Firma", "Firma" },
{ "Abteilung", "Abteilung" },
{ "Position", "Position" },
{ "Geburtstag", "Geburtstag" },
{ "PhotoId", "" },
{ "Kurzwahlnummer", ""},
{ "1. Typ Telefonnummer", "" },
{ "1. Telefonnummer", "" },
{ "2. Typ Telefonnummer", "" },
{ "2. Telefonnummer", "" },
{ "3. Typ Telefonnummer", "" },
{ "3. Telefonnummer", "" },
{ "4. Typ Telefonnummer", "" },
{ "4. Telefonnummer", "" },
{ "1. Typ von Emailadresse", "" },
{ "1. Emailadresse", "" },
{ "2. Typ von Emailadresse", "" },
{ "2. Emailadresse", "" },
{ "3. Typ von Emailadresse", "" },
{ "3. Emailadresse", "" },
{ "1. Typ Internetadresse", "" },
{ "1. Internetadresse", "" },
{ "2. Typ Internetadresse", "" },
{ "2. Internetadresse", "" },
{ "3. Typ Internetadresse", "" },
{ "3. Internetadresse", "" },
{ "1. Typ Adresse", "" },
{ "1. Strasse", "" },
{ "1. Postleitzahl", "" },
{ "1. Ort", "" },
{ "1. Staat", "" },
{ "2. Typ Adresse", "" },
{ "2. Strasse", "" },
{ "2. Postleitzahl", "" },
{ "2. Ort", "" },
{ "2. Staat", "" },
{ "3. Typ Adresse", "" },
{ "3. Strasse", "" },
{ "3. Postleitzahl", "" },
{ "3. Ort", "" },
{ "3. Staat", "" },
{ "", "" } // Gott, warum auch immer...
};
/*
REGEX_EINGABESPALTE => AUSGABESPALTE
*/
private static Dictionary<Regex, string> clPhoneNumberMatching = new Dictionary<Regex, string>() {
{ new Regex(@"haupttelefon", RegexOptions.IgnoreCase), "Haupttelefon" },
{ new Regex(@"telefon.*privat.*", RegexOptions.IgnoreCase), "Privat" },
{ new Regex(@"telefon.*(gesch.ftlich|firma)", RegexOptions.IgnoreCase), "Geschäftlich" },
{ new Regex(@"mobiltelefon", RegexOptions.IgnoreCase), "Mobil" }
};
public static CSVFile Convert(CSVFile inCsv)
{
CSVFile outCsv = new CSVFile {new CSVRow()};
// Insert headline of outfile
foreach (KeyValuePair<string, string> kvp in clConfig)
{
outCsv[0].Add(kvp.Key);
}
// Dictionary<Spaltenindex Eingabedatei, Spaltenname Ausgabedatei>
Dictionary<int, String> clPhoneColumnMap = new Dictionary<int, String>();
for (int i = 0; i < inCsv[0].Count; ++i)
{
foreach (KeyValuePair<Regex, string> kvp in clPhoneNumberMatching)
{
if (kvp.Key.IsMatch(inCsv[0][i]))
{
clPhoneColumnMap.Add(i, kvp.Value);
}
}
}
for (int r = 1; r < inCsv.Count; ++r)
{
outCsv.Add(new CSVRow());
foreach (KeyValuePair<string, string> kvp in clConfig)
{
// leave column empty || fill it with function
if (kvp.Value == String.Empty)
{
if (kvp.Key == "Displayname")
{
outCsv[r].Add(inCsv.GetField(r, "Vorname") + " " + inCsv.GetField(r, "Nachname"));
}
else
outCsv[r].Add("");
}
// Simple copy of column
else
{
outCsv[r].Add(inCsv.GetField(r, kvp.Value));
}
}
// Phone-Number-Import
int iPhoneNumber = 1;
int maxPhoneNumber = 4;
string phoneNumber;
foreach (KeyValuePair<int, string> kvp in clPhoneColumnMap)
{
if ((phoneNumber = inCsv[r][kvp.Key]) == String.Empty)
continue;
outCsv.SetField(r, "" + iPhoneNumber + ". Typ Telefonnummer", kvp.Value);
outCsv.SetField(r, "" + iPhoneNumber + ". Telefonnummer", Regex.Replace(phoneNumber, "\\D", ""));
if (++iPhoneNumber > maxPhoneNumber)
break;
}
}
return outCsv;
}
}
class Converter
{
static void Main(string[] args)
{
string logFile = "log.txt";
// delimiter config
char inFileDelimiter = ',';
char inFileEnclosure = '"';
char outFileDelimiter = ';';
char outFileEnclosure = '"';
using (StreamWriter logWriter = File.AppendText(logFile))
{
try
{
if (args.Length == 0)
throw new Exception("Das Programm benoetigt mindestens eine Eingabe-Datei!");
foreach (string inFile in args)
{
CSVFile inCsv = CSVFileReader.Open(inFile,
Encoding.GetEncoding("iso-8859-1"),
inFileDelimiter,
inFileEnclosure
);
CSVFile outCsv = CSVFileConverter.Convert(inCsv);
string outFile = inFile + ".konvertiert.csv";
CSVFileWriter writer = new CSVFileWriter(outFile,
Encoding.GetEncoding("iso-8859-1"),
outFileDelimiter,
outFileEnclosure
);
writer.WriteCsv(outCsv);
}
}
catch (Exception e)
{
logWriter.WriteLine(e.ToString());
Console.WriteLine("Ein Fehler ist aufgetreten. Weitere Informationen befinden sich in der Log-Datei.\n\n> {0}\n", e.Message);
Console.WriteLine("Enter zum Beenden druecken...");
Console.ReadLine();
}
}
}
}