#include "read_uses.h"

read_uses::read_uses(std::string name)
{
  size_t pos=name.find("/");
  if(pos==std::string::npos){
      this->short_name=name;
    }else{
      this->short_name=name.substr(pos, name.size());
    }
  this->file=std::ifstream("/etc/portage/package.use/"+this->short_name, std::ifstream::in);
  this->exist = file.good();
}

bool read_uses::exist_file(){
  return this->exist;
}

std::string* read_uses::get_uses(){
  std::string *ret=nullptr;
  if(this->exist){
      ret=new std::string;
      std::string line;
      while(std::getline(file, line)){
        std::string no_coment=line.substr(0,line.find("#"));
        std::size_t found=no_coment.find(this->short_name);
        if(found!=std::string::npos){
            found=no_coment.find(this->short_name);
            if(found!=std::string::npos){
                *ret=no_coment.substr(0, no_coment.length());
              }
          }
      }
    }
  return ret;
}