Dictionary.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_DICTIONARY_HPP
  28. #define ZT_DICTIONARY_HPP
  29. #include <string>
  30. #include <map>
  31. #include <stdexcept>
  32. #include "Constants.hpp"
  33. namespace ZeroTier {
  34. /**
  35. * Simple key/value dictionary with string serialization
  36. *
  37. * The serialization format is a flat key=value with backslash escape.
  38. * It does not support comments or other syntactic complexities. It is
  39. * human-readable if the keys and values in the dictionary are also
  40. * human-readable. Otherwise it might contain unprintable characters.
  41. */
  42. class Dictionary : public std::map<std::string,std::string>
  43. {
  44. public:
  45. Dictionary()
  46. {
  47. }
  48. /**
  49. * @param s String-serialized dictionary
  50. */
  51. Dictionary(const char *s)
  52. {
  53. fromString(s);
  54. }
  55. /**
  56. * @param s String-serialized dictionary
  57. */
  58. Dictionary(const std::string &s)
  59. {
  60. fromString(s.c_str());
  61. }
  62. /**
  63. * Get a key, throwing an exception if it is not present
  64. *
  65. * @param key Key to look up
  66. * @return Reference to value
  67. * @throws std::invalid_argument Key not found
  68. */
  69. inline const std::string &get(const std::string &key) const
  70. throw(std::invalid_argument)
  71. {
  72. const_iterator e(find(key));
  73. if (e == end())
  74. throw std::invalid_argument(std::string("missing required field: ")+key);
  75. return e->second;
  76. }
  77. /**
  78. * Get a key, returning a default if not present
  79. *
  80. * @param key Key to look up
  81. * @param dfl Default if not present
  82. * @return Value or default
  83. */
  84. inline const std::string &get(const std::string &key,const std::string &dfl) const
  85. {
  86. const_iterator e(find(key));
  87. if (e == end())
  88. return dfl;
  89. return e->second;
  90. }
  91. /**
  92. * @param key Key to check
  93. * @return True if dictionary contains key
  94. */
  95. inline bool contains(const std::string &key) const
  96. {
  97. return (find(key) != end());
  98. }
  99. /**
  100. * @return String-serialized dictionary
  101. */
  102. inline std::string toString() const
  103. {
  104. std::string s;
  105. for(const_iterator kv(begin());kv!=end();++kv) {
  106. _appendEsc(kv->first.data(),(unsigned int)kv->first.length(),s);
  107. s.push_back('=');
  108. _appendEsc(kv->second.data(),(unsigned int)kv->second.length(),s);
  109. s.append(ZT_EOL_S);
  110. }
  111. return s;
  112. }
  113. /**
  114. * Clear and initialize from a string
  115. *
  116. * @param s String-serialized dictionary
  117. */
  118. inline void fromString(const char *s)
  119. {
  120. clear();
  121. bool escapeState = false;
  122. std::string keyBuf;
  123. std::string *element = &keyBuf;
  124. while (*s) {
  125. if (escapeState) {
  126. escapeState = false;
  127. switch(*s) {
  128. case '0':
  129. element->push_back((char)0);
  130. break;
  131. case 'r':
  132. element->push_back('\r');
  133. break;
  134. case 'n':
  135. element->push_back('\n');
  136. break;
  137. default:
  138. element->push_back(*s);
  139. break;
  140. }
  141. } else {
  142. if (*s == '\\') {
  143. escapeState = true;
  144. } else if (*s == '=') {
  145. if (element == &keyBuf)
  146. element = &((*this)[keyBuf]);
  147. } else if ((*s == '\r')||(*s == '\n')) {
  148. if ((element == &keyBuf)&&(keyBuf.length() > 0))
  149. (*this)[keyBuf];
  150. keyBuf = "";
  151. element = &keyBuf;
  152. } else element->push_back(*s);
  153. }
  154. ++s;
  155. }
  156. if ((element == &keyBuf)&&(keyBuf.length() > 0))
  157. (*this)[keyBuf];
  158. }
  159. inline void fromString(const std::string &s)
  160. {
  161. fromString(s.c_str());
  162. }
  163. private:
  164. static inline void _appendEsc(const char *data,unsigned int len,std::string &to)
  165. {
  166. for(unsigned int i=0;i<len;++i) {
  167. switch(data[i]) {
  168. case 0:
  169. to.append("\\0");
  170. break;
  171. case '\r':
  172. to.append("\\r");
  173. break;
  174. case '\n':
  175. to.append("\\n");
  176. break;
  177. case '\\':
  178. to.append("\\\\");
  179. break;
  180. case '=':
  181. to.append("\\=");
  182. break;
  183. default:
  184. to.push_back(data[i]);
  185. break;
  186. }
  187. }
  188. }
  189. };
  190. } // namespace ZeroTier
  191. #endif