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.

5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*------------ -------------- -------- --- ----- --- -- - -
  2. * Libre Blackjack - configuration handling
  3. *
  4. * Copyright (C) 2020 jeremy theler
  5. *
  6. * This file is part of Libre Blackjack.
  7. *
  8. * Libre Blackjack is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Libre Blackjack is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Libre Blackjack. If not, see <http://www.gnu.org/licenses/>.
  20. *------------------- ------------ ---- -------- -- - - -
  21. */
  22. #include "conf.h"
  23. #include <iostream>
  24. #include <fstream>
  25. #include <algorithm>
  26. #include <map>
  27. #include <getopt.h>
  28. #include <unistd.h>
  29. // source https://www.walletfox.com/course/parseconfigfile.php
  30. Configuration::Configuration(int argc, char **argv) {
  31. const struct option longopts[] = {
  32. ///op+conf+option `-c`path or `--conf=`path
  33. ///op+conf+desc Specify the path to the [configuration file]. Default is `./blackjack.conf`
  34. {"conf", required_argument, NULL, 'c'},
  35. ///op+hands+option `-n`$n$ or `--hands=`$n$
  36. ///op+hands+desc Specify the number of hands to play. Corresponds to the `hands` variable in the [configuration file].
  37. {"hands", required_argument, NULL, 'n'},
  38. ///op+decks+option `-d`$n$ or `--decks=`$n$
  39. ///op+decks+desc Specify the number of decks to use in the shoe. Corresponds to the `decks` variable in the [configuration file].
  40. {"decks", required_argument, NULL, 'd'},
  41. ///op+flatbet+option `-f` or `--flatbet`
  42. ///op+flatbet+desc Do not ask for the amount to bet before starting a new hand and use a flat unit bet. Corresponds to the `flat_bet` variable in the [configuration file].
  43. {"flatbet", optional_argument, NULL, 'f'},
  44. ///op+general+option `--`configuration_variable`[=`*value*`]`
  45. ///op+general+desc Any configuration variable from the [configuration file] can be set from the command line.
  46. ///op+general+desc For example, passing `--no_insurance` is like setting `no_insurance = 1` in the configuration file. Command-line options override configuration options.
  47. ///op+internal+option `-i` or `--internal`
  48. ///op+internal+desc Use the internal player to play against itself. See [internal player] for details.
  49. {"internal", no_argument, NULL, 'i'},
  50. ///op+help+option `-h` or `--help`
  51. ///op+help+desc Print this informative help message on standard output and exit successfully.
  52. {"help", no_argument, NULL, 'h'},
  53. ///op+version+option `-v` or `--version`
  54. ///op+version+desc Print the version number and licensing information of Hello on standard output and then exit successfully.
  55. {"version", no_argument, NULL, 'v'},
  56. {NULL, 0, NULL, 0}
  57. };
  58. int optc = 0;
  59. int option_index = 0;
  60. opterr = 0;
  61. while ((optc = getopt_long_only(argc, argv, "c:hvd:n:if:", longopts, &option_index)) != -1) {
  62. switch (optc) {
  63. case 'h':
  64. show_help = true;
  65. break;
  66. case 'v':
  67. show_version = true;
  68. break;
  69. case 'c':
  70. std::cout << "custom conf " << optarg << std::endl;
  71. configFilePath = std::move(std::string(optarg));
  72. explicitConfigFile = true;
  73. break;
  74. case 'd':
  75. data["decks"] = std::move(std::string(optarg));
  76. break;
  77. case 'n':
  78. data["hands"] = std::move(std::string(optarg));
  79. break;
  80. case 'i':
  81. data["player"] = "internal";
  82. break;
  83. case 'f':
  84. if (optarg != NULL) {
  85. data["flat_bet"] = optarg;
  86. } else {
  87. data["flat_bet"] = "yes";
  88. }
  89. break;
  90. case '?':
  91. {
  92. std::string line(argv[optind - 1]);
  93. std::size_t delimiterPos = line.find("=");
  94. if (delimiterPos != std::string::npos) {
  95. std::size_t offset = 0;
  96. if (line.substr(0, 2) == "--") {
  97. offset = 2;
  98. } else if (line.substr(0, 1) == "-") {
  99. offset = 1;
  100. }
  101. auto name = line.substr(offset, delimiterPos-offset);
  102. auto value = line.substr(delimiterPos + 1);
  103. data[name] = value;
  104. } else {
  105. data[line] = "true";
  106. }
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. std::ifstream default_file(configFilePath);
  114. if (default_file.good()) {
  115. readConfigFile(configFilePath, explicitConfigFile);
  116. }
  117. if (set(dealer, {"dealer", "game"}) == false) {
  118. // we play old-school blackjack by default
  119. dealer = "blackjack";
  120. }
  121. if (set(player, {"player"}) == false) {
  122. // if we are on an interactive terminal we play through tty otherwise stdinout
  123. if (isatty(fileno(stdin)) && isatty(fileno(stdout))) {
  124. player = "tty";
  125. } else {
  126. player = "stdio";
  127. }
  128. }
  129. // common settings to all dealers and players
  130. set(&max_incorrect_commands, {"max_incorrect_commands"});
  131. set(&error_standard_deviations, {"error_standard_deviations"});
  132. set(yaml_report_path, {"yaml_report", "yaml_report_path"});
  133. return;
  134. }
  135. int Configuration::readConfigFile(std::string filePath, bool mandatory) {
  136. // std::ifstream is RAII, i.e. no need to call close
  137. std::ifstream fileStream(filePath);
  138. std::size_t delimiterPos;
  139. std::string name;
  140. std::string value;
  141. if (fileStream.is_open()) {
  142. std::string line;
  143. while(getline(fileStream, line)) {
  144. line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
  145. if (line[0] == '#' || line[0] == ';' || line.empty()) {
  146. continue;
  147. }
  148. // TODO: comments on the same line
  149. delimiterPos = line.find("=");
  150. if (delimiterPos != std::string::npos) {
  151. name = line.substr(0, delimiterPos);
  152. value = line.substr(delimiterPos + 1);
  153. if (!exists(name)) {
  154. data[name] = value;
  155. }
  156. // TODO: add another map of string to bools to mark wheter the option was used or not
  157. } else {
  158. // TODO: warning?
  159. }
  160. }
  161. } else {
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. bool Configuration::set(bool *value, std::list<std::string> key) {
  167. for (auto it : key) {
  168. if (exists(*(&it))) {
  169. auto s = data[*(&it)];
  170. if (s == "true" || s == "yes" || s == "y" || s == "") {
  171. *value = true;
  172. } else if (s == "false" || s == "no" || s == "n") {
  173. *value = false;
  174. } else {
  175. *value = std::stoi(s);
  176. }
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. bool Configuration::set(int *value, std::list<std::string> key) {
  183. for (auto it : key) {
  184. if (exists(*(&it))) {
  185. *value = std::stoi(data[*(&it)]);
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. bool Configuration::set(unsigned int *value, std::list<std::string> key) {
  192. for (auto it : key) {
  193. if (exists(*(&it))) {
  194. *value = std::stoi(data[*(&it)]);
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. bool Configuration::set(unsigned long int *value, std::list<std::string> key) {
  201. for (auto it : key) {
  202. if (exists(*(&it))) {
  203. *value = (unsigned long int)std::stod(data[*(&it)]);
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. bool Configuration::set(std::string &value, std::list<std::string> key) {
  210. for (auto it : key) {
  211. if (exists(*(&it))) {
  212. value = data[*(&it)];
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. bool Configuration::set(double *value, std::list<std::string> key) {
  219. for (auto it : key) {
  220. if (exists(*(&it))) {
  221. *value = std::stof(data[*(&it)]);
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. void Configuration::show(void) {
  228. for (auto &it : data) {
  229. std::cout << it.first << " = " << it.second << std::endl;
  230. }
  231. }
  232. int Configuration::getInt(std::string key) {
  233. auto it = data.find(key);
  234. return (it != data.end()) ? std::stoi(it->second) : 0;
  235. }
  236. std::string Configuration::getString(std::string key) {
  237. auto it = data.find(key);
  238. return (it != data.end()) ? it->second : "";
  239. }
  240. Configuration::~Configuration() {
  241. data.clear();
  242. }