//________________________________________________________________________
// NoCurl.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "NoCurl.h"
#define ASSEZ       0x40000          // 256ko, (32000 auparavant)
#define FIXEDLENGTH 128

extern FILE *Door[NBFILES];
extern char *CurUrl[NBFILES], UrlName[NBFILES][1024];
char        *Large[NBFILES];         // buffer plus grand que 0x1000 octets
char        UrlOri[] = "https://www.cortalconsors.fr/euroWebFr/-?$part=financeinfosHome.Desks.futures.Desks.overview";
static char Header[] = "https://www.cortalconsors.fr", *Milieu,
            Paris[] = ">Paris<", Instrument[] = "INSTRUMENT",
            Page[]  = "            Page", Date[] = "class=\"date\"",
            Fixed[FIXEDLENGTH], *Champ = Fixed;
static int  IsParis = 0, Utile = 0, Alloc = FIXEDLENGTH - 1;
//________________________________________________________________________
long ReadString ( int Idx )
{
  char *Cur, *End;
  int  Utile, More, Eof;

  Utile = 0;
  Champ[0] = '\0';                   // empty string
  do {
    More = FIXEDLENGTH + Utile;
    if ( More > Alloc ) {
      Alloc = More << 1;
      char* New = new char[Alloc + 1];
      strcpy ( New, Champ );
      if ( Champ != Fixed )   delete Champ;
      Champ = New;                   // not freed
    }
    if ( !Door[Idx]->_cnt ) {
      fgetc ( Door[Idx] );           // buffer filling
      Door[Idx]->_ptr--;
      Door[Idx]->_cnt++;
    }
    Cur = Door[Idx]->_ptr;
    More = FIXEDLENGTH <= Door[Idx]->_cnt ? FIXEDLENGTH : Door[Idx]->_cnt;
    if ( End = (char *) memchr ( Cur, '\n', More ) )
      More = End - Cur + 1;
    memcpy ( Champ + Utile, Cur, More );
    Utile           += More;
    Door[Idx]->_ptr += More;
    Door[Idx]->_cnt -= More;
    Eof = feof ( Door[Idx] );
    Champ[Utile] = '\0';
  }
  while ( !End   &&   !Eof );

  if ( Utile   &&   Champ[Utile - 1] == '\n' ) {
    Utile--;                         // removes eventual '\n', '\r'
    Champ[Utile] = '\0';
  }
  if ( Utile   &&   Champ[Utile - 1] == '\r' ) {
    Utile--;
    Champ[Utile] = '\0';
  }
  return Eof == 0;                   // 0 if meets end of file
}
//________________________________________________________________________
int NewUrl ()
{
  if ( !IsParis )
    IsParis = strstr ( Champ, Paris ) != 0;
  else if ( strstr ( Champ, Instrument ) ) {
    int Len, Hen = strlen ( Header );
    Milieu = strchr ( Champ, '"' ) + 1;
    Len    = strchr ( Milieu, '"' ) - Milieu;
    memcpy ( CurUrl[0], Header, Hen );
    memcpy ( CurUrl[0] + Hen, Milieu, Len );
    CurUrl[0][Hen + Len] = '\0';
    return 1;
  }
  return 0;
}
//________________________________________________________________________
void VeriSign ( int& MaxLon )
{
  char Tampon[48], Surf[] = "&$event=page&offset=0000", *Car;

  CurUrl[0] = UrlName[0];
  fflush ( Door[0] );
  fseek  ( Door[0], 0, SEEK_SET );
  while  ( ReadString ( 0 )   &&   !NewUrl () );
  fflush ( Door[0] );
  fseek  ( Door[0], 0, SEEK_SET );
  Car = strstr ( CurUrl[0], "Desks.overview" ) + 6;
  strncpy ( Tampon, Car + 54, 47 );
  strncpy ( Car, "snapshot.content.snapshot.snapshot.timesandsales", 48 );
  strncpy ( Car + 48, Tampon, 47 );
  strcpy  ( Car + 48 + 47, Surf );
  MaxLon = Car - CurUrl[0] + 48 + 47 + strlen ( Surf ) - 4;
  strcpy ( CurUrl[1], CurUrl[0] );
}
//________________________________________________________________________
void Extrait ( int Fdx )
{
  fflush ( Door[Fdx] );
  fseek  ( Door[Fdx], 0, SEEK_SET );
  while ( ReadString ( Fdx )   &&   !strstr ( Champ, Page ) );
  while ( ReadString ( Fdx ) ) {
    if ( strstr ( Champ, Date ) ) {
      ReadString ( Fdx );
      printf ( "%.8s ++++++++++++++++++++++++++++++++++++++++++++++\n",
               Champ + 16 );         // BUG: both writes are identical...
      break;                         //      ... or only one write
    }
    else if ( strstr ( Champ, Page ) )   break;
  }
}
//________________________________________________________________________
void Ouvre ( int Idx )
{
  char TempName[] = "Temp000";
  sprintf ( TempName + 4, "%03d", Idx );
  Large[Idx]   = (char*) malloc ( ASSEZ + 0x40 + 0x10 );
  Door[Idx]    = fopen ( TempName, "w+b" );
  char *Aligne = (char*) ( ( int(Large[Idx]) + 3 ) & -4 );
  setvbuf ( Door[Idx], Aligne, _IOFBF, ASSEZ );
}
//________________________________________________________________________
void Ferme ( int Idx )
{
  fclose ( Door[Idx] );
  free ( (void*) Large[Idx] );
}
//________________________________________________________________________
void OriginalUrl ( char* &Url )
{
  Url = UrlOri;
}
//________________________________________________________________________
