Skip to content

Commit

Permalink
MS VC support for str.[ch]
Browse files Browse the repository at this point in the history
  • Loading branch information
nyetwurk committed Sep 24, 2014
1 parent 309048c commit 344c294
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 17 additions & 3 deletions str.c
Expand Up @@ -97,10 +97,14 @@ void sbensure(struct strbuf *sb, int n)

buf = realloc(sb->pbuf, len);
// ASSERT_INFO((buf != NULL), "%zu", len);
#ifdef _WIN32
sb->len = len;
#else
usable = malloc_usable_size(buf);
sb->pbuf = buf;
/* Take advantage of full usable allocation */
sb->len = (usable > len) ? usable : len;
#endif
sb->pbuf = buf;
}

/** Printf into an automatically extended string buffer */
Expand All @@ -119,26 +123,29 @@ int vsbprintf(struct strbuf *param, const char *fmt, va_list ap)
param->len = 100;
buf = malloc(param->len);
// ASSERT_INFO((buf != NULL), "%zu", param->len);
#ifndef _WIN32
/* Take advantage of full usable allocation */
usable = malloc_usable_size(buf);
if (param->len < usable)
param->len = usable;
#endif
}

while(1) {
int rem = param->len - param->offset;
va_list cp;
va_copy(cp, ap);
va_list cp = ap;
ret = vsnprintf(buf + param->offset, rem, fmt, cp);
va_end(cp);
if(ret == -1) buf = realloc(buf, param->len *= 2);
else if(ret >= rem) buf = realloc(buf, param->len += ret);
else break;
//ASSERT_INFO((buf != NULL), "%zu", param->len);
/* Take advantage of full usable allocation */
#ifndef _WIN32
usable = malloc_usable_size(buf);
if (param->len < usable)
param->len = usable;
#endif
}
param->offset += ret;
param->pbuf = buf;
Expand Down Expand Up @@ -340,6 +347,13 @@ void sbfree(struct strbuf *sb)
}
}

#ifdef _WIN32
static char *strtok_r(char *str, const char *delim, char **saveptr)
{
return strtok(str, delim);
}
#endif

int str_split(char *buf, char *argv[], int maxargc, const char *split)
{
int argc;
Expand Down
10 changes: 8 additions & 2 deletions str.h
Expand Up @@ -50,14 +50,20 @@ char *sbfill(struct strbuf *sb, char fill, int width);
void sbfree(struct strbuf *sb);

/* init sb with malloc'ed string. */
static inline void strbuf_from_str(struct strbuf *sb, char *str)
#ifdef _WIN32
#define __INLINE __inline
#else
#define __INLINE static inline
#endif

__INLINE void strbuf_from_str(struct strbuf *sb, char *str)
{
sb->pbuf = str;
sb->len = sb->offset = strlen(str);
}

/* safety string copy */
static inline int snputs(char *dest, const char *src, size_t n)
__INLINE int snputs(char *dest, const char *src, size_t n)
{
if (n<=0) return 0;

Expand Down

0 comments on commit 344c294

Please sign in to comment.