#include <sys/stat.h>

#ifndef WIN32
#include <unistd.h>
#endif

#include <libdbox.h>
#include <tools.h>
#include <logf.h>

bool filet::open(const char *filename, const char *mode)
 {
  if(file!=NULL) abort();

  file=fopen(filename, mode);

  return (file==NULL);
 }

void filet::close()
 {
  if(file==NULL) abort();

  fclose(file);
  file=(FILE *)NULL;
 }

bool filet::eof()
 {
  if(file==NULL) abort();

  return feof(file);
 }

size_t filet::read(void *data, size_t size)
 {
  return fread(data, 1, size, file);
 }

size_t filet::write(void *data, size_t size)
 {
  return fwrite(data, 1, size, file);
 }

#ifndef WIN32  
dword filelength(int handle)
 {
  dword oldpos=lseek(handle, 0, SEEK_CUR); // Unix - lseek gibt die
  dword length=lseek(handle, 0, SEEK_END); // resultierende Position zurück
  lseek(handle, oldpos, SEEK_SET);
  return length;
 }
#endif

dword filelength(const char *filename)
 {
  struct stat buf;
  if(stat(filename, &buf)!=0) return 0;
  return buf.st_size;
 }

static dword gettextfilesize(FILE *infile)
 {
  char tempstr[1000], *tptr;
  dword len=0;

  while(fgets(tempstr, 999, infile)!=NULL)
   {
    tptr=strchr(tempstr, '\n'); if(tptr!=NULL) *tptr=0;
    tptr=strchr(tempstr, '\r'); if(tptr!=NULL) *tptr=0;

    if(tempstr[0]!=0 && tempstr[0]!='#' && tempstr[0]!=';' && tempstr[0]!=' ')
      len+=strlen(tempstr)+1;
   }
  len++;
  return len;
 }

static void gettextfile(FILE *infile, char *data)
 {
  char tempstr[1000], *tptr;

  while(fgets(tempstr, 999, infile)!=NULL)
   {
    tptr=strchr(tempstr, '\n'); if(tptr!=NULL) *tptr=0;
    tptr=strchr(tempstr, '\r'); if(tptr!=NULL) *tptr=0;

    if(tempstr[0]!=0 && tempstr[0]!='#' && tempstr[0]!=';' && tempstr[0]!=' ')
     {
      strcpy(data, tempstr); // data is assumed to be big enough
      data+=strlen(tempstr)+1;
     }
   }
  *data=0;
 }

char *readtextconfigfile(const char *filename)
 {
  FILE *infile;
  char *result;

  if((infile=fopen(filename, "rt"))==NULL)
    return (char *)NULL;

  result=readtextconfigfile(infile);

  fclose(infile);

  return result;
 }

char *readtextconfigfile(FILE *infile)
 {
  char *data;
  dword datalen;

  datalen=gettextfilesize(infile);
  if(datalen==0)
   {
    fclose(infile);
    return (char *)NULL;
   }

  data=(char *)malloc(datalen);
  if(data==NULL)
    logf.internalerror("readtextconfigfile", "malloc failed!");

  rewind(infile);
  gettextfile(infile, data);

  return data;
 }

bool checkload(byte maxload)
 {
  if(maxload!=0)
   {
    #ifdef WIN32

    #else
    #ifdef OS_WIN32

    #else
    FILE *infile;
    char tempstr[1000];

    infile=fopen("/proc/loadavg", "rt");

    if(infile==NULL) { maxload=0; return FALSE; }

    if(fgets(tempstr, 900, infile)==NULL) 
     { maxload=0; fclose(infile); return FALSE; }

    fclose(infile);

    if(atoi(tempstr)>=maxload)
      return TRUE;
    #endif
    #endif
   }

  return FALSE;
 }

bool renamedirectory(const char *path, const char *dir)
 {
  char tempstr1[DIRLEN], tempstr2[DIRLEN], *tptr;
  dword nr=0;

  snprintf(tempstr1, DIRLEN, "%s%s", path, dir);
  snprintf(tempstr2, DIRLEN, "%s_%s", path, dir);

  if(access(tempstr1, 00)!=0) return TRUE;

  tptr=tempstr2+strlen(tempstr2);

  do
   {
    sprintf(tptr, ".%lu", nr);
    nr++;
   }
  while(access(tempstr2, 00)==0);

  if(rename(tempstr1, tempstr2)!=0) return TRUE;

  return FALSE;
 }

int readconfigline(istream &in, char *line1, char *line2, word maxlen)
 {
  char tempstr[1000], *tptr, *tptr2;
  char ch;

  line1[0]=line2[0]=0;

  do
   {
    if(in.eof() || in.fail())
      return 1;

    if(!in.get(tempstr, 900))
      return 1;

    in.get(ch); // \n lesen

    tptr=strchr(tempstr, '\n'); if(tptr!=NULL) *tptr=0;
    tptr=strchr(tempstr, '\r'); if(tptr!=NULL) *tptr=0;

    tptr=tempstr;
    while(*tptr==' ' || *tptr=='\t') tptr++;

   }
  while(*tptr==0 || *tptr==';' || *tptr=='#');

  tptr2=tptr;
  while(*tptr2!=0 && *tptr2!='=' && *tptr2!=':')
    tptr2++;

  if(*tptr2==0) return 2;

  *(tptr2++)=0;
  while(*tptr2==' ' || *tptr2=='\t') tptr2++;

  strmaxcpy(line1, tptr, maxlen);
  strmaxcpy(line2, tptr2, maxlen);

  tptr=line1+strlen(line1);
  while(tptr>line1 && (*(tptr-1)==' ' || *(tptr-1)=='\t')) tptr--;
  *tptr=0;

  tptr=line2+strlen(line2);
  while(tptr>line2 && (*(tptr-1)==' ' || *(tptr-1)=='\t')) tptr--;
  *tptr=0;

  return 0;
 }

bool filecopy(std::istream &in, std::ostream &out, dword size)
 {
  dword buffersize, bytestocopy, bytestocopy2;
  char *buffer;

  buffersize=64000;
  if((buffer=(char *)malloc(buffersize))==NULL) return TRUE;
  bytestocopy=size;

  while(bytestocopy!=0)
   {
    bytestocopy2=bytestocopy>buffersize?buffersize:bytestocopy;

    if(!in.read(buffer, bytestocopy2))
     {
      free(buffer);
      return TRUE;
     }

    if(!out.write(buffer, bytestocopy2))
     {
      free(buffer);
      return TRUE;
     }

    bytestocopy-=bytestocopy2;
   }
  free(buffer);
  return FALSE;
 }

bool filecopy(std::istream &in, std::ostream &out)
 {
  dword buffersize;
  long int bytesread;
  char *buffer;

  buffersize=64000l;
  if((buffer=(char *)malloc(buffersize))==NULL) return TRUE;

  while(TRUE)
   {
    in.read(buffer, buffersize);

    bytesread=in.gcount();

    if(bytesread==0)
     {
      free(buffer);
      return FALSE;
     }

    if(!out.write(buffer, bytesread))
     {
      free(buffer);
      return TRUE;
     }
   }
 }

bool filecopy(int file, std::ostream &out)
 {
  dword buffersize;
  long int bytesread;
  char *buffer;

  buffersize=64000l;
  if((buffer=(char *)malloc(buffersize))==NULL) return TRUE;

  while(TRUE)
   {
    bytesread=read(file, buffer, buffersize);
 
    if(bytesread==0)
     {
      free(buffer);
      return FALSE;
     }
    else if(bytesread<0)
     {
      free(buffer);
      return TRUE;
     }

    if(!out.write(buffer, bytesread))
     {
      free(buffer);
      return TRUE;
     }
   }
 }

bool filecopy(std::istream &in, int out, dword size)
 {
  dword buffersize, bytestocopy, bytestocopy2;
  char *buffer;

  buffersize=64000l;
  if((buffer=(char *)malloc(buffersize))==NULL) return TRUE;
  bytestocopy=size;

  while(bytestocopy!=0)
   {
    bytestocopy2=bytestocopy>buffersize?buffersize:bytestocopy;

    if(!in.read(buffer, bytestocopy2))
     {
      free(buffer);
      return TRUE;
     }

    if(write(out, buffer, bytestocopy2)!=(ssize_t)bytestocopy2)
     {
      free(buffer);
      return TRUE;
     }

    bytestocopy-=bytestocopy2;
   }
  free(buffer);
  return FALSE;
 }



syntax highlighted by Code2HTML, v. 0.9.1