/* Copyright (C) 2005 MySQL AB

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

#ifndef myx_util_functions_h
#define myx_util_functions_h

#include "myx_util.h"

#if !defined(_WIN32)
  #include <iconv.h>
  #include <sys/types.h>
  #include <string.h>
  #include <errno.h>
  #include <stdio.h>

  #include <stdio.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <sys/time.h>
#else
//  #include <winsock2.h>
  #include <windows.h>
//  #include <direct.h>
#endif

#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
#define g_filename_to_utf8 g_filename_to_utf8_utf8
#define g_filename_from_utf8 g_filename_from_utf8_utf8
#define g_filename_from_uri g_filename_from_uri_utf8
#define g_filename_to_uri g_filename_to_uri_utf8
//#define g_file_test g_file_test_utf8
//#define g_file_get_contents g_file_get_contents_utf8
//#define g_mkstemp g_mkstemp_utf8
//#define g_file_open_tmp g_file_open_tmp_utf8
#endif

#include "glib.h"

#ifndef __BORLANDC__
  // On Windows we use a DLL for PCRE.
  #define PCRE_STATIC
#endif
#include "pcre.h"

/* unused?
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
  typedef LARGE_INTEGER MYX_TIMER_VALUE;
  //defined in windows
# define MYX_INT_PTR UINT_PTR
#else
  typedef struct timeval MYX_TIMER_VALUE;
# define MYX_INT_PTR intptr_t
#endif
*/

#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)

#ifdef MYSQLUTILLIBRARY_EXPORTS
#define MYSQLUTILLIBRARY_PUBLIC_FUNC __declspec(dllexport)
#else
#define MYSQLUTILLIBRARY_PUBLIC_FUNC __declspec(dllimport)
#endif

#define _br "\r\n"
#define MYX_PATH_SEPARATOR '\\'
#define MYX_PATH_SEPARATOR_STR "\\"

#else
  #define MYSQLUTILLIBRARY_PUBLIC_FUNC
  #define _br "\n"
  #define MYX_PATH_SEPARATOR '/'
#define MYX_PATH_SEPARATOR_STR "/"
#endif

#define MYX_ORDPTR(value)  ((void*)(unsigned long)(value))

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
  #ifndef strcasecmp
    #define strcasecmp _stricmp
    #define strncasecmp strnicmp
  #endif
  #define bigint __int64
  #define ubigint unsigned __int64
#else
  #define bigint long long
  #define ubigint unsigned long long
#endif

// if my_global.h not included
#ifndef my_sprintf
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
  typedef unsigned int uint;   
  typedef unsigned char uint8;
  typedef unsigned char uchar;
  typedef signed char int8;                   
  typedef unsigned short uint16;                 
  typedef short int16;                  
  typedef unsigned long uint32;                 
  typedef long int32;   
  typedef unsigned long ulong;
  typedef __int64 longlong;
  typedef unsigned __int64 ulonglong;
#else
# define min(a,b) ((a)<(b)?(a):(b))
# define max(a,b) ((a)>(b)?(a):(b))

  typedef unsigned char uint8;
  typedef unsigned char uchar;
  typedef signed char int8;                   
  typedef unsigned short uint16;
  typedef short int16;                  
  typedef unsigned long uint32;                 
  typedef long int32;
  #ifndef __GLIBC__
    typedef unsigned long ulong;
  #endif
  typedef long long longlong;
  typedef unsigned long long ulonglong;
#endif
#endif

/*
 * Enums
 */

/*
 * Structs
 */

/*
 * Functions
 */
MYSQLUTILLIBRARY_PUBLIC_FUNC char* hex_decode(const char *hex_str, int *ret_str_len);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *hex_encode(const char *binary_string, int len);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *iconv_char_name(const char *mysql_character_set_name);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *unquote_identifier(char *identifier);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *quote_identifier(const char *identifier, const char quote_char);
MYSQLUTILLIBRARY_PUBLIC_FUNC int split_schema_table(const char *ident, char **schema, char **table);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *mask_out_string(char *str, const char open_trigger, const char close_trigger, const char mask);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *mask_out_string_re(char *str, const char *open_re, const char open_trigger, const char close_trigger, const char mask);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_g_replace(char *str, const char *search, const char *replace);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_g_subst(const char *str, const char *search, const char *replace);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_g_append(char *base_str, const char *addon);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_g_append_and_free(char *base_str, char *addon);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_g_insert(const char *base_str, const char *addon, unsigned int index);

MYSQLUTILLIBRARY_PUBLIC_FUNC char *strmov(register char *dst, register const char *src);
MYSQLUTILLIBRARY_PUBLIC_FUNC int strcmp2(const char *str1, const char *str2);
MYSQLUTILLIBRARY_PUBLIC_FUNC int strcmp3(const char *str1, const char *str2);
MYSQLUTILLIBRARY_PUBLIC_FUNC unsigned int strlen2(const char *str);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *strcpy2(char *dst, const char *src);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *utf8_str_trim(char *str);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_trim(char *str);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_left(char *dest, char *src, unsigned int len);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_right(char *dest, char *src, unsigned int len);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_align_left(const char *txt, unsigned int width, char linechar);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_align_right(const char *txt, unsigned int width, char linechar);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_align_center(const char *txt, unsigned int width, char linechar);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *auto_line_break(const char *txt, unsigned int width, char sep);
MYSQLUTILLIBRARY_PUBLIC_FUNC int sub_str_count(char *search_str, const char *src);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *name_of_str(char *dst, const char *src);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *value_of_str(char *dst, const char *src);
MYSQLUTILLIBRARY_PUBLIC_FUNC int set_value(char **string_list, unsigned int string_list_num, char *name, char *new_value);
MYSQLUTILLIBRARY_PUBLIC_FUNC int get_str_index(char **string_list, unsigned int string_list_num, const char *search);

MYSQLUTILLIBRARY_PUBLIC_FUNC int strlist_g_indexof(const char **list, const char *value);
MYSQLUTILLIBRARY_PUBLIC_FUNC void strlist_g_append(char ***list, char *value);
MYSQLUTILLIBRARY_PUBLIC_FUNC void strlist_g_append_or_replace(char ***list, char *value);

MYSQLUTILLIBRARY_PUBLIC_FUNC int g_utf8_casecollate(const char *str1, const char *str2);

MYSQLUTILLIBRARY_PUBLIC_FUNC char *str_toupper(char *str);
MYSQLUTILLIBRARY_PUBLIC_FUNC int str_is_numeric(const char *str);

#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
MYSQLUTILLIBRARY_PUBLIC_FUNC int get_value_from_registry(HKEY root_key, const char *sub_key, const char *key, const char *def, char *value);
MYSQLUTILLIBRARY_PUBLIC_FUNC int set_value_to_registry(HKEY root_key, const char *sub_key, const char *key, const char *value);
#else
FILE *myx_popen(char *const argv[], pid_t *pid_ret);
int myx_pclose(FILE *f, pid_t pid);
int myx_read_timeout(FILE *f, int timeout, char *result, size_t result_len);
#endif

MYSQLUTILLIBRARY_PUBLIC_FUNC void set_os_specific_password_functions(char*(*store_func)(const char *host,
                                                                           const char *username,
                                                                           const char *password),
                                                        char*(*retrieve_func)(const char *host,
                                                                              const char *username,
                                                                              const char *password_data));


MYSQLUTILLIBRARY_PUBLIC_FUNC char *get_local_os_name(void);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *get_local_hardware_info(void);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *get_video_driver_info(void);

MYSQLUTILLIBRARY_PUBLIC_FUNC bigint get_physical_memory_size(void);

MYSQLUTILLIBRARY_PUBLIC_FUNC bigint get_file_size(const char *filename); 

MYSQLUTILLIBRARY_PUBLIC_FUNC char *strcasestr_len(const char *haystack, int haystack_len, const char *needle);

MYSQLUTILLIBRARY_PUBLIC_FUNC char * get_value_from_text(const char *txt, int txt_length, const char *regexpr);
MYSQLUTILLIBRARY_PUBLIC_FUNC char * get_value_from_text_ex(const char *txt, int txt_length, const char *regexpr, unsigned int substring_nr);
MYSQLUTILLIBRARY_PUBLIC_FUNC char * get_value_from_text_ex_opt(const char *txt, 
                                                  int txt_length,
                                                  const char *regexpr,
                                                  unsigned int substring_nr,
                                                  int options_for_exec);

MYSQLUTILLIBRARY_PUBLIC_FUNC const char *strfindword(const char *str, const char *word);

MYSQLUTILLIBRARY_PUBLIC_FUNC char *subst_pcre(const char *pattern, const char *repl, 
                                 int flags, int max_matches,
                                 const char *string);

MYSQLUTILLIBRARY_PUBLIC_FUNC char *subst_pcre_matches(const char *src, int *matches, int matchcount, const char *repl);

/* unused
MYSQLUTILLIBRARY_PUBLIC_FUNC void timer_start(MYX_TIMER_VALUE *tval);
MYSQLUTILLIBRARY_PUBLIC_FUNC double timer_stop(MYX_TIMER_VALUE *tval);
*/
MYSQLUTILLIBRARY_PUBLIC_FUNC void *vec_insert_resize(void *vec, guint size, guint *vecsize, guint pos, void *data);
MYSQLUTILLIBRARY_PUBLIC_FUNC void *vec_remove(void *vec, guint size, guint *vecsize, guint pos);

MYSQLUTILLIBRARY_PUBLIC_FUNC char *escape_xml_entities(const char *str);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *escape_html_entities(const char *str);
MYSQLUTILLIBRARY_PUBLIC_FUNC char *unescape_html_entities(const char *str);

MYSQLUTILLIBRARY_PUBLIC_FUNC int myx_mkdir(const char *filename, int mode, int *error_no);
MYSQLUTILLIBRARY_PUBLIC_FUNC int myx_chdir(const char *path);

MYSQLUTILLIBRARY_PUBLIC_FUNC int check_file_exists(const char *filename);
MYSQLUTILLIBRARY_PUBLIC_FUNC int copy_file(const char* source, const char* target);

#ifdef __cplusplus
};
#endif

#endif
