added some stuff
This commit is contained in:
parent
449e9d726f
commit
d8b44c1281
5 changed files with 91 additions and 8 deletions
12
socket.c
12
socket.c
|
|
@ -121,11 +121,19 @@ bool skConnectPro(socket_t sock, const struct sockaddr *name, socket_len_t namel
|
|||
}
|
||||
|
||||
int skSend(socket_t sock, char *buf, int len) {
|
||||
return send(sock, buf, len, 0);
|
||||
return skSendPro(sock, buf, len, 0);
|
||||
}
|
||||
|
||||
int skSendPro(socket_t sock, char *buf, int len, int flags) {
|
||||
return send(sock, buf, len, flags);
|
||||
}
|
||||
|
||||
int skReceive(socket_t sock, char *buf, int len) {
|
||||
return recv(sock, buf, len, 0);
|
||||
return skReceivePro(sock, buf, len, 0);
|
||||
}
|
||||
|
||||
int skReceivePro(socket_t sock, char *buf, int len, int flags) {
|
||||
return recv(sock, buf, len, flags);
|
||||
}
|
||||
|
||||
bool skIsValid(socket_t sock) {
|
||||
|
|
|
|||
8
socket.h
8
socket.h
|
|
@ -16,11 +16,13 @@ extern "C" {
|
|||
struct sockaddr;
|
||||
|
||||
#if SOCK_WINDOWS
|
||||
typedef uintptr_t socket_t;
|
||||
#include <winsock2.h>
|
||||
typedef SOCKET socket_t;
|
||||
typedef int socket_len_t;
|
||||
#define INVALID_SOCKET (socket_t)(~0)
|
||||
#define SOCKET_ERROR (-1)
|
||||
#elif SOCK_POSIX
|
||||
#include <sys/socket.h>
|
||||
typedef int socket_t;
|
||||
typedef uint32_t socket_len_t;
|
||||
#define INVALID_SOCKET (-1)
|
||||
|
|
@ -66,8 +68,12 @@ bool skConnectPro(socket_t sock, const struct sockaddr *name, socket_len_t namel
|
|||
|
||||
// Sends data on a socket, returns true on success
|
||||
int skSend(socket_t sock, char *buf, int len);
|
||||
// Sends data on a socket, returns true on success
|
||||
int skSendPro(socket_t sock, char *buf, int len, int flags);
|
||||
// Receives data from a socket, returns byte count on success, 0 on connection close or -1 on error
|
||||
int skReceive(socket_t sock, char *buf, int len);
|
||||
// Sends data on a socket, returns true on success
|
||||
int skReceivePro(socket_t sock, char *buf, int len, int flags);
|
||||
|
||||
// Checks that a opened socket is valid, returns true on success
|
||||
bool skIsValid(socket_t sock);
|
||||
|
|
|
|||
|
|
@ -324,6 +324,8 @@ str_ostream_t ostrInitStr(const char *cstr, size_t len) {
|
|||
str_ostream_t stream;
|
||||
stream.buf = malloc(len + 1);
|
||||
memcpy(stream.buf, cstr, len);
|
||||
stream.size = len;
|
||||
stream.allocated = len + 1;
|
||||
return stream;
|
||||
}
|
||||
|
||||
|
|
|
|||
62
strutils.c
62
strutils.c
|
|
@ -4,6 +4,68 @@
|
|||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define _BUFSZ 128
|
||||
|
||||
// modified from netbsd source http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getdelim.c?only_with_tag=MAIN
|
||||
ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp) {
|
||||
char *ptr, *eptr;
|
||||
|
||||
if(*buf == NULL || *bufsz == 0) {
|
||||
*bufsz = _BUFSZ;
|
||||
if((*buf = malloc(*bufsz)) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t result = -1;
|
||||
// usually fgetc locks every read, using windows-specific
|
||||
// _lock_file and _unlock_file will be faster
|
||||
_lock_file(fp);
|
||||
|
||||
for(ptr = *buf, eptr = *buf + *bufsz;;) {
|
||||
int c = _getc_nolock(fp);
|
||||
if(c == -1) {
|
||||
if(feof(fp)) {
|
||||
ssize_t diff = (ssize_t)(ptr - *buf);
|
||||
if(diff != 0) {
|
||||
*ptr = '\0';
|
||||
result = diff;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
*ptr++ = c;
|
||||
if(c == delimiter) {
|
||||
*ptr = '\0';
|
||||
result = ptr - *buf;
|
||||
break;
|
||||
}
|
||||
if((ptr + 2) >= eptr) {
|
||||
char *nbuf;
|
||||
size_t nbufsz = *bufsz * 2;
|
||||
ssize_t d = ptr - *buf;
|
||||
if((nbuf = realloc(*buf, nbufsz)) == NULL) {
|
||||
break;
|
||||
}
|
||||
*buf = nbuf;
|
||||
*bufsz = nbufsz;
|
||||
eptr = nbuf + nbufsz;
|
||||
ptr = nbuf + d;
|
||||
}
|
||||
}
|
||||
|
||||
_unlock_file(fp);
|
||||
return result;
|
||||
}
|
||||
|
||||
// taken from netbsd source http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getline.c?only_with_tag=MAIN
|
||||
ssize_t getline(char **line_ptr, size_t *n, FILE *stream) {
|
||||
return getdelim(line_ptr, n, '\n', stream);
|
||||
}
|
||||
#endif
|
||||
|
||||
void strToLower(char *str) {
|
||||
for(char *beg = str; *beg; ++beg) {
|
||||
*beg = tolower(*beg);
|
||||
|
|
|
|||
15
strutils.h
15
strutils.h
|
|
@ -7,12 +7,17 @@ extern "C" {
|
|||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#if _WIN32
|
||||
#define stricmp _stricmp
|
||||
#elif __unix__ || __APPLE__
|
||||
#define stricmp strcasecmp
|
||||
#ifdef _WIN32
|
||||
#include <stdio.h>
|
||||
#include <BaseTsd.h>
|
||||
typedef SSIZE_T ssize_t;
|
||||
ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp);
|
||||
ssize_t getline(char **line_ptr, size_t *n, FILE *stream);
|
||||
#define stricmp _stricmp
|
||||
#else
|
||||
#error "stricmp is not supported"
|
||||
#define stricmp strcasecmp
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
// prefix str -> changes string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue