/* This file is part of Lcrzo.
*/

/*-------------------------------------------------------------*/
int lcrzo_priv_stdin(lcrzo_string *puserstring,
		     lcrzo_int32 *puserstringsize)
#if defined LCRZODEF_SYSTEM_Unix
{ char *ptr;
  lcrzo_int32 fgetstep, ptrallocsize, userstringsize;
  int c;

  fgetstep=3;
  ptrallocsize=fgetstep;
  lcrzo_er(lcrzo_string_alloc(ptrallocsize, &ptr));  

  lcrzo_er(lcrzo_stream_flush(stdin));

  userstringsize=0;
  c=getc(stdin);  
  while ( c!=0x0D && c!=0x0A && c!=0x00 && c!=EOF )
  { ptr[userstringsize]=(char)c;
    userstringsize++;
    c=getc(stdin);  
    if (userstringsize==ptrallocsize)
    { ptrallocsize+=fgetstep;
      lcrzo_er(lcrzo_string_realloc(ptrallocsize, &ptr));  
    }
  }
  ptr[userstringsize]='\0';

  if (puserstring!=NULL)
  { *puserstring=ptr;
  }
  else
  { lcrzo_string_free(ptr);
  }
  if (puserstringsize!=NULL) *puserstringsize=userstringsize;

  return(LCRZO_ERR_OK);
}
#elif defined LCRZODEF_SYSTEM_Windows
{ lcrzo_file file;
  lcrzo_data data;
  lcrzo_int32 datasize;
  int ret;

  lcrzo_er(lcrzo_file_init_fd(0, &file));
  ret=lcrzo_file_readm_line(&file, LCRZO_WAIT_INFINITE, &data, &datasize);
  lcrzo_file_close(&file);
  
  data[datasize++]='\0';
  if (puserstring!=NULL)
  { *puserstring=(lcrzo_string)data;
  }
  else
  { lcrzo_data_free(data);
  }
  if (puserstringsize!=NULL) *puserstringsize=datasize;
  return(ret);
}
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

