PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/jsonimpex/yamlxml2file.h

https://bitbucket.org/gems4/jsonimpex
C Header | 154 lines | 76 code | 29 blank | 49 comment | 18 complexity | 6faeb34f3017ea67efc64c17143f0d40 MD5 | raw file
  1. // This is JSONIO library+API (https://bitbucket.org/gems4/jsonio)
  2. //
  3. /// \file/
  4. /// Declarations&Implementation of TFileBase, FJson, ConfigJson,
  5. /// ConfigArray - classes for file manipulation
  6. //
  7. // JSONIO is a C++ library and API aimed at implementing the interfaces
  8. // for exchanging the structured data between NoSQL database backends,
  9. // JSON/YAML/XML files, and client-server RPC (remote procedure calls).
  10. //
  11. // Copyright (c) 2015-2016 Svetlana Dmytriieva (svd@ciklum.com) and
  12. // Dmitrii Kulik (dmitrii.kulik@psi.ch)
  13. //
  14. // This program is free software: you can redistribute it and/or modify
  15. // it under the terms of the GNU (Lesser) General Public License as published
  16. // by the Free Software Foundation, either version 3 of the License, or
  17. // (at your option) any later version.
  18. //
  19. // This program is distributed in the hope that it will be useful,
  20. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU General Public License
  25. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // JSONIO depends on the following open-source software products:
  28. // Apache Thrift (https://thrift.apache.org); Pugixml (http://pugixml.org);
  29. // YAML-CPP (https://github.com/jbeder/yaml-cpp); EJDB (http://ejdb.org).
  30. //
  31. #ifndef YAMLXML2FILE_H
  32. #define YAMLXML2FILE_H
  33. #include "jsonio/json2file.h"
  34. namespace jsonio {
  35. /// Class for read/write json/yaml/xml files
  36. class FJsonYamlXml : public FJson
  37. {
  38. protected:
  39. /// Load data from yaml file to bson object
  40. void loadYaml( JsonDom* object );
  41. /// Save data from bson object to yaml file
  42. void saveYaml( const JsonDom* object ) const;
  43. /// Load data from xml file to bson object
  44. void loadXml( JsonDom* object );
  45. /// Save data from bson object to xml file
  46. void saveXml( const JsonDom* object ) const;
  47. public:
  48. static char defType; ///< Default cfg file type
  49. /// Constructor
  50. FJsonYamlXml( const std::string& fName, const std::string& fExt, const std::string& fDir=""):
  51. FJson( fName, fExt, fDir )
  52. {
  53. setType( getType(ext_) );
  54. }
  55. /// Constructor from path
  56. FJsonYamlXml( const std::string& path): FJson( path )
  57. {
  58. setType( getType(ext_) );
  59. }
  60. /// Constructor from bson data
  61. FJsonYamlXml( const JsonDom* object ):FJson( object ) {}
  62. /// Destructor
  63. ~FJsonYamlXml() { }
  64. /// Setup file type
  65. void setType(char ftype )
  66. {
  67. if( !( ftype == FileTypes::Json_ || ftype == FileTypes::Yaml_ || ftype == FileTypes::XML_ ) )
  68. {
  69. jsonioErr( "FJsonYamlXml", "Illegal file type" );
  70. }
  71. type_ = ftype;
  72. }
  73. /// Get file type from extension
  74. static char getType(const std::string& fExt )
  75. {
  76. char type__ = defType;
  77. if(fExt == "json" )
  78. type__ = FileTypes::Json_;
  79. else if(fExt == "yaml" )
  80. type__ = FileTypes::Yaml_;
  81. else if(fExt == "xml" )
  82. type__ = FileTypes::XML_;
  83. return type__;
  84. }
  85. /// Load data from file to dom object
  86. void LoadJson( JsonDom* object );
  87. /// Save data from bson object to file
  88. void SaveJson( const JsonDom* object ) const;
  89. };
  90. /// Class for read/write json/yaml/xml arrays files
  91. class FJsonYamlXmlArray : public FJsonArray
  92. {
  93. public:
  94. /// Constructor
  95. FJsonYamlXmlArray( const std::string& fName, const std::string& fExt, const std::string& fDir=""):
  96. FJsonArray( fName, fExt, fDir )
  97. {
  98. setType( FJsonYamlXml::getType(ext_) );
  99. }
  100. /// Constructor from path
  101. FJsonYamlXmlArray( const std::string& path): FJsonArray( path )
  102. {
  103. setType( FJsonYamlXml::getType(ext_) );
  104. }
  105. /// Constructor from bson data
  106. FJsonYamlXmlArray( const JsonDom* object ):FJsonArray( object ) {}
  107. /// Destructor
  108. ~FJsonYamlXmlArray()
  109. { }
  110. /// Setup file type
  111. void setType(char ftype )
  112. {
  113. if( !( ftype == FileTypes::Json_ ||
  114. ftype == FileTypes::Yaml_ ||
  115. ftype == FileTypes::XML_ ) )
  116. {
  117. jsonioErr( "FJsonArray", "Illegal file type" );
  118. }
  119. type_ = ftype;
  120. }
  121. void Close();
  122. void Open(int amode );
  123. };
  124. } // namespace jsonio
  125. #endif // YAMLXML2FILE_H