#include "StringLib.h"


int ToInteger(std::string n) {
  return (int) ToFloat(n);
}

int CharToInt(char c) {
  int b;
  if (c == '0') b = 0;
  if (c == '1') b = 1;
  if (c == '2') b = 2;
  if (c == '3') b = 3;
  if (c == '4') b = 4;
  if (c == '5') b = 5;
  if (c == '6') b = 6;
  if (c == '7') b = 7;
  if (c == '8') b = 8;
  if (c == '9') b = 9;
  return b;
}

char IntToChar(int c) {
  char b;
  if (c == 0) b = '0';
  if (c == 1) b = '1';
  if (c == 2) b = '2';
  if (c == 3) b = '3';
  if (c == 4) b = '4';
  if (c == 5) b = '5';
  if (c == 6) b = '6';
  if (c == 7) b = '7';
  if (c == 8) b = '8';
  if (c == 9) b = '9';
  return b;
}


int StringIncludes(char *str, char *obj) {
  int size1, size2, ind, ind2, b;
  size1=strlen(str);
  size2=strlen(obj);
  ind=0;
  ind2=0;
  b=0;
  while (ind != size1) {
    if (str[ind] == obj[ind2]) {
      ++ind2;
      if (ind2 == size2) {
        b=1;
        break;
      }
    }
    ++ind;
  }
  return b;
}


std::deque<std::string> TurnToParagraph(GFont *f, std::string msg, int w) {
  int size, indexw, indexp, ind, ind2, strsize, sizew, n;
  char str[5012];
  strcpy(str, msg.c_str());
  std::string words[1024];
  std::string word;
  std::string line;
  std::deque<std::string> v;
  bool is_jumper;
  is_jumper=false;
  ind=0;
  while (ind != msg.size()) {
    if (msg[ind] == '^') is_jumper = true;
    ind += 1;
  }
  n = f->Width(msg);
  if ((n < w) && (!is_jumper)) {
    v.push_back(msg);
    return v;
  }
  size=indexw=indexp=0;
  strsize=strlen(str);
  word = "";
  ind=ind2=0;
  while (ind != msg.size()) {
    if (str[ind] == ' ') {
      word += str[ind];
      ind2=0;
      words[indexw] = word;
      word = "";
      indexw += 1;
    }
    else {
      word += str[ind];
      ind2+=1;
    }
    ++ind;
  }
  words[indexw] = word;
  indexw += 1;
  sizew=0;
  ind=0;
  line = "";
  while (ind != indexw) {
    n = f->Width(words[ind]);
    if ((sizew + n < w) && (words[ind] != "^ ")) {
      line += words[ind];
      sizew += n;
    }
    else {
      sizew = 0;
      indexp += 1;
      v.push_back(line);
      line = "";
      if (words[ind] != "^ ") line += words[ind];
      sizew += n;
    }
    ++ind;
  }
  v.push_back(line);
  return v;
}





float ToFloat(std::string n) {
  float v, t;
  v = 0;
  bool dec, neg;
  int dec_s;
  dec_s = 0;
  dec=neg=false;

  if (n.size() == 0) return 1;

  for (int i=n.size()-1; i>=0; i-=1) {
    if (n[i] == '-') neg=true;
    else if (n[i] == '.') {
      dec=true;
      dec_s = i;
    }
  }
  int s, z;
  s= 1;
  z = 0;
  for (int i=n.size()-1; i>=0; i-=1) {
    if (n[i] == '.') {
      dec = false;
      s = 1;
    }
    else if (n[i] == '-') return -v;
    else {
      if (dec == false) {
        if (n[i] == '0') z += 1;
        else {
          float p;
          if (s == 1) p = 1;
          else {
            p = 1;
            for (int d=0; d!=(s-1); d+=1) {
              p = p * 10;
            }
          }
          t = (CharToInt(n[i]) * p);
          v += t;
          z = 0;
        }
      }
      else {
        float dc;
        dc=1;
        for (int d=0; d!=(i-dec_s); d+=1) dc = dc / 10;
        t = CharToInt(n[i]) * dc;
        v += t;
      }
      s+=1;
    }

  }
  return v;
}


std::vector<std::string> GetParameters(std::string line) {
  std::vector<std::string> list;
  std::string param;
  list.clear();
  param = "";
  if (line.size() > 1) {
    if (line[0] != '#') {
      for (int i=0; i!=line.size(); i+=1) {
        if (line[i] == ' ') {
          if (param != "") {
            list.push_back(param);
            param = "";
          }
        }
        else {
          param += line[i];
        }
      }
    }
  }
  return list;
}
