Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
CppProperties.json

#ignore database files
Inventory.csv
Userfile.csv
Userinfo.txt

Expand Down
7 changes: 7 additions & 0 deletions Data/Inventory.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BID,NAME,AUTHOR,COPIES,RENTED,LINK
B123456,FirstBook,AUTHOR Name,0020,0020,LINK
B222222,SecondBook,AUTHOR Name,0020,0014,LINK
B333333,ThirdBook,AUTHOR Name,0020,0000,LINK
B444444,FoursBook,AUTHOR Name,9999,9999,LINK
B555555,FifthBook,AUTHOR Name,0020,0020,LINK
B666666,SixthBook,AUTHOR Name,0020,0020,LINK
Binary file modified LSMS.exe
Binary file not shown.
72 changes: 72 additions & 0 deletions src/Book.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "Book.h"

Book::Book(csv::Row* _ptr_row){
init(_ptr_row);
}

Book::Book(csv::Row* _ptr_row, csv::CSVParser* _ptr_parser) : Book(_ptr_row){
if (!_ptr_parser)
throw csv::Error("Book: Parser points to nullptr");
mptr_parser = _ptr_parser;
}

void Book::init(csv::Row *_ptr_row)
{
if (!_ptr_row)
throw csv::Error("Book: Row points to nullptr");
mptr_info = _ptr_row;
}

Book::~Book(){
delete mptr_info;
}

size_t Book::increase_rented(){
size_t currently_rented = 0;
int rented_length = 4;
std::string currently_rented_s(rented_length--, '0');

try
{
currently_rented = std::stoi(mptr_info->getvalue("RENTED").data());
++currently_rented;
//Format string to 0001
for (int val = (currently_rented < 0) ? -currently_rented : currently_rented; rented_length >= 0 && val != 0; --rented_length, val /= 10)
currently_rented_s[rented_length] = '0' + val % 10;
if (rented_length >= 0 && currently_rented < 0)
currently_rented_s[0] = '-';

}
catch (const std::invalid_argument &e)
{
return csv::npos;
}

if(mptr_info->change_value_in_to("RENTED", currently_rented_s))
if(mptr_parser->updateRow(mptr_info))
return currently_rented;
else
return csv::npos;
else
return csv::npos;
}

bool Book::is_available(){
try{
int copies = std::stoi(mptr_info->getvalue("COPIES").data());
int rented = std::stoi(mptr_info->getvalue("RENTED").data());
return copies - rented;
}
catch(const std::invalid_argument &){
return false;
}
return false;
}

std::string_view Book::get_BID(){
return mptr_info->getvalue(0);
}

csv::Row &Book::get_Row(){
return *mptr_info;
}
22 changes: 22 additions & 0 deletions src/Book.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "CSVParser.h"
#include <string>

class Book{
private:
csv::Row* mptr_info = nullptr;
csv::CSVParser* mptr_parser = nullptr;
public:
Book() = delete;
Book(csv::Row*);
Book(csv::Row*, csv::CSVParser*);
~Book();

void init(csv::Row*);

size_t increase_rented();

bool is_available();
std::string_view get_BID();
csv::Row& get_Row();
};
109 changes: 93 additions & 16 deletions src/CSVParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ namespace csv
mptr_header = _header;
}

Row::Row(std::string_view _row, Row* _header, const size_t& _index = npos): Row(_row) {
mptr_header = _header;
m_index = _index;
}

Row::Row(std::vector<std::string> &_row){
m_data.swap(_row);
_row.clear();
Expand All @@ -32,15 +37,42 @@ namespace csv
m_data.clear();
};

unsigned int Row::size(){
size_t Row::size(){
return m_data.size();
}

void Row::add_value(std::string_view _value){
m_data.push_back(std::string(_value));
};

std::string_view Row::getvalue(_HEADER_TYPE &_header) const
bool Row::change_value_in_to(std::string_view _header, std::string_view _newvalue){
if (mptr_header)
{
size_t pos = mptr_header->get_item_position(_header);
if(_newvalue.size() == m_data.at(pos).size()){
m_data.at(pos) = _newvalue;
} else{
return false;
}
}
else
{
throw Error("Row: not linked to valid header");
}
return true;
}

std::string_view Row::string(){
m_rowstring.clear();
for(int i = 0; i < m_data.size() - 1; i++){
m_rowstring += m_data.at(i) + ",";
}
m_rowstring += m_data.at(m_data.size()-1);
m_rowstring += "\r\n";
return m_rowstring;
}

std::string_view Row::getvalue(unsigned int _header) const
{
if(_header < m_data.size())
return m_data.at(_header);
Expand All @@ -49,7 +81,20 @@ namespace csv
}
};

_HEADER_TYPE &Row::get_item_position(std::string_view _header)
std::string_view Row::getvalue(std::string_view _header) const
{
if (mptr_header)
{
size_t pos = mptr_header->get_item_position(_header);
return m_data.at(pos);
}
else
{
throw Error("Row: not linked to valid header");
}
}

unsigned int &Row::get_item_position(std::string_view _header)
{
std::vector<std::string>::const_iterator it;
m_item_pos = 0;
Expand All @@ -64,19 +109,24 @@ namespace csv
throw Error("Row: Item not found");
}

const size_t& Row::getindex()
{
return m_index;
}

Row& Row::set_headerptr( Row* _headerptr){
mptr_header = _headerptr;
return *mptr_header;
}

std::string_view Row::operator[] (_HEADER_TYPE &_header) const {
std::string_view Row::operator[] (unsigned int &_header) const {
return this->getvalue(_header);
};

std::string_view Row::operator[] (std::string_view _header) const {
if (mptr_header)
{
_HEADER_TYPE pos = mptr_header->get_item_position(_header);
size_t pos = mptr_header->get_item_position(_header);
return m_data.at(pos);
}
else
Expand Down Expand Up @@ -133,7 +183,8 @@ namespace csv
{
while (fm::_getline(m_DATABASE, *tmp_line))
{
m_content.push_back(Row(*tmp_line, _ptr_header));
++m_index_pos;
m_content.push_back(Row(*tmp_line, _ptr_header, m_index_pos));
}
}
catch (const std::fstream::failure &e)
Expand All @@ -149,7 +200,7 @@ namespace csv
catch (const std::fstream::failure &e)
{
try{
m_create_database();
mf_create_database();
_csvgood = true;
}
catch (const std::fstream::failure &e){
Expand All @@ -170,7 +221,7 @@ namespace csv
m_content.clear();
}

const unsigned int CSVParser::size() {
const size_t CSVParser::size() {
return m_content.size();
}

Expand Down Expand Up @@ -222,19 +273,45 @@ namespace csv
}
}

bool CSVParser::find_first_of(std::string_view _str, std::string_view _pos){
_HEADER_TYPE _tmp_pos = _ptr_header->get_item_position(_pos);
bool CSVParser::updateRow(Row* _row)
{
if (!m_DATABASE.is_open())
{
return false;
}
try
{
std::string tmp_line = "";
m_DATABASE.seekp(0, std::ios::beg);
for (int i = 0; i < _row->getindex(); i++)
{
fm::_getline(m_DATABASE, tmp_line);
}
m_DATABASE.seekp(m_DATABASE.tellg());
m_DATABASE << _row->string();
m_DATABASE.flush();
m_DATABASE.clear();
}
catch (std::ios::failure &e)
{
return false;
}
return true;
}

Row* CSVParser::find_first_of(std::string_view _str, std::string_view _header){
unsigned int _tmp_pos = _ptr_header->get_item_position(_header);

for(int i = 0; i < m_content.size(); i++){
if(m_content.at(i).getvalue(_tmp_pos) == _str){
return true;
return &m_content.at(i);
}
}

return false;
return nullptr;
}

std::fstream &CSVParser::m_create_database()
std::fstream &CSVParser::mf_create_database()
{
m_DATABASE.open(m_CURRENT_FILE, std::ios::in | std::ios::out | std::ios::binary | std::ios::app);
addRow(*_ptr_header);
Expand All @@ -251,15 +328,15 @@ namespace csv
#ifdef _DEBUG_CSV
void CSVParser::print_csv()
{
for (_HEADER_TYPE i = 0; i < _ptr_header->_header_size; i++)
for (size_t i = 0; i < _ptr_header->_header_size; i++)
{
std::cout << _ptr_header->getvalue(i);
std::cout << ",";
}
std::cout << std::endl;
for (_HEADER_TYPE i = 0; i < m_content.size(); i++)
for (size_t i = 0; i < m_content.size(); i++)
{
for (_HEADER_TYPE i2 = 0; i2 < _ptr_header->_header_size; i2++)
for (size_t i2 = 0; i2 < _ptr_header->_header_size; i2++)
{
std::cout << m_content.at(i).getvalue(i2);
std::cout << ",";
Expand Down
Loading