Skip to content

Commit 7c3a2ea

Browse files
committed
added config
1 parent 798a768 commit 7c3a2ea

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ else
44
TARGET = Compactifier.out
55
endif
66

7-
SOURCE= src/main.cpp src/fileio.cpp src/formatting/global.cpp src/formatting/languages/python/formatter.cpp
8-
HEADER= headers/fileio.hpp headers/formatting.hpp
7+
SOURCE= src/main.cpp src/fileio.cpp src/formatting/global.cpp src/formatting/languages/python/formatter.cpp src/config.cpp
8+
HEADER= headers/fileio.hpp headers/formatting.hpp headers/config.hpp
99
CC = g++
10-
FLAGS= -Wall -std=c++20
10+
FLAGS= -Wall -std=c++20 -g
1111

1212
all: $(OBJS)
1313
$(CC) ${SOURCE} -o $(TARGET) ${FLAGS}

config.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# =============================================================================
2+
#
3+
# CompactifierPlusPlus V1.0
4+
# Configuration file for the CompactifierPlusPlus
5+
#
6+
# =============================================================================
7+
8+
MAX_INDENT_SPACE = 16

src/config.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <algorithm>
4+
#include <vector>
5+
#include "headers/fileio.hpp"
6+
7+
#define CONFIG_FILE "config.cfg"
8+
9+
std::vector<std::pair<std::string, std::string>> pharseConfig() {
10+
std::vector<std::string> fileContents = readFile(CONFIG_FILE);
11+
std::vector<std::pair<std::string, std::string>> config;
12+
for (int i = 0; i < fileContents.size(); i++) {
13+
if (fileContents[i][0] == '#') continue; // comments
14+
if (fileContents[i][0] == '\n') continue; // empty lines
15+
if (fileContents[i][0] == '\r') continue; // empty lines
16+
if (fileContents[i].find("=") == std::string::npos) continue; // no =
17+
fileContents[i].erase(std::remove(fileContents[i].begin(), fileContents[i].end(), ' '), fileContents[i].end());
18+
std::string key = fileContents[i].substr(0, fileContents[i].find("="));
19+
std::string value = fileContents[i].substr(fileContents[i].find("=") + 1);
20+
config.push_back(std::make_pair(key, value));
21+
}
22+
std::cout << "Config file loaded" << std::endl;
23+
// print keys loaded
24+
std::string keys;
25+
for (int i = 0; i < config.size(); i++) {
26+
keys += config[i].first + ", ";
27+
}
28+
std::cout << "Loaded Keys: " << keys << std::endl;
29+
return config;
30+
}
31+
32+
bool checkKey(std::string key) {
33+
std::vector<std::pair<std::string, std::string>> config = pharseConfig();
34+
for (int i = 0; i < config.size(); i++) {
35+
if (config[i].first == key) return true;
36+
}
37+
return false;
38+
}
39+
40+
std::string getValue(std::string key) {
41+
std::vector<std::pair<std::string, std::string>> config = pharseConfig();
42+
for (int i = 0; i < config.size(); i++) {
43+
if (config[i].first == key) return config[i].second;
44+
}
45+
return "";
46+
}
47+

src/formatting/languages/python/formatter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <algorithm>
44
#include <vector>
55
#include "../../../headers/formatting.hpp"
6+
#include "../../../headers/config.hpp"
67

78
#define PYTHON_ANNATATION "#" // Single line comment
89
#define PYTHON_ML_ANNATATION "\"\"\"" // Multi line comment
@@ -37,8 +38,7 @@ void pyFormat(std::vector<std::string> &fileContents) {
3738
}
3839
for (int i = 0; i < spacesVec.size(); i++) {
3940
if (spacesVec[i] > 0) {
40-
// get max spaces from config
41-
for (int j = 12; j > 1; j--) {
41+
for (int j = stoi(getValue("MAX_INDENT_SPACE")); j > 1; j--) {
4242
if (spacesVec[i] % j == 0) {
4343
indentation = j;
4444
break;
@@ -65,6 +65,7 @@ void pyFormat(std::vector<std::string> &fileContents) {
6565
}
6666

6767
// replace spaces with tabs
68+
// check each module succeeds
6869
// remove multiple spaces after text and before newline
6970
// remove blank lines
7071
// while ((pos=fileContents.find("\n\n", 0)) != -1) { fileContents.erase(pos, 1); }

src/headers/config.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
std::vector<std::pair<std::string, std::string>> pharseConfig();
2+
bool checkKey(std::string);
3+
std::string getValue(std::string);

0 commit comments

Comments
 (0)