KvFileReader.cpp 1.7 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: 2023-02-04
  7. *
  8. * */
  9. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  10. #include <ls-std/io/FileReader.hpp>
  11. #include <ls-std/io/kv/KvFileReader.hpp>
  12. #include <ls-std/io/kv/KvParser.hpp>
  13. ls::std::io::KvFileReader::KvFileReader(const ::std::shared_ptr<ls::std::io::KvDocument> &_document, const ::std::string &_absolutePath) : ls::std::core::Class("KvFileReader"), kvFile(ls::std::io::File{""})
  14. {
  15. this->_assignDocument(_document);
  16. this->_assignFile(ls::std::io::File{_absolutePath});
  17. }
  18. ls::std::io::KvFileReader::~KvFileReader() = default;
  19. ls::std::core::type::byte_field ls::std::io::KvFileReader::read()
  20. {
  21. ls::std::core::type::byte_field data = ls::std::io::FileReader{this->kvFile}.read();
  22. ls::std::io::KvParser{this->document}.parse(data);
  23. return data;
  24. }
  25. ::std::shared_ptr<ls::std::io::KvDocument> ls::std::io::KvFileReader::getDocument()
  26. {
  27. return this->document;
  28. }
  29. void ls::std::io::KvFileReader::setDocument(const ::std::shared_ptr<ls::std::io::KvDocument> &_document)
  30. {
  31. this->_assignDocument(_document);
  32. }
  33. void ls::std::io::KvFileReader::setFile(const ls::std::io::File &_kvFile)
  34. {
  35. this->_assignFile(_kvFile);
  36. }
  37. void ls::std::io::KvFileReader::_assignDocument(const ::std::shared_ptr<ls::std::io::KvDocument> &_document)
  38. {
  39. if (_document == nullptr)
  40. {
  41. throw ls::std::core::IllegalArgumentException{};
  42. }
  43. this->document = _document;
  44. }
  45. void ls::std::io::KvFileReader::_assignFile(ls::std::io::File _kvFile)
  46. {
  47. if (!_kvFile.exists())
  48. {
  49. throw ls::std::core::IllegalArgumentException{};
  50. }
  51. this->kvFile = _kvFile;
  52. }