Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "conf.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <map>
  6. #include <getopt.h>
  7. // source https://www.walletfox.com/course/parseconfigfile.php
  8. Configuration::Configuration(int argc, char **argv) {
  9. const struct option longopts[] = {
  10. ///op+conf+option `-c`path or `--conf=`path
  11. ///op+conf+desc Specify the path to the [configuration file]. Default is `./blackjack.conf`
  12. {"conf", required_argument, NULL, 'c'},
  13. ///op+hands+option `-n`$n$ or `--hands=`$n$
  14. ///op+hands+desc Specify the number of hands to play. Corresponds to the `hands` variable in the [configuration file].
  15. {"hands", required_argument, NULL, 'n'},
  16. ///op+decks+option `-d`$n$ or `--decks=`$n$
  17. ///op+decks+desc Specify the number of decks to use in the shoe. Corresponds to the `decks` variable in the [configuration file].
  18. {"decks", required_argument, NULL, 'd'},
  19. ///op+flatbet+option `-f` or `--flatbet`
  20. ///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].
  21. {"flatbet", optional_argument, NULL, 'f'},
  22. ///op+general+option `--`configuration_variable`[=`*value*`]`
  23. ///op+general+desc Any configuration variable from the [configuration file] can be set from the command line.
  24. ///op+general+desc For example, passing `--no_insurance` is like setting `no_insurance = 1` in the configuration file. Command-line options override configuration options.
  25. ///op+internal+option `-i` or `--internal`
  26. ///op+internal+desc Use the internal player to play against itself. See [internal player] for details.
  27. {"internal", no_argument, NULL, 'i'},
  28. ///op+help+option `-h` or `--help`
  29. ///op+help+desc Print this informative help message on standard output and exit successfully.
  30. {"help", no_argument, NULL, 'h'},
  31. ///op+version+option `-v` or `--version`
  32. ///op+version+desc Print the version number and licensing information of Hello on standard output and then exit successfully.
  33. {"version", no_argument, NULL, 'v'},
  34. {NULL, 0, NULL, 0}
  35. };
  36. int i, optc;
  37. int option_index = 0;
  38. int opterr = 0;
  39. while ((optc = getopt_long_only(argc, argv, "c:hvd:n:if:", longopts, &option_index)) != -1) {
  40. switch (optc) {
  41. case 'h':
  42. show_help = true;
  43. break;
  44. case 'v':
  45. show_version = true;
  46. break;
  47. case 'c':
  48. std::cout << "custom conf " << optarg << std::endl;
  49. configFilePath = std::move(std::string(optarg));
  50. explicitConfigFile = true;
  51. break;
  52. case 'd':
  53. data["decks"] = std::move(std::string(optarg));
  54. break;
  55. case 'n':
  56. data["hands"] = std::move(std::string(optarg));
  57. break;
  58. case 'i':
  59. data["player"] = "internal";
  60. break;
  61. case 'f':
  62. if (optarg != NULL) {
  63. data["flat_bet"] = optarg;
  64. } else {
  65. data["flat_bet"] = "1";
  66. }
  67. break;
  68. case '?':
  69. {
  70. std::string line(argv[optind - 1]);
  71. std::size_t delimiterPos = line.find("=");
  72. if (delimiterPos != std::string::npos) {
  73. auto name = line.substr(0, delimiterPos);
  74. auto value = line.substr(delimiterPos + 1);
  75. data[name] = value;
  76. } else {
  77. data[line] = "true";
  78. }
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. std::ifstream default_file(configFilePath);
  86. if (default_file.good()) {
  87. readConfigFile(configFilePath, explicitConfigFile);
  88. }
  89. }
  90. int Configuration::readConfigFile(std::string filePath, bool mandatory) {
  91. // std::ifstream is RAII, i.e. no need to call close
  92. std::ifstream fileStream(filePath);
  93. std::size_t delimiterPos;
  94. std::string name;
  95. std::string value;
  96. if (fileStream.is_open()) {
  97. std::string line;
  98. while(getline(fileStream, line)) {
  99. line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
  100. if (line[0] == '#' || line[0] == ';' || line.empty()) {
  101. continue;
  102. }
  103. // TODO: comments on the same line
  104. delimiterPos = line.find("=");
  105. if (delimiterPos != std::string::npos) {
  106. name = line.substr(0, delimiterPos);
  107. value = line.substr(delimiterPos + 1);
  108. if (!exists(name)) {
  109. data[name] = value;
  110. }
  111. // TODO: add another map of string to bools to mark wheter the option was used or not
  112. } else {
  113. // TODO: warning?
  114. }
  115. }
  116. } else {
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. void Configuration::show(void) {
  122. for (auto &it : data) {
  123. std::cout << it.first << " = " << it.second << std::endl;
  124. }
  125. }
  126. int Configuration::getInt(std::string key) {
  127. auto it = data.find(key);
  128. return (it != data.end()) ? std::stoi(it->second) : 0;
  129. }
  130. Configuration::~Configuration() {
  131. data.clear();
  132. }