Skip to content

Commit f6d2e1d

Browse files
committed
converted strings to vector strings
1 parent 129e769 commit f6d2e1d

File tree

6 files changed

+73
-57
lines changed

6 files changed

+73
-57
lines changed

src/fileio.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <filesystem>
22
#include <fstream>
33
#include <string>
4-
#include <sstream>
4+
#include <vector>
55
#include <iostream>
66

77
bool isFileReal(std::string file) {
@@ -12,20 +12,29 @@ std::string filetype(std::string file) {
1212
return std::filesystem::path(file).extension().string();
1313
}
1414

15-
std::string readFile(std::string file) {
16-
std::ifstream t(file);
17-
std::stringstream buffer;
18-
buffer << t.rdbuf();
19-
return buffer.str();
15+
std::vector<std::string> readFile(std::string file) {
16+
std::ifstream in(file);
17+
if (!in) {
18+
return std::vector<std::string>();
19+
}
20+
std::string str;
21+
std::vector<std::string> fileContents;
22+
while (std::getline(in, str)) {
23+
fileContents.push_back(str);
24+
}
25+
in.close();
26+
return fileContents;
2027
}
2128

2229
int fileSize(std::string file) {
2330
std::ifstream in(file, std::ifstream::ate | std::ifstream::binary);
2431
return (int)in.tellg();
2532
}
2633

27-
void writeFile(std::string file, std::string contents) {
28-
std::ofstream infile(file);
29-
infile << contents;
30-
infile.close();
34+
void writeFile(std::string file, std::vector<std::string>& contents) {
35+
std::ofstream in(file);
36+
for (int i = 0; i < contents.size(); i++) {
37+
in << contents[i] << std::endl;
38+
}
39+
in.close();
3140
}

src/formatting/global.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
#include <string>
22
#include <iostream>
3+
#include <vector>
34
#include "../headers/formatting.hpp"
45

5-
std::string removeSingleAnn(std::string fileContents, std::string ann) {
6-
size_t pos;
7-
int newline;
8-
while ((pos = fileContents.find(ann, 0)) != -1) {
9-
newline = fileContents.find("\n", pos);
10-
if (newline == -1) {
11-
fileContents.erase(pos, fileContents.length() - pos);
12-
} else {
13-
fileContents.erase(pos, newline - pos);
6+
void removeSingleAnn(std::vector<std::string> &fileContents, std::string ann) {
7+
for (int i = 0; i < fileContents.size(); i++) {
8+
if (fileContents[i].find(ann) != -1) {
9+
fileContents[i] = fileContents[i].substr(0, fileContents[i].find(ann));
1410
}
1511
}
16-
return fileContents;
1712
}
18-
std::string removeMultiAnn(std::string fileContents, std::string ann) {
13+
void removeMultiAnn(std::vector<std::string> &fileContents, std::string ann) {
1914
size_t pos;
20-
int end;
21-
while ((pos = fileContents.find(ann, 0)) != -1) {
22-
end = fileContents.find(ann, pos + ann.length());
23-
if (end == -1) {
24-
fileContents.erase(pos, fileContents.length() - pos);
25-
} else {
26-
fileContents.erase(pos, end - pos + ann.length());
15+
size_t pos2;
16+
for (int i = 0; i < fileContents.size(); i++) {
17+
if ((pos=fileContents[i].find(ann)) != -1) {
18+
if ((pos2=fileContents[i].find(ann, pos + ann.length())) != -1) {
19+
fileContents[i] = fileContents[i].substr(0, pos) + fileContents[i].substr(pos2 + ann.length());
20+
} else {
21+
fileContents[i] = fileContents[i].substr(0, pos);
22+
i++;
23+
while ((pos=fileContents[i].find(ann)) == -1) {
24+
fileContents.erase(fileContents.begin() + i);
25+
i++;
26+
}
27+
fileContents[i] = fileContents[i].substr(pos + ann.length());
28+
}
2729
}
2830
}
29-
return fileContents;
3031
}

src/formatting/languages/python/formatter.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
#define PYTHON_ANNATATION "#" // Single line comment
77
#define PYTHON_ML_ANNATATION "\"\"\"" // Multi line comment
88

9-
std::string pyRemoveAnnatations(std::string fileContents) {
10-
fileContents = removeSingleAnn(fileContents, PYTHON_ANNATATION);
11-
fileContents = removeMultiAnn(fileContents, PYTHON_ML_ANNATATION);
12-
return fileContents;
9+
10+
void pyRemoveAnnatations(std::vector<std::string> &fileContents) {
11+
removeSingleAnn(fileContents, PYTHON_ANNATATION);
12+
removeMultiAnn(fileContents, PYTHON_ML_ANNATATION);
1313
}
1414

15-
std::string pyFormat(std::string fileContents) {
16-
size_t pos;
17-
fileContents.erase(std::remove(fileContents.begin(), fileContents.end(), '\t'), fileContents.end());
18-
fileContents.replace(std::remove(fileContents.begin(), fileContents.end(), '\r'), fileContents.end(), "\n");
19-
while ((pos=fileContents.find("\n\n", 0)) != -1) { fileContents.erase(pos, 1); }
20-
if (fileContents[fileContents.length() - 1] == '\n') { fileContents.erase(fileContents.length() - 1); }
21-
if (fileContents[0] == '\n') { fileContents.erase(0, 1); }
22-
return fileContents;
15+
void pyFormat(std::vector<std::string> &fileContents) {
16+
std::cout << "Formatting Python" << std::endl;
17+
18+
// size_t pos;
19+
20+
21+
// // replace \r\n with \n
22+
// fileContents.replace(std::remove(fileContents.begin(), fileContents.end(), '\r'), fileContents.end(), "\n");
23+
// // replace double newline with one newline
24+
// while ((pos=fileContents.find("\n\n", 0)) != -1) { fileContents.erase(pos, 1); }
25+
// // remove newlines at the end of the file and at the beginning of the file
26+
// if (fileContents[fileContents.length() - 1] == '\n') { fileContents.erase(fileContents.length() - 1); }
27+
// if (fileContents[0] == '\n') { fileContents.erase(0, 1); }
28+
// // remove multiple spaces after text and before newline
29+
// // remove blank lines
30+
// while ((pos=fileContents.find("\n\n", 0)) != -1) { fileContents.erase(pos, 1); }
31+
// // remove space around operators > < = ! () [] {} , : ? | &
2332
}

src/headers/fileio.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
std::string filetype(std::string);
44
bool isFileReal(std::string);
5-
std::string readFile(std::string);
5+
std::vector<std::string> readFile(std::string);
66
int fileSize(std::string);
7-
void writeFile(std::string, std::string);
7+
void writeFile(std::string, std::vector<std::string>&);

src/headers/formatting.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

33
// GLOBAL
4-
std::string removeSingleAnn(std::string, std::string);
5-
std::string removeMultiAnn(std::string, std::string);
4+
void removeSingleAnn(std::vector<std::string>&, std::string);
5+
void removeMultiAnn(std::vector<std::string>&, std::string);
66

77
// PYTHON
8-
std::string pyRemoveAnnatations(std::string);
9-
std::string pyFormat(std::string);
8+
void pyRemoveAnnatations(std::vector<std::string>&);
9+
void pyFormat(std::vector<std::string>&);

src/main.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ bool debug = false;
1919
}
2020
}
2121
if (!!file.compare("")) {
22-
std::string fileContents = readFile(file);
22+
std::vector<std::string> fileContents = readFile(file);
23+
if (fileContents.empty()) {
24+
std::cout << "File not found" << std::endl;
25+
return 1;
26+
}
2327
std::cout << "Opened File " << file << std::endl << "Type " << filetype(file) << std::endl << "File size: " << fileSize(file) << " Bytes" << std::endl;
2428
// Compaction begins here
2529
if (!filetype(file).compare(".py")) {
26-
// struct indentation indent = getIndent(fileContents);
27-
// std::cout << fileContents << std::endl;
28-
fileContents = pyRemoveAnnatations(fileContents);
29-
fileContents = pyFormat(fileContents);
30-
std::cout << fileContents << std::endl;
30+
pyRemoveAnnatations(fileContents);
31+
pyFormat(fileContents);
3132
} else {
3233
std::cout << "File type not supported" << std::endl;
3334
}
@@ -44,7 +45,3 @@ bool debug = false;
4445
std::cout << "usage stuff..." << std::endl;
4546
}
4647
}
47-
48-
typedef struct indentation {
49-
int* indent, lenindent;
50-
} indentation;

0 commit comments

Comments
 (0)