/* This file is part of Lcrzo.
*/

int lcrzo_ipc_init(lcrzo_ipc *pipc)
{ int ret;

  /*parameters verification*/
  if (pipc==NULL) return(LCRZO_ERR_PANULLPTR);

  /*ouvre le tube*/
#if defined LCRZODEF_SYSTEM_Unix
  { int tube[2];
    ret=pipe(tube);
    if (ret<0) return(LCRZO_ERR_FUPIPE);
    pipc->readpipe[0]=tube[0];
    pipc->writepipe[1]=tube[1];
    ret=pipe(tube);
    if (ret<0) return(LCRZO_ERR_FUPIPE);
    pipc->readpipe[1]=tube[0];
    pipc->writepipe[0]=tube[1];
  }
  pipc->readhandle[0]=NULL; /* unused */
  pipc->writehandle[0]=NULL; /* unused */
  pipc->readhandle[1]=NULL; /* unused */
  pipc->writehandle[1]=NULL; /* unused */
#elif defined LCRZODEF_SYSTEM_Windows
  ret=CreatePipe(&(pipc->readhandle[0]), &(pipc->writehandle[1]), NULL, 0);
  if (!ret) return(LCRZO_ERR_FUCREATEPIPE);
  ret=CreatePipe(&(pipc->readhandle[1]), &(pipc->writehandle[0]), NULL, 0);
  if (!ret) return(LCRZO_ERR_FUCREATEPIPE);
  pipc->readpipe[0]=-1; /* unused */
  pipc->writepipe[0]=-1; /* unused */
  pipc->readpipe[1]=-1; /* unused */
  pipc->writepipe[1]=-1; /* unused */
#else
#error "Unknown value for LCRZODEF_SYSTEM"
#endif

  /*affecte les elements de la structure*/
  pipc->side=-1;
  pipc->dataend=LCRZO_FALSE;
  pipc->openforreading=LCRZO_TRUE;
  pipc->openforwriting=LCRZO_TRUE;
  /*storeddata is empty*/
  pipc->storeddata=NULL;
  pipc->storeddatasize=0;
      
  return(LCRZO_ERR_OK);
}

