/* * 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 */ #include "Config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "Util.h" /** * Implementation of the Util interface * * @version \$Id: Util.c,v 1.20 2007/02/13 15:11:38 hauk Exp $ * @file */ /* ----------------------------------------------------- Protected methods */ #ifdef PACKAGE_PROTECTED #pragma GCC visibility push(hidden) #endif int Util_isEqual(const char *a, const char *b) { if(!a || !b) return FALSE; while(*a && *b) if(toupper(*a++)!=toupper(*b++)) return FALSE; return (*a==*b); } int Util_isByteEqual(const char *a, const char *b) { if(!a || !b) return FALSE; while(*a && *b) if(*a++ != *b++) return FALSE; return (*a==*b); } int Util_startsWith(const char *a, const char *b) { const char *s= a; if(! (a && b)) return FALSE; while(*a && *b) { if(toupper(*a++)!=toupper(*b++)) return FALSE; } return ((*a==*b)||(a!=s)); } char *Util_strncpy(char *dest, const char *src, int n) { char *p= dest; if(!(src && dest)) { if(dest) *dest= 0; return dest; } for(; (*src && n--); src++, p++) *p= *src; *p= 0; return dest; } char *Util_strdup(const char *s) { return (s?Util_strndup(s, strlen(s)):NULL); } char *Util_strndup(const char *s, int n) { char *t= NULL; if(s) { t= ALLOC(n + 1); memcpy(t, s, n); t[n]= 0; } return t; } char *Util_getString(const char *s, ...) { char *v= NULL; if(s) { int l; va_list ap; va_start(ap, s); v= Util_format(s, ap, &l); va_end(ap); } return v; } char *Util_format(const char *s, va_list ap, int *len) { int n= 0; char *buf= NULL; if(s) { int size= STRLEN; buf= ALLOC(size); while(TRUE) { n= vsnprintf(buf, size, s, ap); if(n > -1 && n < size) break; if(n > -1) size= n+1; else size*= 2; RESIZE(buf, size); } } *len= n; return buf; } int Util_parseInt(const char *s, int *e) { int i; *e= 0; if(!(s&&*s)) return 0; errno= 0; i= (int)strtol(s, NULL, 10); if(errno) { *e= errno; THROW(SQLException); } return i; } long long int Util_parseLLong(const char *s, int *e) { long long l; *e= 0; if(!(s&&*s)) return 0; errno= 0; l= strtoll(s, NULL, 10); if(errno) { *e= errno; THROW(SQLException); } return l; } double Util_parseDouble(const char *s, int *e) { double d; *e= 0; if(!(s&&*s)) return 0.0; errno= 0; d= strtod(s, NULL); if(errno) { *e= errno; THROW(SQLException); } return d; } long Util_seconds() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec; } long Util_usleep(long u) { struct timeval tv; tv.tv_sec= u / USEC_PER_SEC; tv.tv_usec= u % USEC_PER_SEC; select(0, NULL, NULL, NULL, &tv); return u; } void *Util_alloc(long size, const char *file, int line){ void *p; assert(size > 0); p= malloc(size); if(p == NULL) ABORT("MemoryException -- %s at %s in line %d\n", STRERROR, file, line); return p; } void *Util_calloc(long count, long size, const char *file, int line) { void *p; assert(count > 0); assert(size > 0); p= calloc(count, size); if(p == NULL) ABORT("MemoryException -- %s at %s in line %d\n", STRERROR, file, line); return p; } void Util_free(void *p, const char *file, int line) { if(p) free(p); } void *Util_resize(void *p, long size, const char *file, int line) { assert(p); assert(size > 0); p= realloc(p, size); if(p == NULL) ABORT("MemoryException -- %s at %s in line %d\n", STRERROR, file, line); return p; } void Util_debug(const char *s, ...) { va_list ap; va_start(ap, s); vfprintf(stdout, s, ap); va_end(ap); } void Util_abort(const char *e, ...) { va_list ap; uchar_T buf[ERROR_SIZE + 1]; va_start(ap, e); vsnprintf(buf, ERROR_SIZE, e, ap); va_end(ap); if(AbortHandler == NULL) { fprintf(stderr, buf); abort(); } AbortHandler(buf); } #ifdef PACKAGE_PROTECTED #pragma GCC visibility pop #endif