/*
** SellaNMS
** $Id: util.h,v 1.2 2005/11/13 23:34:56 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_COMMON_UTIL_H
#define __DGS_COMMON_UTIL_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include <unistd.h>
#include <sys/time.h>
#include <stl/slist>
#include <stl/string>
#include <stl/vector>

#define MAX_REGREPLACE_SUBS	20

extern "C" {
	char* trim(char*);
	char* rtrim(char*);
	char* ltrim(char*);
	unsigned short in_cksum(unsigned short* packet, unsigned long size);
	void SubStringReplace(string &str, char* orgsubstr, char* newsubstr);
	int SubStringSearch(const char *str, const char* substr);
	void tokenize_string(slist<string>& tokens, const string& str, const string& delimiters);

	void add_timeval(timeval &dst, timeval src);
	void subtract_timeval(timeval &dst, timeval src);
	int tvcmp(timeval dst, timeval src);
	long tvtousec(timeval tv);
	void hexdump(char *p, size_t length);
	RESULT flag_init(int pipes[2]);
	RESULT flag_set(int pipe[2]);
	RESULT flag_wait(int pipe[2], bool *interrupt = NULL, int timeout_secs = 0);

	/* Non-destructive, returns malloc()'d memory or NULL on failure. */
	char *ereg_replace(const char *pattern, const char *replace, const char *str);
	char *eregi_replace(const char *pattern, const char *replace, const char *str);
	char* _reg_replace(const char *pattern, const char *replace, const char *str, int icase, int extended);
	char* _reg_replace_delme(const char *pattern, const char *replace, const char *str, int icase, int extended);

	/* Returns new[]'d memory. */
	char *dbtstamp_from_unixtstamp(time_t t);
}

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/
