Configurations.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* ----------------------------------------------------------------- */
  2. /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
  3. /* developed by Sinsy Working Group */
  4. /* http://sinsy.sourceforge.net/ */
  5. /* ----------------------------------------------------------------- */
  6. /* */
  7. /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
  8. /* Department of Computer Science */
  9. /* */
  10. /* All rights reserved. */
  11. /* */
  12. /* Redistribution and use in source and binary forms, with or */
  13. /* without modification, are permitted provided that the following */
  14. /* conditions are met: */
  15. /* */
  16. /* - Redistributions of source code must retain the above copyright */
  17. /* notice, this list of conditions and the following disclaimer. */
  18. /* - Redistributions in binary form must reproduce the above */
  19. /* copyright notice, this list of conditions and the following */
  20. /* disclaimer in the documentation and/or other materials provided */
  21. /* with the distribution. */
  22. /* - Neither the name of the Sinsy working group nor the names of */
  23. /* its contributors may be used to endorse or promote products */
  24. /* derived from this software without specific prior written */
  25. /* permission. */
  26. /* */
  27. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
  28. /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  29. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  30. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  31. /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
  32. /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  33. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
  34. /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  35. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
  36. /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
  37. /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
  38. /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  39. /* POSSIBILITY OF SUCH DAMAGE. */
  40. /* ----------------------------------------------------------------- */
  41. #include <fstream>
  42. #include "Configurations.h"
  43. #include "util_log.h"
  44. #include "util_string.h"
  45. #include "StringTokenizer.h"
  46. namespace sinsy
  47. {
  48. namespace
  49. {
  50. const std::string SEPARATOR = "=";
  51. };
  52. /*!
  53. constructor
  54. */
  55. Configurations::Configurations()
  56. {
  57. }
  58. /*!
  59. destructor
  60. */
  61. Configurations::~Configurations()
  62. {
  63. clear();
  64. }
  65. /*!
  66. clear
  67. */
  68. void Configurations::clear()
  69. {
  70. configs.clear();
  71. }
  72. /*!
  73. read configurations from file
  74. */
  75. bool Configurations::read(const std::string& fpath)
  76. {
  77. std::ifstream ifs(fpath.c_str());
  78. if (!ifs) {
  79. ERR_MSG("Cannot open config file : " << fpath);
  80. return false;
  81. }
  82. clear();
  83. std::string buffer;
  84. while (!ifs.eof()) {
  85. std::getline(ifs, buffer);
  86. // remove comments
  87. size_t idx = buffer.find("#");
  88. if (idx != std::string::npos) {
  89. buffer.resize(idx);
  90. }
  91. StringTokenizer st(buffer, SEPARATOR);
  92. size_t sz(st.size());
  93. if (0 == sz) {
  94. continue;
  95. } else if (2 != st.size()) {
  96. ERR_MSG("Syntax error : " << fpath);
  97. return false;
  98. }
  99. std::string key(st.at(0));
  100. cutBlanks(key);
  101. std::string value(st.at(1));
  102. cutBlanks(value);
  103. if (key.empty() || value.empty()) {
  104. ERR_MSG("Syntax error : " << fpath);
  105. return false;
  106. }
  107. // cut " and '
  108. if (1 < value.size()) {
  109. if ((('\"' == value[0]) && ('\"' == value[value.size() - 1])) || (('\'' == value[0]) && ('\'' == value[value.size() - 1]))) {
  110. value = value.substr(1, value.size() - 2);
  111. cutBlanks(value);
  112. if (value.empty()) {
  113. ERR_MSG("Syntax error : " << fpath);
  114. return false;
  115. }
  116. }
  117. }
  118. configs.insert(std::make_pair/*<std::string, std::string>*/(key, value));
  119. }
  120. return true;
  121. }
  122. /*!
  123. for std::string (need default)
  124. */
  125. template<>
  126. std::string Configurations::get<std::string>(const std::string& key, const std::string& def) const
  127. {
  128. Configs::const_iterator itr(configs.find(key));
  129. if (configs.end() != itr) {
  130. return itr->second;
  131. }
  132. return def;
  133. }
  134. /*!
  135. for std::string (not need default)
  136. */
  137. std::string Configurations::get(const std::string& key) const
  138. {
  139. Configs::const_iterator itr(configs.find(key));
  140. if (configs.end() != itr) {
  141. return itr->second;
  142. }
  143. return std::string();
  144. }
  145. /*!
  146. for bool
  147. */
  148. template<>
  149. bool Configurations::get<bool>(const std::string& key, const bool& def) const
  150. {
  151. Configs::const_iterator itr(configs.find(key));
  152. if (configs.end() != itr) {
  153. std::string value(itr->second);
  154. toLower(value);
  155. if (value == "true") {
  156. return true;
  157. } else if (value == "false") {
  158. return false;
  159. }
  160. int i(-1);
  161. std::istringstream iss(value);
  162. iss >> i;
  163. if (0 == i) {
  164. return false;
  165. } else if (0 < i) {
  166. return true;
  167. }
  168. }
  169. return def;
  170. }
  171. }; // namespace sinsy