From: Sebastian Harl Date: Thu, 13 Dec 2012 12:35:34 +0000 (+0100) Subject: store: Changed various parameters to 'const char *'. X-Git-Tag: sysdb-0.1.0~452 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ad0100d18b3e1b754a92f36ef91f52382b0b6ead;p=sysdb.git store: Changed various parameters to 'const char *'. None of these parameters will be modified, so all to pass in constant strings. --- diff --git a/src/core/store.c b/src/core/store.c index 162a3fc..888fbb9 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -40,6 +40,17 @@ #include +/* + * private data types + */ + +/* type used for looking up named objects */ +typedef struct { + sc_object_t parent; + const char *obj_name; +} sc_store_lookup_obj_t; +#define SC_STORE_LOOKUP_OBJ_INIT { SC_OBJECT_INIT, NULL } + /* * private variables */ @@ -61,10 +72,20 @@ sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b) return strcasecmp(h1->name, h2->name); } /* sc_store_obj_cmp_by_name */ +static int +sc_cmp_store_obj_with_name(const sc_object_t *a, const sc_object_t *b) +{ + const sc_store_obj_t *obj = (const sc_store_obj_t *)a; + const sc_store_lookup_obj_t *lookup = (const sc_store_lookup_obj_t *)b; + + assert(obj && lookup); + return strcasecmp(obj->name, lookup->obj_name); +} /* sc_cmp_store_obj_with_name */ + static int sc_host_init(sc_object_t *obj, va_list ap) { - char *name = va_arg(ap, char *); + const char *name = va_arg(ap, const char *); SC_HOST(obj)->host_name = strdup(name); if (! SC_HOST(obj)->host_name) @@ -94,8 +115,8 @@ sc_host_destroy(sc_object_t *obj) static int sc_svc_init(sc_object_t *obj, va_list ap) { - char *hostname = va_arg(ap, char *); - char *name = va_arg(ap, char *); + const char *hostname = va_arg(ap, const char *); + const char *name = va_arg(ap, const char *); SC_SVC(obj)->hostname = strdup(hostname); SC_SVC(obj)->svc_name = strdup(name); @@ -123,7 +144,7 @@ sc_svc_destroy(sc_object_t *obj) */ sc_host_t * -sc_host_create(char *name) +sc_host_create(const char *name) { sc_object_t *obj; @@ -162,6 +183,7 @@ sc_host_clone(const sc_host_t *host) int sc_store_host(const sc_host_t *host) { + sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT; sc_time_t last_update; sc_host_t *old; @@ -183,8 +205,9 @@ sc_store_host(const sc_host_t *host) } } - old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)host, - sc_store_obj_cmp_by_name)); + lookup.obj_name = host->host_name; + old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); if (old) { if (old->host_last_update > last_update) { @@ -231,17 +254,17 @@ sc_store_host(const sc_host_t *host) } /* sc_store_host */ const sc_host_t * -sc_store_get_host(char *name) +sc_store_get_host(const char *name) { - sc_host_t tmp = SC_HOST_INIT; + sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT; sc_host_t *host; if (! name) return NULL; - tmp.host_name = name; - host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp, - sc_store_obj_cmp_by_name)); + lookup.obj_name = name; + host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); if (! host) return NULL; @@ -249,7 +272,7 @@ sc_store_get_host(char *name) } /* sc_store_get_host */ sc_service_t * -sc_service_create(char *hostname, char *name) +sc_service_create(const char *hostname, const char *name) { sc_object_t *obj; @@ -279,7 +302,7 @@ sc_service_clone(const sc_service_t *svc) int sc_store_service(const sc_service_t *svc) { - sc_host_t tmp = SC_HOST_INIT; + sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT; sc_host_t *host; sc_service_t *old; @@ -300,15 +323,16 @@ sc_store_service(const sc_service_t *svc) pthread_rwlock_wrlock(&host_lock); - tmp.host_name = svc->hostname; - host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp, - sc_store_obj_cmp_by_name)); + lookup.obj_name = svc->hostname; + host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); if (! host) return -1; - old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)svc, - sc_store_obj_cmp_by_name)); + lookup.obj_name = svc->svc_name; + old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); if (old) { if (old->host_last_update > last_update) { @@ -343,17 +367,17 @@ sc_store_service(const sc_service_t *svc) } /* sc_store_service */ const sc_service_t * -sc_store_get_service(const sc_host_t *host, char *name) +sc_store_get_service(const sc_host_t *host, const char *name) { - sc_service_t tmp = SC_SVC_INIT; + sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT; sc_service_t *svc; if ((! host) || (! name)) return NULL; - tmp.svc_name = name; - svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&tmp, - sc_store_obj_cmp_by_name)); + lookup.obj_name = name; + svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); if (! svc) return NULL; diff --git a/src/include/core/store.h b/src/include/core/store.h index 5d11c09..82507a9 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -69,7 +69,7 @@ typedef struct { #define SC_HOST(obj) ((sc_host_t *)(obj)) sc_host_t * -sc_host_create(char *name); +sc_host_create(const char *name); sc_host_t * sc_host_clone(const sc_host_t *host); @@ -92,10 +92,10 @@ int sc_store_host(const sc_host_t *host); const sc_host_t * -sc_store_get_host(char *name); +sc_store_get_host(const char *name); sc_service_t * -sc_service_create(char *hostname, char *name); +sc_service_create(const char *hostname, const char *name); sc_service_t * sc_service_clone(const sc_service_t *svc); @@ -120,7 +120,7 @@ int sc_store_service(const sc_service_t *svc); const sc_service_t * -sc_store_get_service(const sc_host_t *host, char *name); +sc_store_get_service(const sc_host_t *host, const char *name); int sc_store_dump(FILE *fh);