int Util::parseSizeOrPercent()

in src/oomd/util/Util.cpp [132:166]


int Util::parseSizeOrPercent(
    const std::string& input,
    int64_t* output,
    int64_t total) {
  try {
    if (input.size() > 0 && input.at(input.size() - 1) == '%') {
      int64_t pct = std::stoi(input.substr(0, input.size() - 1));
      if (pct < 0 || pct > 100) {
        return -1;
      }

      *output = total * pct / 100;
      return 0;
    } else {
      int64_t v;
      size_t end_pos;

      // compat - a bare number is interpreted as megabytes
      v = std::stoll(input, &end_pos);
      if (end_pos == input.length()) {
        *output = v << 20;
        return 0;
      }

      if (Util::parseSize(input, &v) == 0) {
        *output = v;
        return 0;
      }

      return -1;
    }
  } catch (...) {
    return -1;
  }
}