Skip to content

Commit

Permalink
Added support for compiling the parser code with Microsoft Visual C c…
Browse files Browse the repository at this point in the history
…ompiler.

For hiredis-py and others support on windows.
  • Loading branch information
tzickel committed Mar 13, 2015
1 parent 37c06fa commit ec22967
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
2 changes: 2 additions & 0 deletions read.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
#include "fmacros.h"
#include <string.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <assert.h>
#include <errno.h>
#include <ctype.h>
Expand Down
22 changes: 11 additions & 11 deletions sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void sdsfree(sds s) {
* the output will be "6" as the string was modified but the logical length
* remains 6 bytes. */
void sdsupdatelen(sds s) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);
int reallen = strlen(s);
sh->free += (sh->len-reallen);
sh->len = reallen;
Expand All @@ -114,7 +114,7 @@ void sdsupdatelen(sds s) {
* so that next append operations will not require allocations up to the
* number of bytes previously available. */
void sdsclear(sds s) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);
sh->free += sh->len;
sh->len = 0;
sh->buf[0] = '\0';
Expand All @@ -133,7 +133,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {

if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-sizeof *sh);;
sh = (void*) (s-sizeof *sh);
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
Expand All @@ -155,7 +155,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
sds sdsRemoveFreeSpace(sds s) {
struct sdshdr *sh;

sh = (void*) (s-sizeof *sh);;
sh = (void*) (s-sizeof *sh);
sh = realloc(sh, sizeof *sh+sh->len+1);
sh->free = 0;
return sh->buf;
Expand All @@ -169,7 +169,7 @@ sds sdsRemoveFreeSpace(sds s) {
* 4) The implicit null term.
*/
size_t sdsAllocSize(sds s) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);

return sizeof(*sh)+sh->len+sh->free+1;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ size_t sdsAllocSize(sds s) {
* sdsIncrLen(s, nread);
*/
void sdsIncrLen(sds s, int incr) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);

assert(sh->free >= incr);
sh->len += incr;
Expand Down Expand Up @@ -240,7 +240,7 @@ sds sdscatlen(sds s, const void *t, size_t len) {

s = sdsMakeRoomFor(s,len);
if (s == NULL) return NULL;
sh = (void*) (s-sizeof *sh);;
sh = (void*) (s-sizeof *sh);
memcpy(s+curlen, t, len);
sh->len = curlen+len;
sh->free = sh->free-len;
Expand All @@ -267,13 +267,13 @@ sds sdscatsds(sds s, const sds t) {
/* Destructively modify the sds string 's' to hold the specified binary
* safe string pointed by 't' of length 'len' bytes. */
sds sdscpylen(sds s, const char *t, size_t len) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);
size_t totlen = sh->free+sh->len;

if (totlen < len) {
s = sdsMakeRoomFor(s,len-sh->len);
if (s == NULL) return NULL;
sh = (void*) (s-sizeof *sh);;
sh = (void*) (s-sizeof *sh);
totlen = sh->free+sh->len;
}
memcpy(s, t, len);
Expand Down Expand Up @@ -541,7 +541,7 @@ sds sdscatfmt(sds s, char const *fmt, ...) {
* Output will be just "Hello World".
*/
void sdstrim(sds s, const char *cset) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);
char *start, *end, *sp, *ep;
size_t len;

Expand Down Expand Up @@ -573,7 +573,7 @@ void sdstrim(sds s, const char *cset) {
* sdsrange(s,1,-1); => "ello World"
*/
void sdsrange(sds s, int start, int end) {
struct sdshdr *sh = (void*) (s-sizeof *sh);;
struct sdshdr *sh = (void*) (s-sizeof *sh);
size_t newlen, len = sdslen(s);

if (len == 0) return;
Expand Down
3 changes: 3 additions & 0 deletions sds.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

#include <sys/types.h>
#include <stdarg.h>
#ifdef _MSC_VER
#include "win32.h"
#endif

typedef char *sds;

Expand Down
42 changes: 42 additions & 0 deletions win32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef _WIN32_HELPER_INCLUDE
#define _WIN32_HELPER_INCLUDE
#ifdef _MSC_VER

#ifndef inline
#define inline __inline
#endif

#ifndef va_copy
#define va_copy(d,s) ((d) = (s))
#endif

#ifndef snprintf
#define snprintf c99_snprintf

__inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
{
int count = -1;

if (size != 0)
count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);

return count;
}

__inline int c99_snprintf(char* str, size_t size, const char* format, ...)
{
int count;
va_list ap;

va_start(ap, format);
count = c99_vsnprintf(str, size, format, ap);
va_end(ap);

return count;
}
#endif

#endif
#endif

0 comments on commit ec22967

Please sign in to comment.