-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.cpp
More file actions
324 lines (279 loc) · 8.61 KB
/
template.cpp
File metadata and controls
324 lines (279 loc) · 8.61 KB
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
/*********************************************************
* template builds template .cpp, .c, .h and .java files (will support
* more file types in the future) based on a file name and
* #include, import lines provided at the command line. The
* file is opened after creation.
*
*
* .cpp Example:
* template awesome.cpp iostream vector string
* Output:
*
* #include <iostream>
* #include <vector>
* #include <string>
*
* using namespace std;
*
* int main(int argc, char * argv[])
* {
*
* return 0;
* }
*
* .c Example:
* template something.c stdio.h
* Output:
*
* #include <stdio.h>
*
* int main(int argc, char * argv[])
* {
*
* return 0;
* }
*
*
* .h Example:
* template awesome.h vector string
*
* Output:
*
* #ifndef TEST_H
* #def TEST_H
*
*
* #include <iostream>
* #include <string>
*
*
* #endif // TEST_H
*
*
*
*
* .java Example:
* template something.java util.* math.*
*
* Output:
*
* import java.util.*;
* import java.math.*;
*
* public class something {
*
* public static void main(String[] args) {
*
*
*
*
* }
* }
*
*********************************************************/
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// helper functions
string getFileType(string & filename);
void addImportAndIncludeLines(vector<string> & linesOfFile,
string & fileType, char * dependency);
void buildFile(vector<string> & linesOfFile, string & fileType);
void buildCOrCPlusPlusFile(vector<string> & linesOfFile, string & fileType);
void buildHeaderFile(vector<string> & linesOfFile, string & filename);
void buildJavaFile(vector<string> & linesOfFile, string & filename);
void writeToFile(vector<string> & linesOfFile, string & filename) throw(string);
void addHeaderFileDeclaration(vector<string> & linesOfFile, string & filename);
void capitalizeHeaderFileName(string & filename);
/*********************************************************
* main - handles the command line arguments and builds
* the template file, then opens it.
*********************************************************/
int main(int argc, char * argv[])
{
// declare variables
string filename;
string fileType;
vector<string> linesOfFile;
// make sure something was actually passed in
// if not, show proper usage
if (argc > 1)
{
filename = argv[1];
}
else
{
cout << "Usage: template <filename> (optional: <files> <to> <include>)\n";
return 0;
}
// get the file extension
fileType = getFileType(filename);
if (strcmp(fileType.c_str(), "h") == 0)
addHeaderFileDeclaration(linesOfFile, filename);
// add any #include / import files that were entered
// at the command line
for (int i = 2; i < argc; i++)
addImportAndIncludeLines(linesOfFile, fileType, argv[i]);
// build the template file
buildFile(linesOfFile, filename);
// try to write the template to the provided file name
// then open it to begin editing
try
{
writeToFile(linesOfFile, filename);
// command to open the file after creation
string command = "open ";
command += filename;
// open the file
system(command.c_str());
}
catch (string error)
{
cout << error << endl;
}
return 0;
}
/*********************************************************
* returns the file extension
*********************************************************/
string getFileType(string & filename)
{
return filename.substr(filename.find('.') + 1);
}
/*********************************************************
* addHeaderFileDeclaration adds the #ifndef statement
* for the header file
*********************************************************/
void addHeaderFileDeclaration(vector<string> & linesOfFile, string & filename)
{
string headerDeclaration = filename.substr(0, filename.find('.'));
capitalizeHeaderFileName(headerDeclaration);
string ifStatement = "#ifndef ";
ifStatement += headerDeclaration;
linesOfFile.push_back(ifStatement);
string defStatement = "#def ";
defStatement += headerDeclaration;
linesOfFile.push_back(defStatement);
linesOfFile.push_back("\n");
}
/*********************************************************
* capitalizeHeaderFileName capitilatizes the provided
* filename and adds '_H' to it for the #ifndef statement
*********************************************************/
void capitalizeHeaderFileName(string & filename)
{
transform(filename.begin(), filename.end(),filename.begin(), ::toupper);
filename += "_H";
}
/*********************************************************
* addImportAndIncludeLines adds the dependency properly
* formatted based on the file type:
* #include <dependency>;
* import java.dependency;
*********************************************************/
void addImportAndIncludeLines(vector<string> & linesOfFile, string & fileType, char * dependency)
{
// decide whether to #include or import.java.. based on
// file extension
switch (fileType.at(fileType.size() - 1)) {
case 'c':
case 'h':
case 'p':
{
string includeString = "#include <";
includeString += dependency;
includeString += ">";
linesOfFile.push_back(includeString);
break;
}
case 'a':
{
string importString = "import java.";
importString += dependency;
importString += ";";
linesOfFile.push_back(importString);
break;
}
}
}
/*********************************************************
* buildFile builds the correct file based on file type
*********************************************************/
void buildFile(vector<string> & linesOfFile, string & filename)
{
// calls the proper template builder based on file extension
string fileType = getFileType(filename);
switch (fileType.at(fileType.size() - 1)) {
case 'p':
case 'c':
buildCOrCPlusPlusFile(linesOfFile, fileType);
break;
case 'h':
buildHeaderFile(linesOfFile, filename);
break;
case 'a':
buildJavaFile(linesOfFile, filename);
}
}
/*********************************************************
* buildCPlusPlusFile - builds the template .cpp file plus
* the #include lines added at the command line
*********************************************************/
void buildCOrCPlusPlusFile(vector<string> & linesOfFile, string & fileType)
{
if (strcmp(fileType.c_str(), "cpp") == 0) {
linesOfFile.push_back("\nusing namespace std;");
}
linesOfFile.push_back("\nint main(int argc, char * argv[])\n{\n");
linesOfFile.push_back("\treturn 0;\n}");
}
/*********************************************************
* buildHeaderFile - builds the template .h file plus
* the #include lines added at the command line
*********************************************************/
void buildHeaderFile(vector<string> & linesOfFile, string & filename)
{
string headerDeclaration = filename.substr(0, filename.find('.'));
capitalizeHeaderFileName(headerDeclaration);
linesOfFile.push_back("\n");
string endIf = "#endif // ";
endIf += headerDeclaration;
linesOfFile.push_back(endIf);
}
/*********************************************************
* buildJavaFile - builds the template .java file plus
* the import lines added at the command line
*********************************************************/
void buildJavaFile(vector<string> & linesOfFile, string & filename)
{
// adds the class name based on the file name
string className = "\npublic class ";
className += filename.substr(0, filename.find('.'));
className += " {";
linesOfFile.push_back(className);
// ...a lot of \'s, sorry!
linesOfFile.push_back("\n\tpublic static void main(String[] args) {\n\n\n\n\n\t}\n}");
}
/*********************************************************
* writeToFile writes the template to a file with the
* provided file name.
*********************************************************/
void writeToFile(vector<string> & linesOfFile, string & filename) throw(string)
{
ofstream output;
output.open(filename.c_str());
// throws error if the file can't be opened to write
if (!output.is_open())
{
string error = "Error opening file \'";
error += filename;
error += "\'";
throw error;
}
for (int i = 0; i < linesOfFile.size(); i++)
output << linesOfFile.at(i) << endl;
output.close();
}