Repository navigation
Expand file tree
/
Copy pathcmdc.cpp
More file actions
169 lines (146 loc) · 4.32 KB
/
Copy pathcmdc.cpp
File metadata and controls
169 lines (146 loc) · 4.32 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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
#define VERSION 1.2f
std::string setName()
{
std::string nomFichier;
std::cout << "Entrer le nom du fichier : ";
std::cin >> nomFichier;
return nomFichier;
}
char* toUpper(char* className)
{
char temp;
for (unsigned i = 0; i < strlen(className); i++)
{
temp = className[i];
className[i] = toupper(temp);
}
return className;
}
void createSourceFile(std::string nomFichier)
{
std::ofstream fichier(nomFichier + ".h");
// Vérifie si le fichier est bien ouvert
if (fichier.is_open())
{
fichier << "#ifndef "<< toUpper(nomFichier.data()) << "_H" << endl;
fichier << "#define "<< toUpper(nomFichier.data()) << "_H\n"<< endl;
// Écrit les en-têtes d'include de base
fichier << "#include <stdio.h>\n";
fichier << "#include <stdlib.h>\n\n";
fichier << "#endif //"<< toUpper(nomFichier.data()) << "_H" << endl;
// Ferme le fichier
fichier.close();
std::cout << "Le fichier " << nomFichier << ".h a ete cree avec succes." << std::endl;
}
else
{
std::cerr << "Erreur: Impossible de créer le fichier " << nomFichier << std::endl;
}
}
void createCFile(bool isCreateSourceFile, std::string nomFichier)
{
std::ofstream fichier(nomFichier + ".c");
// Vérifie si le fichier est bien ouvert
if (fichier.is_open())
{
// Écrit les en-têtes d'include de base
if (isCreateSourceFile)
{
fichier << "#include \"";
fichier << nomFichier;
fichier << ".h\"\n";
}
fichier << "#include <stdio.h>\n";
fichier << "#include <stdlib.h>\n\n";
fichier << "int main() {\n";
fichier << " // Code ici\n";
fichier << " printf(\"%s\", \"hello world\");\n";
fichier << " return 0;\n";
fichier << "}\n";
// Ferme le fichier
fichier.close();
std::cout << "Le fichier " << nomFichier << ".c a ete cree avec succes." << std::endl;
}
else
{
std::cerr << "Erreur: Impossible de creer le fichier " << nomFichier << std::endl;
}
}
void openEditor()
{
// Ouvre automatiquement l'éditeur de code avec le dossier courant
#ifdef _WIN32
system("code . || subl . || atom .");
#elif __APPLE__
system("open -a 'Visual Studio Code' . || open -a 'Sublime Text' . || open -a Atom .");
#else
system("code . || subl . || atom .");
#endif
}
float get_version()
{
return VERSION;
}
void displayHelp()
{
cout << "\n\nCommand create C file [version " << get_version() << "]\n" << endl;
cout << "MENU HELP : \n"
<< endl;
cout << "Create a C file in your project directory : \n"
<< endl;
cout << "write : <cmdc> in your command console\n" << endl;
cout << "\t o add option : [-o] or [-open] => open the file in your code editor" << endl;
cout << "\t o add option : [-s] or [-source] => create a source file linked to the .c file" << endl;
cout << "\t o add option : [-h] or [-help] => open the menu help" << endl;
cout << "\nexample : \tcmdc -o " << endl;
}
int main(int argc, char **argv)
{
bool isOpenFile = false;
bool isHelp = false;
bool isCreateSourceFile = false;
// Vérifie s'il y a au moins deux arguments (nom du programme + au moins un argument)
if (argc >= 2)
{
for (int i = 1; i < argc; i++)
{
std::string argument = argv[i];
if (argument == "-o" || argument == "-open")
{
isOpenFile = true;
}
if (argument == "-s" || argument == "-source")
{
isCreateSourceFile = true;
}
if (argument == "-h" || argument == "-help")
{
isHelp = true;
displayHelp();
exit(0);
}
}
std::string fileName = setName();
if (isCreateSourceFile)
{
createSourceFile(fileName);
}
createCFile(isCreateSourceFile, fileName);
}
else
{
std::string fileName = setName();
createCFile(false, fileName);
}
if (isOpenFile)
{
openEditor();
}
return 0;
}