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
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace KeePass.DataExchange
{
internal delegate T CsvTableObjectFunc<T>(string[] vContextRow);
internal delegate void CsvTableObjectAction<T>(T t, string[] vContextRow);
internal delegate void CsvTableDataHandler<T>(string strData, T tContext,
string[] vContextRow);
internal abstract class CsvTableObjectReader<T>
{
// For all streams
private readonly CsvTableObjectFunc<T> m_fObjectNew;
private readonly CsvTableObjectAction<T> m_fObjectCommit;
private readonly Dictionary<string, CsvTableDataHandler<T>> m_dHandlers =
new Dictionary<string, CsvTableDataHandler<T>>();
// For the current stream
private readonly Dictionary<string, int> m_dColumns =
new Dictionary<string, int>();
public CsvTableObjectReader(CsvTableObjectFunc<T> fObjectNew,
CsvTableObjectAction<T> fObjectCommit)
{
m_fObjectNew = fObjectNew;
m_fObjectCommit = fObjectCommit;
}
public void SetDataHandler(string strColumn, CsvTableDataHandler<T> f)
{
if(strColumn == null) { Debug.Assert(false); return; }
m_dHandlers[strColumn] = f;
}
public void Read(CsvStreamReaderEx csr)
{
if(csr == null) { Debug.Assert(false); return; }
string[] vHeader;
while(true)
{
vHeader = csr.ReadLine();
if(vHeader == null) return;
if(vHeader.Length == 0) continue;
if((vHeader.Length == 1) && (vHeader[0].Length == 0)) continue;
break;
}
m_dColumns.Clear();
CsvTableDataHandler<T>[] vHandlers = new CsvTableDataHandler<T>[vHeader.Length];
for(int i = vHeader.Length - 1; i >= 0; --i)
{
string str = vHeader[i];
m_dColumns[str] = i;
CsvTableDataHandler<T> f;
m_dHandlers.TryGetValue(str, out f);
vHandlers[i] = f;
}
while(true)
{
string[] vRow = csr.ReadLine();
if(vRow == null) break;
if(vRow.Length == 0) continue;
if((vRow.Length == 1) && (vRow[0].Length == 0)) continue;
T t = ((m_fObjectNew != null) ? m_fObjectNew(vRow) : default(T));
Debug.Assert(vRow.Length == vHandlers.Length);
int m = Math.Min(vRow.Length, vHandlers.Length);
for(int i = 0; i < m; ++i)
{
CsvTableDataHandler<T> f = vHandlers[i];
if(f != null) f(vRow[i], t, vRow);
}
if(m_fObjectCommit != null) m_fObjectCommit(t, vRow);
}
}
public string GetData(string[] vRow, string strColumn, string strDefault)
{
if(vRow == null) { Debug.Assert(false); return strDefault; }
if(strColumn == null) { Debug.Assert(false); return strDefault; }
int i;
if(!m_dColumns.TryGetValue(strColumn, out i)) return strDefault;
return ((i < vRow.Length) ? vRow[i] : strDefault);
}
}
}
|