From: Uwe Hermann Date: Thu, 27 Jan 2011 23:11:00 +0000 (+0100) Subject: libsigrokdecode: Always load all decoders upon init. X-Git-Tag: libsigrokdecode-0.1.0~296 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=53bec0cfaef9ce8c646deee7d9ba6340137713a1;p=libsigrokdecode.git libsigrokdecode: Always load all decoders upon init. Let sigrokdecode_init() always load all decoders it can find in the decoders directory, i.e., the user doesn't need to manually load decoders. Instead he can just look up the list via sigrokdecode_list_decoders() after sigrokdecode_init() has run. This is not a problem, as sigrokdecode_init() is only run once per sigrok-cli or sigrok-gui invocation, and even with many decoders this should not take too long. The list of decoders within libsigrokdecode is no longer a string, but rather a list of 'struct sigrokdecode_decoder *' pointers. Add sigrokdecode_get_decoder_by_id() API function which returns the decoder with the specified ID (file name without ".py" suffix, for now), or NULL if it cannot be found. sigrokdecode_load_decoder() is now a private function and not exported via the lib, i.e. not available to users of libsigrokdecode. --- diff --git a/decode.c b/decode.c index b1acff4..97a13c4 100644 --- a/decode.c +++ b/decode.c @@ -47,6 +47,9 @@ GSList *list_pds = NULL; * Py_XDECREF()ing it (someone else will do it for you at some point). */ +static int sigrokdecode_load_decoder(const char *name, + struct sigrokdecode_decoder **dec); + /** * Initialize libsigrokdecode. * @@ -56,7 +59,9 @@ int sigrokdecode_init(void) { DIR *dir; struct dirent *dp; - char *tmp; + char *decodername; + struct sigrokdecode_decoder *dec; + int ret; /* Py_Initialize() returns void and usually cannot fail. */ Py_Initialize(); @@ -73,9 +78,18 @@ int sigrokdecode_init(void) while ((dp = readdir(dir)) != NULL) { if (!g_str_has_suffix(dp->d_name, ".py")) continue; - /* For now use the filename (without .py) as decoder name. */ - if ((tmp = g_strndup(dp->d_name, strlen(dp->d_name) - 3))) - list_pds = g_slist_append(list_pds, tmp); + + /* Decoder name == filename (without .py suffix). */ + decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); + + /* TODO: Error handling. */ + dec = malloc(sizeof(struct sigrokdecode_decoder)); + + /* Load the decoder. */ + ret = sigrokdecode_load_decoder(decodername, &dec); + + /* Append it to the list of supported/loaded decoders. */ + list_pds = g_slist_append(list_pds, dec); } closedir(dir); @@ -94,6 +108,26 @@ GSList *sigrokdecode_list_decoders(void) return list_pds; } +/** + * Get the decoder with the specified ID. + * + * @param id The ID string of the decoder to return. + * @return The decoder with the specified ID, or NULL if not found. + */ +struct sigrokdecode_decoder *sigrokdecode_get_decoder_by_id(const char *id) +{ + GSList *l; + struct sigrokdecode_decoder *dec; + + for (l = sigrokdecode_list_decoders(); l; l = l->next) { + dec = l->data; + if (!strcmp(dec->id, id)) + return dec; + } + + return NULL; +} + /** * Helper function to handle Python strings. * @@ -153,7 +187,7 @@ err_h_decref_func: * * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise. */ -int sigrokdecode_load_decoder(const char *name, +static int sigrokdecode_load_decoder(const char *name, struct sigrokdecode_decoder **dec) { struct sigrokdecode_decoder *d; diff --git a/sigrokdecode.h b/sigrokdecode.h index 4876b5b..ede6d66 100644 --- a/sigrokdecode.h +++ b/sigrokdecode.h @@ -68,7 +68,7 @@ struct sigrokdecode_decoder { int sigrokdecode_init(void); GSList *sigrokdecode_list_decoders(void); -int sigrokdecode_load_decoder(const char *name, struct sigrokdecode_decoder **dec); +struct sigrokdecode_decoder *sigrokdecode_get_decoder_by_id(const char *id); int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec, uint8_t *inbuf, uint64_t inbuflen, uint8_t **outbuf, uint64_t *outbuflen);