-
Notifications
You must be signed in to change notification settings - Fork 99
/
ParsingFunctions.m
1358 lines (1011 loc) · 51.2 KB
/
ParsingFunctions.m
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
/* Note: NSMethodSignature does not support unions or unknown structs on input.
// However, using NSMethodSignature to break ObjC types apart for parsing seemed to me very convenient.
// My implementation below encodes the unknown structs
// and unions as a special, impossible to conflict struct that is accepted on input.
// They are then decoded back in the output of getArgumentTypeAtIndex:
// This actually adds support for unions and undefined structs. */
@implementation NSMethodSignature (classdump_dyld_helper)
+(id)cd_signatureWithObjCTypes:(const char *)types{
__block NSString *text=[NSString stringWithCString:types encoding:NSUTF8StringEncoding];
while ([text rangeOfString:@"("].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\(([^\\(\\)]+)\\)" options:nil error:nil];
// test if the anticipated union (embraced in parentheseis) is actually a function definition rather than a union
NSRange range=[text rangeOfString:@"\\(([^\\(\\)]+)\\)" options:NSRegularExpressionSearch];
NSString *rep=[text substringWithRange:range];
NSString *testUnion=[rep stringByReplacingOccurrencesOfString:@"(" withString:@"{"]; //just to test if it internally passes as a masqueraded struct
testUnion=[testUnion stringByReplacingOccurrencesOfString:@")" withString:@"}"];
if ([testUnion rangeOfString:@"="].location==NSNotFound){
// its a function!
text=[text stringByReplacingOccurrencesOfString:@"(" withString:@"__FUNCTION_START__"];
text=[text stringByReplacingOccurrencesOfString:@")" withString:@"__FUNCTION_END__"];
continue;
}
[regex enumerateMatchesInString:text options:0
range:NSMakeRange(0, [text length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSString *textFound=[text substringWithRange:[result rangeAtIndex:i]];
text=[text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"(%@)",textFound] withString:[NSString stringWithFormat:@"{union={%@}ficificifloc}",textFound]]; //add an impossible match of types
*stop=YES;
}
}];
}
if ([text rangeOfString:@"{"].location!=NSNotFound){
BOOL FOUND=1;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\^)\\{([^\\{^\\}]+)\\}" options:nil error:nil];
while (FOUND){
NSRange range = [regex rangeOfFirstMatchInString:text options:0 range:NSMakeRange(0, [text length])];
if (range.location!=NSNotFound){
FOUND=1;
NSString *result = [text substringWithRange:range];
text=[text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",result] withString:[NSString stringWithFormat:@"^^^%@",result]];
}
else{
FOUND=0;
}
}
FOUND=1;
regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\^)\\{([^\\}]+)\\}" options:nil error:nil];
while (FOUND){
NSRange range = [regex rangeOfFirstMatchInString:text options:0 range:NSMakeRange(0, [text length])];
if (range.location!=NSNotFound){
FOUND=1;
NSString *result = [text substringWithRange:range];
text=[text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",result] withString:[NSString stringWithFormat:@"^^^%@",result]];
}
else{
FOUND=0;
}
}
}
text=[text stringByReplacingOccurrencesOfString:@"__FUNCTION_START__" withString:@"("];
text=[text stringByReplacingOccurrencesOfString:@"__FUNCTION_END__" withString:@")"];
types=[text UTF8String];
return [self signatureWithObjCTypes:types];
}
-(const char *)cd_getArgumentTypeAtIndex:(int)anIndex{
const char *argument= [self getArgumentTypeAtIndex:anIndex];
NSString *char_ns=[NSString stringWithCString:argument encoding:NSUTF8StringEncoding];
__block NSString *text=char_ns;
if ([text rangeOfString:@"^^^"].location!=NSNotFound){
text=[text stringByReplacingOccurrencesOfString:@"^^^" withString:@""];
}
while ([text rangeOfString:@"{union"].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\{union.+?ficificifloc\\})" options:nil error:nil];
[regex enumerateMatchesInString:text options:0
range:NSMakeRange(0, [text length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSString *textFound=[text substringWithRange:[result rangeAtIndex:i]];
NSString *textToPut=[textFound substringFromIndex:8];
textToPut=[textToPut substringToIndex:textToPut.length-1-(@"ficificifloc".length+1)];
text=[text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",textFound] withString:[NSString stringWithFormat:@"(%@)",textToPut]];
*stop=YES;
}
}];
}
char_ns=text;
return [char_ns UTF8String];
}
@end
/****** String Parsing Functions ******/
/****** Properties Parser ******/
NSString * propertyLineGenerator(NSString *attributes,NSString *name){
NSCharacterSet *parSet=[NSCharacterSet characterSetWithCharactersInString:@"()"];
attributes=[attributes stringByTrimmingCharactersInSet:parSet];
NSMutableArray *attrArr=(NSMutableArray *)[attributes componentsSeparatedByString:@","];
NSString *type=[attrArr objectAtIndex:0] ;
type=[type stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:@""] ;
if ([type rangeOfString:@"@"].location==0 && [type rangeOfString:@"\""].location!=NSNotFound){ //E.G. @"NSTimer"
type=[type stringByReplacingOccurrencesOfString:@"\"" withString:@""];
type=[type stringByReplacingOccurrencesOfString:@"@" withString:@""];
type=[type stringByAppendingString:@" *"] ;
NSString *classFoundInProperties=[type stringByReplacingOccurrencesOfString:@" *" withString:@""];
if (![classesInClass containsObject:classFoundInProperties] && [classFoundInProperties rangeOfString:@"<"].location==NSNotFound){
[classesInClass addObject:classFoundInProperties];
}
if ([type rangeOfString:@"<"].location!=NSNotFound){
type=[type stringByReplacingOccurrencesOfString:@"> *" withString:@">"];
if ([type rangeOfString:@"<"].location==0){
type=[@"id" stringByAppendingString:type];
}
else{
type=[type stringByReplacingOccurrencesOfString:@"<" withString:@"*<"];
}
}
}
else if ([type rangeOfString:@"@"].location==0 && [type rangeOfString:@"\""].location==NSNotFound){
type=@"id";
}
else{
type=commonTypes(type,&name,NO);
}
if ([type rangeOfString:@"="].location!=NSNotFound){
type=[type substringToIndex:[type rangeOfString:@"="].location];
if ([type rangeOfString:@"_"].location==0){
type=[type substringFromIndex:1];
}
}
type=[type stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
attrArr=[NSMutableArray arrayWithArray:attrArr];
[attrArr removeObjectAtIndex:0];
NSMutableArray *newPropsArray=[NSMutableArray array];
NSString *synthesize=@"";
for (NSString *attr in attrArr){
NSString *vToClear=nil;
if ([attr rangeOfString:@"V_"].location==0){
vToClear=attr;
attr=[attr stringByReplacingCharactersInRange:NSMakeRange(0,2) withString:@""] ;
synthesize=[NSString stringWithFormat:@"\t\t\t\t//@synthesize %@=_%@ - In the implementation block",attr,attr];
}
if ([attr length]==1){
NSString *translatedProperty = attr;
if ([attr isEqual:@"R"]){ translatedProperty = @"readonly"; }
if ([attr isEqual:@"C"]){ translatedProperty = @"copy"; }
if ([attr isEqual:@"&"]){ translatedProperty = @"retain"; }
if ([attr isEqual:@"N"]){ translatedProperty = @"nonatomic";}
//if ([attr isEqual:@"D"]){ translatedProperty = @"@dynamic"; }
if ([attr isEqual:@"D"]){ continue; }
if ([attr isEqual:@"W"]){ translatedProperty = @"__weak"; }
if ([attr isEqual:@"P"]){ translatedProperty = @"t<encoding>";}
[newPropsArray addObject:translatedProperty];
}
if ([attr rangeOfString:@"G"].location==0){
attr=[attr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:@""] ;
attr=[NSString stringWithFormat:@"getter=%@",attr];
[newPropsArray addObject:attr];
}
if ([attr rangeOfString:@"S"].location==0){
attr=[attr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:@""] ;
attr=[NSString stringWithFormat:@"setter=%@",attr];
[newPropsArray addObject:attr];
}
}
if ([newPropsArray containsObject:@"nonatomic"] && ![newPropsArray containsObject:@"assign"] && ![newPropsArray containsObject:@"readonly"] && ![newPropsArray containsObject:@"copy"] && ![newPropsArray containsObject:@"retain"]){
[newPropsArray addObject:@"assign"];
}
newPropsArray=[newPropsArray reversedArray];
NSString *rebuiltString=[newPropsArray componentsJoinedByString:@","];
NSString *attrString=[newPropsArray count]>0 ? [NSString stringWithFormat:@"(%@)",rebuiltString] : @"(assign)";
return [[NSString alloc] initWithFormat:@"\n%@%@ %@ %@; %@",@"@property ",attrString,type,name,synthesize];
}
/****** Properties Combined Array (for fixing non-matching types) ******/
static NSMutableArray * propertiesArrayFromString(NSString *propertiesString){
NSMutableArray *propertiesExploded=[[propertiesString componentsSeparatedByString:@"\n"] mutableCopy];
NSMutableArray *typesAndNamesArray=[NSMutableArray array];
for (NSString *string in propertiesExploded){
if (string.length<1){
continue;
}
int startlocation=[string rangeOfString:@")"].location;
int endlocation=[string rangeOfString:@";"].location;
if ([string rangeOfString:@";"].location==NSNotFound || [string rangeOfString:@")"].location==NSNotFound){
continue;
}
NSString *propertyTypeFound=[string substringWithRange:NSMakeRange(startlocation+1,endlocation-startlocation-1)];
int firstSpaceLocationBackwards=[propertyTypeFound rangeOfString:@" " options:NSBackwardsSearch].location;
if ([propertyTypeFound rangeOfString:@" " options:NSBackwardsSearch].location==NSNotFound){
continue;
}
NSMutableDictionary *typesAndNames=[NSMutableDictionary dictionary];
NSString *propertyNameFound=[propertyTypeFound substringFromIndex:firstSpaceLocationBackwards+1];
propertyTypeFound=[propertyTypeFound substringToIndex:firstSpaceLocationBackwards];
//propertyTypeFound=[propertyTypeFound stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([propertyTypeFound rangeOfString:@" "].location==0){
propertyTypeFound=[propertyTypeFound substringFromIndex:1];
}
propertyNameFound=[propertyNameFound stringByReplacingOccurrencesOfString:@" " withString:@""];
[typesAndNames setObject:propertyTypeFound forKey:@"type"];
[typesAndNames setObject:propertyNameFound forKey:@"name"];
[typesAndNamesArray addObject:typesAndNames];
}
[propertiesExploded release];
return typesAndNamesArray;
}
/****** Protocol Parser ******/
NSString * buildProtocolFile(Protocol *currentProtocol){
NSMutableString * protocolsMethodsString=[[NSMutableString alloc] init];
NSString *protocolName=[NSString stringWithCString:protocol_getName(currentProtocol) encoding:NSUTF8StringEncoding];
[protocolsMethodsString appendString:[NSString stringWithFormat:@"\n@protocol %@",protocolName]];
NSMutableArray *classesInProtocol=[[NSMutableArray alloc] init];
unsigned int outCount=0;
Protocol ** protList=protocol_copyProtocolList(currentProtocol,&outCount);
if (outCount>0){
[protocolsMethodsString appendString:@" <"];
}
for (int p=0; p<outCount; p++){
NSString *end= p==outCount-1 ? [@"" retain] : [@"," retain];
[protocolsMethodsString appendString:[NSString stringWithFormat:@"%s%@",protocol_getName(protList[p]),end]];
[end release];
}
if (outCount>0){
[protocolsMethodsString appendString:@">"];
}
free(protList);
NSMutableString *protPropertiesString=[[NSMutableString alloc] init];
unsigned int protPropertiesCount;
objc_property_t * protPropertyList=protocol_copyPropertyList(currentProtocol,&protPropertiesCount);
for (int xi=0; xi<protPropertiesCount; xi++){
const char *propname=property_getName(protPropertyList[xi]);
const char *attrs=property_getAttributes(protPropertyList[xi]);
NSCharacterSet *parSet=[NSCharacterSet characterSetWithCharactersInString:@"()"];
NSString *attributes=[[NSString stringWithCString:attrs encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:parSet];
NSMutableArray *attrArr=(NSMutableArray *)[attributes componentsSeparatedByString:@","];
NSString *type=[attrArr objectAtIndex:0] ;
type=[type stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:@""] ;
if ([type rangeOfString:@"@"].location==0 && [type rangeOfString:@"\""].location!=NSNotFound){ //E.G. @"NSTimer"
type=[type stringByReplacingOccurrencesOfString:@"\"" withString:@""];
type=[type stringByReplacingOccurrencesOfString:@"@" withString:@""];
type=[type stringByAppendingString:@" *"] ;
NSString *classFoundInProperties=[type stringByReplacingOccurrencesOfString:@" *" withString:@""];
if (![classesInProtocol containsObject:classFoundInProperties] && [classFoundInProperties rangeOfString:@"<"].location==NSNotFound){
[classesInProtocol addObject:classFoundInProperties];
}
}
NSString *newString=propertyLineGenerator([NSString stringWithCString:attrs encoding:NSUTF8StringEncoding],[NSString stringWithCString:propname encoding:NSUTF8StringEncoding]);
if ([protPropertiesString rangeOfString:newString].location==NSNotFound){
[protPropertiesString appendString:newString];
}
[newString release];
}
[protocolsMethodsString appendString:protPropertiesString];
free(protPropertyList);
for (int acase=0; acase<4; acase++){
unsigned int protocolMethodsCount=0;
BOOL isRequiredMethod=acase<2 ? NO : YES;
BOOL isInstanceMethod=(acase==0 || acase==2) ? NO : YES;
objc_method_description *protMeths=protocol_copyMethodDescriptionList(currentProtocol, isRequiredMethod, isInstanceMethod, &protocolMethodsCount);
for (unsigned gg=0; gg<protocolMethodsCount; gg++){
if (acase<2 && [protocolsMethodsString rangeOfString:@"@optional"].location==NSNotFound){
[protocolsMethodsString appendString:@"\n@optional\n"];
}
if (acase>1 && [protocolsMethodsString rangeOfString:@"@required"].location==NSNotFound){
[protocolsMethodsString appendString:@"\n@required\n"];
}
NSString *startSign=isInstanceMethod==NO ? @"+" : @"-";
objc_method_description selectorsAndTypes=protMeths[gg];
SEL selector=selectorsAndTypes.name;
char *types=selectorsAndTypes.types;
NSString *protSelector=NSStringFromSelector(selector);
NSString *finString=@"";
//CDLog(@"\t\t\t\tAbout to call cd_signatureWithObjCTypes of current protocol with types: %s",types);
NSMethodSignature *signature=[NSMethodSignature cd_signatureWithObjCTypes:types];
//CDLog(@"\t\t\t\tGot cd_signatureWithObjCTypes of current protocol");
NSString *returnType=commonTypes([NSString stringWithCString:[signature methodReturnType] encoding:NSUTF8StringEncoding],nil,NO);
NSArray *selectorsArray=[protSelector componentsSeparatedByString:@":"];
if (selectorsArray.count>1){
int argCount=0;
for (unsigned ad=2;ad<[signature numberOfArguments]; ad++){
argCount++;
NSString *space=ad==[signature numberOfArguments]-1 ? @"" : @" ";
finString=[finString stringByAppendingString:[NSString stringWithFormat:@"%@:(%@)arg%d%@" ,[selectorsArray objectAtIndex:ad-2],commonTypes([NSString stringWithCString:[signature cd_getArgumentTypeAtIndex:ad] encoding:NSUTF8StringEncoding],nil,NO),argCount,space]];
}
}
else{
finString=[finString stringByAppendingString:[NSString stringWithFormat:@"%@" ,[selectorsArray objectAtIndex:0]] ];
}
finString=[finString stringByAppendingString:@";"];
[protocolsMethodsString appendString:[NSString stringWithFormat:@"%@(%@)%@\n",startSign,returnType,finString]];
}
free(protMeths);
}
//FIX EQUAL TYPES OF PROPERTIES AND METHODS
NSArray *propertiesArray=propertiesArrayFromString(protPropertiesString);
[protPropertiesString release];
NSArray *lines=[protocolsMethodsString componentsSeparatedByString:@"\n"];
NSMutableString *finalString=[[NSMutableString alloc] init];
for (NSString *line in lines){
if (line.length>0 && ([line rangeOfString:@"-"].location==0 || [line rangeOfString:@"+"].location==0)){
NSString *methodInLine=[line substringFromIndex:[line rangeOfString:@")"].location+1];
methodInLine=[methodInLine substringToIndex:[methodInLine rangeOfString:@";"].location];
for (NSDictionary *dict in propertiesArray){
NSString *propertyName=[dict objectForKey:@"name"];
if ([methodInLine rangeOfString:@"set"].location!=NSNotFound){
NSString *firstCapitalized=[[propertyName substringToIndex:1] capitalizedString];
NSString *capitalizedFirst=[firstCapitalized stringByAppendingString:[propertyName substringFromIndex:1]];
if ([methodInLine isEqual:[NSString stringWithFormat:@"set%@",capitalizedFirst] ]){
// replace setter
NSString *newLine=[line substringToIndex:[line rangeOfString:@":("].location+2];
newLine=[newLine stringByAppendingString:[dict objectForKey:@"type"]];
newLine=[newLine stringByAppendingString:[line substringFromIndex:[line rangeOfString:@")" options:4].location]];
line=newLine;
}
}
if ([methodInLine isEqual:propertyName]){
NSString *newLine=[line substringToIndex:[line rangeOfString:@"("].location+1];
newLine=[newLine stringByAppendingString:[NSString stringWithFormat:@"%@)%@;",[dict objectForKey:@"type"],[dict objectForKey:@"name"]]];
line=newLine;
}
}
}
[finalString appendString:[line stringByAppendingString:@"\n"]];
}
if ([classesInProtocol count]>0){
NSMutableString *classesFoundToAdd=[[NSMutableString alloc] init];
[classesFoundToAdd appendString:@"@class "];
for (int f=0; f<classesInProtocol.count; f++){
NSString *classFound=[classesInProtocol objectAtIndex:f];
if (f<classesInProtocol.count-1){
[classesFoundToAdd appendString:[NSString stringWithFormat:@"%@, ",classFound]];
}
else{
[classesFoundToAdd appendString:[NSString stringWithFormat:@"%@;",classFound]];
}
}
[classesFoundToAdd appendString:@"\n\n"];
[classesFoundToAdd appendString:finalString];
[finalString release];
finalString=[classesFoundToAdd mutableCopy];
[classesFoundToAdd release];
}
[classesInProtocol release];
[protocolsMethodsString release];
[finalString appendString:@"@end\n\n"];
return finalString;
}
static BOOL hasMalformedID(NSString *parts){
if ([parts rangeOfString:@"@\""].location!=NSNotFound && [parts rangeOfString:@"@\""].location+2<parts.length-1 && ([[parts substringFromIndex:[parts rangeOfString:@"@\""].location+2] rangeOfString:@"\""].location==[[parts substringFromIndex:[parts rangeOfString:@"@\""].location+2] rangeOfString:@"\"\""].location || [[parts substringFromIndex:[parts rangeOfString:@"@\""].location+2] rangeOfString:@"\""].location==[[parts substringFromIndex:[parts rangeOfString:@"@\""].location+2] rangeOfString:@"\"]"].location || [[parts substringFromIndex:[parts rangeOfString:@"@\""].location+2] rangeOfString:@"\""].location==[parts substringFromIndex:[parts rangeOfString:@"@\""].location+2].length-1)){
return YES;
}
return NO;
}
/****** Structs Parser ******/
static NSString *representedStructFromStruct(NSString *inStruct,NSString *inName, BOOL inIvarList,BOOL isFinal){
if ([inStruct rangeOfString:@"\""].location==NSNotFound){ // not an ivar type struct, it has the names of types in quotes
if ([inStruct rangeOfString:@"{?="].location==0){
// UNKNOWN TYPE, WE WILL CONSTRUCT IT
NSString *types=[inStruct substringFromIndex:3];
types=[types substringToIndex:types.length-1];
for (NSDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"types"] isEqual:types]){
return [dict objectForKey:@"name"];
}
}
__block NSMutableArray *strctArray=[NSMutableArray array];
while ([types rangeOfString:@"{"].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{([^\\{^\\}]+)\\}" options:NSRegularExpressionCaseInsensitive error:nil];
__block NSString *blParts;
[regex enumerateMatchesInString:types options:0
range:NSMakeRange(0, [types length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSString *stringToPut=representedStructFromStruct([NSString stringWithFormat:@"{%@}",[types substringWithRange:[result rangeAtIndex:i]]],nil,NO,0);
blParts=[types stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%@}",[types substringWithRange:[result rangeAtIndex:i]]] withString:stringToPut];
if ([blParts rangeOfString:@"{"].location==NSNotFound){
[strctArray addObject:stringToPut];
}
break;
}
}];
types=blParts;
}
NSMutableArray *alreadyFoundStructs=[NSMutableArray array];
for (NSDictionary *dict in allStructsFound){
if ([types rangeOfString:[dict objectForKey:@"name"]].location!=NSNotFound || [types rangeOfString:@"CFDictionary"].location!=NSNotFound ){
BOOL isCFDictionaryHackException=0;
NSString *str;
if ([types rangeOfString:@"CFDictionary"].location!=NSNotFound){
str=@"CFDictionary";
isCFDictionaryHackException=1;
}
else{
str=[dict objectForKey:@"name"];
}
while ([types rangeOfString:str].location!=NSNotFound){
if ([str isEqual:@"CFDictionary"]){
[alreadyFoundStructs addObject:@"void*"];
}
else{
[alreadyFoundStructs addObject:str];
}
int replaceLocation=[types rangeOfString:str].location;
int replaceLength=str.length;
types=[types stringByReplacingCharactersInRange:NSMakeRange(replaceLocation,replaceLength) withString:@"+"];
}
}
}
__block NSMutableArray *arrArray=[NSMutableArray array];
while ([types rangeOfString:@"["].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[([^\\[^\\]]+)\\]" options:NSRegularExpressionCaseInsensitive error:nil];
__block NSString *blParts2;
[regex enumerateMatchesInString:types options:0
range:NSMakeRange(0, [types length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSString *stringToPut=[NSString stringWithFormat:@"[%@]",[types substringWithRange:[result rangeAtIndex:i]]];
NSRange range=[types rangeOfString:stringToPut];
blParts2=[types stringByReplacingCharactersInRange:NSMakeRange(range.location,range.length) withString:@"~"];
[arrArray addObject:stringToPut];
*stop=1;
break;
}
}];
types=blParts2;
}
__block NSMutableArray *bitArray=[NSMutableArray array];
while ([types rangeOfString:@"b1"].location!=NSNotFound || [types rangeOfString:@"b2"].location!=NSNotFound || [types rangeOfString:@"b3"].location!=NSNotFound || [types rangeOfString:@"b4"].location!=NSNotFound || [types rangeOfString:@"b5"].location!=NSNotFound || [types rangeOfString:@"b6"].location!=NSNotFound || [types rangeOfString:@"b7"].location!=NSNotFound || [types rangeOfString:@"b8"].location!=NSNotFound || [types rangeOfString:@"b9"].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(b[0-9]+)" options:nil error:nil];
__block NSString *blParts3;
[regex enumerateMatchesInString:types options:0
range:NSMakeRange(0, [types length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
NSString *stringToPut=[types substringWithRange:[result rangeAtIndex:i]];
blParts3=[types stringByReplacingOccurrencesOfString:[types substringWithRange:[result rangeAtIndex:i]] withString:@"§"];
[bitArray addObject:stringToPut];
break;
}
}];
types=blParts3;
}
for (NSString *string in strctArray){
if ([types rangeOfString:string].location==NSNotFound){
break;
}
int loc=[types rangeOfString:string].location;
int length=string.length;
types=[types stringByReplacingCharactersInRange:NSMakeRange(loc,length) withString:@"!"];
}
int fieldCount=0;
for (int i=0; i<types.length; i++){
NSString *string=[types substringWithRange:NSMakeRange(i,1)];
if (![string isEqual:@"["] && ![string isEqual:@"]"] && ![string isEqual:@"{"] && ![string isEqual:@"}"] && ![string isEqual:@"\""] && ![string isEqual:@"b"] && ![string isEqual:@"("] && ![string isEqual:@")"] ){
fieldCount++;
NSString *newString=[NSString stringWithFormat:@"\"field%d\"%@",fieldCount,commonTypes(string,nil,NO)];
types=[types stringByReplacingCharactersInRange:NSMakeRange(i,1) withString:[NSString stringWithFormat:@"\"field%d\"%@",fieldCount,commonTypes(string,nil,NO)]];
i+=newString.length-1;
}
}
int fCounter=-1; // Separate counters used for debugging purposes
while ([types rangeOfString:@"!"].location!=NSNotFound){
fCounter++;
int loc=[types rangeOfString:@"!"].location;
types=[types stringByReplacingCharactersInRange:NSMakeRange(loc,1) withString:[strctArray objectAtIndex:fCounter]];
}
int fCounter2=-1;
while ([types rangeOfString:@"~"].location!=NSNotFound){
fCounter2++;
int loc=[types rangeOfString:@"~"].location;
types=[types stringByReplacingCharactersInRange:NSMakeRange(loc,1) withString:[arrArray objectAtIndex:fCounter2]];
}
int fCounter3=-1;
while ([types rangeOfString:@"§"].location!=NSNotFound){
fCounter3++;
int loc=[types rangeOfString:@"§"].location;
types=[types stringByReplacingCharactersInRange:NSMakeRange(loc,1) withString:[bitArray objectAtIndex:fCounter3]];
}
int fCounter4=-1;
while ([types rangeOfString:@"+"].location!=NSNotFound){
fCounter4++;
int loc=[types rangeOfString:@"+"].location;
types=[types stringByReplacingCharactersInRange:NSMakeRange(loc,1) withString:[alreadyFoundStructs objectAtIndex:fCounter4]];
}
NSString *whatIBuilt=[NSString stringWithFormat:@"{?=%@}",types];
NSString *whatIReturn=representedStructFromStruct(whatIBuilt,nil,NO,YES);
return whatIReturn;
}
else{
if ([inStruct rangeOfString:@"="].location==NSNotFound){
inStruct=[inStruct stringByReplacingOccurrencesOfString:@"{" withString:@""];
inStruct=[inStruct stringByReplacingOccurrencesOfString:@"}" withString:@""];
return inStruct ;
}
int firstIson=[inStruct rangeOfString:@"="].location;
inStruct=[inStruct substringToIndex:firstIson];
inStruct=[inStruct substringFromIndex:1];
return inStruct;
}
}
int firstBrace=[inStruct rangeOfString:@"{"].location;
int ison=[inStruct rangeOfString:@"="].location;
NSString *structName=[inStruct substringWithRange:NSMakeRange(firstBrace+1,ison-1)];
NSString *parts=[inStruct substringFromIndex:ison+1];
parts=[parts substringToIndex:parts.length-1]; // remove last character "}"
if ([parts rangeOfString:@"{"].location==NSNotFound){ //does not contain other struct
if (hasMalformedID(parts)){
while ([parts rangeOfString:@"@"].location!=NSNotFound && hasMalformedID(parts)){
NSString *trialString=[parts substringFromIndex:[parts rangeOfString:@"@"].location+2];
if ([trialString rangeOfString:@"\""].location!=[trialString rangeOfString:@"\"\""].location && [trialString rangeOfString:@"\""].location!=trialString.length-1 && [trialString rangeOfString:@"]"].location!=[trialString rangeOfString:@"\""].location+1){
int location=[parts rangeOfString:@"@"].location;
parts=[parts stringByReplacingCharactersInRange:NSMakeRange(location-1,3) withString:@"\"id\""];
}
int location=[parts rangeOfString:@"@"].location;
if ([parts rangeOfString:@"@"].location!=NSNotFound){
NSString *asubstring=[parts substringFromIndex:location+2];
int nextlocation=[asubstring rangeOfString:@"\""].location;
asubstring=[asubstring substringWithRange:NSMakeRange(0,nextlocation)];
if ([classesInStructs indexOfObject:asubstring]==NSNotFound){
[classesInStructs addObject:asubstring];
}
parts=[parts stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"@\"%@\"",asubstring] withString:[NSString stringWithFormat:@"^%@",asubstring]];
}
}
}
NSMutableArray *brokenParts=[[parts componentsSeparatedByString:@"\""] mutableCopy];
[brokenParts removeObjectAtIndex:0];
NSString *types=@"";
BOOL reallyIsFlagInIvars=0;
if (inIvarList && [inName rangeOfString:@"flags" options:NSCaseInsensitiveSearch].location!=NSNotFound){
reallyIsFlagInIvars=1;
}
BOOL wasKnown=1;
if ([structName isEqual:@"?"]){
wasKnown=0;
structName=[NSString stringWithFormat:@"SCD_Struct_%@%d",classID,(int)[allStructsFound count]];
}
if ([structName rangeOfString:@"_"].location==0){
structName=[structName substringFromIndex:1];
}
NSString *representation=reallyIsFlagInIvars ? @"struct {\n" : (wasKnown ? [NSString stringWithFormat:@"typedef struct %@ {\n",structName] : @"typedef struct {\n");
for (int i=0; i<[brokenParts count]-1; i+=2){ // always an even number
NSString *nam=[brokenParts objectAtIndex:i];
NSString *typ=[brokenParts objectAtIndex:i+1];
types=[types stringByAppendingString:[brokenParts objectAtIndex:i+1]];
representation=reallyIsFlagInIvars ? [representation stringByAppendingString:[NSString stringWithFormat:@"\t\t%@ %@;\n",commonTypes(typ,&nam,NO),nam]] : [representation stringByAppendingString:[NSString stringWithFormat:@"\t%@ %@;\n",commonTypes(typ,&nam,NO),nam]];
}
representation=reallyIsFlagInIvars ? [representation stringByAppendingString:@"\t} "] : [representation stringByAppendingString: @"} "];
if ([structName rangeOfString:@"_"].location==0){
structName=[structName substringFromIndex:1];
}
if ([structName rangeOfString:@"_"].location==0){
structName=[structName substringFromIndex:1];
}
representation=reallyIsFlagInIvars ? representation : [representation stringByAppendingString:[NSString stringWithFormat:@"%@;\n\n",structName]];
if (isFinal && !reallyIsFlagInIvars){
for (NSMutableDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"types"] isEqual:types] && !wasKnown && ![[dict objectForKey:@"name"] isEqual:[dict objectForKey:@"types"]]){
NSString *repr=[dict objectForKey:@"representation"];
if ([repr rangeOfString:@"field"].location!=NSNotFound && [representation rangeOfString:@"field"].location==NSNotFound && ![structName isEqual:types]){
representation=[representation stringByReplacingOccurrencesOfString:structName withString:[dict objectForKey:@"name"]];
[dict setObject:representation forKey:@"representation"];
structName=[dict objectForKey:@"name"];
break;
}
}
}
}
BOOL found=NO;
for (NSDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"name"] isEqual:structName]){
found=YES;
return structName;
break;
}
}
if (!found){
for (NSMutableDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"types"] isEqual:types] && !wasKnown){
found=YES;
return [dict objectForKey:@"name"];
}
}
}
if (!found && !reallyIsFlagInIvars){
[allStructsFound addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:representation,@"representation",structName,@"name",types,@"types",nil]];
}
return reallyIsFlagInIvars ? representation : structName;
}
else{
// contains other structs,attempt to break apart
while ([parts rangeOfString:@"{"].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{([^\\{^\\}]+)\\}" options:NSRegularExpressionCaseInsensitive error:nil];
__block NSString *blParts;
[regex enumerateMatchesInString:parts options:0
range:NSMakeRange(0, [parts length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
blParts=[parts stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%@}",[parts substringWithRange:[result rangeAtIndex:i]]] withString:representedStructFromStruct([NSString stringWithFormat:@"{%@}",[parts substringWithRange:[result rangeAtIndex:i]]],nil,NO,0)];
break;
}
}];
parts=blParts;
}
NSString *rebuiltStruct=[NSString stringWithFormat:@"{%@=%@}",structName,parts];
NSString *final=representedStructFromStruct(rebuiltStruct,nil,NO,YES);
return final;
}
return inStruct;
}
/****** Unions Parser ******/
NSString *representedUnionFromUnion(NSString *inUnion){
if ([inUnion rangeOfString:@"\""].location==NSNotFound){
if ([inUnion rangeOfString:@"{?="].location==0){
NSString *types=[inUnion substringFromIndex:3];
types=[types substringToIndex:types.length-1];
for (NSDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"types"] isEqual:types]){
return [dict objectForKey:@"name"];
}
}
return inUnion;
}
else{
if ([inUnion rangeOfString:@"="].location==NSNotFound){
inUnion=[inUnion stringByReplacingOccurrencesOfString:@"{" withString:@""];
inUnion=[inUnion stringByReplacingOccurrencesOfString:@"}" withString:@""];
return inUnion ;
}
int firstIson=[inUnion rangeOfString:@"="].location;
inUnion=[inUnion substringToIndex:firstIson];
inUnion=[inUnion substringFromIndex:1];
return inUnion;
}
}
int firstParenthesis=[inUnion rangeOfString:@"("].location;
int ison=[inUnion rangeOfString:@"="].location;
NSString *unionName=[inUnion substringWithRange:NSMakeRange(firstParenthesis+1,ison-1)];
NSString *parts=[inUnion substringFromIndex:ison+1];
parts=[parts substringToIndex:parts.length-1]; // remove last character "}"
if ([parts rangeOfString:@"\"\"{"].location!=NSNotFound){
parts=[parts stringByReplacingOccurrencesOfString:@"\"\"{" withString:@"\"field0\"{"];
}
if ([parts rangeOfString:@"("].location!=NSNotFound){
while ([parts rangeOfString:@"("].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\(([^\\(^\\)]+)\\)" options:NSRegularExpressionCaseInsensitive error:nil];
__block NSString *unionParts;
[regex enumerateMatchesInString:parts options:0
range:NSMakeRange(0, [parts length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
unionParts=[parts stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"(%@)",[parts substringWithRange:[result rangeAtIndex:i]]] withString:representedUnionFromUnion([NSString stringWithFormat:@"(%@)",[parts substringWithRange:[result rangeAtIndex:i]]])];
break;
}
}];
parts=unionParts;
}
}
if ([parts rangeOfString:@"{"].location!=NSNotFound){
while ([parts rangeOfString:@"{"].location!=NSNotFound){
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{([^\\{^\\}]+)\\}" options:NSRegularExpressionCaseInsensitive error:nil];
__block NSString *structParts;
[regex enumerateMatchesInString:parts options:0
range:NSMakeRange(0, [parts length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
for (int i = 1; i< [result numberOfRanges] ; i++) {
structParts=[parts stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%@}",[parts substringWithRange:[result rangeAtIndex:i]]] withString:representedStructFromStruct([NSString stringWithFormat:@"{%@}",[parts substringWithRange:[result rangeAtIndex:i]]],nil,NO,NO)];
break;
}
}];
parts=structParts;
}
}
if (hasMalformedID(parts)){
while ([parts rangeOfString:@"@"].location!=NSNotFound && hasMalformedID(parts)){
NSString *trialString=[parts substringFromIndex:[parts rangeOfString:@"@"].location+2];
if ([trialString rangeOfString:@"\""].location!=[trialString rangeOfString:@"\"\""].location && [trialString rangeOfString:@"\""].location!=trialString.length-1 && [trialString rangeOfString:@"]"].location!=[trialString rangeOfString:@"\""].location+1){
int location=[parts rangeOfString:@"@"].location;
parts=[parts stringByReplacingCharactersInRange:NSMakeRange(location-1,3) withString:@"\"id\""];
}
int location=[parts rangeOfString:@"@"].location;
if ([parts rangeOfString:@"@"].location!=NSNotFound){
NSString *asubstring=[parts substringFromIndex:location+2];
int nextlocation=[asubstring rangeOfString:@"\""].location;
asubstring=[asubstring substringWithRange:NSMakeRange(0,nextlocation)];
if ([classesInStructs indexOfObject:asubstring]==NSNotFound){
[classesInStructs addObject:asubstring];
}
parts=[parts stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"@\"%@\"",asubstring] withString:[NSString stringWithFormat:@"^%@",asubstring]];
}
}
}
NSMutableArray *brokenParts=[[parts componentsSeparatedByString:@"\""] mutableCopy];
[brokenParts removeObjectAtIndex:0];
NSString *types=@"";
BOOL wasKnown=1;
if ([unionName isEqual:@"?"]){
wasKnown=0;
unionName=[NSString stringWithFormat:@"SCD_Union_%@%d",classID,(int)[allStructsFound count]];
}
if ([unionName rangeOfString:@"_"].location==0){
unionName=[unionName substringFromIndex:1];
}
NSString *representation=wasKnown ? [NSString stringWithFormat:@"typedef union %@ {\n",unionName] : @"typedef union {\n" ;
int upCount=0;
for (int i=0; i<[brokenParts count]-1; i+=2){ // always an even number
NSString *nam=[brokenParts objectAtIndex:i];
upCount++;
if ([nam rangeOfString:@"field0"].location!=NSNotFound){
nam=[nam stringByReplacingOccurrencesOfString:@"field0" withString:[NSString stringWithFormat:@"field%d",upCount]];
}
NSString *typ=[brokenParts objectAtIndex:i+1];
types=[types stringByAppendingString:[brokenParts objectAtIndex:i+1]];
representation=[representation stringByAppendingString:[NSString stringWithFormat:@"\t%@ %@;\n",commonTypes(typ,&nam,NO),nam]];
}
representation=[representation stringByAppendingString:@"} "];
representation=[representation stringByAppendingString:[NSString stringWithFormat:@"%@;\n\n",unionName]];
BOOL found=NO;
for (NSDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"name"] isEqual:unionName]){
found=YES;
return unionName;
break;
}
}
if (!found){
for (NSDictionary *dict in allStructsFound){
if ([[dict objectForKey:@"types"] isEqual:types] && !wasKnown){
found=YES;
return [dict objectForKey:@"name"];
break;
}
}
}
[allStructsFound addObject:[NSDictionary dictionaryWithObjectsAndKeys:representation,@"representation",unionName,@"name",types,@"types",nil]];
return unionName!=nil ? unionName : inUnion;