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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
| // ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "TDataBase.h"
#include "FileCtrl.hpp"
#include <comobj.hpp>
#include "IniFiles.hpp"
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <msxmldom.hpp>
#include <XMLDoc.hpp>
#include <xmldom.hpp>
#include <XMLIntf.hpp>
#include <ComCtrls.hpp>
#include <Dialogs.hpp>
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TStringList * DFM_ERRORS = new TStringList;
TStringList * FILE_NAMES_DFM = new TStringList;
TStringList * FILE_NAMES_PAS = new TStringList;
TStringList * OUT_DFM = new TStringList;
TStringList * OUT_PAS = new TStringList;
TStringList * ADD_S = new TStringList;
TMemoryStream * MS = new TMemoryStream;
TIniFile * settings;
bool record_sub = false;
bool add_string = false;
bool have_add = false;
bool base_connect = false;
int row_count;
TDataBase data;
AnsiString FileName = ExtractFilePath(Application->ExeName) + "options.ini";
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
}
// ---------------------------------------------------------------------------
// -----------------Реализация класса TDataBase-------------------------------
// -----------------Конструктор по умолчанию----------------------------------
TDataBase::TDataBase() {
base = new TADOConnection(Form1);
}
// -----------------Функция подключения к серверу-----------------------------
void TDataBase::set_connect(String ServerName, String BaseName) {
base->Connected = false;
base->ConnectionString =
"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" +
BaseName + ";Data Source=" + ServerName + "";
try {
base->Connected = true;
sql = new TADOQuery(Form1);
ShowMessage("Успешно");
base_connect = true;
settings = new TIniFile(FileName);
settings->WriteString("database", "base_name", Form1->base_name->Text);
settings->WriteString("database", "server_name",
Form1->server_name->Text);
settings->UpdateFile();
delete settings;
}
catch (const Exception & e) {
ShowMessage(e.Message);
}
}
// -----------------Функция подключения к серверу-----------------------------
void TDataBase::set_connect(String ServerName, String BaseName, String UserName,
String UserPassword) {
base->ConnectionString =
"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + UserName +
";Initial Catalog=" + BaseName + ";Data Source=" + ServerName + "";
try {
base->Connected = true;
sql = new TADOQuery(Form1);
ShowMessage("Успешно");
base_connect = true;
settings = new TIniFile(FileName);
settings->WriteString("database", "base_name", Form1->base_name->Text);
settings->WriteString("database", "server_name",
Form1->server_name->Text);
settings->UpdateFile();
delete settings;
}
catch (const Exception & e) {
ShowMessage(e.Message);
}
}
// -----------------Функция посылки файла на сервер---------------------------
void TDataBase::send_file(TMemoryStream * MS) {
sql->Connection = base;
sql->SQL->Clear();
sql->SQL->Text = Form1->SQL_text->Text;
sql->Parameters->Clear();
sql->Parameters->CreateParameter(":file", ftVarBytes, pdInput, 0, "null");
sql->Parameters->ParamByName(":file")->LoadFromStream(MS, ftVarBytes);
try {
sql->ExecSQL();
}
catch (const Exception & e) {
ShowMessage(e.Message);
}
}
// ---------------------------------------------------------------------------
// ------------------Загрузка конфигураций приложения-------------------------
void load_app() {
TStringList * INI_STRING = new TStringList;
settings = new TIniFile(FileName);
Form1->SQL_text->Text = settings->ReadString("database", "sql_text", "");
Form1->base_name->Text = settings->ReadString("database", "base_name", "");
Form1->server_name->Text = settings->ReadString("database",
"server_name", "");
settings->ReadSection("string", INI_STRING);
for (int i = 0; i < INI_STRING->Count; i++) {
Form1->ListBox1->Items->Add(settings->ReadString("string",
INI_STRING->Strings[i], ""));
}
delete INI_STRING;
delete settings;
}
// ------------------Выбор директории-----------------------------------------
WideString select_dir() {
String initfolder = "Desktop", selected_folder;
SelectDirectory("Выберите папку", "Desktop", selected_folder,
TSelectDirExtOpts() << sdNewUI << sdValidateDir << sdShowEdit <<
sdNewFolder, 0);
WideString myPath = selected_folder + "\\";
return myPath;
}
// --------------------Формирование xml файла---------------------------------
void xml_file(TStringList * OUT_SL, String save_path, String file_name) {
TXMLDocument * XMLFile = new TXMLDocument(NULL);
try {
XMLFile->Active = true;
XMLFile->Encoding = "UTF-16";
XMLFile->Options = XMLFile->Options << doNodeAutoIndent;
_di_IXMLNode nodElement = XMLFile->CreateElement(L"translate", L"");
XMLFile->ChildNodes->Add(nodElement);
for (int i = 0; i < OUT_SL->Count; i++) {
IXMLNode *nodNew = XMLFile->ChildNodes->Last()->AddChild(L"string");
nodNew->SetAttribute("id", IntToStr(i + 1));
nodNew->SetAttribute("text", OUT_SL->Strings[i]);
}
XMLFile->SaveToFile(save_path + file_name + ".xml");
}
catch (const Exception & e) {
ShowMessage(e.Message);
}
//__finally {
delete XMLFile;
// }
}
// ------------------------Поиск нужных фалов---------------------------------
void FindFiles(TStringList * FILE_NAMES_DFM, TStringList * FILE_NAMES_PAS,
String start_path, TCheckBox * dfm, TCheckBox * pas) {
TSearchRec sr;
if (start_path.Length()) {
if (!FindFirst(start_path + "\\*.*", faAnyFile, sr))
do {
if (!(sr.Name == "." || sr.Name == ".."))
if (((sr.Attr & faDirectory) == faDirectory) ||
(sr.Attr == faDirectory)) {
FindFiles(FILE_NAMES_DFM, FILE_NAMES_PAS,
start_path + "\\" + sr.Name, dfm, pas);
}
else {
AnsiString Ext = ExtractFileExt(sr.Name).UpperCase();
if (Ext == ".DFM") {
if (dfm->Checked) {
FILE_NAMES_DFM->Add
(start_path + "\\" + sr.Name);
}
}
else if (Ext == ".PAS") {
if (pas->Checked) {
FILE_NAMES_PAS->Add
(start_path + "\\" + sr.Name);
}
}
}
}
while (!FindNext(sr));
FindClose(sr);
}
}
// -----------------------Проверка строки-------------------------------------
bool validation_string(String STRING) {
if ((STRING.Pos("{")) == 0 || (STRING.Pos("}")) == 0) {
if (STRING.Pos("//") == 0) {
if (STRING.Pos("'") != 0) {
if (!STRING.IsEmpty()) {
return true;
}
}
}
}
return false;
}
// ------------------Проверка на исключения исключения------------------------
bool exception_str(String STRING, TListBox * a) {
for (int i = 0; i < a->Items->Count; i++) {
if (STRING.Pos(a->Items->Strings[i])) {
return true;
}
}
return false;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) {
FILE_NAMES_DFM->Clear();
FILE_NAMES_PAS->Clear();
String myPath = select_dir();
dir_path->Text = myPath;
save_path->Text = myPath;
FindFiles(FILE_NAMES_DFM, FILE_NAMES_PAS, myPath, dfm_files, pas_files);
file_l->Caption = "Всего файлов: " +
IntToStr(FILE_NAMES_DFM->Count + FILE_NAMES_PAS->Count);
file_p->Min = 0;
file_p->Max = FILE_NAMES_DFM->Count + FILE_NAMES_PAS->Count;
}
// ------------------------Функция анализа DFM файлов-------------------------
void analyze_dfm(TStringList * FILE_NAMES_DFM, TListBox * a,
TProgressBar * string_p, TProgressBar * file_p) {
TStringList * INTERMEDIATE_SL = new TStringList;
TStringList * IN_SL = new TStringList;
DFM_ERRORS->Clear();
bool new_word;
bool exception_string;
bool have_addition;
char Separator = '\'';
char Addition = '+';
AnsiString STRING = "";
String INTERMEDIATE_STRING = "";
for (int i = 0; i < FILE_NAMES_DFM->Count; i++) {
INTERMEDIATE_SL->Clear();
IN_SL->LoadFromFile(FILE_NAMES_DFM->Strings[i]);
string_p->Min = 0;
string_p->Max = IN_SL->Count;
for (int lines = 0; lines < IN_SL->Count; lines++) {
new_word = false;
have_addition = false;
exception_string = false;
if (exception_str(IN_SL->Strings[lines], a)) {
exception_string = true;
}
if (validation_string(IN_SL->Strings[lines])) {
STRING = IN_SL->Strings[lines];
int start_word = STRING.Pos(Separator);
int end_word = STRING.Length();
for (int symbol = start_word + 1; symbol < STRING.Length() + 1;
symbol++) {
if (STRING[symbol] == Separator) {
if (!new_word) {
end_word = symbol;
new_word = true;
have_addition = false;
}
else {
start_word = symbol;
new_word = false;
}
}
else if (STRING[symbol] == Addition) {
if (new_word && !have_addition) {
INTERMEDIATE_STRING =
INTERMEDIATE_STRING + STRING.SubString
(start_word + 1,
end_word - start_word - 1) + " ";
have_addition = true;
}
}
}
if (new_word && !have_addition) {
INTERMEDIATE_STRING =
INTERMEDIATE_STRING + STRING.SubString(start_word + 1,
end_word - start_word - 1) + " ";
if (!exception_string) {
OUT_DFM->Add(INTERMEDIATE_STRING);
INTERMEDIATE_SL->Add(INTERMEDIATE_STRING);
}
INTERMEDIATE_STRING = "";
}
}
string_p->Position = lines + 1;
row_count++;
}
file_p->Position = file_p->Position + 1;
if (INTERMEDIATE_SL->Text.IsEmpty()) {
DFM_ERRORS->Add(FILE_NAMES_DFM->Strings[i]);
}
}
delete IN_SL;
delete INTERMEDIATE_SL;
}
// ------------------------Функция анализа PAS файлов-------------------------
void analyze_pas(TStringList * FILE_NAMES_PAS, TListBox * a,
TProgressBar * string_p, TProgressBar * file_p) {
TStringList * IN_SL = new TStringList;
bool new_word;
bool exception_string = false;
bool have_addition;
char Separator = '\'';
char Addition = '+';
char End = ';';
AnsiString STRING = "";
String INTERMEDIATE_STRING = "";
for (int i = 0; i < FILE_NAMES_PAS->Count; i++) {
IN_SL->LoadFromFile(FILE_NAMES_PAS->Strings[i]);
string_p->Min = 0;
string_p->Max = IN_SL->Count;
for (int lines = 0; lines < IN_SL->Count; lines++) {
new_word = false;
have_addition = false;
if (exception_str(IN_SL->Strings[lines], a)) {
exception_string = true;
}
if (validation_string(IN_SL->Strings[lines])) {
STRING = IN_SL->Strings[lines];
int start_word = STRING.Pos(Separator);
int end_word = STRING.Length();
for (int symbol = start_word + 1; symbol < STRING.Length() + 1;
symbol++) {
if (STRING[symbol] == Separator) {
if (!new_word) {
end_word = symbol;
new_word = true;
have_addition = false;
}
else {
start_word = symbol;
new_word = false;
}
}
else if (STRING[symbol] == Addition) {
if (new_word && !have_addition) {
INTERMEDIATE_STRING =
INTERMEDIATE_STRING + STRING.SubString
(start_word + 1,
end_word - start_word - 1) + " ";
have_addition = true;
}
}
else if (STRING[symbol] == End) {
if (new_word) {
INTERMEDIATE_STRING =
INTERMEDIATE_STRING + STRING.SubString
(start_word + 1, end_word - start_word - 1);
if (!exception_string) {
OUT_PAS->Add(INTERMEDIATE_STRING);
}
have_addition = false;
new_word = false;
exception_string = false;
INTERMEDIATE_STRING = "";
}
}
}
}
string_p->Position = lines + 1;
row_count++;
}
file_p->Position = file_p->Position + 1;
}
delete IN_SL;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) {
String myPath = select_dir();
save_path->Text = myPath;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) {
delete DFM_ERRORS;
delete FILE_NAMES_DFM;
delete FILE_NAMES_PAS;
delete OUT_DFM;
delete OUT_PAS;
delete ADD_S;
data.~TDataBase();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender) {
ListBox1->Items->Add(exception_text->Text);
exception_text->Text = "";
settings = new TIniFile(FileName);
for (int i = 0; i < ListBox1->Items->Count; i++) {
settings->WriteString("string", "exception" + IntToStr(i),
ListBox1->Items->Strings[i]);
}
settings->UpdateFile();
delete settings;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender) {
settings = new TIniFile(FileName);
for (int i = 0; i < ListBox1->Items->Count; i++) {
settings->DeleteKey("string", "exception" + IntToStr(i));
}
ListBox1->DeleteSelected();
for (int i = 0; i < ListBox1->Items->Count; i++) {
settings->WriteString("string", "exception" + IntToStr(i),
ListBox1->Items->Strings[i]);
}
settings->UpdateFile();
delete settings;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender) {
if (RadioButton1->Checked) {
data.set_connect(server_name->Text, base_name->Text);
}
else {
data.set_connect(server_name->Text, base_name->Text, base_login->Text,
base_password->Text);
}
if (base_connect) {
CheckBox1->Enabled = true;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender) {
if (RadioButton2->Checked) {
base_login->Enabled = true;
base_password->Enabled = true;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender) {
if (RadioButton1->Checked) {
base_login->Enabled = false;
base_password->Enabled = false;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender) {
if (CheckBox1->Checked) {
if (SQL_text->Text.IsEmpty()) {
ShowMessage("Необходимо составить SQL запрос");
CheckBox1->Checked = false;
}
else if (SQL_text->Text.Pos(":file") == 0) {
ShowMessage("В SQL запросе не найден параметр :file");
CheckBox1->Checked = false;
}
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender) {
row_count = 0;
if (dfm_files->Checked) {
analyze_dfm(FILE_NAMES_DFM, ListBox1, string_p, file_p);
DFM_ERRORS->SaveToFile(save_path->Text + "dfm_errors.txt");
}
if (pas_files->Checked) {
analyze_pas(FILE_NAMES_PAS, ListBox1, string_p, file_p);
}
string_l->Caption = "Обработанно строк файлов: " + IntToStr(row_count);
TStringList * OUT_SL = new TStringList;
OUT_SL->Clear();
OUT_SL->AddStrings(OUT_DFM);
OUT_SL->AddStrings(OUT_PAS);
xml_file(OUT_SL, save_path->Text, out_xml_name->Text);
if (CheckBox1->Checked) {
MS->Clear();
OUT_SL->SaveToStream(MS);
data.send_file(MS);
settings = new TIniFile(FileName);
settings->WriteString("database", "sql_text ", Form1->SQL_text->Text);
settings->UpdateFile();
delete settings;
}
delete OUT_SL;
}
void __fastcall TForm1::FormCreate(TObject *Sender) {
load_app();
}
// --------------------------------------------------------------------------- |