Skip to content

Commit 8e8f5ed

Browse files
committed
Extract function "copy_plist_item"
Improve readability of the function "update_client_info". It is now easy to see that we copy four items with the same algorithm.
1 parent 9af2b12 commit 8e8f5ed

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

src/client.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -576,30 +576,22 @@ static char* plist_dict_get_string_val(plist_t dict, const char* key)
576576
return str;
577577
}
578578

579-
static void update_client_info(struct mux_client *client, plist_t dict)
579+
static void copy_plist_item(const char* key, plist_type type, plist_t src, plist_t dest)
580580
{
581-
plist_t node = NULL;
582-
plist_t info = plist_new_dict();
583-
584-
node = plist_dict_get_item(dict, "BundleID");
585-
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
586-
plist_dict_set_item(info, "BundleID", plist_copy(node));
587-
}
588-
589-
node = plist_dict_get_item(dict, "ClientVersionString");
590-
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
591-
plist_dict_set_item(info, "ClientVersionString", plist_copy(node));
581+
plist_t item = plist_dict_get_item(src, key);
582+
if (item && (plist_get_node_type(item) == type)) {
583+
plist_dict_set_item(dest, key, plist_copy(item));
592584
}
585+
}
593586

594-
node = plist_dict_get_item(dict, "ProgName");
595-
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
596-
plist_dict_set_item(info, "ProgName", plist_copy(node));
597-
}
587+
static void update_client_info(struct mux_client *client, plist_t dict)
588+
{
589+
plist_t info = plist_new_dict();
598590

599-
node = plist_dict_get_item(dict, "kLibUSBMuxVersion");
600-
if (node && (plist_get_node_type(node) == PLIST_UINT)) {
601-
plist_dict_set_item(info, "kLibUSBMuxVersion", plist_copy(node));
602-
}
591+
copy_plist_item("BundleID", PLIST_STRING, dict, info);
592+
copy_plist_item("ClientVersionString", PLIST_STRING, dict, info);
593+
copy_plist_item("ProgName", PLIST_STRING, dict, info);
594+
copy_plist_item("kLibUSBMuxVersion", PLIST_UINT, dict, info);
603595
plist_free(client->info);
604596
client->info = info;
605597
}

0 commit comments

Comments
 (0)