mirror of
https://gitlab.com/manzerbredes/chess-move-interface.git
synced 2025-05-01 09:27:44 +00:00
Create project
This commit is contained in:
commit
fd78f92863
10 changed files with 21790 additions and 0 deletions
81
src/pgnp.cpp
Normal file
81
src/pgnp.cpp
Normal file
|
@ -0,0 +1,81 @@
|
|||
|
||||
#include "pgnp.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#define IS_BLANK(c) (c==' ' || c=='\n' || c=='\t')
|
||||
#define IS_EOF(loc) (loc>=pgn_content.size())
|
||||
#define EOF_CHECK(loc) {if(IS_EOF(loc)) throw UnexpectedEOF();}
|
||||
|
||||
namespace pgnp {
|
||||
|
||||
void PGN::FromFile(std::string filepath){
|
||||
std::ifstream file(filepath);
|
||||
|
||||
std::string content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
FromString(content);
|
||||
}
|
||||
|
||||
|
||||
void PGN::FromString(std::string pgn_content){
|
||||
this->pgn_content=pgn_content;
|
||||
int loc=0;
|
||||
while(!IS_EOF(loc)) {
|
||||
char c=pgn_content[loc];
|
||||
if(!IS_BLANK(c)){
|
||||
switch (c) {
|
||||
case '[':
|
||||
loc=ParseNextTag(loc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
loc++;
|
||||
}
|
||||
|
||||
/*for (auto const& [key, val] : tags){
|
||||
std::cout << key <<"="<<val<<std::endl;
|
||||
}*/
|
||||
}
|
||||
|
||||
int PGN::ParseNextTag(int start_loc){
|
||||
// Parse key
|
||||
std::string key;
|
||||
int keyloc=start_loc+1;
|
||||
EOF_CHECK(keyloc);
|
||||
char c=pgn_content[keyloc];
|
||||
while(!IS_BLANK(c)){
|
||||
key+=c;
|
||||
keyloc++;
|
||||
EOF_CHECK(keyloc);
|
||||
c=pgn_content[keyloc];
|
||||
}
|
||||
|
||||
// Parse value
|
||||
std::string value;
|
||||
int valueloc=NextNonBlank(keyloc)+1;
|
||||
EOF_CHECK(keyloc);
|
||||
c=pgn_content[valueloc];
|
||||
while(c!='"' or IS_EOF(valueloc)){
|
||||
value+=c;
|
||||
valueloc++;
|
||||
EOF_CHECK(keyloc);
|
||||
c=pgn_content[valueloc];
|
||||
}
|
||||
|
||||
// Add tag
|
||||
tags[key]=value;
|
||||
|
||||
|
||||
return(valueloc+1); // +1 For the last char of the tag which is ']'
|
||||
}
|
||||
|
||||
int PGN::NextNonBlank(int loc){
|
||||
char c=pgn_content[loc];
|
||||
while(IS_BLANK(c)){
|
||||
loc++;
|
||||
c=pgn_content[loc];
|
||||
}
|
||||
return(loc);
|
||||
}
|
||||
|
||||
}
|
46
src/pgnp.hpp
Normal file
46
src/pgnp.hpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <streambuf>
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
namespace pgnp {
|
||||
|
||||
|
||||
typedef struct HalfMove {
|
||||
|
||||
} HalfMove;
|
||||
|
||||
|
||||
class PGN {
|
||||
private:
|
||||
std::unordered_map<std::string,std::string> tags;
|
||||
HalfMove moves;
|
||||
std::string pgn_content;
|
||||
|
||||
public:
|
||||
void FromFile(std::string);
|
||||
void FromString(std::string);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/// @brief Populate @a tags with by parsing the one starting at location in argument
|
||||
int ParseNextTag(int);
|
||||
|
||||
/// @brief Get the next non-blank char location starting from location in argument
|
||||
int NextNonBlank(int);
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct UnexpectedEOF : public std::exception
|
||||
{
|
||||
const char * what () const throw ()
|
||||
{
|
||||
return "Unexpected end of pgn file";
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue