diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-08-04 10:51:32 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-08-04 10:51:32 +0900 |
| commit | 0fa3668e917c45409acd56ba624db24d95699895 (patch) | |
| tree | 76085b468fb85bb932b2f7b0c2e8538aa245900e | |
| parent | 29bfbac86ddd829326df025c009264cebd243610 (diff) | |
| parent | b6e27218ad1ffdba6d2103b85d5d9231b6380008 (diff) | |
| download | mruby-0fa3668e917c45409acd56ba624db24d95699895.tar.gz mruby-0fa3668e917c45409acd56ba624db24d95699895.zip | |
Merge pull request #2498 from cremno/msvc-snprintf
MSVC: add simple (v)snprintf implementation
| -rw-r--r-- | include/mruby/value.h | 8 | ||||
| -rw-r--r-- | src/etc.c | 37 |
2 files changed, 43 insertions, 2 deletions
diff --git a/include/mruby/value.h b/include/mruby/value.h index e2c9223e9..30c39c5f2 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -31,7 +31,7 @@ struct mrb_state; # define MRB_INT_MIN (INT32_MIN>>MRB_FIXNUM_SHIFT) # define MRB_INT_MAX (INT32_MAX>>MRB_FIXNUM_SHIFT) #endif - + #ifdef MRB_USE_FLOAT typedef float mrb_float; # define mrb_float_to_str(buf, i) sprintf(buf, "%.7e", i) @@ -47,7 +47,11 @@ struct mrb_state; # define inline __inline # endif # if _MSC_VER < 1900 -# define snprintf _snprintf +# include <stdarg.h> +MRB_API int mrb_msvc_vsnprintf(char *s, size_t n, const char *format, va_list arg); +MRB_API int mrb_msvc_snprintf(char *s, size_t n, const char *format, ...); +# define vsnprintf(s, n, format, arg) mrb_msvc_vsnprintf(s, n, format, arg) +# define snprintf(s, n, format, ...) mrb_msvc_snprintf(s, n, format, __VA_ARGS__) # endif # if _MSC_VER < 1800 # include <float.h> @@ -183,3 +183,40 @@ mrb_regexp_p(mrb_state *mrb, mrb_value v) { return mrb_class_defined(mrb, REGEXP_CLASS) && mrb_obj_is_kind_of(mrb, v, mrb_class_get(mrb, REGEXP_CLASS)); } + +#if defined _MSC_VER && _MSC_VER < 1900 + +#ifndef va_copy +static void +mrb_msvc_va_copy(va_list *dest, va_list src) +{ + *dest = src; +} +#define va_copy(dest, src) msvc_va_copy(&(dest), src) +#endif + +MRB_API int +mrb_msvc_vsnprintf(char *s, size_t n, const char *format, va_list arg) +{ + int cnt; + va_list argcp; + va_copy(argcp, arg); + if (n == 0 || (cnt = _vsnprintf_s(s, n, _TRUNCATE, format, argcp)) < 0) { + cnt = _vscprintf(format, arg); + } + va_end(argcp); + return cnt; +} + +MRB_API int +mrb_msvc_snprintf(char *s, size_t n, const char *format, ...) +{ + va_list arg; + int ret; + va_start(arg, format); + ret = mrb_msvc_vsnprintf(s, n, format, arg); + va_end(arg); + return ret; +} + +#endif /* defined _MSC_VER && _MSC_VER < 1900 */ |
