#include <stdio.h>
#include <string.h>
#include "../winuser.h"
#include "../../kernel/scolobj.h"

int OBJTYPFILE;
/* reflexes type FILE */
#define RFLFILE_NB      0

int _DLGOpenFile(mmachine m)
{ m->pp+=5;return MMpush(m,NIL); }
int _DLGOpenFileMulti(mmachine m)
{ m->pp+=5;return MMpush(m,NIL); }
int _DLGSaveFile(mmachine m)
{ m->pp+=5;return MMpush(m,NIL); }
int _DLGMessageBox(mmachine m)
{ m->pp+=5;return MMpush(m,NIL); }
int _DLGrflopen(mmachine m)
{ m->pp+=3;return MMpush(m,NIL); }
int _DLGrflopenMulti(mmachine m)
{ m->pp+=3;return MMpush(m,NIL); }
int _DLGrflsave(mmachine m)
{ m->pp+=3;return MMpush(m,NIL); }
int _DLGrflmessage(mmachine m)
{ m->pp+=3;return MMpush(m,NIL); }


/* File fun [Chn channel P filename] */
int _FILEOpen(mmachine m)
{
  int s;
  int filename;
  FILE *f;

  filename=MMpull(m);

  if ((MMget(m,0)==NIL)||(filename==NIL))
  {
	  MMset(m,0,NIL);
	  return 0;
  }
  
  f=fopen(MMstartstr(m,filename>>1),"rb");
  MMechostr(MSKDEBUG,"\nOpening the file %s : %d\n\n",
	  MMstartstr(m,filename>>1),f);
  if (!f)
  {
	  MMset(m,0,NIL);
	  return 0;
  }

  s=MMmalloc(m,2,TYPEBUF); if (s==NIL) return MERRMEM;
  *(FILE**)MMstart(m,s)=f;
  if (MMpush(m,s+s+1)) return MERRMEM;

  return OBJcreate(m,OBJTYPFILE,(int)f,-1,0);
}

int FILEdelete2(mmachine m,int handsys,int objm)
{
  FILE *f;
  if (objm==NIL) return 0;
  f=*(FILE**)MMstart(m,objm>>1);
  fclose(f);
  *(FILE**)MMstart(m,objm>>1)=NULL;
  return 0;
}

int _FILEClose (mmachine m)
{
  int p;
  FILE *file;

  p=MMget(m,0)>>1;
  if (p==NIL) return 0;
  file=*(FILE**)MMstart(m,p);

  MMset(m,0,NIL);
  if (file==NULL) return 0;
  OBJdelTH(m,OBJTYPFILE,(int)file);
  MMset(m,0,0);
  return 0;
}

/* I fun [File f, I pos, I from] */
int _FILESeek (mmachine m)
{
  int p;
  FILE *file;
  int pos;
  int from;
  int res;

  from=MMpull(m)>>1;
  pos=MMpull(m)>>1;
  p=MMget(m,0)>>1;
  if (p==NIL) return 0;
  file=*(FILE**)MMstart(m,p);

  MMset(m,0,NIL);
  if (file==NULL) return 0;
  res=fseek(file,pos,from);
  MMset(m,0,res<<1);

  return 0;
}

int _FILETell (mmachine m)
{
  int p;
  FILE *file;
  int res;

  p=MMget(m,0)>>1;
  if (p==NIL) return 0;
  file=*(FILE**)MMstart(m,p);

  MMset(m,0,NIL);
  if (file==NULL) return 0;
  res=ftell(file);
  MMset(m,0,res<<1);

  return 0;
}

int _FILESize (mmachine m)
{
  int p;
  FILE *file;
  int temp;
  int res;

  p=MMget(m,0)>>1;
  if (p==NIL) return 0;
  file=*(FILE**)MMstart(m,p);

  MMset(m,0,NIL);
  if (file==NULL) return 0;
  temp=ftell(file);
  fseek(file,0,SEEK_END);
  res=ftell(file);
  fseek(file,temp,SEEK_SET);
  MMset(m,0,res<<1);

  return 0;
}


/* S fun [File f,I size] */
int _FILERead (mmachine m)
{
  int p;
  FILE *file;
  int s;
  int size;
  int res;
  
  size=MMpull(m)>>1;
  p=MMget(m,0)>>1;
  if (p==NIL) return 0;
  file=*(FILE**)MMstart(m,p);
  
  MMset(m,0,NIL);
  
  if (size<0) return 0;
  if (file==NULL) return 0;
  
  s=MMmalloc(m,((size+5)>>2)+1,TYPEBUF);
  if (s==NIL) return MERRMEM;
  res=fread(MMstartstr(m,s),1,size,file);
  MMstore(m,s,0,res);
  /*
  MMechostr(1,"\nRead %d bytes (%d requested) from a file [%X] (now at pos %d) \n\n",res,size,file,ftell(file));
  */
  ((char*)MMstartstr(m,s))[size]=0;
  MMset(m,0,(s<<1)|1);
  
  return 0;
}

int _GetFileNameFromP (mmachine m)
{
  int filename;
  int len;
  int rlen;
  char * S;
  char buf[256];
  
  filename=MMpull(m)>>1;
  
  if (filename==NIL) 
    {
      MMpush(m,NIL);
      return 0;
    }
  
  rlen=MMsizestr(m,filename);
  len=rlen;
  S=MMstartstr(m,filename);
  
  while ((len>=0) && (S[len]!='/') && (S[len]!='\\')) len--; 
  strcpy(buf,S+len+1);
  return Mpushstrbloc(m,buf);
}

int _PtoScol(mmachine m)
{ 
  packdir p;
  int q,i,l,k;
  char *s;
  char buf[1024];
  char file[1024];

  q=MMpull(m)>>1;
  if (q==NIL) return MMpush(m,NIL);
  s=MMstartstr(m,q);
  l=strlen(s);
  if (l>=1023) return MMpush(m,NIL);

  strcpy(file,s);

  for(i=0;i<l;i++) if (file[i]=='\\') file[i]='/';
  else if ((file[i]>='a')&&(file[i]<='z')) file[i]+='A'-'a';

  p=Firstpack;
  while(p)
    {
          strcpy(buf,p->path);
          k=strlen(buf);
      for(i=0;i<k;i++) if (buf[i]=='\\') buf[i]='/';
      else if ((buf[i]>='a')&&(buf[i]<='z')) buf[i]+='A'-'a';
          if (!strncmp(buf,file,k))
          {
                  strcpy(file,s);
          for(i=0;i<l;i++) if (file[i]=='\\') file[i]='/';
                  return Mpushstrbloc(m,file+k);
          }
      p=p->next;
        }
  return MMpush(m,NIL);
}

#define NDLGPKG 22
char* DLGname[NDLGPKG]=
{"OpenBox","OpenBoxMulti","SaveBox","MessageBox","_DLGOpenFile","_DLGOpenFileMulti","_DLGSaveFile","_DLGMessageBox","_DLGrflopen","_DLGrflopenMulti","_DLGrflsave","_DLGrflmessage","_FILEOpen","_FILEClose","_FILESeek","_FILETell","_FILESize","_FILERead","_GetFileNameFromP","_GetFileNameFromW","_PtoScol","File"};
int (*DLGfun[NDLGPKG])(mmachine m)=
{NULL,NULL,NULL,NULL,_DLGOpenFile,_DLGOpenFileMulti,_DLGSaveFile,_DLGMessageBox,_DLGrflopen,_DLGrflopenMulti,_DLGrflsave,_DLGrflmessage,_FILEOpen,_FILEClose,_FILESeek,_FILETell,_FILESize,_FILERead,_GetFileNameFromP,_GetFileNameFromP,_PtoScol,NULL};
int DLGnarg[NDLGPKG]=
{TYPTYPE,TYPTYPE,TYPTYPE,TYPTYPE,5,5,5,5,3,3,3,3,2,1,3,1,1,2,1,1,1,TYPTYPE};
char* DLGtype[NDLGPKG]=
{NULL,NULL,NULL,NULL,"fun [Chn ObjWin S S S] OpenBox","fun [Chn ObjWin S S S] OpenBoxMulti","fun [Chn ObjWin S S S] SaveBox","fun [Chn ObjWin S S I] MessageBox","fun [OpenBox fun [OpenBox u0 P] u1 u0] OpenBox","fun [OpenBoxMulti fun [OpenBoxMulti u0 [P r1]] u1 u0] OpenBoxMulti","fun [SaveBox fun [SaveBox u0 W] u1 u0] SaveBox","fun [MessageBox fun [MessageBox u0 I] u1 u0] MessageBox","fun [Chn P] File","fun [File] I","fun [File I I] I","fun [File] I","fun [File] I","fun [File I] S","fun [P] S","fun [W] S","fun [P] S",NULL};


int SCOLloadDLG(mmachine m)
{
  OBJTYPFILE=OBJregister(RFLFILE_NB,0,FILEdelete2,"OBJTYPFILE");
  return PKhardpak(m,"DLG.pkg",NDLGPKG,DLGname,DLGfun,DLGnarg,DLGtype);
}


