Choreonoid  1.1
YamlWriter.h
説明を見る。
1 
5 #ifndef CNOID_UTIL_YAML_WRITER_H_INCLUDED
6 #define CNOID_UTIL_YAML_WRITER_H_INCLUDED
7 
8 #include "YamlNodes.h"
9 #include <stack>
10 #include <string>
11 #include <fstream>
12 #include <boost/lexical_cast.hpp>
13 #include <boost/intrusive_ptr.hpp>
14 #include "exportdecl.h"
15 
16 namespace cnoid {
17 
19  {
20  public:
21  YamlWriter(const std::string filename);
22  ~YamlWriter();
23 
24  void putNode(YamlNode& node);
25  void putNode(const YamlNodePtr& node);
26 
27  void setIndentWidth(int n);
28  void setKeyOrderPreservationMode(bool on);
29 
30  void startDocument();
31 
32  void putComment(const std::string& comment, bool doNewLine = true);
33 
34  void putString(const std::string& value);
35  void putSingleQuotedString(const std::string& value);
36  void putDoubleQuotedString(const std::string& value);
37  void putBlockStyleString(const std::string& value, bool isLiteral);
38  inline void putLiteralString(const std::string& value) { putBlockStyleString(value, true); }
39  inline void putFoldedString(const std::string& value) { putBlockStyleString(value, false); }
40 
41  template <class DataType> inline void putScalar(const DataType& value){
42  putString(boost::lexical_cast<std::string>(value));
43  }
44 
45  void putScalar(const double& value);
46  void setDoubleFormat(const char* format);
47 
48  void startMapping();
49  void startFlowStyleMapping();
50 
51  void putKey(const std::string& key, YamlStringStyle style = YAML_PLAIN_STRING);
52 
53  template <class DataType> inline void putKeyValue(const std::string& key, const DataType& value){
54  putKey(key);
55  putScalar(value);
56  }
57 
58  inline void putKeyValue(const std::string& key, const std::string& value){
59  putKey(key);
60  putDoubleQuotedString(value);
61  }
62 
63  void endMapping();
64 
65  void startSequence();
66  void startFlowStyleSequence();
67 
68  void endSequence();
69 
70  private:
71 
72  std::ofstream ofs;
73 
74  int indentWidth;
75  bool isCurrentNewLine;
76  int numDocuments;
77  bool isKeyOrderPreservationMode;
78  bool doInsertLineFeed;
79 
80  const char* doubleFormat;
81 
82  enum { TOP, MAPPING, SEQUENCE };
83 
84  struct State {
85  int type;
86  bool isFlowStyle;
87  bool isKeyPut;
88  bool hasValuesBeenPut;
89  std::string indentString;
90  };
91 
92  std::stack<State> states;
93 
94  State* current;
95 
96  bool isTopLevel();
97  State& pushState(int type, bool isFlowStyle);
98  void popState();
99  void indent();
100  void newLine();
101  bool makeValuePutReady();
102  bool startValuePut();
103  void endValuePut();
104  void putString_(const std::string& value);
105  void putSingleQuotedString_(const std::string& value);
106  void putDoubleQuotedString_(const std::string& value);
107  void putKey_(const std::string& key, YamlStringStyle style);
108  void startMappingSub(bool isFlowStyle);
109  void startSequenceSub(bool isFlowStyle);
110  void putMappingNode(const YamlMapping* mapping);
111  void putSequenceNode(const YamlSequence* sequence);
112  };
113 }
114 
115 
116 #endif