1 #include "pool.h" 2 #include "tool.h" 3 #include "debug.h" 4 5 #include <stdlib.h> 6 #include <stdio.h> 7 #include <string.h> 8 addPoolUrl(urlPool_t * pool,char * url)9urlPool_t *addPoolUrl(urlPool_t *pool, char *url) 10 { 11 urlPool_t *new; 12 13 if (pool != NULL) 14 { 15 /* Go to the end of the list */ 16 while (pool->next != NULL) 17 pool = pool->next; 18 } 19 20 /* Allocate a new address */ 21 new = malloc(sizeof(urlPool_t)); 22 if (new == NULL) 23 { 24 perror("malloc"); 25 return pool; 26 } 27 28 new->url = strdup(url); 29 30 new->settings = parseURL(url); 31 new->next = NULL; 32 33 if (new->settings == NULL) 34 { 35 _ERROR("Error parsing URL `%s'.\n", url); 36 (void)free(new->url); 37 (void)free(new); 38 return pool; 39 } 40 41 if (pool != NULL) 42 pool->next = new; 43 else 44 pool = new; 45 46 MESSAGE("%s was added to pool.\n", url); 47 48 return pool; 49 } 50 getPoolLen(urlPool_t * pool)51int getPoolLen(urlPool_t *pool) 52 { 53 int i = 0; 54 55 while (pool->next != NULL) 56 { 57 pool = pool->next; 58 i++; 59 } 60 61 return i; 62 } 63 _getPool_pos(urlPool_t * pool,int pos)64urlPool_t *_getPool_pos(urlPool_t *pool, int pos) 65 { 66 int i; 67 68 for (i = 0; ((i < pos) && (pool != NULL)); i++) 69 pool = pool->next; 70 71 return pool; 72 } 73 getSettings(urlPool_t * pool,int * position)74serverSettings_t *getSettings(urlPool_t *pool, int *position) 75 { 76 serverSettings_t *set; 77 urlPool_t *work; 78 79 work = _getPool_pos(pool, *position); 80 81 if ((*position+1) > (getPoolLen(pool))) 82 *position = 0; 83 else 84 *position = *position+1; 85 86 if (work == NULL) 87 set = NULL; 88 else 89 set = work->settings; 90 91 return set; 92 } 93