This commit is contained in:
hyung-hwan 2008-03-13 05:55:39 +00:00
parent 51a77e109e
commit 06fae5f95c
2 changed files with 76 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: stdio.c 116 2008-03-03 11:15:37Z baconevi $
* $Id: stdio.c 122 2008-03-12 11:55:39Z baconevi $
*
* {License}
*/
@ -369,3 +369,73 @@ FILE* ase_popen (const ase_char_t* cmd, const ase_char_t* mode)
#endif
}
ase_ssize_t ase_getline (ase_char_t **buf, ase_size_t *n, FILE *fp)
{
return ase_getdelim (buf, n, ASE_T('\n'), fp);
}
ase_ssize_t ase_getdelim (
ase_char_t **buf, ase_size_t *n, ase_char_t delim, FILE *fp)
{
ase_char_t* b;
ase_size_t capa;
ase_size_t len = 0;
ASE_ASSERT (buf != ASE_NULL);
ASE_ASSERT (n != ASE_NULL);
b = *buf;
capa = *n;
if (b == ASE_NULL)
{
capa = 256;
#if (defined(vms) || defined(__vms)) && (ASE_SIZEOF_VOID_P >= 8)
b = (ase_char_t*) _malloc32 (sizeof(ase_char_t)*(capa+1));
#else
b = (ase_char_t*) malloc (sizeof(ase_char_t)*(capa+1));
#endif
if (b == ASE_NULL) return -1;
}
while (1)
{
ase_cint_t c = ase_fgetc(fp);
if (c == ASE_CHAR_EOF)
{
if (ase_ferror(fp))
{
len = (ase_size_t)-1;
goto exit_task;
}
break;
}
if (len+1 >= capa)
{
ase_size_t ncapa = capa + 256;
ase_char_t* nb;
nb = realloc (b, ncapa*sizeof(ase_char_t));
if (nb == ASE_NULL)
{
len = (ase_size_t)-1;
goto exit_task;
}
b = nb;
capa = ncapa;
}
b[len++] = c;
if (c == delim) break;
}
b[len] = ASE_T('\0');
exit_task:
*buf = b;
*n = capa;
return (ase_ssize_t)len;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: stdio.h 121 2008-03-09 05:59:34Z baconevi $
* $Id: stdio.h 122 2008-03-12 11:55:39Z baconevi $
*/
#ifndef _ASE_UTL_STDIO_H_
@ -64,6 +64,10 @@ int ase_dprintf (const ase_char_t* fmt, ...);
FILE* ase_fopen (const ase_char_t* path, const ase_char_t* mode);
FILE* ase_popen (const ase_char_t* cmd, const ase_char_t* mode);
ase_ssize_t ase_getline (ase_char_t **buf, ase_size_t *n, FILE *fp);
ase_ssize_t ase_getdelim (
ase_char_t **buf, ase_size_t *n, ase_char_t delim, FILE *fp);
#ifdef __cplusplus
}
#endif