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 година
пре 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 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. Configuration::Configuration(int argc, char **argv) {
  30. const struct option longopts[] = {
  31. ///op+conf+option `-c`path or `--conf=`path
  32. ///op+conf+desc Specify the path to the [configuration file]. Default is `./blackjack.conf`
  33. {"conf", required_argument, NULL, 'c'},
  34. ///op+hands+option `-n`$n$ or `--hands=`$n$
  35. ///op+hands+desc Specify the number of hands to play. Corresponds to the `hands` variable in the [configuration file].
  36. {"hands", required_argument, NULL, 'n'},
  37. ///op+decks+option `-d`$n$ or `--decks=`$n$
  38. ///op+decks+desc Specify the number of decks to use in the shoe. Corresponds to the `decks` variable in the [configuration file].
  39. {"decks", required_argument, NULL, 'd'},
  40. ///op+flatbet+option `-f` or `--flatbet`
  41. ///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].
  42. {"flatbet", optional_argument, NULL, 'f'},
  43. ///op+general+option `--`configuration_variable`[=`*value*`]`
  44. ///op+general+desc Any configuration variable from the [configuration file] can be set from the command line.
  45. ///op+general+desc For example, passing `--no_insurance` is like setting `no_insurance = 1` in the configuration file. Command-line options override configuration options.
  46. ///op+internal+option `-i` or `--internal`
  47. ///op+internal+desc Use the internal player to play against itself. See [internal player] for details.
  48. {"internal", no_argument, NULL, 'i'},
  49. ///op+help+option `-h` or `--help`
  50. ///op+help+desc Print this informative help message on standard output and exit successfully.
  51. {"help", no_argument, NULL, 'h'},
  52. ///op+version+option `-v` or `--version`
  53. ///op+version+desc Print the version number and licensing information of Hello on standard output and then exit successfully.
  54. {"version", no_argument, NULL, 'v'},
  55. {NULL, 0, NULL, 0}
  56. };
  57. int optc = 0;
  58. int option_index = 0;
  59. opterr = 0;
  60. while ((optc = getopt_long_only(argc, argv, "c:hvd:n:if:", longopts, &option_index)) != -1) {
  61. switch (optc) {
  62. case 'h':
  63. show_help = true;
  64. break;
  65. case 'v':
  66. show_version = true;
  67. break;
  68. case 'c':
  69. configFilePath = std::move(std::string(optarg));
  70. explicitConfigFile = true;
  71. break;
  72. case 'd':
  73. data["decks"] = std::move(std::string(optarg));
  74. break;
  75. case 'n':
  76. data["hands"] = std::move(std::string(optarg));
  77. break;
  78. case 'i':
  79. data["player"] = "internal";
  80. break;
  81. case 'f':
  82. if (optarg != NULL) {
  83. data["flat_bet"] = optarg;
  84. } else {
  85. data["flat_bet"] = "yes";
  86. }
  87. break;
  88. case '?':
  89. {
  90. std::string line(argv[optind - 1]);
  91. std::size_t delimiterPos = line.find("=");
  92. if (delimiterPos != std::string::npos) {
  93. std::size_t offset = 0;
  94. if (line.substr(0, 2) == "--") {
  95. offset = 2;
  96. } else if (line.substr(0, 1) == "-") {
  97. offset = 1;
  98. }
  99. auto name = line.substr(offset, delimiterPos-offset);
  100. auto value = line.substr(delimiterPos + 1);
  101. data[name] = value;
  102. } else {
  103. data[line] = "true";
  104. }
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. std::ifstream default_file(configFilePath);
  112. if (default_file.good()) {
  113. readConfigFile(configFilePath, explicitConfigFile);
  114. }
  115. if (set(dealer, {"dealer", "game"}) == false) {
  116. // we play old-school blackjack by default
  117. dealer = "blackjack";
  118. }
  119. if (set(player, {"player"}) == false) {
  120. // if we are on an interactive terminal we play through tty otherwise stdinout
  121. if (isatty(fileno(stdin)) && isatty(fileno(stdout))) {
  122. player = "tty";
  123. } else {
  124. player = "stdio";
  125. }
  126. }
  127. // common settings to all dealers and players
  128. set(&max_incorrect_commands, {"max_incorrect_commands"});
  129. return;
  130. }
  131. // source https://www.walletfox.com/course/parseconfigfile.php
  132. int Configuration::readConfigFile(std::string filePath, bool mandatory) {
  133. // std::ifstream is RAII, i.e. no need to call close
  134. std::ifstream fileStream(filePath);
  135. std::size_t delimiterPos;
  136. std::string name;
  137. std::string value;
  138. if (fileStream.is_open()) {
  139. std::string line;
  140. while(getline(fileStream, line)) {
  141. line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
  142. if (line[0] == '#' || line[0] == ';' || line.empty()) {
  143. continue;
  144. }
  145. // TODO: comments on the same line
  146. delimiterPos = line.find("=");
  147. if (delimiterPos != std::string::npos) {
  148. name = line.substr(0, delimiterPos);
  149. value = line.substr(delimiterPos + 1);
  150. if (!exists(name)) {
  151. data[name] = value;
  152. }
  153. // TODO: add another map of string to bools to mark wheter the option was used or not
  154. } else {
  155. // TODO: warning?
  156. }
  157. }
  158. } else {
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. bool Configuration::set(bool *value, std::list<std::string> key) {
  164. for (auto it : key) {
  165. if (exists(*(&it))) {
  166. auto s = data[*(&it)];
  167. if (s == "true" || s == "yes" || s == "y" || s == "") {
  168. *value = true;
  169. } else if (s == "false" || s == "no" || s == "n") {
  170. *value = false;
  171. } else {
  172. *value = std::stoi(s);
  173. }
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. bool Configuration::set(int *value, std::list<std::string> key) {
  180. for (auto it : key) {
  181. if (exists(*(&it))) {
  182. *value = std::stoi(data[*(&it)]);
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. bool Configuration::set(unsigned int *value, std::list<std::string> key) {
  189. // check for negative values
  190. for (auto it : key) {
  191. if (exists(*(&it))) {
  192. int tmp = std::stoi(data[*(&it)]);
  193. if (tmp < 0) {
  194. std::cerr << "key " << *(&it) << " cannot be negative" << std::endl;
  195. exit(-1);
  196. }
  197. *value = std::stoi(data[*(&it)]);
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. bool Configuration::set(unsigned long int *value, std::list<std::string> key) {
  204. for (auto it : key) {
  205. if (exists(*(&it))) {
  206. *value = (unsigned long int)std::stod(data[*(&it)]);
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. bool Configuration::set(std::string &value, std::list<std::string> key) {
  213. for (auto it : key) {
  214. if (exists(*(&it))) {
  215. value = data[*(&it)];
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. bool Configuration::set(double *value, std::list<std::string> key) {
  222. for (auto it : key) {
  223. if (exists(*(&it))) {
  224. *value = std::stof(data[*(&it)]);
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. void Configuration::show(void) {
  231. for (auto &it : data) {
  232. std::cout << it.first << " = " << it.second << std::endl;
  233. }
  234. }
  235. int Configuration::getInt(std::string key) {
  236. auto it = data.find(key);
  237. return (it != data.end()) ? std::stoi(it->second) : 0;
  238. }
  239. std::string Configuration::getString(std::string key) {
  240. auto it = data.find(key);
  241. return (it != data.end()) ? it->second : "";
  242. }
  243. Configuration::~Configuration() {
  244. data.clear();
  245. }