Skip to content

Commit

Permalink
Extract function "copy_plist_item"
Browse files Browse the repository at this point in the history
Improve readability of the function "update_client_info". It is now
easier to see that it copies four items with the same algorithm.
  • Loading branch information
stefanbirkner committed Jun 9, 2020
1 parent 28ddecc commit 2b55bbc
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,30 +615,22 @@ static char* plist_dict_get_string_val(plist_t dict, const char* key)
return str;
}

static void update_client_info(struct mux_client *client, plist_t dict)
static void copy_plist_item(const char* key, plist_type type, plist_t src, plist_t dest)
{
plist_t node = NULL;
plist_t info = plist_new_dict();

node = plist_dict_get_item(dict, "BundleID");
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
plist_dict_set_item(info, "BundleID", plist_copy(node));
}

node = plist_dict_get_item(dict, "ClientVersionString");
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
plist_dict_set_item(info, "ClientVersionString", plist_copy(node));
plist_t item = plist_dict_get_item(src, key);
if (item && (plist_get_node_type(item) == type)) {
plist_dict_set_item(dest, key, plist_copy(item));
}
}

node = plist_dict_get_item(dict, "ProgName");
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
plist_dict_set_item(info, "ProgName", plist_copy(node));
}
static void update_client_info(struct mux_client *client, plist_t dict)
{
plist_t info = plist_new_dict();

node = plist_dict_get_item(dict, "kLibUSBMuxVersion");
if (node && (plist_get_node_type(node) == PLIST_UINT)) {
plist_dict_set_item(info, "kLibUSBMuxVersion", plist_copy(node));
}
copy_plist_item("BundleID", PLIST_STRING, dict, info);
copy_plist_item("ClientVersionString", PLIST_STRING, dict, info);
copy_plist_item("ProgName", PLIST_STRING, dict, info);
copy_plist_item("kLibUSBMuxVersion", PLIST_UINT, dict, info);
plist_free(client->info);
client->info = info;
}
Expand Down

0 comments on commit 2b55bbc

Please sign in to comment.