belle_sip_utils.c 10.1 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "belle_sip_utils.h"


static FILE *__log_file=0;

/**
 *@param file a FILE pointer where to output the belle logs.
 *
**/
void belle_sip_set_log_file(FILE *file)
{
        __log_file=file;
}

static void __belle_sip_logv_out(belle_sip_log_level lev, const char *fmt, va_list args);

22
belle_sip_log_function_t belle_sip_logv_out=__belle_sip_logv_out;
jehan's avatar
jehan committed
23 24 25 26 27

/**
 *@param func: your logging function, compatible with the OrtpLogFunc prototype.
 *
**/
28
void belle_sip_set_log_handler(belle_sip_log_function_t func){
jehan's avatar
jehan committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        belle_sip_logv_out=func;
}


unsigned int __belle_sip_log_mask=BELLE_SIP_WARNING|BELLE_SIP_ERROR|BELLE_SIP_FATAL;

/**
 * @ param levelmask a mask of BELLE_SIP_DEBUG, BELLE_SIP_MESSAGE, BELLE_SIP_WARNING, BELLE_SIP_ERROR
 * BELLE_SIP_FATAL .
**/
void belle_sip_set_log_level_mask(int levelmask){
        __belle_sip_log_mask=levelmask;
}

char * belle_sip_strdup_vprintf(const char *fmt, va_list ap)
{
        /* Guess we need no more than 100 bytes. */
        int n, size = 200;
        char *p,*np;
#ifndef WIN32
        va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/
#endif
        if ((p = (char *) malloc (size)) == NULL)
                return NULL;
        while (1)
        {
                /* Try to print in the allocated space. */
#ifndef WIN32
                va_copy(cap,ap);
                n = vsnprintf (p, size, fmt, cap);
                va_end(cap);
#else
                /*this works on 32 bits, luckily*/
                n = vsnprintf (p, size, fmt, ap);
#endif
                /* If that worked, return the string. */
                if (n > -1 && n < size)
                        return p;
                //printf("Reallocing space.\n");
                /* Else try again with more space. */
                if (n > -1)     /* glibc 2.1 */
                        size = n + 1;   /* precisely what is needed */
                else            /* glibc 2.0 */
                        size *= 2;      /* twice the old size */
                if ((np = (char *) realloc (p, size)) == NULL)
                  {
                    free(p);
                    return NULL;
                  }
                else
                  {
                    p = np;
                  }
        }
}

char *belle_sip_strdup_printf(const char *fmt,...){
        char *ret;
        va_list args;
        va_start (args, fmt);
        ret=belle_sip_strdup_vprintf(fmt, args);
        va_end (args);
        return ret;
}

#if     defined(WIN32) || defined(_WIN32_WCE)
#define ENDLINE "\r\n"
#else
#define ENDLINE "\n"
#endif

#if     defined(WIN32) || defined(_WIN32_WCE)
void belle_sip_logv(int level, const char *fmt, va_list args)
{
        if (belle_sip_logv_out!=NULL && belle_sip_log_level_enabled(level))
                belle_sip_logv_out(level,fmt,args);
#if !defined(_WIN32_WCE)
        if ((level)==BELLE_SIP_FATAL) abort();
#endif
}
#endif

static void __belle_sip_logv_out(belle_sip_log_level lev, const char *fmt, va_list args){
        const char *lname="undef";
        char *msg;
        if (__log_file==NULL) __log_file=stderr;
        switch(lev){
                case BELLE_SIP_DEBUG:
                        lname="debug";
                        break;
                case BELLE_SIP_MESSAGE:
                        lname="message";
                        break;
                case BELLE_SIP_WARNING:
                        lname="warning";
                        break;
                case BELLE_SIP_ERROR:
                        lname="error";
                        break;
                case BELLE_SIP_FATAL:
                        lname="fatal";
                        break;
                default:
                        belle_sip_fatal("Bad level !");
        }
        msg=belle_sip_strdup_vprintf(fmt,args);
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
        OutputDebugString(msg);
        OutputDebugString("\r\n");
#endif
        fprintf(__log_file,"belle-sip-%s-%s" ENDLINE,lname,msg);
        fflush(__log_file);
        free(msg);
}

144 145 146
belle_sip_list_t* belle_sip_list_new(void *data){
	belle_sip_list_t* new_elem=(belle_sip_list_t* )malloc(sizeof(belle_sip_list_t));
	memset(new_elem,0,sizeof(belle_sip_list_t));
jehan's avatar
jehan committed
147 148 149 150 151
	new_elem->prev=new_elem->next=NULL;
	new_elem->data=data;
	return new_elem;
}

152 153 154
belle_sip_list_t*  belle_sip_list_append(belle_sip_list_t* elem, void * data){
	belle_sip_list_t* new_elem=belle_sip_list_new(data);
	belle_sip_list_t* it=elem;
jehan's avatar
jehan committed
155 156 157 158 159 160 161
	if (elem==NULL) return new_elem;
	while (it->next!=NULL) it=belle_sip_list_next(it);
	it->next=new_elem;
	new_elem->prev=it;
	return elem;
}

162 163
belle_sip_list_t*  belle_sip_list_prepend(belle_sip_list_t* elem, void *data){
	belle_sip_list_t* new_elem=belle_sip_list_new(data);
jehan's avatar
jehan committed
164 165 166 167 168 169 170 171
	if (elem!=NULL) {
		new_elem->next=elem;
		elem->prev=new_elem;
	}
	return new_elem;
}


172 173
belle_sip_list_t*  belle_sip_list_concat(belle_sip_list_t* first, belle_sip_list_t* second){
	belle_sip_list_t* it=first;
jehan's avatar
jehan committed
174 175 176 177 178 179 180
	if (it==NULL) return second;
	while(it->next!=NULL) it=belle_sip_list_next(it);
	it->next=second;
	second->prev=it;
	return first;
}

181 182 183
belle_sip_list_t*  belle_sip_list_free(belle_sip_list_t* list){
	belle_sip_list_t* elem = list;
	belle_sip_list_t* tmp;
jehan's avatar
jehan committed
184 185 186 187 188 189 190 191 192 193
	if (list==NULL) return NULL;
	while(elem->next!=NULL) {
		tmp = elem;
		elem = elem->next;
		free(tmp);
	}
	free(elem);
	return NULL;
}

194 195
belle_sip_list_t*  belle_sip_list_remove(belle_sip_list_t* first, void *data){
	belle_sip_list_t* it;
jehan's avatar
jehan committed
196 197 198 199 200 201 202 203
	it=belle_sip_list_find(first,data);
	if (it) return belle_sip_list_remove_link(first,it);
	else {
		belle_sip_warning("belle_sip_list_remove: no element with %p data was in the list", data);
		return first;
	}
}

204
int belle_sip_list_size(const belle_sip_list_t* first){
jehan's avatar
jehan committed
205 206 207 208 209 210 211 212
	int n=0;
	while(first!=NULL){
		++n;
		first=first->next;
	}
	return n;
}

213
void belle_sip_list_for_each(const belle_sip_list_t* list, void (*func)(void *)){
jehan's avatar
jehan committed
214 215 216 217 218
	for(;list!=NULL;list=list->next){
		func(list->data);
	}
}

219
void belle_sip_list_for_each2(const belle_sip_list_t* list, void (*func)(void *, void *), void *user_data){
jehan's avatar
jehan committed
220 221 222 223 224
	for(;list!=NULL;list=list->next){
		func(list->data,user_data);
	}
}

225 226
belle_sip_list_t* belle_sip_list_remove_link(belle_sip_list_t* list, belle_sip_list_t* elem){
	belle_sip_list_t* ret;
jehan's avatar
jehan committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	if (elem==list){
		ret=elem->next;
		elem->prev=NULL;
		elem->next=NULL;
		if (ret!=NULL) ret->prev=NULL;
		free(elem);
		return ret;
	}
	elem->prev->next=elem->next;
	if (elem->next!=NULL) elem->next->prev=elem->prev;
	elem->next=NULL;
	elem->prev=NULL;
	free(elem);
	return list;
}

243
belle_sip_list_t* belle_sip_list_find(belle_sip_list_t* list, void *data){
jehan's avatar
jehan committed
244 245 246 247 248 249
	for(;list!=NULL;list=list->next){
		if (list->data==data) return list;
	}
	return NULL;
}

250
belle_sip_list_t* belle_sip_list_find_custom(belle_sip_list_t* list, belle_sip_compare_func compare_func, const void *user_data){
jehan's avatar
jehan committed
251 252 253 254 255 256
	for(;list!=NULL;list=list->next){
		if (compare_func(list->data,user_data)==0) return list;
	}
	return NULL;
}

257
void * belle_sip_list_nth_data(const belle_sip_list_t* list, int index){
jehan's avatar
jehan committed
258 259 260 261 262 263 264 265
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (i==index) return list->data;
	}
	belle_sip_error("belle_sip_list_nth_data: no such index in list.");
	return NULL;
}

266
int belle_sip_list_position(const belle_sip_list_t* list, belle_sip_list_t* elem){
jehan's avatar
jehan committed
267 268 269 270 271 272 273 274
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (elem==list) return i;
	}
	belle_sip_error("belle_sip_list_position: no such element in list.");
	return -1;
}

275
int belle_sip_list_index(const belle_sip_list_t* list, void *data){
jehan's avatar
jehan committed
276 277 278 279 280 281 282 283
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (data==list->data) return i;
	}
	belle_sip_error("belle_sip_list_index: no such element in list.");
	return -1;
}

284 285 286 287
belle_sip_list_t* belle_sip_list_insert_sorted(belle_sip_list_t* list, void *data, int (*compare_func)(const void *, const void*)){
	belle_sip_list_t* it,*previt=NULL;
	belle_sip_list_t* nelem;
	belle_sip_list_t* ret=list;
jehan's avatar
jehan committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	if (list==NULL) return belle_sip_list_append(list,data);
	else{
		nelem=belle_sip_list_new(data);
		for(it=list;it!=NULL;it=it->next){
			previt=it;
			if (compare_func(data,it->data)<=0){
				nelem->prev=it->prev;
				nelem->next=it;
				if (it->prev!=NULL)
					it->prev->next=nelem;
				else{
					ret=nelem;
				}
				it->prev=nelem;
				return ret;
			}
		}
		previt->next=nelem;
		nelem->prev=previt;
	}
	return ret;
}

311 312
belle_sip_list_t* belle_sip_list_insert(belle_sip_list_t* list, belle_sip_list_t* before, void *data){
	belle_sip_list_t* elem;
jehan's avatar
jehan committed
313 314 315 316 317 318
	if (list==NULL || before==NULL) return belle_sip_list_append(list,data);
	for(elem=list;elem!=NULL;elem=belle_sip_list_next(elem)){
		if (elem==before){
			if (elem->prev==NULL)
				return belle_sip_list_prepend(list,data);
			else{
319
				belle_sip_list_t* nelem=belle_sip_list_new(data);
jehan's avatar
jehan committed
320 321 322 323 324 325 326 327 328 329
				nelem->prev=elem->prev;
				nelem->next=elem;
				elem->prev->next=nelem;
				elem->prev=nelem;
			}
		}
	}
	return list;
}

330 331 332
belle_sip_list_t* belle_sip_list_copy(const belle_sip_list_t* list){
	belle_sip_list_t* copy=NULL;
	const belle_sip_list_t* iter;
jehan's avatar
jehan committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	for(iter=list;iter!=NULL;iter=belle_sip_list_next(iter)){
		copy=belle_sip_list_append(copy,iter->data);
	}
	return copy;
}

char * belle_sip_concat (const char *str, ...) {
  va_list ap;
  size_t allocated = 100;
  char *result = (char *) malloc (allocated);

  if (result != NULL)
    {
      char *newp;
      char *wp;
      const char* s;

      va_start (ap, str);

      wp = result;
      for (s = str; s != NULL; s = va_arg (ap, const char *)) {
          size_t len = strlen (s);

          /* Resize the allocated memory if necessary.  */
          if (wp + len + 1 > result + allocated)
            {
              allocated = (allocated + len) * 2;
              newp = (char *) realloc (result, allocated);
              if (newp == NULL)
                {
                  free (result);
                  return NULL;
                }
              wp = newp + (wp - result);
              result = newp;
            }
          memcpy (wp, s, len);
          wp +=len;
        }

      /* Terminate the result string.  */
      *wp++ = '\0';

      /* Resize memory to the optimal size.  */
      newp = realloc (result, wp - result);
      if (newp != NULL)
        result = newp;

      va_end (ap);
    }

  return result;
}