// NoCurl.cpp
//________________________________________________________________________
// initialize Idx in VeriSign () to 0:
//   CurUrl[0] "19400827" <-> E (FCE)/200804
//   CurUrl[1] "17175185" <-> E (FCE)/200806
//
// initialize Idx in VeriSign () to -1:
//   CurUrl[0] "19400827" <-> E (FCE)/200804
//   CurUrl[1] "17229744" <-> (FLOOR)/200806
//
// anomaly when both writes in Extrait() are identical...

#include <stdlib.h>
#include <string.h>
#include "NoCurl.h"
#define ASSEZ 0x40000                // 256 Kbytes

extern FILE *Door[NBFILES];
extern char *CurUrl[NBFILES], UrlName[NBFILES][1024];
static char UrlOri[] = "https://www.cortalconsors.fr/euroWebFr/-?$part=financeinfosHome.Desks.futures.Desks.overview",
            Host[] = "https://www.cortalconsors.fr",
            *Milieu, *Champ, *Buffer, *Large[NBFILES];
static int  IsParis = 0, Utile, NbRead;
//________________________________________________________________________
static void FillBuffer ( int Fdx )   // one shot read
{
  fflush ( Door[Fdx] );
  fseek  ( Door[Fdx], 0, SEEK_SET );
  NbRead = fread ( Buffer, 1, ASSEZ, Door[Fdx] );
  Champ  = Buffer;
  Utile  = -1;
}
//________________________________________________________________________
static long ReadString ()
{
  void *End;
  Champ  += Utile + 1;
  NbRead -= Utile + 1;
  if ( !NbRead || !(End = memchr ( Champ, '\n', NbRead )) )   return 0;
  Utile = (char*) End - Champ;
  Champ[Utile] = '\0';
  return 1;
}
//________________________________________________________________________
void VeriSign ()
{
  int Hen = strlen ( Host ), Idx = -1;
  CurUrl[0] = UrlName[0];
  FillBuffer ( 0 );
  while ( ReadString () ) {
    if ( !IsParis )
      IsParis = strstr ( Champ, ">Paris<" ) != 0;
    else if ( strstr ( Champ, "INSTRUMENT" ) ) {
      if ( Idx >= 0 ) {
        Milieu  = strchr ( Champ, '"' ) + 1;
        int Len = strchr ( Milieu, '"' ) - Milieu;
        memcpy ( CurUrl[Idx], UrlOri, Hen );
        memcpy ( CurUrl[Idx] + Hen, Milieu, Len );
        CurUrl[Idx][Hen + Len] = '\0';
      }
      if ( ++Idx == 2 )   break;
    }
  }
  fseek ( Door[0], 0, SEEK_SET );
}
//________________________________________________________________________
void Extrait ( int Fdx )
{
  FillBuffer ( Fdx );
  while ( ReadString ()   &&   ( !(Milieu = strstr ( Champ, ")/20" ))
                          ||   Champ != strstr ( Champ, "        " ) ) );
  printf ( "\n%.14s ++++++++++++++++++++++++++++++++++\n", Milieu - 6 );
}
//________________________________________________________________________
void Ouvre ( int Idx )
{
  char TempName[] = "Temp000";
  sprintf ( TempName + 4, "%03d", Idx );
  Large[Idx] = (char*) malloc ( ASSEZ + 0x40 + 0x10 );
  if ( !Idx )   Buffer = (char*) malloc ( ASSEZ );
  Door[Idx]  = fopen ( TempName, "w+b" );
  setvbuf ( Door[Idx], Large[Idx], _IOFBF, ASSEZ );
}
//________________________________________________________________________
void Ferme ( int Idx )
{
  fclose ( Door[Idx] );
  free ( (void*) Large[Idx] );
  if ( !Idx )   free ( (void*) Buffer );
}
//________________________________________________________________________
void OriginalUrl ( char* &Url )
{
  Url = UrlOri;
}
//________________________________________________________________________
