C++#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <dirent.h>
using namespace std;
char curpath[100005];
bool flag = false;
string oldpa, pa, en;
ifstream fpr;
ofstream fop, frp;
void rte(){
frp.open("path.txt", ios::out);
frp << pa;
frp.close();
}
void bke(){
pa.pop_back();
while(pa.back() != '/' && pa.length() != 0) pa.pop_back();
rte();
return ;
}
bool isex(string name){
ifstream f(name.data());
return f.good();
}
int main()
{
if(_getcwd(curpath, sizeof(curpath)) != NULL) printf("Create files from '%s'\n\n", curpath);
else{
printf("Failed to load current path.");
return 0;
}
if(!isex("path.txt")) ofstream outfile("path.txt");
fpr.open("path.txt", ios::in);
fpr >> pa;
fpr.close();
if(pa.length() != 0){
while(opendir(pa.data()) == NULL){
if(pa.length() == 0) break;
if(!flag){
oldpa = pa;
flag = true;
}
bke();
rte();
}
}
if(flag){
printf("Detect that last path is '");
cout << oldpa;
printf("', but some folders has not existed, so the path modified automatically.\n\n");
}
cout << pa << ">>";
while(cin >> en){
if(en == "back"){
if(pa.length() != 0) bke();
else printf("Cannot back anymore.\n");
}
else if(en == "goto"){
string dir, tot;
cin >> dir;
if(dir == "." || dir == ".."){
printf("Invalid open.\n");
cout << pa << ">>";
continue;
}
tot = pa + dir + "/";
if(opendir(tot.data()) == NULL){
printf("Invalid open.\n");
cout << pa << ">>";
continue;
}
pa = tot;
rte();
}
else if(en == "create"){
string name;
cin >> name;
string tot = pa + name + ".cpp";
if(isex(tot)){
printf("There has already been a file named '");
cout << name << ".cpp";
printf("'.\n");
cout << pa << ">>";
continue;
}
ofstream outfile(tot);
string wrt[10];
fop.open(tot, ios::out);
if(fop.bad()){
printf("Invalid output.\n");
cout << pa << ">>";
continue;
}
wrt[1] = "#include <iostream>";
wrt[2] = "#include <cstdio>";
wrt[3] = "using namespace std;";
wrt[4] = "int main()";
wrt[5] = "{";
wrt[6] = " return 0;";
wrt[7] = "}";
wrt[8] = "// g++ " + name + ".cpp -o " + name + " -std=c++14 -static";
for(int i = 1;i <= 8;i++) fop << wrt[i] << endl;
fop.close();
}
else if(en == "dirfile"){
string name;
cin >> name;
string tot = pa + name + "/";
if(opendir(tot.data()) == NULL) mkdir(tot.data());
tot = tot + name + ".cpp";
if(isex(tot)){
printf("There has already been a file named '");
cout << name << ".cpp";
printf("'.\n");
cout << pa << ">>";
continue;
}
ofstream outfile(tot);
string wrt[10];
fop.open(tot, ios::out);
if(fop.bad()){
printf("Invalid output.\n");
cout << pa << ">>";
continue;
}
wrt[1] = "#include <iostream>";
wrt[2] = "#include <cstdio>";
wrt[3] = "using namespace std;";
wrt[4] = "int main()";
wrt[5] = "{";
wrt[6] = " return 0;";
wrt[7] = "}";
wrt[8] = "// g++ " + name + ".cpp -o " + name + " -std=c++14 -static";
for(int i = 1;i <= 8;i++) fop << wrt[i] << endl;
fop.close();
}
else if(en == "cd") printf("If you want to modify the path. Please use 'goto <foldername>'\n");
else if(en == "cd..") printf("If you want to modify the path. Please use 'back'\n");
else if(en == "print"){
string prt;
cin >> prt;
cout << prt << endl;
}
else if(en == "printf"){
string ptr;
cin >> ptr;
ptr = ptr + "\n";
printf(ptr.data());
}
else if(en == "scan" || en == "scanf"){
string sca;
cin >> sca;
bool flg = false;
if(isdigit(sca[0])) printf("Invalid variable name.\n");
else{
for(int i = 0;i < sca.length();i++){
if(i == 0 && (sca[i] == '&' || sca[i] == '%')) continue;
if(!(sca[i] >= 'A' && sca[i] <= 'Z') && !(sca[i] >= 'a' && sca[i] <= 'z') && sca[i] != '_' && !isdigit(sca[i])){
flg = true;
break;
}
}
if(!flg) printf("Successfully scanned!\n");
else printf("Invalid variable name.\n");
}
}
else if(en == "exit") break;
else printf("Invalid command.\n");
cout << pa << ">>";
}
rte();
return 0;
}