Add IO type checking functions, add ReadLine

This commit is contained in:
2025-11-08 18:50:13 +03:00
parent 44057e96f3
commit 6d02598e20
14 changed files with 310 additions and 0 deletions

View File

@@ -302,3 +302,12 @@ BH_IO *BH_BufferNew(BH_IO *device,
return (BH_IO*)buffer;
}
int BH_IOIsBuffer(BH_IO *device)
{
if (!device)
return 0;
return device->callback == (BH_IOCallback)bufferCallback;
}

View File

@@ -215,3 +215,13 @@ BH_IO *BH_BytesNew(char *data,
return (BH_IO*)bytes;
}
int BH_IOIsBytes(BH_IO *device)
{
if (!device)
return 0;
return device->callback == (BH_IOCallback)bytesCallback;
}

View File

@@ -178,3 +178,53 @@ int BH_IOEndOfFile(BH_IO *device)
return flags & BH_IO_FLAG_EOF;
}
char *BH_IOReadLine(BH_IO *device,
char *str,
size_t size)
{
size_t i, actual;
if (size < 1)
return NULL;
i = 0;
while (i < size - 1)
{
if (BH_IORead(device, str + i, 1, &actual) || actual != 1)
break;
if (str[i++] == '\n')
break;
}
str[i] = 0;
return i ? str : NULL;
}
char *BH_IOReadLineFull(BH_IO *device,
char *str,
size_t size)
{
size_t i, actual;
char data;
if (size < 1)
return NULL;
i = 0;
while (1)
{
if (BH_IORead(device, &data, 1, &actual) || actual != 1)
break;
if (i < size - 1)
str[i++] = data;
if (data == '\n')
break;
}
str[i] = 0;
return i ? str : NULL;
}

View File

@@ -13,3 +13,9 @@ BH_IO *BH_FileNew(const char *path,
return NULL;
}
int BH_IOIsFile(BH_IO *device)
{
return 0;
}

View File

@@ -306,3 +306,12 @@ BH_IO *BH_FileNew(const char *path,
return (BH_IO*)file;
}
int BH_IOIsFile(BH_IO *device)
{
if (!device)
return 0;
return device->callback == (BH_IOCallback)fileCallback;
}

View File

@@ -315,3 +315,12 @@ BH_IO *BH_FileNew(const char *path,
return (BH_IO*)file;
}
int BH_IOIsFile(BH_IO *device)
{
if (!device)
return 0;
return device->callback == (BH_IOCallback)fileCallback;
}