aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/pgnp.cpp81
-rw-r--r--src/pgnp.hpp46
2 files changed, 127 insertions, 0 deletions
diff --git a/src/pgnp.cpp b/src/pgnp.cpp
new file mode 100644
index 0000000..391e6ee
--- /dev/null
+++ b/src/pgnp.cpp
@@ -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);
+ }
+
+} \ No newline at end of file
diff --git a/src/pgnp.hpp b/src/pgnp.hpp
new file mode 100644
index 0000000..25e74c8
--- /dev/null
+++ b/src/pgnp.hpp
@@ -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";
+ }
+ };
+
+}