-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLocalizeRCDlg.cpp
2009 lines (1750 loc) · 58 KB
/
LocalizeRCDlg.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
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// LocalizeRCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "IniEx.h"
#include "LocalizeRC.h"
#include "LocalizeRCDlg.h"
#include <math.h>
#include <boost/regex/mfc.hpp>
#define OLDFILEFORMAT -2
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//#define _MFC_VER 0x0420
int _LineCount( CString &srchBuf, int startPos = 0, int endPos = -1 )
{
// Purpose: Get count of text lines in a specified area of a string buffer
// Parameters:
// srchBuf (in) The input string buffer to search for lines. Must be null terminated.
// startPos (in) Character offset into buffer where search is to begin (default = 0)
// endPos (in) Character offset into buffer one beyond last character to search.
// Default (-1) searches entire length (up to a null terminator)
// Note: all character offsets are zero based
//
// Returns: number of text lines found in specified area of string buffer
//
int lfnd = startPos;
int lend = srchBuf.GetLength();
if ( endPos > -1 && endPos < lend )
lend = endPos;
//
int linecount = 0;
while ( lfnd < lend )
{
lfnd = srchBuf.Find( _T('\n'), lfnd ) + 1;
if ( lfnd < 0 || lfnd > lend )
break;
//
linecount++;
};
return linecount;
}
int _GetLinePos( CString &srchBuf, int startPos, int line )
{
// Get buffer offset in characters to the start of a specified line number
// Parameters:
// srchBuf (in) The input string buffer to search. Must be null terminated.
// startPos (in) Character offset into buffer to begin the search (0 based)
// line (in) The line number whose position is needed (0 based)
//
// Returns: Character offset into srchBuf to the start of the specified line, or -1
// if line is <0, line exceeds number of lines in the buffer, or startPos exceeds
// number characters in string buffer.
int lfnd = startPos; // Search begins at this offset
int lend = srchBuf.GetLength(); // Search ends at this offset
int iRet = -1;
if ( startPos > -1 && startPos < lend )
{
for ( int i = 0 ; i < line ; i++ )
{
lfnd = srchBuf.Find( _T('\n'), lfnd ) + 1;
if ( lfnd < 0 || lfnd > lend )
{
lfnd = -1; // Search failed
break;
}
}
iRet = lfnd;
}
return iRet;
}
int _GetLengthOfValue( CString &strbuf, int startpos )
{
// Purpose: return length of a number string including any leading spaces
// Parameters:
// strbuf (in) String buffer containing substring
// startpos (in) Starting character offset into string buffer to substring
//
// Returns: length of number including any leading spaces; returns 0 if not a number
//
int digits = 0;
int retlen = 0;
for ( int i = startpos; ; i++, retlen++ )
{
TCHAR ch = strbuf.GetAt(i);
if ( ch == _T(' ') && digits == 0 ) // Allow leading spaces
continue;
else if ( ch < _T('0') || ch > _T('9') ) // Done with number
break;
else // Count off a digit
digits++;
//
}
return retlen;
}
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
private:
CString m_strAbout;
public:
virtual BOOL OnInitDialog();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
, m_strAbout(_T(""))
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_ABOUT, m_strAbout);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_strAbout.LoadString( IDS_ABOUT );
UpdateData( false );
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
// CLocalizeRCDlg dialog
CLocalizeRCDlg::CLocalizeRCDlg(CString& strWorkspace, CWnd* pParent /*=NULL*/)
: CDialog(CLocalizeRCDlg::IDD, pParent)
, m_bCopy(FALSE)
, m_nObsoleteItems(0)
, m_strWorkspace(strWorkspace)
, m_strEdit(_T(""))
, m_strTextmode(_T(""))
, m_bNoSort(FALSE)
, m_strInputRC(_T(""))
, m_strLangINI(_T(""))
, m_strOutputRC(_T(""))
, m_strAbout(_T(""))
{
if(m_strWorkspace.IsEmpty())
{
// last used Workspace from registry
m_strWorkspace = AfxGetApp()->GetProfileString(SEC_LASTPROJECT, ENT_WORKSPACE);
}
}
void CLocalizeRCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Check(pDX, IDC_COPY, m_bCopy);
DDX_CBIndex(pDX, IDC_OBS_ITEMS, m_nObsoleteItems);
DDX_Text(pDX, IDC_WORKSPACE, m_strWorkspace);
DDX_Control(pDX, IDC_LANGUAGE, m_CtrlLanguage);
DDX_Text(pDX, IDC_EDIT, m_strEdit);
DDX_Text(pDX, IDC_TEXTMODE, m_strTextmode);
DDX_Check(pDX, IDC_NOSORT, m_bNoSort);
DDX_Text(pDX, IDC_INPUTRC, m_strInputRC);
DDX_Text(pDX, IDC_LANGINI, m_strLangINI);
DDX_Text(pDX, IDC_OUTPUTRC, m_strOutputRC);
DDX_Text(pDX, IDC_ABOUT, m_strAbout);
}
BEGIN_MESSAGE_MAP(CLocalizeRCDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_CREATEINI, OnBnClickedCreateini)
ON_BN_CLICKED(IDC_OPENINI, OnBnClickedOpenini)
ON_BN_CLICKED(IDC_CREATEOUTPUT, OnBnClickedCreateoutput)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_REVERSEINI, OnBnClickedReverseini)
ON_BN_CLICKED(IDC_CHNG_WORKSPACE, OnBnClickedChngWorkspace)
ON_CBN_SELCHANGE(IDC_LANGUAGE, OnCbnSelchangeLanguage)
ON_CBN_SELCHANGE(IDC_OBS_ITEMS, OnCbnSelchangeObsItems)
ON_BN_KILLFOCUS(IDC_COPY, OnBnKillfocusCopy)
ON_BN_KILLFOCUS(IDC_NOSORT, OnBnKillfocusNosort)
ON_BN_CLICKED(IDC_NEW_WORKSPACE, OnBnClickedNewWorkspace)
ON_BN_CLICKED(IDC_CHNG_INPUTRC, OnBnClickedChngInputrc)
ON_BN_CLICKED(IDC_CHNG_LANGINI, OnBnClickedChngLangini)
ON_BN_CLICKED(IDC_CHNG_OUTPUTRC, OnBnClickedChngOutputrc)
END_MESSAGE_MAP()
// CLocalizeRCDlg message handlers
BOOL CLocalizeRCDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// LoadIcon only loads the 32x32 icon, therefore use ::LoadImage for other sizes (16x16)
hLargeIcon = AfxGetApp()->LoadIcon ( IDR_MAINFRAME );
hSmallIcon = (HICON) ::LoadImage ( AfxGetResourceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR );
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(hLargeIcon, TRUE); // Set big icon
SetIcon(hSmallIcon, FALSE); // Set small icon
// display text mode (UNICODE/ANSI)
#ifdef UNICODE
m_strTextmode.LoadString( IDS_UNICODE );
#else
m_strTextmode.LoadString( IDS_ANSI );
#endif
m_strAbout.LoadString( IDS_ABOUT );
UpdateData(false);
// get installed languages
CFileFind Finder;
TCHAR szFilename[MAX_PATH];
GetModuleFileName( NULL, szFilename, MAX_PATH );
AddLanguage( &m_CtrlLanguage, _T("09"), LangID );
// the last 2 chars are the LanguageID in hexadecimal form
CString strSearch;
strSearch.Format( _T("%sLocalizeRC??.dll"), GetFolder(szFilename) );
BOOL bResult = Finder.FindFile( strSearch );
CString strLangCode;
while( bResult )
{
bResult = Finder.FindNextFile();
// extract 2-digit language code
strLangCode = Finder.GetFileTitle().Right(2);
AddLanguage( &m_CtrlLanguage, strLangCode, LangID );
}
Finder.Close();
// Load last workspace
LoadWorkspace(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLocalizeRCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLocalizeRCDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, hSmallIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLocalizeRCDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(hSmallIcon);
}
#define NUMKEYWORDS 8
LPCTSTR strKeyword[NUMKEYWORDS] =
{
_T("ACCELERATORS"),
_T("DIALOG"),
_T("DIALOGEX"),
_T("MENU"),
_T("MENUEX"),
_T("STRINGTABLE"),
_T("DLGINIT"),
_T("code_page")
};
// random string, that indicates an error
#define ERR_STR _T("asfdshkagzuwrthgadsfhgkh12385143258")
BOOL CLocalizeRCDlg::OpenInputRC(BOOL bShowError)
{
return OpenRCFile( m_strInputRC, newRCdata, bShowError );
}
void CLocalizeRCDlg::LogUserMessage( int strID )
{
CString msg;
msg.FormatMessage( strID );
LogUserMessage( msg );
}
void CLocalizeRCDlg::LogUserMessage( CString msg )
{
m_strEdit.Append( msg );
m_strEdit.Append( _T("\n") );
UpdateData( false );
}
BOOL CLocalizeRCDlg::OpenRCFile( CString filename, CString &strbuf, BOOL bShowError )
{
// The file from which to load the contents of rich edit control
CStdioFile* pFile = NULL;
if( filename.IsEmpty() )
{
if( bShowError )
LogUserMessage( IDS_ERR_FILENAMEEMPTY );
// clear contents in edit-control
strbuf = _T("");
UpdateData( false );
return false;
}
CStdioUnicodeFile::FILEENCODING encoding = CStdioUnicodeFile::GetFileEncoding( filename );
try
{
#ifdef UNICODE
switch(encoding)
{
case CStdioUnicodeFile::FILEENCODING_UTF16LE:
case CStdioUnicodeFile::FILEENCODING_UTF8:
pFile = new CStdioUnicodeFile(filename, CFile::modeRead | CFILEFLAG_UNICODEHELPER, encoding);
static_cast<CStdioUnicodeFile*>(pFile)->ReadBOM();
break;
default:
pFile = new CStdioUnicodeFile( filename, CFile::modeRead, CStdioUnicodeFile::FILEENCODING_ANSI );
}
#else
if( bIsUnicode )
{
CString strErr;
strErr.Format( IDS_UNICODEFILE, filename );
AfxMessageBox( strErr );
// clear contents in edit-control
strbuf = _T("");
UpdateData( false );
return false; // cancel
}
else
pFile = new CStdioUnicodeFile( filename, CFile::modeRead );
#endif
}
catch( CFileException* e )
{
TCHAR szCause[255];
e->GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
e->Delete();
// clear contents in edit-control
strbuf = _T("");
UpdateData( false );
return false; // cancel
}
// clear contents in edit-control
strbuf = _T("");
// fill edit-control with contents of the input RC-file
// Update dialog size/pos data from an existing output RC file, if any
CString strLine;
while( pFile->ReadString( strLine ) )
strbuf += strLine + _T("\r\n");
//
UpdateData( false );
pFile->Close();
delete pFile;
if ( bShowError ) // Report open status if ok to show it
{
CString msg;
msg.FormatMessage( IDS_LFILEOPENED, filename );
LogUserMessage( msg );
}
return true;
}
void CLocalizeRCDlg::RemoveNewRCFileRESItems( )
{
// Disable ICON, BITMAP, and CURSOR entries that reference the "Res" folder.
// NOTE: HTML entries are a special case - these are not disabled
//
// This cleanup is normally done only if the user has opted to NOT copy the Res
// folder, which may be the case for a language DLL since those resources can
// continue to be loaded from the main app.
//
LogUserMessage( IDS_LRESREMOVAL );
boost::tregex expr( _T("^\\w+\\h+(ICON|BITMAP|CURSOR)\\h+\"(?i)res.+\"$") );
boost::tregex_iterator m1(boost::make_regex_iterator(newRCdata, expr, boost::match_not_dot_newline)), mEnd;
int startpos = 0; // Starting position of current match
int processedcount = 0; // Total number of match hits processed
while ( m1 != mEnd ) // Iterate all matching entries and comment out each one
{
startpos = (*m1)[0].first - newRCdata;
CString chk = newRCdata.Mid( startpos, (*m1)[0].length() );
// Note that edits must not alter the length of the buffer since it will affect
// the active regex search iteration
// Comment out the line by overwriting first 2 characters with //
newRCdata.SetAt( startpos++, _T('/') );
newRCdata.SetAt( startpos, _T('/') );
m1++;
processedcount++;
} // endwhile: iterate all matching entries
CString msg;
msg.FormatMessage( IDS_LRESREMSTATS, processedcount );
LogUserMessage( msg );
}
void CLocalizeRCDlg::MergeOldRCFileDesignInfo( CString &oldRCdata )
{
// Preserve dialog designer info changes made to accommodate translation string size
// differences in a previously generated output RC file
// Parameters:
// oldRCdata (in) Reference containing buffered contents of existing RC data
// containing possible dialog layout updates (read only)
//
// Progress information is reported to the log output
CString msg; // For user messages
int secitmsfound = 0; // Items found by regex parser
int secitmsskipped = 0;
int secitmsupdated = 0;
// Pointers into old RC data
int designinfosecpos = -1; // Buffer character offset to start of DESIGNINFO section
int subsectionpos = 0; // Buffer character offset to current sub-section
CString linekey; // Key for current sub-section line entry
//
// Pointers into new RC data
int ndesigninfosecpos = -1; // Buffer character offset to start of DESIGNINFO section
int nsubsectionpos = 0; // Buffer character offset to matching sub-section
LogUserMessage( IDS_LMERGINGOLDRC2 );
// Find the design info section in the new RC data
ndesigninfosecpos = newRCdata.Find( _T("GUIDELINES DESIGNINFO"), 0);
if ( ndesigninfosecpos < 0 ) // The section doesn't exist in the new data (not expected)
return;
//
// Find the design info section in old RC data and parse it
boost::tregex expr( _T("^GUIDELINES DESIGNINFO\\s+BEGIN$((?:\\s+(.+?(?:, *DIALOG))\\s+(?:.+?)END$)+)") );
boost::tregex_iterator m1(boost::make_regex_iterator(oldRCdata, expr)), mEnd;
while ( m1 != mEnd ) // ToDo: Don't really need an iterator here since there's only one section
{
// Get design info section position in buffer and body text handy
designinfosecpos = (*m1)[1].first - oldRCdata; // Start pos in buffer of design info section
CString dinfobody = CString( (*m1)[1].first, (*m1)[1].length() ); // Body of design info section
//
// Parse section for sub-section blocks
boost::tregex expr2( _T("(?:\\s+(.+?(?:, *DIALOG))\\s+(?:.+?)END$)") );
boost::tregex_iterator m2(boost::make_regex_iterator(dinfobody, expr2)), mEnd2;
while ( m2 != mEnd2 ) // Iterate over all sub-sections in the design info section
{
secitmsfound = 0; // Reset items stats for this sub-section
secitmsskipped = 0;
secitmsupdated = 0;
// Get sub-section position in buffer, key name, and body text handy
subsectionpos = ((*m2)[0].first - dinfobody) + designinfosecpos; // Start pos in buffer of this sub-section
CString dinfoblk = CString( (*m2)[0].first, (*m2)[0].length() ); // Current sub-section text
CString dinfokey = CString( (*m2)[1].first, (*m2)[1].length() ); // Key for current sub-section
// Try to find corresponding sub-section in new data
nsubsectionpos = newRCdata.Find( dinfokey, ndesigninfosecpos );
if ( nsubsectionpos < 0 ) // Sub-section not found in new data
{
msg.FormatMessage( IDS_LDESIGNSECSKIPNF, dinfokey );
continue; // Nothing to update for this section...
}
//
// Parse sub-section for numeric margin values
boost::tregex expr3( _T("^(.*?(?:,| ))( *\\d+)") );
boost::tregex_iterator m3(boost::make_regex_iterator(dinfoblk, expr3, boost::match_not_dot_newline)), mEnd3;
while ( m3 != mEnd3 ) // Iterate over all numeric margin settings in the sub-section body
{
CString subentrykey = CString( (*m3)[1].first, (*m3)[1].length() ); // Current entry key
subentrykey = subentrykey.TrimLeft(); // Clean up the key
CString subentryval = CString( (*m3)[2].first, (*m3)[2].length() ); // Current entry value
// Try to find the corresponding entry in the new data
int nsubentrypos = newRCdata.Find( subentrykey, nsubsectionpos );
if ( nsubentrypos < 0 ) // Entry not found in the new RC data
{
secitmsskipped++;
continue;
}
//
// Update the entry value in new data from matching entry in old data
int nvalpos = newRCdata.Find( _T(","), nsubentrypos ) + 1; // Starting pos of number string
// int nvallen = newRCdata.Find( _T("\n"), nvalpos ) - nvalpos; // Length of number string
int nvallen = _GetLengthOfValue( newRCdata, nvalpos );
if ( nvalpos < 1 || nvallen < 1 ) // Safety check
continue;
//
CString chk = newRCdata.Mid( nvalpos, nvallen ); // Number string in new data
if ( chk != subentryval ) // Need to update
{
newRCdata.Delete( nvalpos, nvallen );
newRCdata.Insert( nvalpos, subentryval );
secitmsupdated++;
}
secitmsfound++;
m3++;
} // endwhile: iterate sub-section entries
m2++;
msg.FormatMessage( IDS_LDESIGNSECSTAT, dinfokey, secitmsfound, secitmsupdated, secitmsskipped );
LogUserMessage( msg );
} // endwhile: iterate sub-sections
m1++;
} // endwhile: iterate design info sections
}
void CLocalizeRCDlg::MergeOldRCFileDialogLayout( CString &oldRCdata )
{
// Preserve dialog layout changes made to accommodate translation string size
// differences in a previously generated output RC file
// Parameters:
// oldRCdata (in) Reference containing buffered contents of existing RC data
// containing possible dialog layout updates (read only)
//
// Progress information is reported to the log output
//
CString msg; // For user messages
int secitmsfound = 0; // Items found by regex parser
int secitmsskipped = 0;
int secitmsupdated = 0;
LogUserMessage( IDS_LMERGINGOLDRC );
boost::tregex dsetexpr( _T("^(.*?(?:,| ))( ?\\d+, *\\d+, *\\d+, *\\d+(, *\\d+)*)") );
boost::tregex_iterator m1(boost::make_regex_iterator(oldRCdata, dsetexpr, boost::match_not_dot_newline)), mEnd;
int nsecpos = -1; // Start position of a section key in new RC data
int nendpos = -1; // Ending position of a section key in new RC data
int nseclines = 0; // Number of lines of text in section in new RC data
int secpos = -1; // Start position of a section key in old RC data
int endpos = -1; // Ending position of a section key in old RC data
int seclines = 0; // Number of lines of text in section in old RC data
CString seckey; // Key for current section
int iDEX = -1;
// Find each size/pos data set, which is 4 or 5 numbers separated by commas, and replace
// corresponding data set (according to preceding key name) in new RC output.
while ( m1 != mEnd ) // Scan old RC data for size/pos data
{
// Get the item key and try to find it in the new RC output
// In this the "key" is everything to the left of the size/pos hit on the line
// which is not reliable due to text translations and static controls that lack a unqiue ID.
// Logic compensates for this by verifying the two files match up by section key (which are
// unique) and then using line offset within the matching section.
CString key = CString( (*m1)[1].first, (*m1)[1].length() );
CString val = CString( (*m1)[2].first, (*m1)[2].length() );
CString val5 = CString( (*m1)[3].first, (*m1)[3].length() );
// Skip any false hits that are not to be adjusted
// Known exmaples are FILEVERSION and PRODUCTVERSION
if ( key.Find(_T("FILEVERSION")) > -1 || key.Find(_T("PRODUCTVERSION")) > -1 ) // Skip false hits...
{
m1++;
continue;
}
//
if ( key.Find( _T(" DIALOG ")) > -1 || (iDEX=key.Find(_T(" DIALOGEX "))) > -1 ) // Key is a dialog section
{
if ( !seckey.IsEmpty() ) // Just finished a prior section - report stats
{
msg.FormatMessage( IDS_LDIALOGSECFOUND, seckey, secitmsfound, secitmsupdated, secitmsskipped );
LogUserMessage( msg );
}
seckey = key.Trim(); // Start of new section (trim spaces - seckey is used in log output)
secitmsfound = 0;
secitmsskipped = 0;
secitmsupdated = 0;
nendpos = -1;
nseclines = 0;
// Try to find the matching dialog section in the new RC output data
nsecpos = newRCdata.Find( key ); // Find section key in new RC output, if present
if ( nsecpos < 0 ) // Allow section types DIALOG and DIALOGEX to match each other
{
if ( iDEX > -1 ) // The section key is a DIALOGEX
key.Replace( _T(" DIALOGEX "), _T(" DIALOG ") ); // Try DIALOG
else // The section key is a DIALOG
key.Replace( _T(" DIALOG "), _T(" DIALOGEX ") ); // Try DIALOGEX
//
nsecpos = newRCdata.Find( key ); // One more try...
}
if ( nsecpos > -1 ) // Match was found in new RC data
{
// Get vital data on the dialog section in the new RC data
nendpos = newRCdata.Find( _T("END"), nsecpos ); // Find end marker for the section
nseclines = _LineCount( newRCdata, nsecpos, nendpos ); // Compute number of lines of text in new section
//
// Get the same vital data on the matching dialog section in the old RC data
secpos = (*m1)[1].first - oldRCdata; // Offset to start of matching section key in old RC data
endpos = -1;
seclines = 0;
if ( secpos > -1 ) // Safety check, should not normally fail
{
endpos = oldRCdata.Find( _T("END"), secpos ); // Find end marker for the section in old RC data
seclines = _LineCount( oldRCdata, secpos, endpos ); // Compute number of lines of text in old section
}
if ( seclines != nseclines ) // Don't attempt updates if sections are different due to changes
{
msg.FormatMessage( IDS_LSECSKIPSIZE, key );
LogUserMessage( msg );
nsecpos = -1;
seckey.Empty(); // Clear out section key for next pass
}
//
}
else // Key not found in new data
{
msg.FormatMessage( IDS_LSECSKIPNF, key );
LogUserMessage( msg );
nsecpos = -1;
seckey.Empty(); // Clear out section key for next pass
} // endif: match found in new RC data
}
if ( nsecpos > -1 ) // There's a matching section key in new RC data
{
// Attempt to update the size/pos values for the section entyr in the new RC data
// using values from the matching line entry in the old RC data.
//
// Get line number within section for the current key in old RC data
int keyline = _LineCount( oldRCdata, secpos, (*m1)[2].first - oldRCdata );
// Get starting position of the matching lines within both RC data sets
int nlinepos = _GetLinePos( newRCdata, nsecpos, keyline );
if ( nlinepos > -1 ) // Must have valid line offset into new RC data
{
// Verify that at least the initial part of keyword on the line matches in both files to
// ensure a key match. Note that entire key isn't compared because it likely won't match due
// to translations of literal text.
//
// We will allow matches between xTEXT controls (CTEXT, LTEXT, and RTEXT), but not EDITTEXT
CString newkey = newRCdata.Mid(nlinepos, 10); // Isolate keys to compare
CString oldkey = key.Left(10);
BOOL keymatch = (oldkey == newkey); // Initial match result
if ( !keymatch ) // Match failed, check for allowable xTEXT key matchups
{
int keyxTextpos = oldkey.Find( _T("TEXT") ) - 1; // Position of xTEXT entry in old key or -2 if none
int newkeyxTextpos = newkey.Find( _T("TEXT") ) - 1; // Position of xTEXT entry in new key or -2 if none
if ( keyxTextpos > -1 && newkeyxTextpos > -1 ) // Both keys have an xTEXT entry
{
// If neither key is an EDITTEXT control then the match is allowed
if ( oldkey.Mid(keyxTextpos,1).FindOneOf(_T("CLR")) > -1 &&
newkey.Mid(newkeyxTextpos,1).FindOneOf(_T("CLR")) > -1 )
keymatch = TRUE;
//
}
}
if ( keymatch ) // Merge size/pos data from matching old RC data key
{
// Locate the size/pos data within the line of new RC data and update it
boost::tmatch mrslt;
int nlinelen = newRCdata.Find( _T('\n'), nlinepos ) - nlinepos;
CString sline = newRCdata.Mid( nlinepos, nlinelen ); // Substring to search
if ( boost::regex_search( sline, mrslt, dsetexpr ) ) // Found it
{
CString nval = CString(mrslt[2].first, mrslt[2].length());
CString nval5 = CString(mrslt[3].first, mrslt[3].length());
int nmatchpos = nlinepos + mrslt[2].first - sline;
int nmatchlen = mrslt[2].length();
CString chk = newRCdata.Mid( nmatchpos, nmatchlen );
if ( nmatchpos >= nlinepos && nmatchlen > 0 && chk != val )
{
newRCdata.Delete( nmatchpos, nmatchlen );
newRCdata.Insert( nmatchpos, val );
secitmsupdated++;
}
}
else // Unexpected failure to match size/pos data
{
// Log it
msg.FormatMessage( IDS_LERRSPFAIL, key.Left(10), sline );
LogUserMessage( msg );
}
}
else // Key not found in new RC output
{
// Log it
msg.FormatMessage( IDS_LKEYSKIPPED, key, newRCdata.Mid(nlinepos, key.GetLength()) );
LogUserMessage( msg );
secitmsskipped++;
} // endif: verify key line match
} // endif: line offset to new RC data is valid
} // endif: there's a matching section
secitmsfound++;
m1++;
} // endwhile: scan old RC data for size/pos values
//
// Report any stats for last section processed
if ( !seckey.IsEmpty() )
{
msg.FormatMessage( IDS_LDIALOGSECFOUND, seckey, secitmsfound, secitmsupdated, secitmsskipped );
LogUserMessage( msg );
}
}
void CLocalizeRCDlg::CheckTextClipping()
{
LogUserMessage(_T("**************************************************"));
LogUserMessage(_T("* detection of undersized labels"));
LogUserMessage(_T("**************************************************"));
CString msg;
// Check text clipping
// Parameters:
//
// Rectangles too small are reported to the log output
//
boost::tregex dsetexpr( _T("^ *[RCL]TEXT +\"([^\"]*)\", *[^,]+ *, *(\\d+) *, *(\\d+) *, *(\\d+) *, *(\\d+)") );
boost::tregex_iterator m1(boost::make_regex_iterator(newRCdata, dsetexpr, boost::match_not_dot_newline)), mEnd;
CDC dc;
dc.Attach(::GetDC(m_hWnd));
/*
// FONT pointsize, "typeface", weight, italic, charset
// FONT 8, "MS Shell Dlg", 400, 0, 0x1
int nHeight = -::MulDiv(8, dc.GetDeviceCaps(LOGPIXELSY), 72);
CFont font;
font.CreateFont(
nHeight, // pointsize
0,
0,
0,
400, // weight
0, // italic
FALSE,
FALSE,
0x1, // charset
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH,
_T("MS Shell Dlg"));
dc.SelectObject(font.m_hObject);
*/
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
LONG baseunitX = tm.tmAveCharWidth;
LONG baseunitY = tm.tmHeight;
/*
// we test system font
LONG l = ::GetDialogBaseUnits();
WORD baseunitX = LOWORD(l);
WORD baseunitY = HIWORD(l);
*/
msg.FormatMessage(
IDS_TEXTCLIPPED4,
baseunitX, baseunitY);
LogUserMessage(msg);
for( ; m1 != mEnd; ++m1 )
{
CString str( (*m1)[1].first, (*m1)[1].length() );
CString x ( (*m1)[2].first, (*m1)[2].length() );
CString y ( (*m1)[3].first, (*m1)[3].length() );
CString w ( (*m1)[4].first, (*m1)[4].length() );
CString h ( (*m1)[5].first, (*m1)[5].length() );
LONG baseX = _ttoi(x);
LONG baseY = _ttoi(y);
LONG pixelX = MulDiv(baseX, baseunitX, 4);
LONG pixelY = MulDiv(baseY, baseunitY, 8);
LONG baseW = _ttoi(w);
LONG baseH = _ttoi(h);
LONG pixelW = MulDiv(baseW, baseunitX, 4);
LONG pixelH = MulDiv(baseH, baseunitY, 8);
POINT point = { pixelX, pixelY };
SIZE size = { pixelW, pixelH };
CRect rect1(point, size);
CRect rect2 = rect1;
CRect rect3 = rect1;
dc.DrawText(str, rect2, DT_CALCRECT | DT_WORDBREAK);
dc.DrawText(str, rect3, DT_CALCRECT);
if( MulDiv(rect2.Width(), 4, baseunitX) <= MulDiv(rect1.Width(), 4, baseunitX)
&&
MulDiv(rect2.Height(), 8, baseunitY) <= MulDiv(rect1.Height(), 8, baseunitY) )
continue;
if( MulDiv(rect3.Width(), 4, baseunitX) <= MulDiv(rect1.Width(), 4, baseunitX)
&&
MulDiv(rect3.Height(), 8, baseunitY) <= MulDiv(rect1.Height(), 8, baseunitY) )
continue;
{
int line = _LineCount(newRCdata, 0, (*m1)[0].first - newRCdata) + 1;
// Log it
msg.FormatMessage(
IDS_TEXTCLIPPED,
str,
x, y, w, h,
line);
LogUserMessage(msg);
// vertically stretched
msg.FormatMessage(
IDS_TEXTCLIPPED2,
MulDiv(rect2.Width(), 4, baseunitX),
MulDiv(rect2.Height(), 8, baseunitY));
LogUserMessage(msg);
// horizontally stretched
msg.FormatMessage(
IDS_TEXTCLIPPED2,
MulDiv(rect3.Width(), 4, baseunitX),
MulDiv(rect3.Height(), 8, baseunitY));
LogUserMessage(msg);
// try with double width
pixelY = MulDiv(baseY - 4, baseunitY, 8);
pixelH = MulDiv(baseH + 8, baseunitY, 8);
POINT point = { pixelX, pixelY };
SIZE size = { pixelW, pixelH };
CRect rectA(point, size);
CRect rectB = rectA;
dc.DrawText(str, rectB, DT_CALCRECT | DT_WORDBREAK);
if( MulDiv(rectB.Width(), 4, baseunitX) <= MulDiv(rectA.Width(), 4, baseunitX)
&&
MulDiv(rectB.Height(), 8, baseunitY) <= MulDiv(rectA.Height(), 8, baseunitY) )
{
msg.FormatMessage(
IDS_TEXTCLIPPED3,
MulDiv(rectA.left, 4, baseunitX),
MulDiv(rectA.top, 8, baseunitY),
MulDiv(rectA.Width(), 4, baseunitX),
MulDiv(rectA.Height(), 8, baseunitY));
LogUserMessage(msg);
}
}
}
}
void CLocalizeRCDlg::OnBnClickedCreateini()
{
// Reload InputRC
if( !OpenInputRC() )
return;
// Write/Actualize INI-File
if( WriteReadIni(true) == OLDFILEFORMAT )
LogUserMessage( IDS_OLDFILEFORMAT );
}
// extract first caption after *pnPosition
CString CLocalizeRCDlg::ExtractCaption(CString& strText, int* pnPosition, CString strKeyword, CString &strIDC )
{
CString strCaption, strLine;
int nStart, nEnd, nStartQuote, nEndQuote;
nStart = *pnPosition;
// DLGINIT: Microsoft-Format to store data for comboboxes
if( strKeyword == _T("DLGINIT") )
{
if( nStart == 0 )
{
// remove BEGIN and END
nStart = FindSeperateWord( strText, _T("BEGIN"), 0 );
if( nStart != -1 )
nStart += 7;
}
// FORMAT: <IDC>, 0x403, <LENGTH IN BYTES>, 0
// FORMAT: <WORD> as Hex Wert chars in LOBYTE und HIGHBYTE
// comma seperated, null-terminated, ends with comma
// comma seperated, null-terminated, ends with comma
CString strHelp;
LPTSTR pHelp;
int nWord, nToken = 0, nLength = 0;
CString strToken = StringTokenize( strText, _T(",\r\n"), &nStart);
while (nStart != -1)
{
strToken.TrimLeft();
strToken.TrimRight();
if( strToken.IsEmpty() )
{
strToken = StringTokenize( strText, _T(",\r\n"),&nStart);
continue;
}
switch( nToken )
{
case 0: // IDC oder 0 bei Ende von Liste
if( strToken == "0" )
{
*pnPosition = nStart;
return strCaption;
}
else // IDC
{
if( strIDC.IsEmpty() )
{
strIDC = strToken; // new IDC
}
else
{
if( strIDC != strToken )
return strCaption;
}
}
break;
case 1: // 0x403 for Combo-Boxes
if( strToken != "0x403" )
return ERR_STR;
break;
case 2: // Length of String
nLength = _ttoi( strToken );
nLength = (int)floor((nLength / 2.0)+0.5);
break;
case 3: // always 0
break;
default: // everything above 3 is word values
if( nToken <= nLength + 3 )
{
// convert string to hexadecimal integer