]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - util.c
fix build on big endian arches
[xonotic/gmqcc.git] / util.c
diff --git a/util.c b/util.c
index 113bc93bdca5ef5fa30457b401e67fc02dfb6a45..466729eceb5631f7b399a5978ebd7910b044086b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,9 +21,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <string.h>
+#define GMQCC_PLATFORM_HEADER
 #include <stdlib.h>
-
 #include "gmqcc.h"
 #include "platform.h"
 
@@ -225,9 +224,21 @@ size_t util_optimizationtostr(const char *in, char *out, size_t outsz) {
     return util_strtransform(in, out, outsz, "_ ", 'a'-'A');
 }
 
+int util_snprintf(char *str, size_t size, const char *fmt, ...) {
+    va_list  arg;
+    int      ret;
+
+    va_start(arg, fmt);
+    ret = platform_vsnprintf(str, size, fmt, arg);
+    va_end(arg);
+
+    return ret;
+}
+
 int util_asprintf(char **ret, const char *fmt, ...) {
     va_list  args;
     int      read;
+
     va_start(args, fmt);
     read = platform_vasprintf(ret, fmt, args);
     va_end  (args);
@@ -235,9 +246,76 @@ int util_asprintf(char **ret, const char *fmt, ...) {
     return read;
 }
 
-void util_seed(uint32_t value) {
-    srand((int)value);
+int util_sscanf(const char *str, const char *format, ...) {
+    va_list  args;
+    int      read;
+
+    va_start(args, format);
+    read = platform_vsscanf(str, format, args);
+    va_end(args);
+
+    return read;
+}
+
+char *util_strncpy(char *dest, const char *src, size_t n) {
+    return platform_strncpy(dest, src, n);
+}
+char *util_strncat(char *dest, const char *src, size_t n) {
+    return platform_strncat(dest, src, n);
+}
+char *util_strcat(char *dest, const char *src) {
+    return platform_strcat(dest, src);
+}
+const char *util_strerror(int err) {
+    return platform_strerror(err);
+}
+
+const struct tm *util_localtime(const time_t *timer) {
+    return platform_localtime(timer);
+}
+const char *util_ctime(const time_t *timer) {
+    return platform_ctime(timer);
 }
+
+bool util_isatty(fs_file_t *file) {
+    if (file == (fs_file_t*)stdout) return !!platform_isatty(STDOUT_FILENO);
+    if (file == (fs_file_t*)stderr) return !!platform_isatty(STDERR_FILENO);
+    return false;
+}
+/*
+ * A small noncryptographic PRNG based on:
+ * http://burtleburtle.net/bob/rand/smallprng.html
+ */
+static uint32_t util_rand_state[4] = {
+    0xF1EA5EED, 0x00000000,
+    0x00000000, 0x00000000
+};
+
+#define util_rand_rot(X, Y) (((X)<<(Y))|((X)>>(32-(Y))))
+
 uint32_t util_rand() {
-    return rand();
+    uint32_t last;
+
+    last               = util_rand_state[0] - util_rand_rot(util_rand_state[1], 27);
+    util_rand_state[0] = util_rand_state[1] ^ util_rand_rot(util_rand_state[2], 17);
+    util_rand_state[1] = util_rand_state[2] + util_rand_state[3];
+    util_rand_state[2] = util_rand_state[3] + last;
+    util_rand_state[3] = util_rand_state[0] + last;
+
+    return util_rand_state[3];
 }
+
+#undef util_rand_rot
+
+void util_seed(uint32_t value) {
+    size_t i;
+
+    util_rand_state[0] = 0xF1EA5EED;
+    util_rand_state[1] = value;
+    util_rand_state[2] = value;
+    util_rand_state[3] = value;
+
+    for (i = 0; i < 20; ++i)
+        (void)util_rand();
+}
+