aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/util.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-07-24 00:50:58 -0700
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-07-24 00:59:27 -0700
commitb4f53143b0e05fd3061cdf2e65e17a6a2904090b (patch)
tree4785bf31f7b89fc2420aa740d9a6967dc6c6f9b1 /src/util.cpp
parent9c2fdc6da57032c4448b59de5872086eea626b74 (diff)
Migrate source files into src/ directory
This change moves source files into a src/ directory, and puts object files into an obj/ directory. The Makefile and xcode project are updated accordingly. Fixes #1866
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp
new file mode 100644
index 00000000..85b9d8d7
--- /dev/null
+++ b/src/util.cpp
@@ -0,0 +1,127 @@
+/** \file util.c
+ Generic utilities library.
+
+ Contains datastructures such as automatically growing array lists, priority queues, etc.
+*/
+
+#include "config.h"
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+#include <math.h>
+#include <sys/time.h>
+#include <stdarg.h>
+#include <string.h>
+#include <ctype.h>
+#include <wctype.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "fallback.h"
+#include "util.h"
+
+#include "common.h"
+#include "wutil.h"
+
+/**
+ Minimum allocated size for data structures. Used to avoid excessive
+ memory allocations for lists, hash tables, etc, which are nearly
+ empty.
+*/
+#define MIN_SIZE 32
+
+/**
+ Maximum number of characters that can be inserted using a single
+ call to sb_printf. This is needed since vswprintf doesn't tell us
+ what went wrong. We don't know if we ran out of space or something
+ else went wrong. We assume that any error is an out of memory-error
+ and try again until we reach this size. After this size has been
+ reached, it is instead assumed that something was wrong with the
+ format string.
+*/
+#define SB_MAX_SIZE (128*1024*1024)
+
+int wcsfilecmp(const wchar_t *a, const wchar_t *b)
+{
+ CHECK(a, 0);
+ CHECK(b, 0);
+
+ if (*a==0)
+ {
+ if (*b==0)
+ return 0;
+ return -1;
+ }
+ if (*b==0)
+ {
+ return 1;
+ }
+
+ long secondary_diff=0;
+ if (iswdigit(*a) && iswdigit(*b))
+ {
+ wchar_t *aend, *bend;
+ long al;
+ long bl;
+ long diff;
+
+ errno = 0;
+ al = wcstol(a, &aend, 10);
+ bl = wcstol(b, &bend, 10);
+
+ if (errno)
+ {
+ /*
+ Huuuuuuuuge numbers - fall back to regular string comparison
+ */
+ return wcscmp(a, b);
+ }
+
+ diff = al - bl;
+ if (diff)
+ return diff > 0 ? 2 : -2;
+
+ secondary_diff = (aend-a) - (bend-b);
+
+ a=aend-1;
+ b=bend-1;
+ }
+ else
+ {
+ int diff = towlower(*a) - towlower(*b);
+ if (diff != 0)
+ return (diff>0)?2:-2;
+
+ secondary_diff = *a-*b;
+ }
+
+ int res = wcsfilecmp(a+1, b+1);
+
+ if (abs(res) < 2)
+ {
+ /*
+ No primary difference in rest of string.
+ Use secondary difference on this element if found.
+ */
+ if (secondary_diff)
+ {
+ return secondary_diff > 0 ? 1 :-1;
+ }
+ }
+
+ return res;
+}
+
+long long get_time()
+{
+ struct timeval time_struct;
+ gettimeofday(&time_struct, 0);
+ return 1000000ll*time_struct.tv_sec+time_struct.tv_usec;
+}
+