Choreonoid  1.5
YAMLWriter.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_UTIL_YAML_WRITER_H_INCLUDED
6 #define CNOID_UTIL_YAML_WRITER_H_INCLUDED
7 
8 #include "ValueTree.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(std::ostream& os);
23  ~YAMLWriter();
24 
25  void putNode(const ValueNode* 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, StringStyle style = 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 startListing();
66  void startFlowStyleListing();
67  void endListing();
68 
69 #ifdef CNOID_BACKWARD_COMPATIBILITY
70  void startSequence() { startListing(); }
71  void startFlowStyleSequence() { startFlowStyleListing(); }
72  void endSequence() { endListing(); }
73 #endif
74 
75 private:
76 
77  std::ofstream ofs;
78  std::ostream& os;
79 
80  int indentWidth;
81  bool isCurrentNewLine;
82  int numDocuments;
83  bool isKeyOrderPreservationMode;
84  bool doInsertLineFeed;
85 
86  const char* doubleFormat;
87 
88  enum { TOP, MAPPING, LISTING };
89 
90  struct State {
91  int type;
92  bool isFlowStyle;
93  bool isKeyPut;
94  bool hasValuesBeenPut;
95  std::string indentString;
96  };
97 
98  std::stack<State> states;
99 
100  State* current;
101 
102  bool isTopLevel();
103  State& pushState(int type, bool isFlowStyle);
104  void popState();
105  void indent();
106  void newLine();
107  bool makeValuePutReady();
108  bool startValuePut();
109  void endValuePut();
110  void putString_(const std::string& value);
111  void putSingleQuotedString_(const std::string& value);
112  void putDoubleQuotedString_(const std::string& value);
113  void putKey_(const std::string& key, StringStyle style);
114  void startMappingSub(bool isFlowStyle);
115  void startListingSub(bool isFlowStyle);
116  void putNodeMain(const ValueNode* node, bool doCheckLF);
117  void putScalarNode(const ScalarNode* scalar);
118  void putMappingNode(const Mapping* mapping);
119  void putListingNode(const Listing* listing);
120 };
121 
122 #ifdef CNOID_BACKWARD_COMPATIBILITY
123 typedef YAMLWriter YamlWriter;
124 #endif
125 
126 }
127 
128 
129 #endif
Definition: ValueTree.h:224
Definition: YAMLWriter.h:18
StringStyle
Definition: ValueTree.h:24
void putKeyValue(const std::string &key, const std::string &value)
Definition: YAMLWriter.h:58
Definition: ValueTree.h:424
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
Definition: ValueTree.h:34
void putKeyValue(const std::string &key, const DataType &value)
Definition: YAMLWriter.h:53
void putLiteralString(const std::string &value)
Definition: YAMLWriter.h:38
void putFoldedString(const std::string &value)
Definition: YAMLWriter.h:39
void putScalar(const DataType &value)
Definition: YAMLWriter.h:41
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
Definition: ValueTree.h:24
Definition: ValueTree.h:200