#include "helper.h"
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

apstring substring(apstring str, int index, int step)
{
   apstring ret; //to be returned
   if(step>0)
   {
      for(int i=index; i<str.length() && i<index+step; i++)
         ret+=str[i];
   }
   else
   {
      for(int i=index; i<str.length()+step; i++)
         ret+=str[i];
   }
   return ret;
}

double a2d(apstring str)
{
   double ret=0;
   int decimalcount=0; bool decfound=false;
   for(int i=0; i<str.length(); i++)
   {
      if('0'<=str[i] && str[i]<='9')
      {
         ret*=10;
         ret+=int(str[i])-48;
         decimalcount+=1*decfound;
      }
      if('.'==str[i])
         decfound=true;
   }
   for(int i=0; i<decimalcount; i++)
      ret/=10.0;
   return ret;
}

void get_file_list(vector <apstring> &list, apstring folder)
{
   list.resize(0);
   apstring temp="dir "; temp+=folder; temp+=" > TEMPFILE.x.y.z"; //command to use
   system(temp.c_str());

   ifstream inf;
   inf.open("TEMPFILE.x.y.z");
   apstring dummy;
   getline(inf,dummy);
   while(!inf.eof())
   {
      if(dummy!="")
         if(substring(dummy,14,1)==":" && substring(dummy,24,5)!="<DIR>" && substring(dummy,39,0)!="TEMPFILE.x.y.z")
         {
            list.resize(int(list.size())+1);
            list[int(list.size())-1]=substring(dummy,39,0);
         }
      getline(inf,dummy);
   }
   inf.close();
   system("del TEMPFILE.x.y.z");
}

void get_file_list_unix(vector <apstring> &list, apstring folder)
{
   list.resize(0);
 //  apstring temp="ls -l "; temp+=folder; temp+=" | grep '^-' > TEMPFILE.x.y.z"; //command to use
apstring temp="ls -la "; 
         temp+=folder; 
         temp+=" | grep '^-' | sed 's/ \\+/ /g' | cut -d\\  -f8 > TEMPFILE.x.y.z";

   system(temp.c_str());

   ifstream inf;
   inf.open("TEMPFILE.x.y.z");
   apstring dummy;
   getline(inf,dummy);
   while(!inf.eof())
   {
//      if(dummy!="")
//         if(substring(dummy,55,0)!="TEMPFILE.x.y.z")
//         {
            list.resize(int(list.size())+1);
            list[int(list.size())-1]=dummy;
//         }
      getline(inf,dummy);
   }
   inf.close();
   system("rm TEMPFILE.x.y.z");
}

double hrconvert(int year, int month, int day, int hour, int min, int sec)
{//converts sanepeople time into an absolute format, in number of whole hours
//since 1/1/70
   double baseyear=1970; //year  ///***BASE VALUES
   double basemonth=1; //month
   double baseday=1; //day
   double temp=0; //hour
   baseday+=temp/24.0; // day+=hours
   temp=0; //minute
   baseday+=temp/1440.0; //day+=minutes

   double givenyear=year;       ////****GIVEN VALUES
   double givenmonth=month;
   double givenday=day;
   temp=hour;
   givenday+=temp/24.0;
   temp=min;
   givenday+=temp/1440.0;
   temp=sec;
   givenday+=temp/86400;

   double tempwork,totaldays=0;
   if(givenyear>baseyear)
   {
      tempwork=baseyear;
      while(tempwork!=givenyear)
      {
         totaldays+=365;
         if(tempwork/4==int(tempwork/4) && tempwork/100!=int(tempwork/100) && basemonth<3)
            totaldays++;
         if((tempwork+1)/4==int((tempwork+1)/4) && (tempwork+1)/100!=int((tempwork+1)/100) && basemonth>=3)
            totaldays++;
         if(tempwork/400==int(tempwork/400) && basemonth<3)
            totaldays++;
         if((tempwork+1)/400==int((tempwork+1)/400) && basemonth>=3)
            totaldays++;
         tempwork++;
      }
   }
   if(givenyear<baseyear)
   {
      tempwork=baseyear;
      while(tempwork!=givenyear)
      {
         totaldays-=365;
         if(tempwork/4==int(tempwork/4) && tempwork/100!=int(tempwork/100) && givenmonth>=3)
            totaldays--;
         if((tempwork-1)/4==int((tempwork-1)/4) && (tempwork-1)/100!=int((tempwork-1)/100) && givenmonth<3)
            totaldays--;
         if(tempwork/400==int(tempwork/400) && givenmonth>=3)
            totaldays--;
         if((tempwork-1)/400==int((tempwork-1)/400) && givenmonth<3)
            totaldays--;
         tempwork--;
      }
   }
   if(givenyear==baseyear)
   {
      if(basemonth<=2 && givenmonth>2 && baseyear/4==int(baseyear/4) && baseyear/100!=int(baseyear/100))
         totaldays++;
      if(baseyear/400==int(baseyear/400) && basemonth<=2 && givenmonth>2)
         totaldays++;
      if(basemonth>2 && givenmonth<=2 && baseyear/4==int(baseyear/4) && baseyear/100!=int(baseyear/100))
         totaldays--;
      if(baseyear/400==int(baseyear/400) && basemonth>2 && givenmonth<=2)
         totaldays--;
   }
   if(givenmonth==2) totaldays+=31;
   if(givenmonth==3) totaldays+=59;
   if(givenmonth==4) totaldays+=90;
   if(givenmonth==5) totaldays+=120;
   if(givenmonth==6) totaldays+=151;
   if(givenmonth==7) totaldays+=181;
   if(givenmonth==8) totaldays+=212;
   if(givenmonth==9) totaldays+=243;
   if(givenmonth==10)totaldays+=273;
   if(givenmonth==11)totaldays+=304;
   if(givenmonth==12)totaldays+=334;
   if(basemonth==2)  totaldays-=31;
   if(basemonth==3)  totaldays-=59;
   if(basemonth==4)  totaldays-=90;
   if(basemonth==5)  totaldays-=120;
   if(basemonth==6)  totaldays-=151;
   if(basemonth==7)  totaldays-=181;
   if(basemonth==8)  totaldays-=212;
   if(basemonth==9)  totaldays-=243;
   if(basemonth==10) totaldays-=273;
   if(basemonth==11) totaldays-=304;
   if(basemonth==12) totaldays-=334;
   totaldays+=givenday-baseday;
   totaldays*=24; //days->hours
   return totaldays;
}
