Merge pull request 'Add way to extract numbers from the csv file' (#1) from read-file into main

Reviewed-on: #1
This commit is contained in:
Rasmus Rasmussen 2025-03-18 09:53:32 +00:00
commit b99b13383f
8 changed files with 103 additions and 6 deletions

13
Include/FileHelper.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef FILEHELPER_H
#define FILEHELPER_H
class FileHelper {
public:
FileHelper();
~FileHelper();
static bool FileExists(char *path);
};
#endif

View File

@ -1,13 +1,16 @@
#ifndef FILEREADER_H #ifndef FILEREADER_H
#define FILEREADER_H #define FILEREADER_H
#include <string>
#include <vector>
class FileReader { class FileReader {
public: public:
FileReader(); FileReader();
~FileReader(); ~FileReader();
char *Read(); static std::string GetFile(char *path);
}; };
#endif #endif

16
Include/NumberHelper.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef NUMBERHELPER_H
#define NUMBERHELPER_H
#include <string>
#include <vector>
class NumberHelper {
public:
NumberHelper();
~NumberHelper();
static std::vector<int> GetNumbers(const std::string csv);
};
#endif

1
rolls.txt Normal file
View File

@ -0,0 +1 @@
2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2

16
src/FileHelper.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "../Include/FileHelper.h"
#include <sys/stat.h>
FileHelper::FileHelper() {};
FileHelper::~FileHelper() {};
bool FileHelper::FileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.
if (stat(path, &s) == 0 && !(s.st_mode & S_IFDIR)){
return true;
}
return false;
}

View File

@ -1,9 +1,18 @@
#include "../Include/FileReader.h" #include "../Include/FileReader.h"
#include <iostream> #include <string>
#include <fstream>
FileReader::FileReader() {}; FileReader::FileReader() {};
FileReader::~FileReader() {}; FileReader::~FileReader() {};
char *FileReader::Read(){ std::string FileReader::GetFile(char *path){
return nullptr; std::ifstream file(path);
std::string line;
std::getline(file, line);
file.close();
return line;
} }

19
src/NumberHelper.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "../Include/NumberHelper.h"
#include <string>
#include <sstream>
#include <vector>
NumberHelper::NumberHelper() {};
NumberHelper::~NumberHelper() {};
std::vector<int> NumberHelper::GetNumbers(const std::string csv){
std::vector<int> rolls;
std::stringstream ss(csv);
std::string number;
while (std::getline(ss, number, ',')) {
rolls.push_back(std::stoi(number));
}
return rolls;
}

View File

@ -1,7 +1,27 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "../Include/FileReader.h"
#include "../Include/FileHelper.h"
#include "../Include/NumberHelper.h"
int main() { int main(int argc, char *argv[]) {
std::cout << "Hello, World!" << std::endl; if(argc < 2) {
std::cerr << "Please provide a CSV formatted file.";
return 0;
}
if(!FileHelper::FileExists(argv[1])) {
std::cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
std::string file = FileReader::GetFile(argv[1]);
std::vector<int> numbers = NumberHelper::GetNumbers(file);
for(int i = 0; i < numbers.size(); i++) {
std::cout << numbers[i] << ' ';
}
return 0; return 0;
} }