-
Notifications
You must be signed in to change notification settings - Fork 0
/
classCreator.c
executable file
·142 lines (132 loc) · 3.2 KB
/
classCreator.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
#include "stdio.h"
#include "unistd.h"
#include "fcntl.h"
#include "errno.h"
#include "stdlib.h"
#include "string.h"
static void write_to_file(char *s, int fd)
{
int i;
i = write(fd, s, strlen(s));
if (i < 0)
printf("%d", errno);
//printf("i->%d\n", i);
}
static void write_hpp(int fd, char *name)
{
write_to_file("#pragma once\n\n#include <iostream>\n\nclass ", fd);
write_to_file(name, fd);
write_to_file("{\n\tprivate:\n\n\tpublic:\n\t\t", fd);
//default
write_to_file(name, fd);
write_to_file("( void );\n\t\t", fd);
//copy
write_to_file(name, fd);
write_to_file("( const ", fd);
write_to_file(name, fd);
write_to_file(" &", fd);
write(fd, name, 1);
write_to_file(" );\n\t\t", fd);
//=
write_to_file(name, fd);
write_to_file("& operator=( const ", fd);
write_to_file(name, fd);
write_to_file(" &", fd);
write(fd, name, 1);
write_to_file(" );\n\t\t~", fd);
//destructor
write_to_file(name, fd);
write_to_file("( void );\n\n};", fd);
}
void write_cpp(int fd, char *name, char *include)
{
write_to_file("#include \"", fd);
write_to_file(include, fd);
write_to_file("\"\n\n",fd);
//default constructor
write_to_file(name, fd);
write_to_file("::",fd);
write_to_file(name, fd);
write_to_file("( void ){\n\tstd::cout << \"", fd);
write_to_file(name, fd);
write_to_file(" created by default!\\n\";\n}\n\n", fd);
//copy
write_to_file(name, fd);
write_to_file("::",fd);
write_to_file(name, fd);
write_to_file("( const ",fd);
write_to_file(name, fd);
write_to_file(" &", fd);
write(fd, name, 1);
write_to_file(" ){\n\tstd::cout << \"", fd);
write_to_file(name, fd);
write_to_file(" Copy created!\\n\";\n\t*this = ", fd);
write(fd, name, 1);
write_to_file(";\n}\n\n", fd);
//=
write_to_file(name, fd);
write_to_file("& ",fd);
write_to_file(name, fd);
write_to_file("::operator=( const ", fd);
write_to_file(name, fd);
write_to_file(" &", fd);
write(fd, name, 1);
write_to_file(" ){\n\tstd::cout << \"", fd);
write_to_file(name, fd);
write_to_file(" Copy created!\\n\";\n\tif (this != &", fd);
write(fd, name, 1);
write_to_file(")\n\n\treturn (*this);\n}\n\n", fd);
//Destructor
write_to_file(name, fd);
write_to_file("::~",fd);
write_to_file(name, fd);
write_to_file("( void ){\n\tstd::cout << \"", fd);
write_to_file(name, fd);
write_to_file(" destroyed!\\n\";\n}\n\n", fd);
}
static char *getName(char *s, char option)
{
char *strReturn = malloc(sizeof(char) * strlen(s) + 5);
int i = 0;
while(s[i])
{
strReturn[i] = s[i];
i++;
}
strReturn[i++] = '.';
if (option == 'h')
strReturn[i++] = 'h';
else if (option == 'c')
strReturn[i++] = 'c';
strReturn[i++] = 'p';
strReturn[i++] = 'p';
strReturn[i] = '\0';
return (strReturn);
}
int main(int ac, char **argv){
int fd_hpp, fd_cpp;
char *n1, *n2;
if (ac != 2)
{
write(2, "Wrong number of arguments!\n", 27);
exit(1);
}
else
{
n1 = getName(argv[1], 'h');
n2 = getName(argv[1], 'c');
fd_hpp = open(n1, O_RDWR | O_CREAT, 0777);
fd_cpp = open(n2, O_RDWR | O_CREAT, 0777);
if (fd_cpp < 0 || fd_hpp <= 0)
fprintf(stderr, "open() not executed correctly! errno:%d\n", errno);
else
{
write_hpp(fd_hpp, argv[1]);
write_cpp(fd_cpp, argv[1], n1);
close(fd_hpp);
close(fd_cpp);
free(n1);
free(n2);
}
}
}