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:
commit
b99b13383f
13
Include/FileHelper.h
Normal file
13
Include/FileHelper.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef FILEHELPER_H
|
||||
#define FILEHELPER_H
|
||||
|
||||
class FileHelper {
|
||||
public:
|
||||
FileHelper();
|
||||
|
||||
~FileHelper();
|
||||
|
||||
static bool FileExists(char *path);
|
||||
};
|
||||
|
||||
#endif
|
@ -1,13 +1,16 @@
|
||||
#ifndef FILEREADER_H
|
||||
#define FILEREADER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class FileReader {
|
||||
public:
|
||||
FileReader();
|
||||
|
||||
~FileReader();
|
||||
|
||||
char *Read();
|
||||
static std::string GetFile(char *path);
|
||||
};
|
||||
|
||||
#endif
|
16
Include/NumberHelper.h
Normal file
16
Include/NumberHelper.h
Normal 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
1
rolls.txt
Normal 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
16
src/FileHelper.cpp
Normal 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;
|
||||
}
|
@ -1,9 +1,18 @@
|
||||
#include "../Include/FileReader.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
FileReader::FileReader() {};
|
||||
FileReader::~FileReader() {};
|
||||
|
||||
char *FileReader::Read(){
|
||||
return nullptr;
|
||||
std::string FileReader::GetFile(char *path){
|
||||
std::ifstream file(path);
|
||||
|
||||
std::string line;
|
||||
|
||||
std::getline(file, line);
|
||||
|
||||
file.close();
|
||||
|
||||
return line;
|
||||
}
|
19
src/NumberHelper.cpp
Normal file
19
src/NumberHelper.cpp
Normal 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;
|
||||
}
|
24
src/main.cpp
24
src/main.cpp
@ -1,7 +1,27 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "../Include/FileReader.h"
|
||||
#include "../Include/FileHelper.h"
|
||||
#include "../Include/NumberHelper.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
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] << ' ';
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user