KvFileReader.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-25
  6. * Changed: 2021-07-15
  7. *
  8. * */
  9. #include <ls_std/io/kv/KvFileReader.hpp>
  10. #include <ls_std/exception/IllegalArgumentException.hpp>
  11. #include <ls_std/io/FileReader.hpp>
  12. #include <ls_std/io/kv/KvParser.hpp>
  13. ls_std::KvFileReader::KvFileReader(const std::shared_ptr<ls_std::KvDocument> &_document, const std::string &_absolutePath)
  14. : ls_std::Class("KvFileReader"),
  15. kvFile(ls_std::File{""})
  16. {
  17. this->_assignDocument(_document);
  18. this->_assignFile(ls_std::File{_absolutePath});
  19. }
  20. ls_std::byte_field ls_std::KvFileReader::read()
  21. {
  22. ls_std::byte_field data = ls_std::FileReader{this->kvFile}.read();
  23. ls_std::KvParser{this->document}.parse(data);
  24. return data;
  25. }
  26. std::shared_ptr<ls_std::KvDocument> ls_std::KvFileReader::getDocument()
  27. {
  28. return this->document;
  29. }
  30. void ls_std::KvFileReader::setDocument(const std::shared_ptr<ls_std::KvDocument> &_document)
  31. {
  32. this->_assignDocument(_document);
  33. }
  34. void ls_std::KvFileReader::setFile(const ls_std::File &_kvFile)
  35. {
  36. this->_assignFile(_kvFile);
  37. }
  38. void ls_std::KvFileReader::_assignDocument(const std::shared_ptr<ls_std::KvDocument> &_document)
  39. {
  40. if (_document == nullptr)
  41. {
  42. throw ls_std::IllegalArgumentException{};
  43. }
  44. this->document = _document;
  45. }
  46. void ls_std::KvFileReader::_assignFile(ls_std::File _kvFile)
  47. {
  48. if (!_kvFile.exists())
  49. {
  50. throw ls_std::IllegalArgumentException{};
  51. }
  52. this->kvFile = _kvFile;
  53. }