Skip to content

File NPInputParser.h

File List > core > NPInputParser.h

Go to the documentation of this file

#ifndef NPParser_h
#define NPParser_h

// STL
#include <map>
#include <memory>
#include <string>
#include <vector>
// YAML-CPP
#include "yaml-cpp/yaml.h"

namespace nptool {
  static std::string token_separator = ": ";
  std::string StripSpaces(std::string line);
  std::string ToLower(std::string line);
  double ApplyUnit(double value, std::string unit);
  unsigned int GetLevel(std::string line);

  // forward declaration
  class InputBlock;
  // useful typedef
  typedef std::shared_ptr<InputBlock> BlockPtr;

  class InputBlock {
   public:
    InputBlock(){};
    InputBlock(std::string line);
    ~InputBlock(){};
    BlockPtr Copy();

   private:
    unsigned int m_Level;
    std::string m_MainToken;
    std::string m_MainValue;
    std::vector<std::string> m_Token;
    std::vector<std::string> m_Value;
    std::vector<std::string> m_Lines;
    std::vector<BlockPtr> m_SubBlock;

   public:
    void AddLine(std::string line);
    void AddLine(std::string token, std::string value);
    std::string ExtractToken(std::string line, std::string separator = "");
    std::string ExtractValue(std::string line, std::string separator = "");

   public:
    std::string GetToken(unsigned int i) { return m_Token[i]; };
    std::string GetValue(unsigned int i) { return m_Value[i]; };
    std::string GetValue(std::string Token);
    void SetValue(unsigned int i, std::string val) { m_Value[i] = val; };

   public:
    bool HasTokenList(std::vector<std::string> TokenList);
    bool HasToken(std::string Token);

   public:
    std::string GetMainToken() { return m_MainToken; };
    std::string GetMainValue() { return m_MainValue; };

    unsigned int GetSize() { return m_Token.size(); };
    double GetDouble(std::string Token, std::string default_unit, bool silent = false);
    int GetInt(std::string Token, bool silent = false);
    bool GetBool(std::string Token, bool silent = false);
    std::string GetString(std::string Token, bool silent = false);
    std::vector<double> GetVectorDouble(std::string Token, std::string default_unit, bool silent = false);
    std::vector<int> GetVectorInt(std::string Token, bool silent = false);
    std::vector<std::string> GetVectorString(std::string Token, bool silent = false);
    std::vector<double> GetVector3(std::string Token, std::string default_unit, bool silent = false);
    std::vector<std::string> GetLines() { return m_Lines; };

   public:
    std::vector<BlockPtr> GetSubBlock(std::string Token);
    void AddSubBlock(BlockPtr sub) { m_SubBlock.push_back(sub); };

   public:
    void Dump();
  };
  class InputParser {
   public:
    InputParser(){};
    InputParser(std::string filename, bool silent = false) { ReadFile(filename, silent); }
    ~InputParser(){};

   private:
    std::vector<BlockPtr> m_Block;
    std::map<std::string, std::vector<std::string>> m_Aliases;

   public:
    void ReadFile(std::string filename, bool silent = false);
    BlockPtr ProcessBlock(YAML::const_iterator it);
    void SuppressAllBlocksWithToken(std::string Token);
    void TreatAliases();
    void Dump();
    void Print() { Dump(); }
    void Clear();
    std::vector<BlockPtr> GetAllBlocksWithToken(std::string Token);
    std::vector<BlockPtr> GetAllBlocksWithTokenAndValue(std::string Token, std::string Value);
    std::vector<std::string> GetAllBlocksValues(std::string);
    std::vector<std::string> GetAllBlocksToken();

   private:
    bool IsNotComment(std::string line);
  };
} // namespace nptool

#endif