nfs-ganesha 1.4
|
00001 #ifdef HAVE_CONFIG_H 00002 #include "config.h" 00003 #endif 00004 00005 #include <unistd.h> 00006 #include <stdlib.h> 00007 #include <stdio.h> 00008 00009 #include <string.h> 00010 00011 #include "nodelist.h" 00012 00013 /* Forward declaration */ 00014 int _nodelist_common_string_get_token_common(char *string, char *separators_list, 00015 int *p_token_nb, int token_id, 00016 char **p_token); 00017 00018 /* 00019 ---------------------------------------------------------------------------------------------------------------- 00020 nodelist_common_string_get_tokens 00021 ------------------------------- 00022 */ 00023 int nodelist_common_string_get_token(char *string, char *separators_list, int token_id, 00024 char **p_token) 00025 { 00026 int fstatus = -1; 00027 int token_nb = 0; 00028 fstatus = 00029 _nodelist_common_string_get_token_common(string, separators_list, &token_nb, 00030 token_id, p_token); 00031 if(*p_token != NULL) 00032 fstatus = 0; 00033 else 00034 fstatus = -1; 00035 return fstatus; 00036 } 00037 00038 /* 00039 ---------------------------- 00040 nodelist_common_string_get_tokens 00041 ---------------------------------------------------------------------------------------------------------------- 00042 */ 00043 00044 /* 00045 ---------------------------------------------------------------------------------------------------------------- 00046 nodelist_common_string_get_tokens_quantity 00047 ------------------------------------- 00048 */ 00049 int nodelist_common_string_get_tokens_quantity(char *string, char *separators_list, 00050 int *p_token_nb) 00051 { 00052 int fstatus = -1; 00053 fstatus = 00054 _nodelist_common_string_get_token_common(string, separators_list, p_token_nb, 0, 00055 NULL); 00056 return fstatus; 00057 } 00058 00059 /* 00060 ------------------------------------- 00061 nodelist_common_string_get_tokens_quantity 00062 ---------------------------------------------------------------------------------------------------------------- 00063 */ 00064 00065 /* 00066 ---------------------------------------------------------------------------------------------------------------- 00067 appends_and_extends_string 00068 -------------------------- 00069 00070 Appends a char* giving a char* to append and an optionnal separator (NULL if no separator) 00071 00072 char** p_io_string : pointer on a char* that will be appended and maybe extended if no enough memory allocated 00073 size_t* p_current_length : pointer on a size_t structure that contains the current size of the char* that will be write 00074 size_t inc_length : the incrementation step that could be use to extend char* memory allocation 00075 char* string2append : string to append 00076 char* separator : separator to put between current char* and sting to append 00077 00078 return 0 if it succeeds 00079 -1 otherwise 00080 ---------------------------------------------------------------------------------------------------------------- 00081 */ 00082 int nodelist_common_string_appends_and_extends(char **p_io_string, 00083 size_t * p_current_length, 00084 size_t inc_length, char *string2append, 00085 char *separator) 00086 { 00087 int fstatus = -1; 00088 00089 size_t new_output_length; 00090 size_t new_string_length; 00091 00092 size_t output_string_length; 00093 size_t separator_length; 00094 size_t append_length; 00095 00096 char *default_separator = " "; 00097 char *local_separator; 00098 00099 if(*p_io_string != NULL && string2append != NULL) 00100 { 00101 00102 if(separator != NULL) 00103 local_separator = separator; 00104 else 00105 local_separator = default_separator; 00106 00107 if(strlen(*p_io_string) == 0) 00108 local_separator = ""; 00109 00110 output_string_length = strlen(*p_io_string); 00111 separator_length = strlen(local_separator); 00112 append_length = strlen(string2append); 00113 new_string_length = output_string_length + separator_length + append_length; 00114 if(new_string_length > *p_current_length) 00115 { 00116 new_output_length = *p_current_length; 00117 while(new_string_length > new_output_length) 00118 new_output_length += inc_length; 00119 *p_io_string = 00120 (char *)realloc(*p_io_string, (new_output_length + 1) * sizeof(char)); 00121 if(*p_io_string != NULL) 00122 *p_current_length = new_output_length; 00123 else 00124 *p_current_length = 0; 00125 } 00126 00127 if(*p_io_string != NULL) 00128 { 00129 00130 strncpy(*p_io_string + output_string_length, local_separator, separator_length); 00131 strncpy(*p_io_string + output_string_length + separator_length, string2append, 00132 append_length); 00133 *(*p_io_string + output_string_length + separator_length + append_length) = 00134 '\0'; 00135 fstatus = 0; 00136 } 00137 00138 } 00139 00140 return fstatus; 00141 } 00142 00143 /* 00144 -------------------------- 00145 appends_and_extends_string 00146 ---------------------------------------------------------------------------------------------------------------- 00147 */ 00148 00149 /* 00150 ---------------------------------------------------------------------------------------------------------------- 00151 nodelist_common_extended2condensed_nodelist 00152 ------------------------------------------- 00153 00154 Construit une liste de noeuds condensee a partir d'une liste de noeuds etendue 00155 00156 code retour : 00157 le nombre de noeud si ok 00158 -1 si erreur 00159 00160 Rq : il est necessaire de liberer la memoire associee a *p_dst_list via un appel a free une fois son 00161 utilisation terminee 00162 ---------------------------------------------------------------------------------------------------------------- 00163 */ 00164 int nodelist_common_extended2condensed_nodelist(char *src_list, char **p_dst_list) 00165 { 00166 00167 int fstatus = -1, status; 00168 00169 nodelist_nodelist_t nodelist; 00170 00171 status = nodelist_nodelist_init(&nodelist, &src_list, 1); 00172 if(status == 0) 00173 { 00174 if(nodelist_nodelist_get_compacted_string(&nodelist, p_dst_list) == 0) 00175 fstatus = nodelist_nodelist_nodes_quantity(&nodelist); 00176 else 00177 fstatus = -1; 00178 nodelist_nodelist_free_contents(&nodelist); 00179 } 00180 else 00181 { 00182 fstatus = -1; 00183 } 00184 00185 return fstatus; 00186 00187 } 00188 00189 /* 00190 ------------------------------------------- 00191 nodelist_common_extended2condensed_nodelist 00192 ---------------------------------------------------------------------------------------------------------------- 00193 */ 00194 00195 /* 00196 ---------------------------------------------------------------------------------------------------------------- 00197 nodelist_common_condensed2extended_nodelist 00198 ------------------------------------------- 00199 00200 Construit une liste de noeuds etendue a partir d'une liste de noeuds condensee 00201 00202 code retour : 00203 le nombre de noeud si ok 00204 -1 si erreur 00205 00206 Rq : il est necessaire de liberer la memoire associee a *p_dst_list via un appel a free une fois son 00207 utilisation terminee 00208 ---------------------------------------------------------------------------------------------------------------- 00209 */ 00210 int nodelist_common_condensed2extended_nodelist(char *src_list, char **p_dst_list) 00211 { 00212 00213 int fstatus, status; 00214 00215 nodelist_nodelist_t nodelist; 00216 00217 status = nodelist_nodelist_init(&nodelist, &src_list, 1); 00218 if(status == 0) 00219 { 00220 if(nodelist_nodelist_get_extended_string(&nodelist, p_dst_list) == 0) 00221 fstatus = nodelist_nodelist_nodes_quantity(&nodelist); 00222 else 00223 fstatus = -1; 00224 nodelist_nodelist_free_contents(&nodelist); 00225 } 00226 else 00227 { 00228 fstatus = -1; 00229 } 00230 00231 return fstatus; 00232 } 00233 00234 /* 00235 ------------------------------------------- 00236 nodelist_common_condensed2extended_nodelist 00237 ---------------------------------------------------------------------------------------------------------------- 00238 */ 00239 00240 /* 00241 ---------------------------------------------------------------------------------------------------------------- 00242 PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE 00243 ---------------------------------------------------------------------------------------------------------------- 00244 */ 00245 static char *get_next_token(char *workingstr, char separator) 00246 { 00247 char *current = workingstr; 00248 int in_bracket = 0; 00249 00250 while(*current) 00251 { 00252 if(!in_bracket && (*current == '[')) 00253 in_bracket = 1; 00254 else if(in_bracket && (*current == ']')) 00255 in_bracket = 0; 00256 else if(!in_bracket && (*current == separator)) 00257 return current; 00258 00259 current++; 00260 } 00261 return NULL; 00262 00263 } /* get_next_token */ 00264 00265 int _nodelist_common_string_get_token_common(char *string, char *separators_list, 00266 int *p_token_nb, int token_id, 00267 char **p_token) 00268 { 00269 int fstatus = -1; 00270 00271 int i; 00272 00273 size_t string_length; 00274 size_t separators_list_length; 00275 00276 char *working_string; 00277 00278 char *current_pointer; 00279 char *best_pointer; 00280 char *old_pointer; 00281 00282 size_t copy_length; 00283 00284 int local_token_nb; 00285 int end_of_loop; 00286 00287 /* 00288 First we check that pointers are not NULL 00289 */ 00290 if(string != NULL && separators_list != NULL) 00291 { 00292 string_length = strlen(string); 00293 separators_list_length = strlen(separators_list); 00294 /* 00295 Then, that their lengths are not null 00296 */ 00297 if(string_length != 0 && separators_list_length != 0) 00298 { 00299 /* 00300 Then, the separators research loop start 00301 */ 00302 working_string = string; 00303 old_pointer = working_string; 00304 local_token_nb = 1; 00305 end_of_loop = 0; 00306 while(!end_of_loop) 00307 { 00308 best_pointer = NULL; 00309 /* 00310 Search the first occurence of a separator 00311 */ 00312 for(i = 0; i < separators_list_length; i++) 00313 { 00314 current_pointer = 00315 get_next_token(working_string, *(separators_list + i)); 00316 if(best_pointer == NULL) 00317 { 00318 best_pointer = current_pointer; 00319 } 00320 else if(best_pointer > current_pointer && current_pointer != NULL) 00321 { 00322 best_pointer = current_pointer; 00323 } 00324 } 00325 /* 00326 If this token must be extracted, extract it 00327 */ 00328 if(token_id == local_token_nb && (*p_token) == NULL) 00329 { 00330 if(best_pointer == NULL) 00331 copy_length = strlen(old_pointer); 00332 else 00333 copy_length = (size_t) (best_pointer - old_pointer); 00334 *p_token = (char *)malloc((copy_length + 1) * sizeof(char)); 00335 if(*p_token != NULL) 00336 { 00337 (*p_token)[copy_length] = '\0'; 00338 strncpy(*p_token, old_pointer, copy_length); 00339 fstatus++; 00340 } 00341 else 00342 { 00343 fstatus = -2; 00344 } 00345 } 00346 /* 00347 If no more occurences, break the loop 00348 */ 00349 if(best_pointer == NULL) 00350 { 00351 end_of_loop = 1; 00352 } 00353 /* 00354 Otherwise, increment token counter and adjust working string 00355 */ 00356 else 00357 { 00358 local_token_nb++; 00359 working_string = best_pointer + 1; 00360 old_pointer = working_string; 00361 } 00362 } 00363 *p_token_nb = local_token_nb; 00364 fstatus++; 00365 } 00366 } 00367 00368 return fstatus; 00369 }