File: Translator.cpp

package info (click to toggle)
ponyprog 3.1.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,904 kB
  • sloc: cpp: 35,932; python: 981; sh: 565; xml: 67; makefile: 45; ansic: 38
file content (764 lines) | stat: -rw-r--r-- 16,097 bytes parent folder | download
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//=========================================================================//
//                                                                         //
//  PonyProg - Serial Device Programmer                                    //
//                                                                         //
//  Copyright (C) 1997-2025   Claudio Lanconelli                           //
//                                                                         //
//  https://github.com/lancos/ponyprog                                        //
//                                                                         //
//-------------------------------------------------------------------------//
//                                                                         //
// 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 version2 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 (see LICENSE);     if not, write to the         //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
//                                                                         //
//=========================================================================//

#include <QDir>
#include <QDebug>
#include <QTextStream>
#include <QMessageBox>
#include <QtCore>

// #include "MainWindow.h"
#include "Translator.h"


QVector<QString> cTranslator::translateTable;


QString cTranslator::engText[] =
{
	"english",

	//Fill this with your reference, if it's empty = ""it will be invisible
	"Translator Credits:",
	"",

	"Serial Device Programmer",
	"You can find last version of",
	"at the address:",

	"No Name",

	//Button captions
#ifdef Q_OS_WIN32
	"&OK",
	"&Cancel",
	"&Help",
	"&Probe",
#else
	"OK",
	"Cancel",
	"Help",
	"Probe",
#endif

	"Hardware Check",

	"Note",
	"Edit",


	//Dialog messages
	"Version",
	"Yes",
	"No",
	"Ok",
	"Test",
	"Failed",
	"About",
	"Page",
	"Unknown",

	"Buffer \"%s\" changed. Save it before closing?",
	"Buffer changed. Save it before opening?",
	"Nothing to save",
	"Nothing to load",
	"Nothing to print",
	"Nothing to write",
	"Nothing to verify",
	"Bus timing calibration.\nBe sure there are no application running other than ",
	"\n(the CPU and hard disk have to be idle)\nThe calibration may take a couple of seconds.\nDo you want to run calibration now?",
	"Calibration OK",
	"Calibration failed",
	"You need to run Calibration from the Options menu\nbefore any read/write operations",
	"You need to run Setup from the Options menu\nbefore any read/write operations",
	"Reading...",
	"Read successful\nDevice Size:",
	"Are you sure you want to write the device?\nAll previous content will be lost",
	"Writing...",
	"Verifying...",
	"Write successful",
	"Write failed",
	"Erasing...",
	"Erase successful",
	"Cannot verify, device missing or bad device",
	"Verify Failed",
	"Verify successful",
	"Program successful",
	"Program Failed",
	"Device reset",
	"Writing security bits...",
	"Security bits write failed",
	"Reading security bits...",
	"Security bits read failed",
	"Reading configuration bits...",
	"Configuration bits read failed",
	"Writing configuration bits...",
	"Configuration bits write failed",

	"Script error at line",
	"Bad command",
	"Bad argument",
	"Argument missing",
	"Do you want to continue?",

	"Buffer empty",
	"Operation not supported",
	"Insert parameters",
	"Bad parameters",
	"Accepting commands...",
	"Open device content file",
	"Open program (FLASH) content file",
	"Open data (EEPROM) content file",
	"File not found",
	"File save failed",
	"Save Device Content File As",
	"Save Program (FLASH) Content File As",
	"Save Data (EEPROM) Content File As",
	"Close last window: do you want to exit?",
	"Buffer changed. Save it before closing?",
	"Open script file",

	//Menu entries

	//Main Menu
	"&File",
	"&Edit",
	"&Device",
	"&Command",
	"Scri&pt",
	"&Utility",
	"&Setup",
	"&Options",
	"&?",

	//Menu File
	"&New Window",
	"&Open Device File...",
	"Open &Program (FLASH) File...",
	"Open &Data (EEPROM) File...",
	"&Save Device File",
	"Save Device File &As...",
	"Save Program (&FLASH) File As...",
	"Save Data (&EEPROM) File As...",
	"&Reload Files",
	"Prin&t...",
	"&Close",
	"E&xit",

	//Menu Edit
	"Edit &Note...",
	"Edit Bu&ffer enabled",

	//Menu Command
	"&Read All",
	"Read Program (FLASH)",
	"Read Data (EEPROM)",
	"Read &Security and Configuration Bits",
	"&Write All",
	"Write Program (&FLASH)",
	"Write Data (EEPROM)",
	"Wr&ite Security and Configuration Bits",
	"&Verify All",
	"Verify Program (FLASH)",
	"Verify Data (EEPROM)",
	"Verif&y Security and Configuration Bits",
	"&Erase",
	"&GetInfo",
	"Rese&t",
	"&Program",
	"Program Options...",

	//Menu Options
	"&Interface Setup...",
	"&Calibration",
	"&Remote Mode",

	//Menu Utility
	"&Clear Buffer",
	"&Fill Buffer...",
	"&Double Bank",
	"&Byte Swap",
	"Set Serial&Number",
	"SerialNumber C&onfig...",

	//Menu Help
	"&Help",
	"&About",

	//Menu Options
	"&Log File...",

	//Menu Script
	"&Edit...",
	"&Load and Run...",
	"&Auto Load",
	"&Run",

	//Tool Bar

	//Dialog Messages
	"Dev.Type",

	//Tool tips
	"Open Device File",
	"Save Device File",
	"Print Buffer",
	"Reload Files",
	"Read Device",
	"Read Program Memory (FLASH)",
	"Read Data Memory (EEPROM)",
	"Read Security and Configuration Bits",
	"Write Device",
	"Write Program Memory (FLASH)",
	"Write Data Memory (EEPROM)",
	//      STR_TTWRITESEC  = "Write Security and Configuration Bits"
	"Verify Program Memory (FLASH)",
	"Verify Data Memory (EEPROM)",
	"Verify Security and Configuration Bits",

	"Select device family",
	"Select device type",

	"Edit device notes",


	//Error Messages
	"Device Not Responding",
	"Bad device, select the correct type",
	"Device missing or unknown device",
	"Unable to probe, bad device or device locked",
	"User abort",
	"Communication port not available",
	"I/O access denied. You must be root to perform I/O",
	"Programmer board not found",
	"Hardware error, is the board properly connected?",
	"Bus busy or hardware error",
	"Missing Acknowledge from the device",
	"Missing device",
	"Timeout on the bus",
	"I2CBus Stop condition error (bad timing?)",
	"Write error",
	"Blank check error, need to erase",
	"Error n.",
	"Script error",


	//----Setup dialog

	//Dialog title
	"I/O port setup",
	"Interface board Setup",

	//Dialog messages
	"COM/LPT interface",
	"Serial",
	"Parallel",
	"Interface select",
	"Interface type",
	"COM Port select",
#ifdef Q_OS_WIN32
	"COM",
	// STR_LBLCOM2             = "COM2",
	// STR_LBLCOM3             = "COM3",
	// STR_LBLCOM4             = "COM4",
#else
	"ttyS",
	// STR_LBLCOM2             = "ttyS1",
	// STR_LBLCOM3             = "ttyS2",
	// STR_LBLCOM4             = "ttyS3",
#endif
	"USB Port select",
#ifdef Q_OS_WIN32
	"USB",
	// STR_LBLUSB2             = "USB2",
	// STR_LBLUSB3             = "USB3",
	// STR_LBLUSB4             = "USB4",
	// STR_LBLUSB5             = "USB5",
	// STR_LBLUSB6             = "USB6",
	// STR_LBLUSB7             = "USB7",
	// STR_LBLUSB8             = "USB8",
#else
	"ttyUSB",
	// STR_LBLUSB2             = "ttyS1",
	// STR_LBLUSB3             = "ttyS2",
	// STR_LBLUSB4             = "ttyS3",
	// STR_LBLUSB5             = "ttyS4",
	// STR_LBLUSB6             = "ttyS5",
	// STR_LBLUSB7             = "ttyS6",
	// STR_LBLUSB8             = "ttyS7",
#endif
	"LPT Port select",
	"LPT",
	// STR_LBLLPT2             = "LPT2",
	// STR_LBLLPT3             = "LPT3",
	// STR_LBLLPT4             = "LPT4",
	"Select Polarity of the Control lines",
	"Invert Reset",
	"Invert SCKL",
	"Invert D-IN",
	"Invert D-OUT",

	//Tool tips

	"Select serial port COM1",
	// STR_TTCOM2              = "Select serial port COM2",
	// STR_TTCOM3              = "Select serial port COM3",
	// STR_TTCOM4              = "Select serial port COM4",
	"Select serial port USB1",
	//  STR_TTUSB2              = "Select serial port USB2",
	//  STR_TTUSB3              = "Select serial port USB3",
	//  STR_TTUSB4              = "Select serial port USB4",
	//  STR_TTUSB5              = "Select serial port USB5",
	//  STR_TTUSB6              = "Select serial port USB6",
	//  STR_TTUSB7              = "Select serial port USB7",
	//  STR_TTUSB8              = "Select serial port USB8",
	"Select parallel port LPT1",
	// STR_TTLPT2              = "Select parallel port LPT2",
	// STR_TTLPT3              = "Select parallel port LPT3",


	//----Fill dialog

	//Dialog title
	"Fill Buffer",

	//Dialog messages: NB should be of the same size (number of char)
	"From",
	"To",
	"Value",



	//----Fuse dialog

	//Button captions
#ifdef Q_OS_WIN32
	"C&lear All",
	"&Set All",
	"&Write",
	"&Read",
#else
	"Clear All",
	"Set All",
	"Write",
	"Read",
#endif

	//Dialog title
	"Configuration and Security bits",

	//Dialog messages: NB should be of the same size (number of char)
	"First Block to Lock",
	"N.of Blocks to Lock",
	"High endurance block",

	//Tool tips
	"All bits to '0'",
	"All bits to '1'",
	"Write bits to the device",
	"Read bits from the device",


	//----Info dialog

	//Button captions
#ifdef Q_OS_WIN32
	"&Close",
#else
	"Close",
#endif

	//Dialog title
	"Device Info",

	//Dialog messages
	"Bank roll-over capability:",
	"Size:",
	"CRC:",
	"Security block:",
	"High endurance block:",


	//----Char Edit dialog

	//Dialog title
	"Edit Buffer",

	//Dialog messages: NB should be of the same size (number of char)
	"Hex",
	"Decimal",
	"Char",

	"Insert the new value",
	"Enter text from the address",

	//Tool tips
	"Hexadecimal value",
	"Decimal value",
	"Char value",


	//----Program Option dialog

	//Dialog title
	"Program Options",

	//Dialog messages
	"Reload Files",
	"Read Program memory (FLASH)",
	"Read Data memory (EEPROM)",
	"Read Seriial Number",
	"Byte Swap",
	"Set Serial Number",
	"Erase",
	"Write Program memory (FLASH)",
	"Write Data memory (EEPROM)",
	"Write Security and Config bits",
	"Verify Security and Config bits",



	//----Serial Number dialog

	//Dialog title
	"Serial Number Config",

	//Dialog messages: NB should be of the same size (number of char)
	"Address",
	"Size",
	"Value",
	"Format",

	//next 2 probably can't be translated
	"Little endian",
	"Big endian",

	"Data memory offset",

	//Tool tips

	"Memory address",
	"Size in bytes",
	"Next value to be used",
	"Assume the Address relative to Data memory instead of Program memory",
	"Most significant Byte at high address",
	"Most significant Byte at low address",


	//----Retry dialog

	//Button captions
#ifdef Q_OS_WIN32
	"&Abort",
	"&Retry",
	"&Ignore",
#else
	"Abort",
	"Retry",
	"Ignore",
#endif

	//Dialog title
	"Alert",


	//----Notes dialog

	//Dialog title
	"Edit Device Notes",


	//----Progress dialog

	//Dialog title
	"Status",


	//----Hardware check dialog
	"Hardware Check",


	//----About dialog
	"Disable sound",

	"Don't show this window on startup",


	//---- Oscillator Calibration Byte
	"Oscillator Calibration Byte",

	"Read Osc.Calibration Byte",
	"Osc. Calibration Options...",
	"Read Osc.Calibration Byte successful",

	"Chip Id",
	"Note",
	"EEPROM Size",
	"FLASH Size",

	"Auto Increment",
	"Increment the serial number after every 'set serial number' operation",

	"Can't know bank roll-over without modify and write, do you want to write?",
	"Buffer changed. Save it before reading?",

	"Open Program Memory (FLASH) File",
	"Open Data Memory (EEPROM) File",
	"Save Program Memory (FLASH) File",
	"Save Data Memory (EEPROM) File",
	"New Window",
	"Launch program cycle",
	"Execute script",
	"Open setup dialog",
	"Erase all the device to FF",

	"Security and Configuration &Bits...",
	"Security and Configuration Bits",

	"Refer to device datasheet, please",
	"Checked items means programmed",
	"UnChecked items means unprogrammed",
	"Save the result in the buffer at given location",

	"Preferences",				//STR_PREFERENCES
	"Be sure the VTarget is on (check J3) and NO target attached for the test",					//STR_EXTRAPROBEMSG_FTPROG
	"Be sure the VTarget is on (check P1) and NO target attached for the test"					//STR_EXTRAPROBEMSG_EGGLINK
};


bool cTranslator::loadTranslation(const QString fname)
{
	QFile langFile(fname);
	short vecSize = (short)(sizeof(engText) / sizeof(char *));
	translateTable.clear();
	translateTable.resize(vecSize + 1);

	if (!langFile.open(QIODevice::ReadOnly))
	{
		return false;
	}

	QTextStream stream(&langFile);
	stream.setCodec("UTF-8");

	QString llEng = stream.readLine();

	if (llEng.length() > 0)
	{
		llEng.remove("\n");
	}

	if (llEng == "LANGUAGE_NAME")
	{
		translateTable << llEng;
	}
	else
	{
		QMessageBox::warning(NULL, "File error", "File is not a language file!", QMessageBox::Close);

		langFile.close();
		return (false);
	}

	bool begTranslation = false;

	while (!stream.atEnd())
	{
		llEng = stream.readLine();

		if (llEng == "LANGUAGE_CHARSET")
		{
			llEng = stream.readLine();
			stream.setCodec(llEng.toLatin1());
			continue;
		}

		QString tmpText = convertString(llEng);

		// when at end of string
		if (tmpText.lastIndexOf("\\") >= tmpText.length() - 2)
		{
			llEng += tmpText;
			//             qDebug() << tmpText;
			continue;
		}
		else
		{
			llEng = tmpText;
		}

		if (llEng.indexOf(QRegExp("t[0-9]+=")) == 0)
		{
			QString str;
			int pos = llEng.indexOf("=");

			if (pos > 0)
			{
				str = llEng.mid(pos + 1);
				//
				int num = llEng.mid(1, pos - 1).toShort();

				//                     qDebug() << num << str;
				if (num >= vecSize)
				{
					qDebug() << "size too big";
				}
				else
				{
					translateTable[num] = str;
				}
			}
		}
		else
		{
			if (llEng == "Ok")   // translation part beginns with "Ok"
			{
				begTranslation = true;
			}

			if (begTranslation == false)
			{
				continue;
			}

			bool found = false;
			QString llTranslate = stream.readLine();

			if (llTranslate.length() == 0)
			{
				continue;
			}

			llTranslate = convertString(llTranslate);

			for (int i = 0; i < vecSize; i++)  // suche
			{
				QString tmpEng = engText[i];

				if (tmpEng.indexOf("%HOME%") >= 0)
				{
					tmpEng.replace("%HOME%", QDir::homePath());
				}

				if (tmpEng == llEng)
				{
					translateTable[i] = llTranslate;
					found = true;
					break;
				}
			}

			if (found == false)
			{
				qDebug() << "no translation found" << llEng;
			}

		}
	}

	langFile.close();

	return (true);
}


QString cTranslator::convertString(const QString &s)
{
	QString res = s;

	if (res.length() > 0)
	{
		res.replace("\\r\\n", "\n");
		res.replace("\\n", "\n");
		res.replace("\\t", "\t");
		res.replace("\\\\", "/");
		res.replace("&amp;", "&");
		res.replace("&gt;", ">");
		res.replace("&lt;", "<");
	}

	if (res.length() == 0)
	{
		return res;
	}

	if (res.indexOf("C:/") >= 0)
	{
		res.replace("C:/", "%HOME%/");
	}

	if (res.indexOf("c:/") >= 0)
	{
		res.replace("c:/", "%HOME%/");
	}

	if (res.indexOf("%HOME%") >= 0)
	{
		res.replace("%HOME%", QDir::homePath());
	}

	return res;
}


QString cTranslator::translate(int id)
{
	QString m;

	if (id < 0)
	{
		return "";
	}

	if (id >= (int)(sizeof(engText) / sizeof(char *)))
	{
		return "";
	}

	if (translateTable.size() == 0)
	{
		return QString(engText[id]);
	}

	//     if (id < (int)(sizeof(engText) / sizeof(char*))) {
	if (translateTable[id].length() == 0)
	{
		m = QString(engText[id]);
	}
	else
	{
		m = translateTable[id];
	}

	if (m.indexOf("%HOME%") >= 0)
	{
		m.replace("%HOME%", QDir::homePath());
	}

	//     }

	return m;
}