{
GArray* longOptions;
GString* optionString;
- GQueue* index;
+ GQueue* longIndex;
+ GHashTable* shortIndex;
};
static void nullLog(const gchar *log_domain, GLogLevelFlags log_level, const
gchar *message, gpointer user_data);
static void gfAddModuleOption(gpointer data, gpointer user_data);
+static guint ghfCharHash(gconstpointer key);
+static gboolean gefCharEqual(gconstpointer a, gconstpointer b);
static ModuleOption optionSyncStats= {
bool stats;
const char* testCaseName;
GString* analysisModulesNames;
+ unsigned int id;
// Initialize data structures
syncState= malloc(sizeof(SyncState));
else
{
syncState->stats= false;
- g_log_set_handler(NULL, G_LOG_LEVEL_DEBUG, nullLog, NULL);
+ id= g_log_set_handler(NULL, G_LOG_LEVEL_DEBUG, nullLog, NULL);
}
if (optionSyncGraphs.present)
endUsage.ru_stime.tv_usec);
}
+ if (!optionSyncStats.present)
+ {
+ g_log_remove_handler(NULL, id);
+ }
+
return EXIT_SUCCESS;
}
extern int optind, opterr, optopt;
GArray* longOptions;
GString* optionString;
- GQueue* index;
+ GQueue* longIndex;
+ int longOption;
+ GHashTable* shortIndex;
longOptions= g_array_sized_new(TRUE, FALSE, sizeof(struct option),
g_queue_get_length(&moduleOptions));
optionString= g_string_new("");
- index= g_queue_new();
+ longIndex= g_queue_new();
+ shortIndex= g_hash_table_new(&ghfCharHash, &gefCharEqual);
g_queue_foreach(&moduleOptions, &gfAddModuleOption, &(struct OptionsInfo)
- {longOptions, optionString, index});
+ {longOptions, optionString, longIndex, shortIndex});
do
{
int optionIndex= 0;
+ ModuleOption* moduleOption;
+ longOption= -1;
c= getopt_long(argc, argv, optionString->str, (struct option*)
longOptions->data, &optionIndex);
- if (c >= 0 && c < g_queue_get_length(index))
+ if (longOption >= 0 && longOption < g_queue_get_length(longIndex))
+ {
+ moduleOption= g_queue_peek_nth(longIndex, longOption);
+ }
+ else if ((moduleOption= g_hash_table_lookup(shortIndex, &c)) != NULL)
{
- ModuleOption* moduleOption= g_queue_peek_nth(index, c);
-
- moduleOption->present= true;
-
- if (moduleOption->hasArg == REQUIRED_ARG || moduleOption->hasArg
- == OPTIONAL_ARG)
- {
- moduleOption->arg= optarg;
- }
}
else if (c == -1)
{
{
g_error("Option parse error");
}
+
+ moduleOption->present= true;
+
+ if (moduleOption->hasArg == REQUIRED_ARG)
+ {
+ moduleOption->arg= optarg;
+ }
+ if (moduleOption->hasArg == OPTIONAL_ARG && optarg)
+ {
+ moduleOption->arg= optarg;
+ }
} while (c != -1);
g_array_free(longOptions, TRUE);
g_string_free(optionString, TRUE);
+ g_queue_free(longIndex);
+ g_hash_table_destroy(shortIndex);
if (argc <= optind)
{
newOption.name= option->longName;
newOption.has_arg= conversion[option->hasArg];
newOption.flag= NULL;
- newOption.val= g_queue_get_length(optionsInfo->index);
+ newOption.val= g_queue_get_length(optionsInfo->longIndex);
g_array_append_val(optionsInfo->longOptions, newOption);
- g_string_append(optionsInfo->optionString, colons[option->hasArg]);
- g_queue_push_tail(optionsInfo->index, option);
+ if (option->shortName)
+ {
+ g_string_append_c(optionsInfo->optionString, option->shortName);
+ g_string_append(optionsInfo->optionString, colons[option->hasArg]);
+
+ g_hash_table_insert(optionsInfo->shortIndex, &option->shortName,
+ option);
+ }
+ g_queue_push_tail(optionsInfo->longIndex, option);
+}
+
+
+/*
+ * A GHashFunc for g_hash_table_new()
+ *
+ * Args:
+ * key char*, just one character
+ */
+static guint ghfCharHash(gconstpointer key)
+{
+ return *(char*) key;
+}
+
+
+/*
+ * A GEqualFunc for g_hash_table_new()
+ *
+ * Args:
+ * a, b char*, just one character each
+ *
+ * Returns:
+ * TRUE if both values are equal
+ */
+static gboolean gefCharEqual(gconstpointer a, gconstpointer b)
+{
+ if (*(char*) a == *(char*) b)
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
}