This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.cpp
275 lines (255 loc) · 8.77 KB
/
import.cpp
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
/*
* exPIMP
*
* Please READ /License.txt FIRST!
*
* Copyright (C)2003,2004,2005 Rafa³ Lindemann, Stamina
*
* $Id: import.cpp 98 2005-06-16 16:18:58Z konnekt $
*/
#include "stdafx.h"
#include "../include/dtablebin.h"
#include "../include/time64.h"
#include "../include/func.h"
#include "../include/simxml.h"
#include "base64.h"
#include "konnekt/plug_export.h"
#include "konnekt/ui.h"
#include "konnekt/plug_func.h"
#include "expimp.h"
#include "konnekt/expimp.h"
cColumn * CheckColumn(int tableID , tColumns & Columns , CStdString id , CStdString name) {
// Okreœlamy ID
int colID = atoi(id);
if (id.empty() || colID & DT_COLID_UNIQUE) colID = DT_COLID_UNIQUE;
// porownujemy z tablicami...
if (!name.empty()) {
int _id = Ctrl->DTgetNameID(tableID , name);
if (_id==-1
//|| ((colID & DT_COLID_UNIQUE)!=(_id & DT_COLID_UNIQUE))
|| (colID!=DT_COLID_UNIQUE && colID != _id)
) {
return 0;
}
colID = _id;
} else if (colID == DT_COLID_UNIQUE)
return 0; // Skoro nie ma ID, musi byæ chocia¿ nazwa...
if (colID != DT_COLID_UNIQUE && colID != -1 && Ctrl->DTgetPos(tableID , colID)!=-1) {
cColumn & c = Columns[colID];
c.id = colID;
c.name = name;
return &c;
} else return 0;
}
struct cReplace {
CStdString pattern;
int limit;
};
void DTBImport(CStdString XMLfile) {
//
cXML XML;
if (!XML.loadFile(XMLfile))
throw "Nie mog³em wczytaæ pliku!";
IMLOG("- Importujê");
// Wczytujemy informacje
// W zasadzie jedynym istotnym parametrem jest rodzaj tabeli.
CStdString tmp = XML.getText("data/info/table");
if (tmp.empty())
throw "Importowane mog¹ byæ tylko tablice ustawieñ i kontaktów!";
int tableID = atoi(tmp);
if (tableID != DTCNT && tableID != DTCFG)
throw "Importowane mog¹ byæ tylko tablice ustawieñ i kontaktów!";
// wczytujemy deskryptor
CStdString info = "Import ";
info+= tableID==DTCNT ? "kontaktów" : "konfiguracji";
info+="\r\n Wygenerowany przez: " + XML.getText("data/info/owner");
info+="\r\n Dnia: " + XML.getText("data/info/date");
info+="\r\n " + XML.getText("data/info/comment");
info+="\r\nZaimportowaæ?";
if (!ICMessage(IMI_CONFIRM , (int)info.c_str())) return;
IMLOG("- Wczytujê deskryptor");
tColumns Columns;
{
cXML XML2;
XML2.loadSource(XML.getContent("data/descriptor").c_str());
CStdString rejected;
while (XML2.prepareNode("col" , true)) {
cColumn * col = CheckColumn(tableID , Columns , XML2.getAttrib("id") , XML2.getAttrib("name"));
if (!col || !col->use) goto col_rejected;
col->type = atoi(XML2.getAttrib("type").c_str());
col->key = atoi(XML2.getAttrib("key").c_str());
col->descriptive = atoi(XML2.getAttrib("desc").c_str());
col->required = atoi(XML2.getAttrib("required").c_str());
col->encoded = atoi(XML2.getAttrib("encoded").c_str());
if ((Ctrl->DTgetType(tableID , col->id) & DT_CT_TYPEMASK) != (col->type & DT_CT_TYPEMASK))
goto col_rejected; // Musz¹ zgadzaæ siê typy!
goto col_ok;
col_rejected:
if (col) {
if (!rejected.empty()) rejected+=", ";
if (col->name.empty()) {
char val [32];
itoa(col->id , val , 10);
rejected+=val;
} else
rejected+=col->name;
col->use = false;
}
col_ok:
XML2.next();
}
if (!rejected.empty()) {
rejected = "Poni¿sze kolumny nie zostan¹ wczytane: \r\n"
+ rejected
+ "\r\n - zmieni³eœ coœ w xml'u,\r\n - masz w³¹czone nie wszystkie wtyczki\r\n - wtyczki s¹ w innych wersjach.\r\n\r\nKontynuowaæ?"
;
if (!ICMessage(IMI_CONFIRM , (int) rejected.c_str())) return;
}
}// deskryptor
// Skoro deskryptor jest za³atwiony mo¿emy zabraæ siê za wczytywanie poszczególnych linijek...
IMLOG("- Wczyta³em %d kolumn" , Columns.size());
IMLOG("- Wczytujê dane");
XML.loadSource(XML.getContent("data/rows").c_str()); // nic wiêcej nie bêdzie ju¿ potrzebne...
cXML XMLr;
cPreg replacer (true);
while (XML.prepareNode("row" , true)) {
int rowID = atoi(XML.getAttrib("id").c_str());
XMLr.loadSource(XML.getContent().c_str());
// Wczytujemy wszystkie komórki...
map <int , CStdString> cells;
map <int , cReplace> regexp;
while (XMLr.prepareNode("cell" , true)) {
cColumn * col = CheckColumn(tableID , Columns , XMLr.getAttrib("id") , XMLr.getAttrib("name"));
if (col && col->use) {
cells[col->id]=XMLr.getText();
if (!XMLr.getAttrib("encoded").empty()) {
CStdString dec;
unsigned int length = cells[col->id].size();
decode64(cells[col->id], cells[col->id].size() , dec.GetBuffer(length) , &length);
dec.ReleaseBuffer(length);
cells[col->id] = dec;
}
if (!XMLr.getAttrib("replace").empty()) {
cReplace re;
re.pattern = XMLr.getAttrib("replace");
re.limit = atoi(XMLr.getAttrib("limit").c_str());
regexp[col->id] = re;
}
}
XMLr.next();
}
if (tableID == DTCFG)
rowID = 0;
else { // sprawdzamy któr¹ linijkê w³aœnie czytamy...
// S¹ max. trzy poziomy kluczy...
// -1 oznacza, ¿e któryœ nie trafi³, a 0 ¿e ¿aden nie zosta³ sprawdzony
rowID = -1;
int key [3] = {0,0,0};
bool keyValid [3];
/* if (tableID == DTCNT
&& cells.find(CNT_NET)!=cells.end()
&& cells[CNT_NET]=="-1") {
rowID = 0;
goto store_data;
}*/
// Przeszukujemy ca³¹ tablicê...
for (int i=0; i<Ctrl->DTgetCount(tableID); i++) {
keyValid[0]=keyValid[1]=keyValid[2]=true;
// Szukamy kluczy
int id = Ctrl->DTgetID(tableID , i);
for (tColumns::iterator ci = Columns.begin(); ci != Columns.end(); ci++) {
cColumn & col = ci->second;
if (col.key>0 && col.key<=3 && (key[col.key-1]==id || key[col.key-1]==0)) {
CStdString value = Ctrl->DTgetStr(tableID , id , col.id);
if (cells.find(col.id) == cells.end() || cells[col.id]=="" || cells[col.id]=="0" || value=="" || value=="0")
keyValid[col.key-1] = false;
key[col.key-1] = (( keyValid[col.key-1] && cells[col.id] == value)?id:-1);
}
}
// zerujemy stany kluczy...
for (int j=0;j<sizeof(key)/4;j++) if (key[j]==-1 || (j>0 && keyValid[j-1])) key[j]=0;
}
for (int j=0;j<sizeof(key)/4;j++)
if (key[j]) {rowID = key[j];break;} // Uda³o siê!
if (rowID == -1) { // Musimy dodaæ
if (tableID == DTCNT) {
rowID = ICMessage(IMC_CNT_ADD , 0 , 0);
}
}
} // != DTCFG
store_data:
if (rowID != -1 && Ctrl->DTgetPos(tableID , rowID)!=-1) {
for (map <int , CStdString>::iterator cell=cells.begin(); cell!=cells.end(); cell++) {
// wykonujemy zleconego replace'a
if (regexp.find(cell->first) != regexp.end()) {
cReplace & re = regexp[cell->first];
cell->second = replacer.replace(re.pattern , cell->second , Ctrl->DTgetStr(tableID , rowID , cell->first) , re.limit);
}
Ctrl->DTsetStr(tableID , rowID , cell->first , (char*)cell->second.c_str());
}
}
if (tableID == DTCNT) ICMessage(IMC_CNT_CHANGED , rowID);
XML.next();
}
if (tableID == DTCFG) ICMessage(IMC_CFG_CHANGED);
if (tableID == DTCFG)
ICMessage(IMC_SAVE_CFG);
else if (tableID == DTCFG)
ICMessage(IMC_SAVE_CNT);
IMLOG("- Gotowe");
}
void DoImport() {
CStdString fileName = "";
try {
OPENFILENAME of;
memset(&of , 0 , sizeof(of));
CStdString title = "Importowanie danych";
of.lpstrTitle = title;
of.hwndOwner = (HWND)ICMessage(IMI_GROUP_GETHANDLE , (int)&sUIAction(0 , IMIG_MAINWND));
of.lStructSize=sizeof(of);
of.lpstrFilter="Plik ExPImP\0*.expimp\0Plik XML\0*.xml\0";
of.lpstrFile=fileName.GetBuffer(MAX_PATH);
of.nMaxFile=MAX_PATH;
// of.nFilterIndex=1;
of.Flags=OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
of.lpstrDefExt="";
sMRU mru;
CStdString mru_result;
mru.name = "exPiMP_dir";
mru.flags = MRU_GET_USETEMP;
mru.buffer = 0;//mru_result.GetBuffer(255);
mru.buffSize = 255;
mru.count = 1;
Ctrl->IMessage(&sIMessage_MRU(IMC_MRU_GET , &mru));
//mru_result.ReleaseBuffer();
mru_result = mru.values[0];
of.lpstrInitialDir = mru_result;
if (!GetOpenFileName(&of)) {
/*IMLOG("GetOpenFileName error Code = %x" , CommDlgExtendedError());
throw "GetOpenFileName zwraca 0";*/
} else {
CStdString dir = of.lpstrFile;
if (dir.find('\\')!=-1) dir.erase(dir.find_last_of('\\'));
mru.current = dir;
Ctrl->IMessage(&sIMessage_MRU(IMC_MRU_SET , &mru));
ICMessage(IMC_RESTORECURDIR);
DTBImport(fileName);
}
} catch (char*e) {
CStdString msg = "Wyst¹pi³ b³¹d podczas importowania:\r\n\r\n";
msg+=e;
ICMessage(IMI_ERROR , (int)msg.c_str());
}
}
void DoImport(const CStdString fileName) {
ICMessage(IMC_RESTORECURDIR);
try {
DTBImport(fileName);
} catch (char*e) {
CStdString msg = "Wyst¹pi³ b³¹d podczas importowania ";
msg+=fileName;
msg+=":\r\n\r\n";
msg+=e;
ICMessage(IMI_ERROR , (int)msg.c_str());
}
}