mirror of
https://github.com/libretro/RetroArch
synced 2025-03-03 04:14:00 +00:00
(Network Stream) Add string functions (#14333)
This commit is contained in:
parent
f3eaa5cfa8
commit
f27c8c0c1d
@ -165,6 +165,33 @@ bool netstream_read_float(netstream_t *stream, float *data);
|
||||
bool netstream_read_double(netstream_t *stream, double *data);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* netstream_read_string:
|
||||
*
|
||||
* @stream : Pointer to a network stream object.
|
||||
* @s : Pointer to a string buffer.
|
||||
* @len : Size of the string buffer.
|
||||
*
|
||||
* Reads a string from the network stream.
|
||||
*
|
||||
* Returns: Length of the original string on success or
|
||||
* a negative value on error.
|
||||
*/
|
||||
int netstream_read_string(netstream_t *stream, char *s, size_t len);
|
||||
|
||||
/**
|
||||
* netstream_read_fixed_string:
|
||||
*
|
||||
* @stream : Pointer to a network stream object.
|
||||
* @s : Pointer to a string buffer.
|
||||
* @len : Size of the string buffer.
|
||||
*
|
||||
* Reads a fixed-length string from the network stream.
|
||||
*
|
||||
* Returns: true on success, false otherwise.
|
||||
*/
|
||||
bool netstream_read_fixed_string(netstream_t *stream, char *s, size_t len);
|
||||
|
||||
/**
|
||||
* netstream_write:
|
||||
*
|
||||
@ -198,6 +225,32 @@ bool netstream_write_float(netstream_t *stream, float data);
|
||||
bool netstream_write_double(netstream_t *stream, double data);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* netstream_write_string:
|
||||
*
|
||||
* @stream : Pointer to a network stream object.
|
||||
* @s : Pointer to a string.
|
||||
*
|
||||
* Writes a null-terminated string into the network stream.
|
||||
*
|
||||
* Returns: true on success, false otherwise.
|
||||
*/
|
||||
bool netstream_write_string(netstream_t *stream, const char *s);
|
||||
|
||||
/**
|
||||
* netstream_write_fixed_string:
|
||||
*
|
||||
* @stream : Pointer to a network stream object.
|
||||
* @s : Pointer to a string.
|
||||
* @len : Size of the string.
|
||||
*
|
||||
* Writes a null-terminated fixed-length string into the network stream.
|
||||
*
|
||||
* Returns: true on success, false otherwise.
|
||||
*/
|
||||
bool netstream_write_fixed_string(netstream_t *stream, const char *s,
|
||||
size_t len);
|
||||
|
||||
RETRO_END_DECLS
|
||||
|
||||
#endif
|
||||
|
@ -178,6 +178,55 @@ NETSTREAM_READ_TYPE(double, double, uint64_t, retro_be_to_cpu64)
|
||||
#undef NETSTREAM_READ_TYPE
|
||||
#endif
|
||||
|
||||
int netstream_read_string(netstream_t *stream, char *s, size_t len)
|
||||
{
|
||||
char c;
|
||||
int ret = 0;
|
||||
|
||||
if (!s || !len)
|
||||
return -1;
|
||||
|
||||
for (; --len; ret++)
|
||||
{
|
||||
if (!netstream_read(stream, &c, sizeof(c)))
|
||||
return -1;
|
||||
|
||||
*s++ = c;
|
||||
|
||||
if (!c)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!len)
|
||||
{
|
||||
*s = '\0';
|
||||
|
||||
for (;; ret++)
|
||||
{
|
||||
if (!netstream_read(stream, &c, sizeof(c)))
|
||||
return -1;
|
||||
if (!c)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool netstream_read_fixed_string(netstream_t *stream, char *s, size_t len)
|
||||
{
|
||||
if (!len)
|
||||
return false;
|
||||
|
||||
if (!netstream_read(stream, s, len))
|
||||
return false;
|
||||
|
||||
/* Ensure the string is always null-terminated. */
|
||||
s[len - 1] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool netstream_write(netstream_t *stream, const void *data, size_t len)
|
||||
{
|
||||
size_t remaining = stream->size - stream->pos;
|
||||
@ -250,3 +299,26 @@ NETSTREAM_WRITE_TYPE(double, double, uint64_t, retro_cpu_to_be64)
|
||||
|
||||
#undef NETSTREAM_WRITE_TYPE
|
||||
#endif
|
||||
|
||||
bool netstream_write_string(netstream_t *stream, const char *s)
|
||||
{
|
||||
if (!s)
|
||||
return false;
|
||||
|
||||
return netstream_write(stream, s, strlen(s) + 1);
|
||||
}
|
||||
|
||||
bool netstream_write_fixed_string(netstream_t *stream, const char *s,
|
||||
size_t len)
|
||||
{
|
||||
char end = '\0';
|
||||
|
||||
if (!netstream_write(stream, s, len))
|
||||
return false;
|
||||
|
||||
/* Ensure the string is always null-terminated. */
|
||||
netstream_seek(stream, -1, NETSTREAM_SEEK_CUR);
|
||||
netstream_write(stream, &end, sizeof(end));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user