#include "config.h"
#include <fstream>
#include <iostream>

// just in case if I need it.
void string_trim(std::string &value) {
  while (*value.begin() == ' ')
    value.erase(value.begin());
  while (*(value.end() - 1) == ' ')
    value.erase(value.end() - 1);
}

/*
#include <algorithm>
void string_toupper(std::string &str) {
std::transform(str.begin(), str.end(),str.begin(), ::toupper);
}
*/

std::map<std::string, std::string> yaml_parse(std::string filename) {
  std::map<std::string, std::string> results;
  std::string line;
  std::ifstream file(filename);
  if (!file.is_open())
    return results;

  while (std::getline(file, line)) {
    if (line.empty())
      continue;
    if (line[0] == '#' || line[0] == ';' || line[0] == ':' || line[0] == '%' ||
        line[0] == '-')
      continue;

    // We're going for YAML-like here
    // # comment
    // ; comment
    // KEY: value
    // KEY: "value is in here"

    size_t pos = line.find(": ");
    if (pos != std::string::npos) {
      // Ok, we found something
      std::string key = line.substr(0, pos);
      std::string value = line.substr(pos + 2);

      if (value[0] == '"' && value[value.size() - 1] == '"') {
        value = value.substr(1, value.size() - 2);
      }
      // std::cout << "KEY[" << key << "] : (" << value << ")" << std::endl;
      results[key] = value;
    }
  }

  return results;
}