summaryrefslogtreecommitdiff
path: root/src/common/socket.hpp
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2013-06-29 23:23:43 -0700
committerBen Longbons <b.r.longbons@gmail.com>2013-08-01 15:19:45 -0700
commit3b98f3439e33b15bba2036c402f9925340fdb2b9 (patch)
treef6a59330bb747d9cc64f5f83d06e7f76dc01d540 /src/common/socket.hpp
parent8d1480c1be7c9741876d89008277a2b3629a4d01 (diff)
downloadtmwa-3b98f3439e33b15bba2036c402f9925340fdb2b9.tar.gz
tmwa-3b98f3439e33b15bba2036c402f9925340fdb2b9.tar.bz2
tmwa-3b98f3439e33b15bba2036c402f9925340fdb2b9.tar.xz
tmwa-3b98f3439e33b15bba2036c402f9925340fdb2b9.zip
Poison std::string and use the various string classes
Diffstat (limited to 'src/common/socket.hpp')
-rw-r--r--src/common/socket.hpp53
1 files changed, 39 insertions, 14 deletions
diff --git a/src/common/socket.hpp b/src/common/socket.hpp
index 2366373..dd1c872 100644
--- a/src/common/socket.hpp
+++ b/src/common/socket.hpp
@@ -96,12 +96,6 @@ void do_socket(void);
// themselves as servers
void set_defaultparse(void(*defaultparse)(int));
-/// Wrappers to track number of free FDs
-void fclose_(FILE * fp);
-FILE *fopen_(const char *path, const char *mode);
-
-bool free_fds(void);
-
template<class T>
uint8_t *pod_addressof_m(T& structure)
{
@@ -149,10 +143,22 @@ void RFIFO_STRUCT(int fd, size_t pos, T& structure)
{
really_memcpy(pod_addressof_m(structure), static_cast<const uint8_t *>(RFIFOP(fd, pos)), sizeof(T));
}
+template<uint8_t len>
+inline
+VString<len-1> RFIFO_STRING(int fd, size_t pos)
+{
+ const char *const begin = static_cast<const char *>(RFIFOP(fd, pos));
+ const char *const end = begin + len-1;
+ const char *const mid = std::find(begin, end, '\0');
+ return XString(begin, mid, nullptr);
+}
inline
-void RFIFO_STRING(int fd, size_t pos, char *out, size_t len)
+FString RFIFO_STRING(int fd, size_t pos, size_t len)
{
- strzcpy(out, static_cast<const char *>(RFIFOP(fd, pos)), len);
+ const char *const begin = static_cast<const char *>(RFIFOP(fd, pos));
+ const char *const end = begin + len;
+ const char *const mid = std::find(begin, end, '\0');
+ return XString(begin, mid, nullptr);
}
inline
void RFIFO_BUF_CLONE(int fd, uint8_t *buf, size_t len)
@@ -189,14 +195,27 @@ void RBUF_STRUCT(const uint8_t *p, size_t pos, T& structure)
{
really_memcpy(pod_addressof_m(structure), p + pos, sizeof(T));
}
+template<uint8_t len>
+inline
+VString<len-1> RBUF_STRING(const uint8_t *p, size_t pos)
+{
+ const char *const begin = static_cast<const char *>(RBUFP(p, pos));
+ const char *const end = begin + len-1;
+ const char *const mid = std::find(begin, end, '\0');
+ return XString(begin, mid, nullptr);
+}
inline
-void RBUF_STRING(const uint8_t *p, size_t pos, char *out, size_t len)
+FString RBUF_STRING(const uint8_t *p, size_t pos, size_t len)
{
- strzcpy(out, static_cast<const char *>(RBUFP(p, pos)), len);
+ const char *const begin = static_cast<const char *>(RBUFP(p, pos));
+ const char *const end = begin + len;
+ const char *const mid = std::find(begin, end, '\0');
+ return XString(begin, mid, nullptr);
}
/// Unused - check how much data can be written
+// the existence of this seems scary
inline
size_t WFIFOSPACE(int fd)
{
@@ -229,9 +248,12 @@ void WFIFO_STRUCT(int fd, size_t pos, T& structure)
really_memcpy(static_cast<uint8_t *>(WFIFOP(fd, pos)), pod_addressof_c(structure), sizeof(T));
}
inline
-void WFIFO_STRING(int fd, size_t pos, const char *s, size_t len)
+void WFIFO_STRING(int fd, size_t pos, XString s, size_t len)
{
- strzcpy(static_cast<char *>(WFIFOP(fd, pos)), s, len);
+ char *const begin = static_cast<char *>(WFIFOP(fd, pos));
+ char *const end = begin + len;
+ char *const mid = std::copy(s.begin(), s.end(), begin);
+ std::fill(mid, end, '\0');
}
inline
void WFIFO_ZERO(int fd, size_t pos, size_t len)
@@ -276,9 +298,12 @@ void WBUF_STRUCT(uint8_t *p, size_t pos, T& structure)
really_memcpy(p + pos, pod_addressof_c(structure), sizeof(T));
}
inline
-void WBUF_STRING(uint8_t *p, size_t pos, const char *s, size_t len)
+void WBUF_STRING(uint8_t *p, size_t pos, XString s, size_t len)
{
- strzcpy(static_cast<char *>(WBUFP(p, pos)), s, len);
+ char *const begin = static_cast<char *>(WBUFP(p, pos));
+ char *const end = begin + len;
+ char *const mid = std::copy(s.begin(), s.end(), begin);
+ std::fill(mid, end, '\0');
}
inline
void WBUF_ZERO(uint8_t *p, size_t pos, size_t len)