Пример #1
0
void load_options()
{
    std::ifstream fin;
    fin.open("data/options.txt");
    if(!fin.is_open()) {
        fin.close();
        create_default_options();
        fin.open("data/options.txt");
        if(!fin.is_open()) {
            DebugLog() << "Could neither read nor create ./data/options.txt\n";
            return;
        }
    }

    while(!fin.eof()) {
        std::string id;
        fin >> id;
        if(id == "") {
            getline(fin, id);    // Empty line, chomp it
        } else if(id[0] == '#') { // # indicates a comment
            getline(fin, id);
        } else {
            option_key key = lookup_option_key(id);
            if(key == OPT_NULL) {
                DebugLog() << "Bad option: " << id << "\n";
                getline(fin, id);
            } else if(option_is_bool(key)) {
                std::string val;
                fin >> val;
                if(val == "T") {
                    OPTIONS[key] = 1.;
                } else {
                    OPTIONS[key] = 0.;
                }
            } else {
                std::string check;
                double val;
                fin >> check;

                if(check == "T" || check == "F") {
                    val = (check == "T") ? 1.: 0.;
                } else {
                    val = atoi(check.c_str());
                }

                // Sanitize option values that are out of range.
                val = std::min(val, (double)option_max_options(key));
                val = std::max(val, (double)option_min_options(key));

                OPTIONS[key] = val;
            }
        }
        if(fin.peek() == '\n') {
            getline(fin, id);    // Chomp
        }
    }
Пример #2
0
void load_options()
{
 std::ifstream fin;
 fin.open("data/options.txt");
 if (!fin.is_open()) {
  fin.close();
  create_default_options();
  fin.open("data/options.txt");
  if (!fin.is_open()) {
   debugmsg("Could neither read nor create ./data/options.txt");
   return;
  }
 }

 while (!fin.eof()) {
  std::string id;
  fin >> id;
  if (id == "")
   getline(fin, id); // Empty line, chomp it
  else if (id[0] == '#') // # indicates a comment
   getline(fin, id);
  else {
   option_key key = lookup_option_key(id);
   if (key == OPT_NULL) {
    debugmsg("Bad option: %s", id.c_str());
    getline(fin, id);
   } else if (option_is_bool(key)) {
    std::string val;
    fin >> val;
    if (val == "T")
     OPTIONS[key] = 1.;
    else
     OPTIONS[key] = 0.;
   } else {
    std::string check;
    double val;
    fin >> check;

    if (check == "T" || check == "F")
     val = (check == "T") ? 1.: 0.;
    else
     val = atoi(check.c_str());

    OPTIONS[key] = val;
   }
  }
  if (fin.peek() == '\n')
   getline(fin, id); // Chomp
 }