Added preliminary Symbian support.
[silc.git] / lib / silcutil / silcstrutil.c
index 8bec2fc7d54f99d0394f2a696471315bb7b3d912..5ba4c02e271d22d1a8d4731e8d67a475d22fc69a 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2002 - 2006 Pekka Riikonen
+  Copyright (C) 2002 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -170,6 +170,15 @@ unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
   return data;
 }
 
+#ifndef HAVE_SNPRINTF
+/* Outputs string according to the `format'. */
+
+int silc_snprintf(char *str, SilcUInt32 size, const char *format, ...)
+{
+
+}
+#endif /* HAVE_SNPRINTF */
+
 /* Concatenates the `src' into `dest'.  If `src_len' is more than the
    size of the `dest' (minus NULL at the end) the `src' will be
    truncated to fit. */
@@ -264,3 +273,52 @@ int silc_string_compare(char *string1, char *string2)
   silc_free(tmpstr2);
   return FALSE;
 }
+
+/* Splits a string containing separator `ch' and returns an array of the
+   splitted strings. */
+
+char **silc_string_split(const char *string, char ch, int *ret_count)
+{
+  char **splitted = NULL, sep[1], *item, *cp;
+  int i = 0, len;
+
+  if (!string)
+    return NULL;
+  if (!ret_count)
+    return NULL;
+
+  splitted = silc_calloc(1, sizeof(*splitted));
+  if (!splitted)
+    return NULL;
+
+  if (!strchr(string, ch)) {
+    splitted[0] = silc_memdup(string, strlen(string));
+    *ret_count = 1;
+    return splitted;
+  }
+
+  sep[0] = ch;
+  cp = (char *)string;
+  while(cp) {
+    len = strcspn(cp, sep);
+    item = silc_memdup(cp, len);
+    if (!item) {
+      silc_free(splitted);
+      return NULL;
+    }
+
+    cp += len;
+    if (strlen(cp) == 0)
+      cp = NULL;
+    else
+      cp++;
+
+    splitted = silc_realloc(splitted, (i + 1) * sizeof(*splitted));
+    if (!splitted)
+      return NULL;
+    splitted[i++] = item;
+  }
+  *ret_count = i;
+
+  return splitted;
+}