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
|
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
|
+
+
+
-
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
+
+
|
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <errno.h>
#import "Property.h"
#import "OFApplication.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFStdIOStream.h"
#import "OFString.h"
#import "OFOpenItemFailedException.h"
void
newClass(OFString *name, OFString *superclass)
newClass(OFString *name, OFString *superclass, OFMutableArray *properties)
{
OFString *headerPath = [name stringByAppendingPathExtension: @"h"];
OFString *implPath = [name stringByAppendingPathExtension: @"m"];
OFFile *headerFile = nil, *implFile = nil;
@try {
headerFile = [OFFile fileWithPath: headerPath mode: @"wx"];
implFile = [OFFile fileWithPath: implPath mode: @"wx"];
} @catch (OFOpenItemFailedException *e) {
if (e.errNo != EEXIST)
@throw e;
[OFStdErr writeFormat: @"File %@ already exists! Aborting...\n",
e.path];
[OFApplication terminateWithStatus: 1];
}
if (superclass == nil)
superclass = @"OFObject";
for (size_t i = 0; i < properties.count; i++) {
Property *property = [Property propertyWithString:
[properties objectAtIndex: i]];
[properties replaceObjectAtIndex: i
withObject: property];
}
[headerFile writeFormat: @"#import <ObjFW/ObjFW.h>\n"
@"\n"
@"OF_ASSUME_NONNULL_BEGIN\n"
@"\n"
@"@interface %@: %@\n"
@"@interface %@: %@\n",
@"@end\n",
name, superclass];
if (properties.count > 0)
[headerFile writeString: @"{\n"];
for (Property *property in properties)
[headerFile writeFormat: @"\t%@_%@;\n",
property.type, property.name];
if (properties.count > 0)
[headerFile writeString: @"}\n\n"];
for (Property *property in properties)
[headerFile writeFormat: @"@property %@%@;\n",
property.type, property.name];
[headerFile writeString: @"@end\n"
@"\n"
@"OF_ASSUME_NONNULL_END\n"];
[implFile writeFormat: @"#import \"%@\"\n"
@"\n"
@"@implementation %@\n"
@"@implementation %@\n",
@"@end\n",
headerPath, name];
for (Property *property in properties)
[implFile writeFormat: @"@synthesize %@ = _%@;\n",
property.name, property.name];
[implFile writeString: @"@end\n"];
[headerFile close];
[implFile close];
}
|