Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00014 #include "common.h"
00015
00016
00017
00018
00019
00020 gearman_conf_module_st *gearman_conf_module_create(gearman_conf_st *conf,
00021 gearman_conf_module_st *module,
00022 const char *name)
00023 {
00024 gearman_conf_module_st **module_list;
00025
00026 if (module == NULL)
00027 {
00028 module= (gearman_conf_module_st *)malloc(sizeof(gearman_conf_module_st));
00029 if (module == NULL)
00030 {
00031 gearman_conf_error_set(conf, "gearman_conf_module_create", "malloc");
00032 return NULL;
00033 }
00034
00035 module->options.allocated= true;
00036 }
00037 else
00038 {
00039 module->options.allocated= false;
00040 }
00041
00042 module->current_option= 0;
00043 module->current_value= 0;
00044 module->conf= conf;
00045 module->name= name;
00046
00047 module_list= (gearman_conf_module_st **)realloc(conf->module_list, sizeof(gearman_conf_module_st *) *
00048 (conf->module_count + 1));
00049 if (module_list == NULL)
00050 {
00051 gearman_conf_module_free(module);
00052 gearman_conf_error_set(conf, "gearman_conf_module_create", "realloc");
00053 return NULL;
00054 }
00055
00056 conf->module_list= module_list;
00057 conf->module_list[conf->module_count]= module;
00058 conf->module_count++;
00059
00060 return module;
00061 }
00062
00063 void gearman_conf_module_free(gearman_conf_module_st *module)
00064 {
00065 if (module->options.allocated)
00066 free(module);
00067 }
00068
00069 gearman_conf_module_st *gearman_conf_module_find(gearman_conf_st *conf,
00070 const char *name)
00071 {
00072 for (uint32_t x= 0; x < conf->module_count; x++)
00073 {
00074 if (name == NULL || conf->module_list[x]->name == NULL)
00075 {
00076 if (name == conf->module_list[x]->name)
00077 return conf->module_list[x];
00078 }
00079 else if (!strcmp(name, conf->module_list[x]->name))
00080 return conf->module_list[x];
00081 }
00082
00083 return NULL;
00084 }
00085
00086 void gearman_conf_module_add_option(gearman_conf_module_st *module,
00087 const char *name, int short_name,
00088 const char *value_name, const char *help)
00089 {
00090 gearman_conf_st *conf= module->conf;
00091 gearman_conf_option_st *option_list;
00092 struct option *option_getopt;
00093
00094
00095 for (uint32_t x= 0; x < conf->option_count && short_name != 0; x++)
00096 {
00097 if (conf->option_getopt[x].val == short_name)
00098 short_name= 0;
00099 }
00100
00101
00102 option_list= (gearman_conf_option_st *)realloc(conf->option_list, sizeof(gearman_conf_option_st) *
00103 (conf->option_count + 1));
00104 if (option_list == NULL)
00105 {
00106 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "realloc");
00107 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00108 return;
00109 }
00110
00111 conf->option_list= option_list;
00112 option_list= &conf->option_list[conf->option_count];
00113
00114 option_getopt= (struct option *)realloc(conf->option_getopt,
00115 sizeof(struct option) * (conf->option_count + 2));
00116 if (option_getopt == NULL)
00117 {
00118 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "realloc");
00119 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00120 return;
00121 }
00122
00123 conf->option_getopt= option_getopt;
00124 option_getopt= &conf->option_getopt[conf->option_count];
00125
00126 conf->option_count++;
00127 memset(&conf->option_getopt[conf->option_count], 0,
00128 sizeof(sizeof(struct option)));
00129
00130 option_list->module= module;
00131 option_list->name= name;
00132 option_list->value_name= value_name;
00133 option_list->help= help;
00134 option_list->value_list= NULL;
00135 option_list->value_count= 0;
00136
00137 if (module->name == NULL)
00138 {
00139
00140 option_getopt->name= strdup(name);
00141 if (option_getopt->name == NULL)
00142 {
00143 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "strdup");
00144 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00145 return;
00146 }
00147 }
00148 else
00149 {
00150
00151 size_t option_string_length= strlen(module->name) + strlen(name) + 2;
00152 char *option_string= (char *) malloc(option_string_length * sizeof(char));
00153
00154 if (option_string == NULL)
00155 {
00156 gearman_conf_error_set(conf, "gearman_conf_module_add_option", "malloc");
00157 conf->last_return= GEARMAN_MEMORY_ALLOCATION_FAILURE;
00158 return;
00159 }
00160
00161 snprintf(option_string, option_string_length, "%s-%s", module->name, name);
00162
00163 option_getopt->name= option_string;
00164 }
00165
00166 option_getopt->has_arg= value_name == NULL ? 0 : 1;
00167 option_getopt->flag= NULL;
00168 option_getopt->val= short_name;
00169
00170
00171 if (short_name != 0 &&
00172 conf->short_count < (GEARMAN_CONF_MAX_OPTION_SHORT - 2))
00173 {
00174 conf->option_short[conf->short_count++]= (char)short_name;
00175 if (value_name != NULL)
00176 conf->option_short[conf->short_count++]= ':';
00177 conf->option_short[conf->short_count]= '0';
00178 }
00179 }
00180
00181 bool gearman_conf_module_value(gearman_conf_module_st *module,
00182 const char **name, const char **value)
00183 {
00184 gearman_conf_option_st *option;
00185
00186 for (; module->current_option < module->conf->option_count;
00187 module->current_option++)
00188 {
00189 option= &module->conf->option_list[module->current_option];
00190 if (option->module != module)
00191 continue;
00192
00193 if (module->current_value < option->value_count)
00194 {
00195 *name= option->name;
00196 *value= option->value_list[module->current_value++];
00197 return true;
00198 }
00199
00200 module->current_value= 0;
00201 }
00202
00203 module->current_option= 0;
00204
00205 return false;
00206 }