![]() |
fuse_kafka
|
00001 00007 #include "string_list.h" 00008 #include "util.c" 00009 #define SERVER_LIST_DEFAULT_MAX_SIZE 10 00010 00016 int string_list_contains(string_list** servers, char* string) 00017 { 00018 int i; 00019 if((*servers) == NULL) 00020 return 0; 00021 for(i = 0; i < (*servers)->size; i++) 00022 { 00023 if(strcmp((*servers)->array[i], string) == 0) 00024 return 1; 00025 } 00026 return 0; 00027 } 00031 int string_list_new(string_list** servers) 00032 { 00033 if(((*servers) = (string_list*) fmalloc(sizeof(string_list))) == NULL) 00034 return 1; 00035 (*servers)->max_size = SERVER_LIST_DEFAULT_MAX_SIZE; 00036 (*servers)->size = 0; 00037 if(((*servers)->array = fcalloc((*servers)->max_size, sizeof(char*))) 00038 == NULL) 00039 return 2; 00040 return 0; 00041 } 00043 void string_list_free(string_list** servers) 00044 { 00045 int i; 00046 if((*servers) == NULL) 00047 return; 00048 if((*servers)->array != NULL) 00049 for(i = 0; i < (*servers)->size; i++) 00050 free((*servers)->array[i]); 00051 free((*servers)->array); 00052 (*servers)->array = NULL; 00053 free((*servers)); 00054 } 00059 int string_list_resize(string_list** servers) 00060 { 00061 int new_size = (*servers)->max_size + SERVER_LIST_DEFAULT_MAX_SIZE; 00062 char** new_array; 00063 if((new_array = (char**) frealloc((*servers)->array, 00064 new_size * sizeof(char*))) == NULL) 00065 return 1; 00066 (*servers)->max_size = new_size; 00067 (*servers)->array = new_array; 00068 return 0; 00069 } 00074 int string_list_add(string_list** servers, char* string) 00075 { 00076 char* str; 00077 if((*servers) == NULL && string_list_new(servers)) 00078 return 1; 00079 if((*servers)->size >= (*servers)->max_size && 00080 string_list_resize(servers)) 00081 return 2; 00082 if((str = (char*) fcalloc(strlen(string) + 1, sizeof(char))) == NULL) 00083 return 3; 00084 (*servers)->array[(*servers)->size] = str; 00085 strcpy((*servers)->array[(*servers)->size], string); 00086 (*servers)->size++; 00087 return 0; 00088 } 00096 int string_list_add_once(string_list** servers, char* string) 00097 { 00098 if(string_list_contains(servers, string)) 00099 return 0; 00100 if(string_list_add(servers, string)) 00101 return 2; 00102 return 1; 00103 }