summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2009-10-31 14:39:49 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2009-10-31 14:40:37 +0800
commit518a016087b0bd94a890837220277bed6cb545fe (patch)
treed26d712310b84daf09f4373033bca729147109fa
parent08b175566ba73450db41efbce2e326878216dc42 (diff)
milkway: add mw-pointer.c & mw-strutil.{c,h}
-rw-r--r--milkway/Makefile.am1
-rw-r--r--milkway/mw-pointer.c33
-rw-r--r--milkway/mw-pointer.h19
-rw-r--r--milkway/mw-strutil.c317
-rw-r--r--milkway/mw-strutil.h86
5 files changed, 456 insertions, 0 deletions
diff --git a/milkway/Makefile.am b/milkway/Makefile.am
index e390287..6130ad4 100644
--- a/milkway/Makefile.am
+++ b/milkway/Makefile.am
@@ -53,6 +53,7 @@ libmilkway_la_SOURCES = \
mw-crypt.c \
mw-checksum.c \
mw-hexlify.c \
+ mw-pointer.c \
mw-strutil.c \
mw-thread.c \
mw-thread-private.h \
diff --git a/milkway/mw-pointer.c b/milkway/mw-pointer.c
new file mode 100644
index 0000000..5828f43
--- /dev/null
+++ b/milkway/mw-pointer.c
@@ -0,0 +1,33 @@
+
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "milkwayint.h"
+
+mw_bool_t
+mw_pointer_equal (const mw_pointer_t ptr1, mw_pointer_t ptr2)
+{
+ return ptr1 == ptr2;
+}
+
+mw_uint_t
+mw_pointer_hash (const mw_pointer_t ptr)
+{
+ return mw_mem_hash ((const mw_pointer_t)&ptr, sizeof (ptr));
+}
diff --git a/milkway/mw-pointer.h b/milkway/mw-pointer.h
index 1753eaf..d7d8760 100644
--- a/milkway/mw-pointer.h
+++ b/milkway/mw-pointer.h
@@ -244,6 +244,25 @@ mw_pointer_offset_align (mw_pointer_t pointer, mw_size_t nummem, mw_size_t size,
return mw_pointer_from_uintptr (aligned);
}
+/**
+ * @brief test two pointers is the same
+ *
+ * @param ptr1 a pointer
+ * @param ptr2 another pointer
+ * @return MW_TRUE, if ptr1 is equal to ptr2.
+ */
+mw_public mw_bool_t
+mw_pointer_equal (const mw_pointer_t ptr1, mw_pointer_t ptr2);
+
+/**
+ * @brief compute hash value from a pointer
+ *
+ * @param ptr the pointer
+ * @return the hash value computed from the pointer value
+ */
+mw_public mw_uint_t
+mw_pointer_hash (const mw_pointer_t ptr);
+
MW_END_DECLS
#endif
diff --git a/milkway/mw-strutil.c b/milkway/mw-strutil.c
new file mode 100644
index 0000000..8eeea87
--- /dev/null
+++ b/milkway/mw-strutil.c
@@ -0,0 +1,317 @@
+#include "milkwayint.h"
+
+#include <stdlib.h>
+#include <stdarg.h>
+
+char*
+mw_strdup(const char *s)
+{
+ char *r;
+
+ if (!s)
+ return NULL;
+
+ r = malloc(strlen(s) + 1);
+ if (!r)
+ return NULL;
+
+ return strcpy(r, s);
+}
+
+char *
+mw_stpcpy (char * dst, const char * src)
+{
+ do {
+ *dst++ = *src;
+ } while (*src++);
+
+ return dst - 1;
+}
+
+int
+mw_strnlen (const char * s, mw_size_t max)
+{
+ const char * p = s;
+
+ while (max && *p) {
+ ++p;
+ --max;
+ }
+
+ return p - s;
+}
+
+char *
+mw_strndup (char * src, mw_size_t size)
+{
+ char * new;
+ mw_size_t len;
+
+ len = mw_strnlen (src, size);
+ new = malloc (len + 1);
+ if (!new)
+ return NULL;
+
+ memcpy (new, src, len);
+ new[len] = '\0';
+ return new;
+}
+
+size_t
+mw_strlcpy (char * dst,
+ const char * src,
+ mw_size_t size)
+{
+ char * d = dst;
+ const char * s = src;
+ mw_size_t n = size;
+
+ if (!dst || !src)
+ return 0;
+
+ if (n && --n) {
+ do {
+ char c = *s++;
+
+ *d++ = c;
+ if (c == '\0')
+ break;
+ } while (--n);
+ }
+
+ if (!n) {
+ if (size)
+ *d = 0;
+ while (*s++)
+ ;
+ }
+
+ return s - src - 1;
+}
+
+size_t
+mw_strlcat (char * dst,
+ const char * src,
+ mw_size_t size)
+{
+ char * d = dst;
+ const char * s = src;
+ mw_size_t left = size;
+ mw_size_t dlen;
+
+ if (!dst || ! src)
+ return 0;
+
+ while (*d && left--)
+ d++;
+ dlen = d - dst;
+ left = size - dlen;
+
+ if (!left)
+ return dlen + strlen (s);
+
+ while (*s) {
+ if (left != 1) {
+ *d++ = *s;
+ left--;
+ }
+ s++;
+ }
+ *d = 0;
+
+ return dlen + (s - src);
+}
+
+int
+mw_strcmp (const char * s1, const char * s2)
+{
+#if defined(HAVE_STRCMP)
+ return strcmp (s1, s2);
+#else
+ for (; *s1 && *s2; s1++, s2++) {
+ if (*s1 != *s2)
+ return *s1 - *s2;
+ }
+
+ return (int)(s1 - s2);
+#endif
+}
+
+int
+mw_strcasecmp (const char * s1, const char * s2)
+{
+#if defined(HAVE_STRCASECMP)
+ return strcasecmp (s1, s2);
+#elif defined(HAVE_STRICMP)
+ return stricmp (s1, s2);
+#else
+ for (; *s1 && *s2; s1++, s2++) {
+ mw_int_t c1;
+ mw_int_t c2;
+
+ c1 = mw_tolower (*s1);
+ c2 = mw_tolower (*s2);
+ if (c1 != c2)
+ return c1 - c2;
+ }
+
+ return (int)(s1 - s2);
+#endif
+}
+
+char *
+mw_strbuild (const char * s, ...)
+{
+ va_list args;
+ mw_size_t len;
+ char * next;
+ char * new;
+ char * result;
+
+ if (!s)
+ return NULL;
+
+ len = strlen (s) + 1;
+ va_start (args, s);
+ for (next = va_arg (args, char *); next;
+ next = va_arg (args, char *))
+ len += strlen (next);
+ va_end (args);
+
+ new = malloc (len);
+ if (!new)
+ return NULL;
+
+ result = mw_stpcpy (new, s);
+ va_start (args, s);
+ for (next = va_arg (args, char *); next;
+ next = va_arg (args, char *))
+ result = mw_stpcpy (result, next);
+ va_end (args);
+
+ return new;
+}
+
+mw_bool_t
+mw_str_equal (const mw_pointer_t s1, const mw_pointer_t s2)
+{
+ return !strcmp (s1, s2);
+}
+
+mw_uint_t
+mw_str_hash (const mw_pointer_t s)
+{
+ return mw_mem_hash (s, strlen (s));
+}
+
+mw_uint_t
+mw_mem_hash (const mw_pointer_t ptr, mw_size_t n)
+{
+ const mw_uint8_t * key = ptr;
+ mw_uint_t hash = 0;
+ mw_size_t i;
+
+ for (i = 0; i < n; i++) {
+ hash += key[i];
+ hash += (hash << 10);
+ hash ^= (hash >> 6);
+ }
+
+ hash += (hash << 3);
+ hash ^= (hash >> 11);
+ hash += (hash << 15);
+ return hash;
+}
+
+void *
+mw_memcpy (void * dst, const void * src, mw_size_t n)
+{
+#ifdef HAVE_MEMCPY
+ return memcpy (dst, src, n);
+#else
+ char * d = dst;
+ const char * s = src;
+
+ if (!n)
+ return dst;
+
+ while (--n)
+ *d++ = *s++;
+
+ return dst;
+#endif
+}
+
+void *
+mw_memmove (void * dst, const void * src, mw_size_t n)
+{
+#ifdef HAVE_MEMMOVE
+ return memmove (dst, src, n);
+#else
+ char * d = dst;
+ const char * s = src;
+
+ if (!n)
+ return dst;
+
+ if (dst > src) {
+ while (--n)
+ *d++ = *s++;
+ } else {
+ d += n;
+ s += n;
+
+ while (--n)
+ *d-- = *s--;
+ }
+
+ return dst;
+#endif
+}
+
+mw_bool_t
+mw_is_alpha (mw_int_t c)
+{
+ return (c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z');
+}
+
+mw_bool_t
+mw_is_upper (mw_int_t c)
+{
+ return c >= 'A' && c <= 'Z';
+}
+
+mw_bool_t
+mw_is_lower (mw_int_t c)
+{
+ return c >= 'a' && c <= 'z';
+}
+
+mw_bool_t
+mw_is_digit (mw_int_t c)
+{
+ return c >= '0' && c <= '9';
+}
+
+mw_bool_t
+mw_is_alnum (mw_int_t c)
+{
+ return mw_is_alpha (c) || mw_is_digit (c);
+}
+
+mw_int_t
+mw_tolower (mw_int_t c)
+{
+ if (c >= 'A' && c <= 'Z')
+ return 'a' + c - 'A';
+ return c;
+}
+
+mw_int_t
+mw_toupper (mw_int_t c)
+{
+ if (c >= 'a' && c <= 'z')
+ return 'A' + c - 'a';
+ return c;
+}
diff --git a/milkway/mw-strutil.h b/milkway/mw-strutil.h
new file mode 100644
index 0000000..8da760f
--- /dev/null
+++ b/milkway/mw-strutil.h
@@ -0,0 +1,86 @@
+#ifndef MW_STRUTIL_H
+#define MW_STRUTIL_H
+
+#ifdef MW_OS_WINDOWS
+#define mw_snprintf _snprintf
+#else
+#define mw_snprintf snprintf
+#endif
+
+/**
+ * @brief Copies a string
+ *
+ * Copies the string pointed to by src, including the terminating null
+ * byte ('\\0'), to the buffer pointed to by dest. The strings may
+ * not overlap, and the destination string dest must be large enough
+ * to receive the copy.
+ *
+ * @param dst the destination string @param src the source string
+ * @return a pointer to the end of the string dest (that is, the
+ * address of the terminating null byte) rather than the beginning.
+ * @see f_strcpy
+ */
+mw_public char *
+mw_stpcpy (char * dst, const char * src);
+
+mw_public char*
+mw_strdup(const char *s);
+
+mw_public int
+mw_strnlen (const char * s, mw_size_t max);
+
+mw_public char *
+mw_strndup (char * src, mw_size_t size);
+
+mw_public size_t
+mw_strlcpy (char * dst, const char * src, mw_size_t size);
+
+mw_public size_t
+mw_strlcat (char * dst, const char * src, mw_size_t size);
+
+mw_public int
+mw_strcmp (const char * s1, const char * s2);
+
+mw_public int
+mw_strcasecmp (const char * s1, const char * s2);
+
+mw_public char *
+mw_strbuild (const char * s, ...);
+
+mw_public mw_uint_t
+mw_str_hash (const mw_pointer_t s);
+
+mw_public mw_bool_t
+mw_str_equal (const mw_pointer_t s1, const mw_pointer_t s2);
+
+mw_public mw_uint_t
+mw_mem_hash (const mw_pointer_t ptr, mw_size_t n);
+
+mw_public void *
+mw_memcpy (void * dst, const void * src, mw_size_t n);
+
+mw_public void *
+mw_memmove (void * dst, const void * src, mw_size_t n);
+
+mw_public mw_bool_t
+mw_is_alpha (mw_int_t c);
+
+mw_public mw_bool_t
+mw_is_upper (mw_int_t c);
+
+mw_public mw_bool_t
+mw_is_lower (mw_int_t c);
+
+mw_public mw_bool_t
+mw_is_digit (mw_int_t c);
+
+mw_public mw_bool_t
+mw_is_alnum (mw_int_t c);
+
+mw_public mw_int_t
+mw_tolower (mw_int_t c);
+
+mw_public mw_int_t
+mw_toupper (mw_int_t c);
+
+#endif