/*
 * frost_str.h
 *
 *  Created on: Jul 1, 2012
 *      Author: ergino
 */

#ifndef FROST_STR_H_
#define FROST_STR_H_

#include "frost_common.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "ice_streamer.h"

#ifdef __cplusplus
extern "C"
{
#endif

/*
 * Strings utility
 */
char       *frost_strdup_printf  (const char *format, ...);
char       *frost_strdup_vprintf (const char *format, va_list args);
char       *frost_strndup        (const char *str, frost_ulong n);
const char *frost_strerror       (frost_int errnum);
void         frost_strfreev       (char **str_array);
char       *frost_strconcat      (const char *first, ...);
char      **frost_strsplit       (const char *string, const char *delimiter, frost_int max_tokens);
char      **frost_strsplit_set   (const char *string, const char *delimiter, frost_int max_tokens);
char       *frost_strreverse     (char *str);
frost_bool     frost_str_has_prefix (const char *str, const char *prefix);
frost_bool     frost_str_has_suffix (const char *str, const char *suffix);
frost_uint        frost_strv_length    (char **str_array);
char       *frost_strjoin        (const char *separator, ...);
char       *frost_strjoinv       (const char *separator, char **str_array);
char       *frost_strchug        (char *str);
char       *frost_strchomp       (char *str);
void         frost_strdown        (char *string);
char       *frost_strnfill       (frost_ulong length, char fill_char);

char       *frost_strdelimit     (char *string, const char *delimiters, char new_delimiter);
char       *frost_strescape      (const char *source, const char *exceptions);

char       *frost_filename_to_uri   (const char *filename, const char *hostname, ice_error_code_t **error);
char       *frost_filename_from_uri (const char *uri, char **hostname, ice_error_code_t **error);

frost_int         frost_printf          (char const *format, ...);
frost_int         frost_fprintf         (FILE *file, char const *format, ...);
frost_int         frost_sprintf         (char *string, char const *format, ...);
frost_int         frost_snprintf        (char *string, frost_ulong n, char const *format, ...);
char * frost_strconcat (const char *first, ...);
#define frost_vprintf vprintf
#define frost_vfprintf vfprintf
#define frost_vsprintf vsprintf
#define frost_vsnprintf vsnprintf
#define frost_vasprintf vasprintf

frost_ulong   frost_strlcpy            (char *dest, const char *src, frost_ulong dest_size);
char  *frost_stpcpy             (char *dest, const char *src);


char   frost_ascii_tolower      (char c);
char   frost_ascii_toupper      (char c);
char  *frost_ascii_strdown      (const char *str, frost_long len);
char  *frost_ascii_strup        (const char *str, frost_long len);
frost_int    frost_ascii_strncasecmp  (const char *s1, const char *s2, frost_ulong n);
frost_int    frost_ascii_strcasecmp   (const char *s1, const char *s2);
frost_int    frost_ascii_xdigit_value (char c);
#define frost_ascii_isspace(c)   (isspace (c) != 0)
#define frost_ascii_isalpha(c)   (isalpha (c) != 0)
#define frost_ascii_isprint(c)   (isprint (c) != 0)
#define frost_ascii_isxdigit(c)  (isxdigit (c) != 0)

/* FIXME: frost_strcasecmp supports utf8 unicode stuff */
#ifdef _MSC_VER
#define frost_strcasecmp stricmp
#define frost_strncasecmp strnicmp
#define frost_strstrip(a) frost_strchug (frost_strchomp (a))
#else
#define frost_strcasecmp strcasecmp
#define frost_ascii_strtoull strtoull
#define frost_strncasecmp strncasecmp
#define frost_strstrip(a) frost_strchug (frost_strchomp (a))
#endif
#define frost_ascii_strdup strdup


typedef struct _FROSTPatternSpec    FROSTPatternSpec;

FROSTPatternSpec* frost_pattern_spec_new       (const char  *pattern);
void          frost_pattern_spec_free      (FROSTPatternSpec *pspec);
frost_bool      frost_pattern_spec_equal     (FROSTPatternSpec *pspec1,
                    FROSTPatternSpec *pspec2);
frost_bool      frost_pattern_match          (FROSTPatternSpec *pspec,
                    frost_uint         string_length,
                    const char  *string,
                    const char  *string_reversed);
frost_bool      frost_pattern_match_string   (FROSTPatternSpec *pspec,
                    const char  *string);
frost_bool      frost_pattern_match_simple   (const char  *pattern,
                    const char  *string);
frost_bool frost_utf8_validate (const char *str, frost_long max_len, const char **end);

#ifdef __cplusplus
}
#endif

#endif /* FROST_STR_H_ */
