diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2015-08-05 16:22:21 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2015-08-05 16:22:21 -0700 |
| commit | 899c9fe94e04f50c6176644b61e2d89cb74a8886 (patch) | |
| tree | 5ffba668fd70c6be47a93eedb9033bb12e1feab6 /src/util.cpp | |
| parent | e66c34980cfd74a2643600822c4dbde93a86e585 (diff) | |
| download | zig-899c9fe94e04f50c6176644b61e2d89cb74a8886.tar.gz zig-899c9fe94e04f50c6176644b61e2d89cb74a8886.zip | |
read a file
Diffstat (limited to 'src/util.cpp')
| -rw-r--r-- | src/util.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000000..15cc6caab4 --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Andrew Kelley + * + * This file is part of zig, which is MIT licensed. + * See http://opensource.org/licenses/MIT + */ + +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> + +#include "util.hpp" + +void zig_panic(const char *format, ...) { + va_list ap; + va_start(ap, format); + vfprintf(stderr, format, ap); + fprintf(stderr, "\n"); + va_end(ap); + abort(); +} + +char *zig_alloc_sprintf(int *len, const char *format, ...) { + va_list ap, ap2; + va_start(ap, format); + va_copy(ap2, ap); + + int len1 = vsnprintf(nullptr, 0, format, ap); + assert(len1 >= 0); + + size_t required_size = len1 + 1; + char *mem = allocate<char>(required_size); + if (!mem) + return nullptr; + + int len2 = vsnprintf(mem, required_size, format, ap2); + assert(len2 == len1); + + va_end(ap2); + va_end(ap); + + if (len) + *len = len1; + return mem; +} + |
