Skip to content

Commit

Permalink
added -h
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeyGilly committed Feb 16, 2022
1 parent a5061f4 commit 72c419b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
38 changes: 19 additions & 19 deletions src/formatting/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
#include "../headers/config.hpp"

void removeSingleAnn(std::vector<std::string> &fileContents, std::string ann) {
for (size_t i = 0; i < fileContents.size(); i++) {
if (fileContents[i].find(ann) != (size_t)-1) {
for (int i = 0; i < fileContents.size(); i++) {
if (fileContents[i].find(ann) != (int)-1) {
fileContents[i] = fileContents[i].substr(0, fileContents[i].find(ann));
}
}
}
void removeMultiAnn(std::vector<std::string> &fileContents, std::string ann) {
size_t pos;
size_t pos2;
for (size_t i = 0; i < fileContents.size(); i++) {
if ((pos=fileContents[i].find(ann)) != (size_t)-1) {
if ((pos2=fileContents[i].find(ann, pos + ann.length())) != (size_t)-1) {
int pos;
int pos2;
for (int i = 0; i < fileContents.size(); i++) {
if ((pos=fileContents[i].find(ann)) != (int)-1) {
if ((pos2=fileContents[i].find(ann, pos + ann.length())) != (int)-1) {
fileContents[i] = fileContents[i].substr(0, pos) + fileContents[i].substr(pos2 + ann.length());
} else {
fileContents[i] = fileContents[i].substr(0, pos);
i++;
while ((pos=fileContents[i].find(ann)) == (size_t)-1) {
while ((pos=fileContents[i].find(ann)) == (int)-1) {
fileContents.erase(fileContents.begin() + i);
i++;
}
Expand All @@ -34,7 +34,7 @@ void removeMultiAnn(std::vector<std::string> &fileContents, std::string ann) {
}

void removeEmptyLines(std::vector<std::string> &fileContents) {
for (size_t i = 0; i < fileContents.size(); i++) {
for (int i = 0; i < fileContents.size(); i++) {
if (fileContents[i].empty()) {
fileContents.erase(fileContents.begin() + i);
i--;
Expand All @@ -43,15 +43,15 @@ void removeEmptyLines(std::vector<std::string> &fileContents) {
}

void replaceWinNewLines(std::vector<std::string> &fileContents) {
for (size_t i = 0; i < fileContents.size(); i++) {
for (int i = 0; i < fileContents.size(); i++) {
fileContents[i].erase(std::remove(fileContents[i].begin(), fileContents[i].end(), '\r'), fileContents[i].end());
}
}

void removeBlankLines(std::vector<std::string> &fileContents) {
bool removeline = true;
for (size_t i = 0; i < fileContents.size(); i++) {
for (size_t j = 0; j < fileContents[i].length(); j++) if (fileContents[i][j] != ' ' && fileContents[i][j] != '\t') removeline = false;
for (int i = 0; i < fileContents.size(); i++) {
for (int j = 0; j < fileContents[i].length(); j++) if (fileContents[i][j] != ' ' && fileContents[i][j] != '\t') removeline = false;
if (removeline) {
fileContents.erase(fileContents.begin() + i);
i--;
Expand All @@ -63,8 +63,8 @@ void removeBlankLines(std::vector<std::string> &fileContents) {
std::pair<bool,bool> isSpaced(std::vector<std::string> &fileContents) {
bool isSpaced = false;
bool error = false;
for (size_t i = 0; i < fileContents.size(); i++) {
if (fileContents[i].find(":") != (size_t)-1) {
for (int i = 0; i < fileContents.size(); i++) {
if (fileContents[i].find(":") != (int)-1) {
if (fileContents[i+1][0] == ' ') isSpaced = true;
else if (fileContents[i+1][0] != '\t') error = true;
}
Expand All @@ -78,9 +78,9 @@ std::tuple<std::vector<int>, int, int> detectIndentation(std::vector<std::string
int spaces;
std::vector<int> spacesVec;
int indentation = 0;
for (size_t i = 0; i < fileContents.size(); i++) {
for (int i = 0; i < fileContents.size(); i++) {
spaces = 0;
for (size_t j = 0; j < fileContents[i].length(); j++) {
for (int j = 0; j < fileContents[i].length(); j++) {
if (fileContents[i][j] == ' ') while (fileContents[i][j] == ' ') {
spaces++;
j++;
Expand All @@ -89,7 +89,7 @@ std::tuple<std::vector<int>, int, int> detectIndentation(std::vector<std::string
}
spacesVec.push_back(spaces);
}
for (size_t i = 0; i < spacesVec.size(); i++) {
for (int i = 0; i < spacesVec.size(); i++) {
if (spacesVec[i] > 0) {
for (int j = std::stoi(getValue("MAX_INDENT_SPACE")); j > 1; j--) {
if (spacesVec[i] % j == 0) {
Expand All @@ -100,12 +100,12 @@ std::tuple<std::vector<int>, int, int> detectIndentation(std::vector<std::string
break;
}
}
for (size_t i = 0; i < spacesVec.size(); i++) if (spacesVec[i] % indentation != 0) {return std::tuple<std::vector<int>, int, int>(spacesVec, indentation, 1);}
for (int i = 0; i < spacesVec.size(); i++) if (spacesVec[i] % indentation != 0) {return std::tuple<std::vector<int>, int, int>(spacesVec, indentation, 1);}
return std::tuple<std::vector<int>, int, int>(spacesVec, indentation, 0);
}

void replaceIndentation(std::vector<std::string> &fileContents, std::vector<int> &spacesVec, int indentation) {
for (size_t i = 0; i < fileContents.size(); i++) {
for (int i = 0; i < fileContents.size(); i++) {
for (int j = spacesVec[i] / indentation; j > 0; j--) {
fileContents[i].replace((indentation * j)-indentation, indentation, "\t");
}
Expand Down
1 change: 0 additions & 1 deletion src/formatting/languages/python/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void pyFormat(std::vector<std::string> &fileContents) {
if (std::get<2>(indent) == -1) return; // if indent error
replaceIndentation(fileContents, std::get<0>(indent), std::get<1>(indent));
}
// check each module succeeds
// remove multiple spaces after text and before newline
// remove space around operators > < = ! () [] {} , : ? | &
}
42 changes: 36 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@
#include "headers/formatting.hpp"

int main(int argc, char* argv[]) {
std::string file; /*int compact = 0;*/ std::string output; std::string language; /*bool debug = false;*/
std::string file; int compact = 0; std::string output; std::string language; bool debug = false;
for (int i = 0; i < argc - 1; i++) {
if (!std::strcmp(argv[i], "-f") && isFileReal(argv[i + 1])) file = argv[i + 1];
// if (!std::strcmp(argv[i], "-c")) compact = atoi(argv[i + 1]);
if (!std::strcmp(argv[i], "-c")) compact = atoi(argv[i + 1]);
if (!std::strcmp(argv[i], "-o")) output = argv[i + 1];
if (!std::strcmp(argv[i], "-l")) language = argv[i + 1];
// if (!std::strcmp(argv[i], "-d")) debug = 1;
if (!std::strcmp(argv[i], "-d")) debug = 1;
if (!std::strcmp(argv[i], "-h")) {
std::cout << "usage stuff..." << std::endl;
std::cout << "No file specified" << std::endl;
std::cout << "CompactiferPlusPlus V1.0" << std::endl;
std::cout << "Usage: " << argv[0] << " [options]" << std::endl;
std::cout << "Compacts a Python File" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "\t-f <file> : Specify the file to be compacted [Required]" << std::endl;
std::cout << "\t-c <int> : Specify the compaction level of the program"
<< std::endl << "\t\t0 - No compaction"
<< std::endl << "\t\t1 - Remove comments and format"
<< std::endl << "\t\t2 - Optimize Code for Compaction"
<< std::endl << "\t\t3 - More optimize"
<< std::endl << "\t\tRead More about compaction levels in https://github.com/JakeyGilly/CompactifierPlusPlus/blob/main/stages.md" << std::endl;
std::cout << "\t-o <file> : Specify the output file" << std::endl;
std::cout << "\t-l <language> : Specify the language of the file" << std::endl;
std::cout << "\t-d : Enable debug mode" << std::endl;
std::cout << "\t-h : Print this help message" << std::endl;
return 0;
}
}
Expand Down Expand Up @@ -41,6 +56,21 @@ int main(int argc, char* argv[]) {
}
} else {
std::cout << "No file specified" << std::endl;
std::cout << "usage stuff..." << std::endl;
std::cout << "CompactiferPlusPlus V1.0" << std::endl;
std::cout << "Usage: " << argv[0] << " [options]" << std::endl;
std::cout << "Compacts a Python File" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "\t-f <file> : Specify the file to be compacted [Required]" << std::endl;
std::cout << "\t-c <int> : Specify the compaction level of the program"
<< std::endl << "\t\t0 - No compaction"
<< std::endl << "\t\t1 - Remove comments and format"
<< std::endl << "\t\t2 - Optimize Code for Compaction"
<< std::endl << "\t\t3 - More optimize"
<< std::endl << "\t\tRead More about compaction levels in https://github.com/JakeyGilly/CompactifierPlusPlus/blob/main/stages.md" << std::endl;
std::cout << "\t-o <file> : Specify the output file" << std::endl;
std::cout << "\t-l <language> : Specify the language of the file" << std::endl;
std::cout << "\t-d : Enable debug mode" << std::endl;
std::cout << "\t-h : Print this help message" << std::endl;
return 0;
}
}
}

0 comments on commit 72c419b

Please sign in to comment.