Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/data pack #508

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4081de5
First commit
Mira-Chronos Feb 26, 2022
0530cff
Bmp file for test : Log Horizon
Mira-Chronos Feb 26, 2022
20f6229
Read with SDL_LoadBMP_RW
Mira-Chronos Feb 26, 2022
62f3bb5
Read from memory
Mira-Chronos Feb 26, 2022
87b2a8d
Update
Mira-Chronos Feb 26, 2022
b100fdf
Read 2° picture from test.pak
Mira-Chronos Feb 26, 2022
e0e2f4f
Update
Mira-Chronos Feb 26, 2022
6c782c2
Rename data
Mira-Chronos Feb 26, 2022
8ece904
Hand writing reading Pack
Mira-Chronos Feb 26, 2022
94f1cbd
Better function's name
Mira-Chronos Feb 26, 2022
2c1899e
WriterPack's skeleton
Mira-Chronos Feb 26, 2022
a7f1611
Fix index in uint32_t
Mira-Chronos Feb 27, 2022
f0ab4ec
WriterPack's implementation
Mira-Chronos Feb 27, 2022
74dd30a
Names's improvement
Mira-Chronos Feb 27, 2022
f237c3b
ReaderPack's squeleton v2
Mira-Chronos Feb 27, 2022
1e8e770
Code cosmetic
Mira-Chronos Feb 27, 2022
23fdaff
ReaderPack's squeleton v3
Mira-Chronos Feb 27, 2022
d1b0ce9
ReaderPack implementation v2
Mira-Chronos Feb 27, 2022
fd4d360
Using ReaderPack
Mira-Chronos Feb 27, 2022
4162be7
Useless File
Mira-Chronos Feb 27, 2022
fc3a8a7
cIniFile first commit
Mira-Chronos Mar 1, 2022
69192d4
cIniFile test
Mira-Chronos Mar 1, 2022
90c0929
Fix cIniFile conversion
Mira-Chronos Mar 1, 2022
ca63f12
Code cosmetic
Mira-Chronos Mar 1, 2022
b7294b7
Read game.ini
Mira-Chronos Mar 1, 2022
7a70d67
Fix bug this data
Mira-Chronos Mar 1, 2022
1ad3315
Implementation getSectionsFromIni && getKeyFromSection
Mira-Chronos Mar 1, 2022
20c58c6
Merge branch 'master' into feature/dataPack
Mira-Chronos Mar 25, 2022
f986f53
Fix memory leak
Mira-Chronos Mar 25, 2022
745b82d
Creation DataPack
Mira-Chronos Mar 25, 2022
ff8a9bf
Using DataPack
Mira-Chronos Mar 25, 2022
2527004
Fix magical number to const int
Mira-Chronos Mar 25, 2022
4f23f13
Using only one vector with struct
Mira-Chronos Mar 25, 2022
0ac5836
Distinction between fileName and fileId
Mira-Chronos Mar 26, 2022
1391120
ESCAPE quit the program
Mira-Chronos Mar 26, 2022
2d6bb06
Clean code
Mira-Chronos Mar 26, 2022
40708f3
Commented code
Mira-Chronos Mar 26, 2022
7150dec
Merge branch 'master' into feature/dataPack
Mira-Chronos Sep 24, 2022
af93516
Make test with audio file
Mira-Chronos Sep 25, 2022
1ffde69
displayPackFile improvement
Mira-Chronos Sep 26, 2022
23864c2
More documentation
Mira-Chronos Sep 26, 2022
f495ccd
Free music on exit
Mira-Chronos Sep 26, 2022
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
14 changes: 14 additions & 0 deletions tools/cIniFile/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CC=g++
CFLAGS=
DFLAGS= -g
DEPS = *.h
OBJ = cIniFile.o main.o

%.o: %.c $(DEPS)
$(CC) -c $@ $< $(DFLAGS)

main: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)

clean :
rm $(OBJ) main
219 changes: 219 additions & 0 deletions tools/cIniFile/cIniFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#include "cIniFile.h"
#include <fstream>
#include <iostream>
#include <algorithm>


static void Trim(std::string& str)
{
str.erase(str.find_last_not_of(" \t")+1); //suffixing spaces
str.erase(0, str.find_first_not_of(" \t")); //prefixing spaces
}


//
// cSection class
//

cSection::cSection()
{}

cSection::~cSection()
{
m_sectionConf.clear();
m_dataConfs.clear();
}

cSection::cSection(const std::string& secName) : m_sectionName(secName)
{}

bool cSection::addValue(const std::string& key, const std::string& value)
{
if (m_sectionConf.find(value) != m_sectionConf.end()) {
std::cout << "Key " << key << " already exist on section " << m_sectionName << std::endl;
return false;
}
m_sectionConf[key] = value;
return true;
}

bool cSection::addData(const std::string& data)
{
m_dataConfs.push_back(data);
return true;
}



std::string cSection::getValue(const std::string& key) const
{
if (m_sectionConf.find(key) != m_sectionConf.end()) {
return m_sectionConf.at(key);
} else {
std::cout << "key " << key << " didn't exist on section " << m_sectionName << std::endl;
return std::string();
}
}

std::list<std::string> cSection::getAllKey() const
{
std::list<std::string> mList;
for (auto& x: m_sectionConf) {
mList.push_back(x.first);
}
return mList;
}


//
// cIniFile class
//

cIniFile::cIniFile(const std::string &configFileName)
:m_fileName(configFileName){
load(m_fileName);
}

cIniFile::~cIniFile(void) { }


bool cIniFile::load(const std::string& config)
{
m_fileName = config;
std::ifstream in(m_fileName.c_str());
if (!in) {
std::cout << "unable to open file " << m_fileName << std::endl;
return false;
}
std::string line, secName, lastSecName;
while (!in.eof()){
// get raw line
getline(in, line);
Trim(line);
// commented line
if (line.empty() || line.find(';') == 0 || line.find('#') == 0)
continue;

//test if new section
if (isSectionName(line) && !m_actualSection.empty()) {
// test if already exist
if (m_mapConfig.find(m_actualSection) != m_mapConfig.end()) {
std::cout << "section " << m_actualSection << " already exist" << std::endl;
continue;
}
m_mapConfig[m_actualSection] = cSection(m_actualSection);
continue;
}
// test if key=value
if (isKeyValue(line) && !m_actualSection.empty()) {
m_mapConfig[m_actualSection].addValue(m_actualKey, m_actualValue);
continue;
}

//none from forward so string is a data from section
if (!m_actualSection.empty()) {
m_mapConfig[m_actualSection].addData(line);
continue;
}

//all tests are wrong
std::cout << "error " << line << " or no section found" << std::endl;
}
return true;
}

bool cIniFile::isSectionName(std::string inputLine)
{
size_t sec_begin_pos = inputLine.find('[');
if (sec_begin_pos == std::string::npos || sec_begin_pos != 0){
return false;
}
size_t sec_end_pos = inputLine.find(']', sec_begin_pos);
if (sec_end_pos == std::string::npos){
return false;
}

m_actualSection = (inputLine.substr(sec_begin_pos + 1, sec_end_pos - sec_begin_pos - 1));
Trim(inputLine);
return true;
}

bool cIniFile::isKeyValue(std::string inputLine)
{
size_t keyPos = inputLine.find('=');
if (keyPos == std::string::npos || keyPos == 0 || keyPos == inputLine.length()-1 ){
m_actualKey.clear();
m_actualValue.clear();
return false;
}
std::string key = inputLine.substr(0, keyPos);
Trim(key);
m_actualKey = key;

std::string value = inputLine.substr(keyPos+1);
Trim(value);
m_actualValue = value;

if (m_actualKey.empty() || m_actualValue.empty()) {
m_actualKey.clear();
m_actualValue.clear();
return false;
}
return true;
}

std::string cIniFile::getStr(const std::string& section, const std::string& key) const
{
if (m_mapConfig.find(section) != m_mapConfig.end()) {
return m_mapConfig.at(section).getValue(key);
} else {
std::cout << " getStr section " << section << " didn't exist" << std::endl;
return std::string();
}
}

template<typename T> T cIniFile::FromString(const std::string& value) const
{
std::istringstream ss(value);
T res;
ss >> res;
return res;
}

int cIniFile::getInt(const std::string& section, const std::string& key) const
{
return FromString<int>(getStr(section, key));
}

double cIniFile::getDouble(const std::string& section, const std::string& key) const
{
return FromString<double>(getStr(section, key));
}

bool cIniFile::getBoolean(const std::string& section, const std::string& key) const
{
std::string value = getStr(section, key);
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c){ return std::tolower(c); });
if (value == "on" || value== "1" || value == "true")
return true;

return false;
}

std::list<std::string> cIniFile::getSectionsFromIni() const
{
std::list<std::string> mList;
for (auto& x: m_mapConfig) {
mList.push_back(x.first);
}
return mList;
}


std::list<std::string> cIniFile::getKeyFromSection(const std::string& section ) const
{
std::list<std::string> mList;
if (m_mapConfig.find(section) != m_mapConfig.end())
mList = m_mapConfig.at(section).getAllKey();
return mList;
}
52 changes: 52 additions & 0 deletions tools/cIniFile/cIniFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <string>
#include <map>
#include <list>
#include <sstream>
#include <vector>


class cSection
{
public:
cSection();
cSection(const std::string& secName);
~cSection();
bool IsEmpty() const { return m_sectionConf.empty(); }
std::string getValue(const std::string& key) const;
std::string getData() const;
std::list<std::string> getAllKey() const;
bool addValue(const std::string& key, const std::string& value);
bool addData(const std::string& data);
private:
std::string m_sectionName;
std::vector<std::string> m_dataConfs;
std::map<std::string, std::string> m_sectionConf;
};

class cIniFile
{
public:
cIniFile(){};
explicit cIniFile(const std::string& configFileName);
~cIniFile();
bool load(const std::string& configFileName);
//bool save(const std::string& savepath);
std::string getStr(const std::string& section, const std::string& key) const;
int getInt(const std::string& section, const std::string& key) const;
double getDouble(const std::string& section, const std::string& key) const;
bool getBoolean(const std::string& section, const std::string& key) const;

std::list<std::string> getKeyFromSection(const std::string& section ) const;
std::list<std::string> getSectionsFromIni() const;
private:
template<typename T> T FromString(const std::string& value) const;
std::string getSectionName(std::string inputLine);

bool isSectionName(std::string inputLine);
bool isKeyValue(std::string inputLine);
std::string m_fileName;
std::map<std::string, cSection> m_mapConfig;
std::string m_actualSection, m_actualKey, m_actualValue;
};
Loading