You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/optionsfile.h"
  28. #include <ctype.h>
  29. #include "talk/base/logging.h"
  30. #include "talk/base/stream.h"
  31. #include "talk/base/stringencode.h"
  32. namespace talk_base {
  33. OptionsFile::OptionsFile(const std::string &path) : path_(path) {
  34. }
  35. bool OptionsFile::Load() {
  36. options_.clear();
  37. // Open file.
  38. FileStream stream;
  39. int err;
  40. if (!stream.Open(path_, "r", &err)) {
  41. LOG_F(LS_WARNING) << "Could not open file, err=" << err;
  42. // We do not consider this an error because we expect there to be no file
  43. // until the user saves a setting.
  44. return true;
  45. }
  46. // Read in all its data.
  47. std::string line;
  48. StreamResult res;
  49. for (;;) {
  50. res = stream.ReadLine(&line);
  51. if (res != SR_SUCCESS) {
  52. break;
  53. }
  54. size_t equals_pos = line.find('=');
  55. if (equals_pos == std::string::npos) {
  56. // We do not consider this an error. Instead we ignore the line and
  57. // keep going.
  58. LOG_F(LS_WARNING) << "Ignoring malformed line in " << path_;
  59. continue;
  60. }
  61. std::string key(line, 0, equals_pos);
  62. std::string value(line, equals_pos + 1, line.length() - (equals_pos + 1));
  63. options_[key] = value;
  64. }
  65. if (res != SR_EOS) {
  66. LOG_F(LS_ERROR) << "Error when reading from file";
  67. return false;
  68. } else {
  69. return true;
  70. }
  71. }
  72. bool OptionsFile::Save() {
  73. // Open file.
  74. FileStream stream;
  75. int err;
  76. if (!stream.Open(path_, "w", &err)) {
  77. LOG_F(LS_ERROR) << "Could not open file, err=" << err;
  78. return false;
  79. }
  80. // Write out all the data.
  81. StreamResult res = SR_SUCCESS;
  82. size_t written;
  83. int error;
  84. for (OptionsMap::const_iterator i = options_.begin(); i != options_.end();
  85. ++i) {
  86. res = stream.WriteAll(i->first.c_str(), i->first.length(), &written,
  87. &error);
  88. if (res != SR_SUCCESS) {
  89. break;
  90. }
  91. res = stream.WriteAll("=", 1, &written, &error);
  92. if (res != SR_SUCCESS) {
  93. break;
  94. }
  95. res = stream.WriteAll(i->second.c_str(), i->second.length(), &written,
  96. &error);
  97. if (res != SR_SUCCESS) {
  98. break;
  99. }
  100. res = stream.WriteAll("\n", 1, &written, &error);
  101. if (res != SR_SUCCESS) {
  102. break;
  103. }
  104. }
  105. if (res != SR_SUCCESS) {
  106. LOG_F(LS_ERROR) << "Unable to write to file";
  107. return false;
  108. } else {
  109. return true;
  110. }
  111. }
  112. bool OptionsFile::IsLegalName(const std::string &name) {
  113. for (size_t pos = 0; pos < name.length(); ++pos) {
  114. if (name[pos] == '\n' || name[pos] == '\\' || name[pos] == '=') {
  115. // Illegal character.
  116. LOG(LS_WARNING) << "Ignoring operation for illegal option " << name;
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. bool OptionsFile::IsLegalValue(const std::string &value) {
  123. for (size_t pos = 0; pos < value.length(); ++pos) {
  124. if (value[pos] == '\n' || value[pos] == '\\') {
  125. // Illegal character.
  126. LOG(LS_WARNING) << "Ignoring operation for illegal value " << value;
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. bool OptionsFile::GetStringValue(const std::string& option,
  133. std::string *out_val) const {
  134. LOG(LS_VERBOSE) << "OptionsFile::GetStringValue "
  135. << option;
  136. if (!IsLegalName(option)) {
  137. return false;
  138. }
  139. OptionsMap::const_iterator i = options_.find(option);
  140. if (i == options_.end()) {
  141. return false;
  142. }
  143. *out_val = i->second;
  144. return true;
  145. }
  146. bool OptionsFile::GetIntValue(const std::string& option,
  147. int *out_val) const {
  148. LOG(LS_VERBOSE) << "OptionsFile::GetIntValue "
  149. << option;
  150. if (!IsLegalName(option)) {
  151. return false;
  152. }
  153. OptionsMap::const_iterator i = options_.find(option);
  154. if (i == options_.end()) {
  155. return false;
  156. }
  157. return FromString(i->second, out_val);
  158. }
  159. bool OptionsFile::SetStringValue(const std::string& option,
  160. const std::string& value) {
  161. LOG(LS_VERBOSE) << "OptionsFile::SetStringValue "
  162. << option << ":" << value;
  163. if (!IsLegalName(option) || !IsLegalValue(value)) {
  164. return false;
  165. }
  166. options_[option] = value;
  167. return true;
  168. }
  169. bool OptionsFile::SetIntValue(const std::string& option,
  170. int value) {
  171. LOG(LS_VERBOSE) << "OptionsFile::SetIntValue "
  172. << option << ":" << value;
  173. if (!IsLegalName(option)) {
  174. return false;
  175. }
  176. return ToString(value, &options_[option]);
  177. }
  178. bool OptionsFile::RemoveValue(const std::string& option) {
  179. LOG(LS_VERBOSE) << "OptionsFile::RemoveValue " << option;
  180. if (!IsLegalName(option)) {
  181. return false;
  182. }
  183. options_.erase(option);
  184. return true;
  185. }
  186. } // namespace talk_base