/* * Copyright (C) 2007 Tildeslash Ltd. All rights reserved. * * 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 * the Free Software Foundation. * * There are special exceptions to the terms and conditions of the GPL * as it is applied to this software. View the full text of the exception * in the file EXCEPTIONS accompanying this software distribution. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef Util_H #define Util_H #include /** * General purpose utility class methods. * * @version \$Id: Util.h,v 1.12 2007/02/11 11:18:56 hauk Exp $ * @file */ /** * Return TRUE if the string a equals the string b. The * test is case-insensitive but depends on that all characters * in the two strings can be translated in the current locale. * @param a The string to test for equality with b * @param b The string to test for equality with a * @return TRUE if a equals b, otherwise FALSE */ int Util_isEqual(const char *a, const char *b); /** * Return TRUE if the string a equals the string b. The * test is case-sensitive and compares byte by byte * @param a The string to test for equality with b * @param b The string to test for equality with a * @return TRUE if a equals b, otherwise FALSE */ int Util_isByteEqual(const char *a, const char *b); /** * Return TRUE if the string a starts with the string * b. The test is case-insensitive but depends * on that all characters in the two strings can be translated * in the current locale. * @param a The string to search for b in * @param b The sub-string to test a against * @return TRUE if a starts with b, otherwise FALSE */ int Util_startsWith(const char *a, const char *b); /** * Strcpy that copy only n char from the given * string. The destination string, dest, is NUL * terminated at length n or if src is * shorter than n at the length of src * @param dest The destination buffer * @param src The string to copy to dest * @param n The number of bytes to copy * @return A pointer to dest */ char *Util_strncpy(char *dest, const char *src, int n); /** * Return a copy of a string * @param s A String to duplicate * @return A duplicate of string s */ char *Util_strdup(const char *s); /** * Strdup that duplicates only n char from the given string * @param s A string to duplicate * @param n The number of bytes to duplicate * @return A n bytes duplicate of string s */ char *Util_strndup(const char *s, int n); /** * Creates a new String by merging a formated string and a variable * argument list. The caller must free the returned String. * @param s A format string * @return The new String or NULL if the string could not be created * @exception MemoryException if memory allocation fails */ char *Util_getString(const char *s, ...); /** * Creates a new String by merging a formated string and a variable * argument list. The caller must free the returned String. * @param s A format string * @param ap A variable argument lists * @param len The length of the returned string * @return a new String concating s and va_list or NULL on error * @exception MemoryException if memory allocation fails */ char *Util_format(const char *s, va_list ap, int *len); /** * Parses the string argument as a signed integer in base 10. * @param s A string * @param e If an error occurred, the errno code is stored in e * @return The integer represented by the string argument. * @exception SQLException if a parse error occurred */ int Util_parseInt(const char *s, int *e); /** * Parses the string argument as a signed long long in base 10. * @param s A string * @param e If an error occurred, the errno code is stored in e * @return The long long represented by the string argument. * @exception SQLException if a parse error occurred */ long long int Util_parseLLong(const char *s, int *e); /** * Parses the string argument as a double. * @param s A string * @param e If an error occurred, the errno code is stored in e * @return The double represented by the string argument. * @exception SQLException if a parse error occurred */ double Util_parseDouble(const char *s, int *e); /** * Returns the time since the Epoch (00:00:00 UTC, January 1, 1970), * measured in seconds. * @return The current time since the Epoch in seconds */ long Util_seconds(); /** * This method suspend the calling process or Thread for * u micro seconds. * @param u Micro seconds to sleep * @return The number of micro seconds slept */ long Util_usleep(long u); /** * Allocate and return size bytes of memory. If * allocation failed this method aborts the program * @param size The number of bytes to allocate * @param file location of caller * @param line location of caller * @return a pointer to the allocated memory */ void *Util_alloc(long size, const char *file, int line); /** * Allocate and return memory for count objects, each of * size bytes. The returned memory is cleared. If allocation * failed this method aborts the program * @param count The number of objects to allocate * @param size The size of each object to allocate * @param file location of caller * @param line location of caller * @return a pointer to the allocated memory */ void *Util_calloc(long count, long size, const char *file, int line); /** * Deallocate the memory pointed to by p * @param p The memory to deallocate * @param file location of caller * @param line location of caller */ void Util_free(void *p, const char *file, int line); /** * Resize the allocation pointed to by p by size * bytes and return the changed allocation. If allocation failed this * method aborts the program * @param p A pointer to the allocation to change * @param size The new size of p * @param file location of caller * @param line location of caller * @return a pointer to the changed memory */ void *Util_resize(void *p, long size, const char *file, int line); /** * Print a formated message to stdout * @param e A formated (printf-style) message string */ void Util_debug(const char *e, ...); /** * Prints the given error message to stderr and * abort(3) the application. If an AbortHandler callback * function is defined for the library, this function is called instead. * @param e A formated (printf-style) message string */ void Util_abort(const char *e, ...); #endif