-
Notifications
You must be signed in to change notification settings - Fork 0
/
commentproc.c
1887 lines (1573 loc) · 62.4 KB
/
commentproc.c
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
/*
COMMENTPROC, a plugin for Pelle's C IDE
---------------------------------------
Copyright (c) 2002-2020 Adrian Petrila, YO3GFH
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 3 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, see <http://www.gnu.org/licenses/>.
* * *
Features
--------
- inserts a function commnent header (synopsis) for the C function at
current cursor/selection
- exports all function prototypes from the current C source file to a
new header file
*/
#pragma warn(disable: 2008 2118 2228 2231 2030 2260)
#define UNICODE
#include <windows.h>
#include <commctrl.h>
#include <addin.h>
#include <strsafe.h>
#define IDI_MAIN 8001 // menu icon
#define IDI_INC 8002 // menu icon
#define IDD_OPT 1001 // options dlg
#define IDC_AUTH 4003 // author editbox
#define IDC_KEYS 4004 // keys cbbox
#define IDC_CLIP 4005 // copy to clip ckbox
#define IDC_HELPTXT 4008 // static ctrl with help text
#define ID_COMMENTPROC 69
#define ID_FINDPROC 70
#define ID_SUBCLASS 0xDEAD1969
#define MAX_LEN 8192
#define MAX_ALLOCSIZE 0x00100000 // cca 1MB
#define MAX_LINE 128
#define MAX_PARAMS 127
#define MAX_MODIFS 127
typedef
BOOL ( CALLBACK * CFSEARCHPROC )
(
BOOL valid, /*a good line?*/
DWORD lines,
DWORD crtline,
WPARAM wParam,
LPARAM lParam
);
typedef
struct tag_cfunction
{
WCHAR * params[MAX_PARAMS]; // param. list
WCHAR * modifs[MAX_MODIFS]; // return type, modifiers, function name
WCHAR * fn_name; // function name, after StripCrap :-)
INT_PTR n_par; // # of params
INT_PTR n_mod; // # of modifiers
INT_PTR par_maxw; // strlen of the longest param.
INT_PTR mod_maxw; // strlen of the longest modif.
BOOL is_decl; // is fn. declaration, or definition
BOOL is_declspec; // is there a declspec(...) mod present?
} CFUNCTION;
WPARAM vkeys[7] = { VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, VK_F11, VK_F12 };
HANDLE g_hMod; // DLL hmod
HWND g_hMain; // main IDE window
WPARAM g_kbshortcut; // vkeys[x]
BOOL g_bcopyclip; // copy to clippy or not
/*-------------------------- Forward declarations --------------------------*/
static BOOL IsEndl ( WCHAR wc );
static BOOL IsWhite ( WCHAR wc );
static BOOL IsCFunction ( const WCHAR * ws, CFUNCTION * cf );
static BOOL CommentCFunction ( HWND hIde, const WCHAR * csrc );
static BOOL ParseCFunction ( const WCHAR * csrc, CFUNCTION * cf );
static BOOL CopyFunctionInfo ( HWND hIde, const CFUNCTION * cf );
static WCHAR * FILE_Extract_filename ( const WCHAR * src );
static WCHAR * FILE_Extract_path ( const WCHAR * src, BOOL last_bslash );
static WCHAR * FILE_Extract_ext ( const WCHAR * src );
static BOOL SourceCopyFunctionInfo
( HWND hDoc, BOOL is_decl, const WCHAR * buf );
static BOOL ReadCCopyFromIni ( const WCHAR * inifile );
static int GetCrtSourceLine ( HWND hDoc );
static HWND GetCrtSourceWnd ( HWND hIde );
static BOOL GetUserSelection ( HWND hIde, WCHAR * wsrc, int cchMax );
static WCHAR * Today ( void );
static WCHAR * IniFromModule ( HMODULE hMod );
static WCHAR * ReadAuthorFromIni ( const WCHAR * inifile );
static WCHAR * StripCrap ( const WCHAR * wsrc, WCHAR * wdest, int cchMax );
static UINT_PTR ReadVKeyFromIni ( const WCHAR * inifile );
static LRESULT CALLBACK DocSubclassProc ( HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData );
static INT_PTR CALLBACK OptDlgProc ( HWND hDlg, UINT msg, WPARAM wParam,
LPARAM lParam );
static UINT_PTR FindCFDefinitions ( HWND hDoc,
CFSEARCHPROC searchproc, LPARAM lParam );
static UINT_PTR GetSourceLines ( HWND hDoc, DWORD line,
WCHAR * dest, WORD cchMax );
static BOOL CALLBACK FindCDefsProc
( BOOL valid, DWORD lines, DWORD crtline, WPARAM wParam, LPARAM lParam );
/*-@@+@@--------------------------------------------------------------------*/
// Function: DllMain
/*--------------------------------------------------------------------------*/
// Type: BOOL WINAPI
// Param. 1: HANDLE hDLL :
// Param. 2: DWORD dwReason :
// Param. 3: LPVOID lpReserved :
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 27.09.2020
// DESCRIPTION: entrypoint to our DLL
/*--------------------------------------------------------------------@@-@@-*/
BOOL WINAPI DllMain ( HANDLE hDLL, DWORD dwReason, LPVOID lpReserved )
/*--------------------------------------------------------------------------*/
{
switch ( dwReason )
{
case DLL_PROCESS_ATTACH:
// Save this, it's not that easy to get hold of it afterwards :-)
g_hMod = hDLL;
return TRUE;
case DLL_PROCESS_DETACH:
return TRUE;
default:
return TRUE;
}
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: AddInMain
/*--------------------------------------------------------------------------*/
// Type: ADDINAPI BOOL WINAPI
// Param. 1: HWND hwnd : main ide window
// Param. 2: ADDIN_EVENT eEvent : whatever ide just did :-)
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: required export for any dll addin for Pelle ide.
/*--------------------------------------------------------------------@@-@@-*/
ADDINAPI BOOL WINAPI AddInMain ( HWND hwnd, ADDIN_EVENT eEvent )
/*--------------------------------------------------------------------------*/
{
ADDIN_ADD_COMMAND add_cmd;
UINT_PTR idx;
switch ( eEvent )
{
case AIE_APP_CREATE: // add our menu to "Source" menu
g_hMain = hwnd;
idx = ReadVKeyFromIni ( IniFromModule ( g_hMod ) );
g_bcopyclip = ReadCCopyFromIni ( IniFromModule ( g_hMod ) );
g_kbshortcut = vkeys[idx];
RtlZeroMemory ( &add_cmd, sizeof (ADDIN_ADD_COMMAND));
add_cmd.cbSize = sizeof (ADDIN_ADD_COMMAND);
add_cmd.pszText = L"Add function description";
add_cmd.hIcon = LoadImage ( g_hMod, MAKEINTRESOURCE ( IDI_MAIN ),
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR | LR_SHARED );
add_cmd.id = ID_COMMENTPROC;
add_cmd.idMenu = AIM_MENU_SOURCE;
AddIn_AddCommand ( hwnd, &add_cmd );
RtlZeroMemory ( &add_cmd, sizeof (ADDIN_ADD_COMMAND));
add_cmd.cbSize = sizeof (ADDIN_ADD_COMMAND);
add_cmd.pszText = L"Export all functions to header";
add_cmd.hIcon = LoadImage ( g_hMod, MAKEINTRESOURCE ( IDI_INC ),
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR | LR_SHARED );
add_cmd.id = ID_FINDPROC;
add_cmd.idMenu = AIM_MENU_SOURCE;
AddIn_AddCommand ( hwnd, &add_cmd );
break;
case AIE_APP_DESTROY:
g_hMain = NULL;
AddIn_RemoveCommand ( hwnd, ID_COMMENTPROC );
AddIn_RemoveCommand ( hwnd, ID_FINDPROC );
break;
case AIE_DOC_CREATE:
if ( AddIn_GetDocumentType ( hwnd ) == AID_SOURCE )
// install our wndproc
SetWindowSubclass ( hwnd, DocSubclassProc, ID_SUBCLASS, 0 );
break;
case AIE_DOC_DESTROY:
if ( AddIn_GetDocumentType ( hwnd ) == AID_SOURCE )
// remove our subclass
RemoveWindowSubclass ( hwnd, DocSubclassProc, ID_SUBCLASS );
break;
default:
break;
}
return TRUE;
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: AddInCommandExW
/*--------------------------------------------------------------------------*/
// Type: ADDINAPI void WINAPI
// Param. 1: int idCmd : menu/toolbar ID that send the command
// Param. 2: LPCVOID pcvData : custom data
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: event handler for menus/toolbar commands
/*--------------------------------------------------------------------@@-@@-*/
ADDINAPI void WINAPI AddInCommandExW ( int idCmd, LPCVOID pcvData )
/*--------------------------------------------------------------------------*/
{
WCHAR wsrc[MAX_LEN];
WCHAR temp[MAX_PATH];
WCHAR * hdrName, * srcPath, * srcExt;
ADDIN_DOCUMENT_INFO adi;
HWND hHdrDoc, hSrcDoc;
switch ( idCmd )
{
case ID_COMMENTPROC:
if ( GetUserSelection ( g_hMain, wsrc, ARRAYSIZE(wsrc) ) )
CommentCFunction ( g_hMain, wsrc );
break;
case ID_FINDPROC:
adi.cbSize = sizeof(ADDIN_DOCUMENT_INFO);
hSrcDoc = GetCrtSourceWnd ( g_hMain );
if ( AddIn_GetDocumentInfo ( hSrcDoc, &adi) )
{
srcExt = FILE_Extract_ext ( adi.szFilename );
if ( (srcExt == NULL) || ((lstrcmpiW ( srcExt, L".c") != 0) &&
( lstrcmpiW ( srcExt, L".h" ) != 0)) )
break;
hdrName = FILE_Extract_filename ( adi.szFilename );
srcPath = FILE_Extract_path ( adi.szFilename, TRUE );
StringCchPrintfW ( adi.szFilename,
ARRAYSIZE(adi.szFilename), L"%ls%ls.h",
((srcPath == NULL) ? L"" : srcPath),
((hdrName == NULL) ? L"untitled" : hdrName));
hHdrDoc = AddIn_NewDocument ( g_hMain, AID_SOURCE );
if ( hHdrDoc )
{
AddIn_SetDocumentInfo ( hHdrDoc, &adi );
CharUpperW ( hdrName );
StringCchPrintfW ( temp, ARRAYSIZE(temp),
L"#ifndef _%ls_H\n#define _%ls_H\n\n",
hdrName, hdrName );
AddIn_ReplaceSourceSelText ( hHdrDoc, temp );
FindCFDefinitions ( hSrcDoc,
(CFSEARCHPROC)FindCDefsProc, (LPARAM)hHdrDoc );
StringCchPrintfW ( temp, ARRAYSIZE(temp),
L"#endif // _%ls_H\n", hdrName );
AddIn_ReplaceSourceSelText ( hHdrDoc, temp );
}
}
else
MessageBox ( g_hMain,
L"Unable to find current source window :-(",
L"Plugin", MB_OK );
break;
default:
break;
}
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: AddInSetup
/*--------------------------------------------------------------------------*/
// Type: ADDINAPI void WINAPI
// Param. 1: HWND hwnd : IDE window
// Param. 2: LPCVOID pcvData : custom data
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: exported, called when user clicks the "Options" button from
// tools->options menu
/*--------------------------------------------------------------------@@-@@-*/
ADDINAPI void WINAPI AddInSetup ( HWND hwnd, LPCVOID pcvData )
/*--------------------------------------------------------------------------*/
{
WCHAR buf[MAX_LEN];
WCHAR * ini, * p;
ini = IniFromModule ( g_hMod );
p = ini;
if ( ini[0] != L'\0' )
{
p += lstrlenW ( ini );
while ( *p != L'\\' && p != ini )
p--;
StringCchPrintfW ( buf, ARRAYSIZE(buf),
L"This will create a file named \"%ls\" in /Bin/Addins64/ folder. "
L"The key \"author=<yourname>\" allows you to customize "
L"the AUTHOR field from the function description block. "
L"You can also configure the keyboard shortcut for the menu - "
L"the key is \"kb_shortcut=<0..6>\", <0> being F6 and <6> F12."
L"Set \"clip_copy\" key to <1> to also copy fn. description "
L"to clipboard." , ++p );
DialogBoxParam ( g_hMod, MAKEINTRESOURCE ( IDD_OPT ),
hwnd, OptDlgProc, ( LPARAM ) buf );
}
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: GetUserSelection
/*--------------------------------------------------------------------------*/
// Type: static BOOL
// Param. 1: HWND hIde : main IDE window
// Param. 2: WCHAR * wsrc: buffer to receive text
// Param. 3: int cchMax : wsrc max. size, in characters
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 27.09.2020
// DESCRIPTION: returns selected text or text from the line under the
// cursor. for one line function decl., just place the caret
// on the line. for Microsoft style multi-line function
// prototypes, you'll have to manually select function
/*--------------------------------------------------------------------@@-@@-*/
static BOOL GetUserSelection ( HWND hIde, WCHAR * wsrc, int cchMax )
/*--------------------------------------------------------------------------*/
{
WCHAR temp[256];
HWND hDoc;
ADDIN_RANGE chr;
int line;
if ( wsrc == NULL || cchMax == 0 )
return FALSE;
wsrc[0] = L'\0';
hDoc = AddIn_GetActiveDocument ( hIde );
if ( hDoc == NULL )
return FALSE;
if ( AddIn_GetDocumentType ( hDoc ) == AID_SOURCE )
{
if ( !AddIn_GetSourceSel ( hDoc, &chr ) )
return FALSE;
if ( chr.iEndPos > chr.iStartPos ) // we have a selection
{
if ( (chr.iEndPos - chr.iStartPos) > cchMax )
{
StringCchPrintfW ( temp, ARRAYSIZE(temp),
L"Selection is too large. Keep it under %d chars :-)",
cchMax );
MessageBox ( g_hMain, temp, L"Commentproc Plugin",
MB_OK | MB_ICONINFORMATION );
return FALSE;
}
AddIn_GetSourceSelText ( hDoc, wsrc, cchMax );
}
else // just get the line under cursor
{
line = AddIn_SourceLineFromChar ( hDoc, chr.iStartPos );
AddIn_GetSourceLine ( hDoc, line, wsrc, cchMax );
}
}
return TRUE;
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: IsEndl
/*--------------------------------------------------------------------------*/
// Type: static BOOL
// Param. 1: WCHAR wc : a widechar :-)
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: are we a terminator of lines?
/*--------------------------------------------------------------------@@-@@-*/
static BOOL IsEndl ( WCHAR wc )
/*--------------------------------------------------------------------------*/
{
return ( wc == L'\r' || wc == L'\n' );
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: IsWhite
/*--------------------------------------------------------------------------*/
// Type: static BOOL
// Param. 1: WCHAR wc :
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: are we whitespace?
/*--------------------------------------------------------------------@@-@@-*/
static BOOL IsWhite ( WCHAR wc )
/*--------------------------------------------------------------------------*/
{
return ( wc == L' ' || wc == L'\t' );
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: IsCFunction
/*--------------------------------------------------------------------------*/
// Type: static BOOL
// Param. 1: const WCHAR * ws: our candidate
// Param. 2: CFUNCTION * cf : struct to fill with var. data
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: some crude attempt to assess if the text we got is a
// C function or not :-) - basically, it needs 1 pair of ()
// or 2 pairs if declspec is present, also checks if it's
// inside quotes, on a comment line or in n if/else/switch/
// ... block and fails accordingly. It can be easily fooled
// and it's not designed to be standalone, but rather as a
// pre-stage for ParseCFunction
/*--------------------------------------------------------------------@@-@@-*/
static BOOL IsCFunction ( const WCHAR * ws, CFUNCTION * cf )
/*--------------------------------------------------------------------------*/
{
INT_PTR i, left, right, len;
BOOL is_declsp;
WCHAR * quot, * comma,
* s, * semi_c;
WCHAR temp[MAX_LEN];
left = 0;
right = 0;
is_declsp = FALSE;
quot = wcschr ( ws, L'"' ); // are there any quotes?
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'=' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'+' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'-' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'?' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'@' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'.' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'!' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'|' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L':' );
if ( quot != NULL )
return FALSE;
quot = wcschr ( ws, L'&' );
if ( quot != NULL )
return FALSE;
semi_c = wcschr ( ws, L';' );
comma = wcschr ( ws, L',' );
// make a copy for _wcstok_ms
StringCchCopy ( temp, ARRAYSIZE(temp), ws );
s = _wcstok_ms ( temp, L" ()[]{},;" );
if ( s != NULL )
{
if ( ( wcscmp ( s, L"_declspec" ) == 0 ) ||
( wcscmp ( s, L"__declspec" ) == 0 ) )
is_declsp = TRUE;
if ( ( wcscmp ( s, L"if" ) == 0 ) ||
( wcscmp ( s, L"else" ) == 0 ) ||
( wcscmp ( s, L"while" ) == 0 ) ||
( wcscmp ( s, L"for" ) == 0 ) ||
( wcscmp ( s, L"switch" ) == 0 ) ||
( wcscmp ( s, L"case" ) == 0 ) ||
( wcscmp ( s, L"default" ) == 0 ) ||
( wcscmp ( s, L"__except" ) == 0 ) ||
( wcscmp ( s, L"defined" ) == 0 ) ||
( wcscmp ( s, L"sizeof" ) == 0 ) ||
( wcscmp ( s, L"assert" ) == 0 ) ||
( wcscmp ( s, L"return" ) == 0 ) )
return FALSE;
while ( (s = _wcstok_ms ( NULL, L" ()[]{},;" )) != NULL )
{
if ( ( wcscmp ( s, L"_declspec" ) == 0 ) ||
( wcscmp ( s, L"__declspec" ) == 0 ) )
{
is_declsp = TRUE;
continue;
}
if ( ( wcscmp ( s, L"if" ) == 0 ) ||
( wcscmp ( s, L"else" ) == 0 ) ||
( wcscmp ( s, L"while" ) == 0 ) ||
( wcscmp ( s, L"for" ) == 0 ) ||
( wcscmp ( s, L"switch" ) == 0 ) ||
( wcscmp ( s, L"case" ) == 0 ) ||
( wcscmp ( s, L"default" ) == 0 ) ||
( wcscmp ( s, L"__except" ) == 0 ) ||
( wcscmp ( s, L"defined" ) == 0 ) ||
( wcscmp ( s, L"sizeof" ) == 0 ) ||
( wcscmp ( s, L"assert" ) == 0 ) ||
( wcscmp ( s, L"return" ) == 0 ) )
return FALSE;
}
}
len = lstrlenW ( ws );
i = len;
while ( i >= 0)
{
if ( ws[i] == L'(' )
{
left++;
if ( semi_c != NULL )
if ( semi_c < ws+i )
return FALSE;
if ( comma != NULL )
if ( comma < ws+i )
return FALSE; // shouldn't have a comma before '('
}
if ( ws[i] == L')' )
{
right++;
if ( semi_c != NULL )
if ( semi_c < ws+i )
return FALSE;
if ( comma != NULL )
if ( comma > ws+i )
// we've hit some multi-line function call so get out
return FALSE;
}
i--;
}
if ( semi_c != NULL )
cf->is_decl = ( ws+(len-1) == semi_c );
else
cf->is_decl = FALSE;
cf->is_declspec = is_declsp;
if ( is_declsp )
{
if ( (left == 2) && (right == 2) )
return TRUE;
else
return FALSE;
}
if ( (left == 1) && (right == 1) )
return TRUE;
return FALSE;
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: StripCrap
/*--------------------------------------------------------------------------*/
// Type: static WCHAR *
// Param. 1: const WCHAR * wsrc: raw text validated by IsCFunction
// Param. 2: WCHAR * wdest : where to put cleaned text
// Param. 3: int cchMax : max. destination, in characters
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: strips all CR/LF and extra tabs/spaces from wsrc. null
// terminates wdest
/*--------------------------------------------------------------------@@-@@-*/
static WCHAR * StripCrap ( const WCHAR * wsrc, WCHAR * wdest, int cchMax )
/*--------------------------------------------------------------------------*/
{
INT_PTR i, len, start;
WCHAR * p, * s;
BOOL skipping = TRUE;
BOOL cr_skipping = TRUE;
BOOL in_comment = FALSE;
BOOL in_macro = FALSE;
BOOL block_comment = FALSE;
p = wdest;
len = lstrlenW ( wsrc );
start = 0;
if ( len > cchMax )
len = cchMax;
s = wcschr ( wsrc, L';' );
// see how many ';' we have and skip
// everything left of them, except the last
while ( s != NULL )
{
s++;
while ( IsWhite(*s) || IsEndl(*s) )
s++;
if ( *s == L'\0' )
break;
start = (s - wsrc);
s = wcschr ( wsrc+start, L';' );
}
// start where last ';' found
for ( i = start; i < len; i++ )
{
if ( block_comment )
{
if ( (i+1 < len) && (wsrc[i] == L'*') &&
(wsrc[i+1] == L'/'))
{
i++;
block_comment = FALSE;
}
continue;
}
if ( in_comment )
{
if ( IsEndl ( wsrc[i]) )
in_comment = FALSE;
continue;
}
if ( in_macro )
{
if ( IsEndl ( wsrc[i]) )
in_macro = FALSE;
continue;
}
if ( (i+1 < len) && (wsrc[i] == L'/') &&
(wsrc[i+1] == '/'))
{
i++;
in_comment = TRUE;
continue;
}
if ( (i+1 < len) && (wsrc[i] == L'/' ) &&
(wsrc[i+1] == L'*'))
{
i++;
block_comment = TRUE;
continue;
}
if ( wsrc[i] == L'#')
{
in_macro = TRUE;
continue;
}
// try to accomodate the multiline MS style function
// "beautification" :-)
// gobble one CR and make it a space
if ( IsEndl ( wsrc[i]) )
{
if ( cr_skipping == FALSE )
{
*p++ = TEXT(' ');
cr_skipping = TRUE;
}
continue;
}
if ( IsWhite(wsrc[i]) )
{
if ( skipping == FALSE )
{
*p++ = wsrc[i]; // keep at least one tab/space
skipping = TRUE;
}
continue;
}
else
{
*p++ = wsrc[i];
skipping = FALSE;
cr_skipping = FALSE;
}
}
*p = L'\0';
return wdest;
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: CommentCFunction
/*--------------------------------------------------------------------------*/
// Type: static BOOL
// Param. 1: HWND hIde : main IDE window
// Param. 2: const WCHAR * csrc : raw text, as returned by
// GetUserSelection
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: just a wrapper for ParseCFunction and CopyFunctionInfo
/*--------------------------------------------------------------------@@-@@-*/
static BOOL CommentCFunction ( HWND hIde, const WCHAR * csrc )
/*--------------------------------------------------------------------------*/
{
CFUNCTION cf;
if ( !ParseCFunction ( csrc, &cf ) )
return FALSE;
return CopyFunctionInfo ( hIde, &cf );
}
/*-@@+@@--------------------------------------------------------------------*/
// Function: ParseCFunction
/*--------------------------------------------------------------------------*/
// Type: static BOOL
// Param. 1: const WCHAR * csrc: raw text, as returned by
// GetUserSelection
// Param. 2: CFUNCTION * cf : pointer to a CFUNCTION struct to fill
// with info
/*--------------------------------------------------------------------------*/
// AUTHOR: Adrian Petrila, YO3GFH
// DATE: 03.10.2020
// DESCRIPTION: This will take a block of text, validate it as a C
// function, then parse it to extract params and modifiers and
// to put them as pointers to null-term. strings in the
// cf->params ans cf->modifs. Will also fill other members of
// CFUNCTION with meaningful info. Returns TRUE if it thinks
// it has a valid C function decl./def. or FALSE otherwise;
// it can return FALSE even if CF_IsCFunction said OK - this
// one checks for a function name and one param at least.
/*--------------------------------------------------------------------@@-@@-*/
static BOOL ParseCFunction ( const WCHAR * csrc, CFUNCTION * cf )
/*--------------------------------------------------------------------------*/
{
static WCHAR work_buf[MAX_LEN];
static WCHAR next_buf[MAX_LEN];
static WCHAR fun_name[MAX_LEN];
WCHAR * pos, * p;
WCHAR * par_start;
WCHAR * mod_start;
INT_PTR i, j, len;
BOOL is_declsp;
RtlZeroMemory ( cf, sizeof(CFUNCTION) );
RtlZeroMemory ( work_buf, sizeof(work_buf) );
RtlZeroMemory ( next_buf, sizeof(next_buf) );
// stage 1: clean source from CR/LF, tabs, etc.
// rough pre-evaluation :-)
pos = StripCrap ( csrc, work_buf, ARRAYSIZE(work_buf) );
if ( !IsCFunction ( pos, cf ) )
return FALSE;
// a small hack, clean fn name from any left
// right curls; could do it in the CF_StripCrap,
// but the function synopsis won't work correctly.
// a very good example of bad design :-)
p = wcschr ( pos, L'}');
if ( p != NULL )
{
p++;
while (((*p == L' ') ||
(*p == L'}')) &&
(*p != L'\0'))
p++;
pos = p;
}
// save the untouched function
StringCchCopyW ( fun_name, ARRAYSIZE(fun_name), pos );
i = lstrlenW ( fun_name );
while ( i >= 0 )
{
if ( (fun_name[i] == L')') )
break;
i--;
}
fun_name[i+1] = L'\0';
cf->fn_name = fun_name;
mod_start = pos;
par_start = pos; // just in case
i = 0;
len = lstrlenW ( pos );
is_declsp = cf->is_declspec;
// stage 2: put a null where '(' is; this will be the boundary between
// fn. name+modifiers and parameter list; also put a null on ')', since
// we don't need anything after that
// if we have declspec(...) replace () with spaces and redo stage 1
for ( j = 0; j < len; j++ )
{
if ( pos[j] == L'(' )
{
if ( is_declsp )
pos[j++] = L' ';
else
{
pos[j++] = L'\0';
par_start = pos + j;
}
continue;
}
if ( pos[j] == L')' )
{
if ( is_declsp ) // redo stage 1
{
pos[j] = L' ';
is_declsp = FALSE;
pos = StripCrap ( work_buf, next_buf, ARRAYSIZE(next_buf) );
mod_start = pos;
par_start = pos;
j = 0;
continue;
}
else
pos[j] = L'\0';
}
}
pos = mod_start;
len = lstrlenW ( mod_start );
// stage 3: parse modifiers and replace every space with a null;
// set the modifiers pointer table from the CFUNCTION struct
for ( j = 0; j < len; j++ )
{
if ( IsWhite(pos[j]) )
{
pos[j++] = L'\0';
cf->modifs[i++] = mod_start;
mod_start = pos + j;
}
else
{
if (pos[j+1] == L'\0')
cf->modifs[i++] = mod_start;
}
// we have a glorious function with 127 modifiers...
if ( i >= MAX_MODIFS-1 )
break;
}
cf->modifs[i] = 0;
cf->n_mod = i;
if ( i == 0 ) // not ok with 0 modifs.
return FALSE;
pos = par_start;
i = 0;
len = lstrlenW ( par_start );
// stage 4: parse parameter list and replace every ',' with a null;
// set the params pointer table from the CFUNCTION struct
for ( j = 0; j < len; j++ )
{
if (pos[j] == L',')
{
pos[j++] = L'\0';
// skip leading space, if any
while ( IsWhite(*par_start) && *par_start != L'\0' )
par_start++;
cf->params[i++] = par_start;
par_start = pos + j;
}
else
{
if (pos[j+1] == L'\0')
{
while ( IsWhite(*par_start) && *par_start != L'\0' )
par_start++;
cf->params[i++] = par_start;
}
}
// ...or another glorious function with 127 params :-)
if ( i >= MAX_PARAMS-1 )
break;
}
cf->params[i] = 0;
cf->n_par = i;
if ( i == 0 )
return FALSE; // not ok with 0 params
// save the longest param/mod for later
for ( j = 0; j < cf->n_mod; j++ )
{
len = lstrlenW ( cf->modifs[j] );
if ( cf->mod_maxw < len )
cf->mod_maxw = len;
}
for ( j = 0; j < cf->n_par; j++ )
{
len = lstrlenW ( cf->params[j] );