Skip to content

Commit baf4731

Browse files
committed
Extract function "copy_plist_item"
Improve readability of the function "update_client_info". It is now easier to see that it copies four items with the same algorithm.
1 parent 63d1164 commit baf4731

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
@@ -628,30 +628,22 @@ static char* plist_dict_get_string_val(plist_t dict, const char* key)
628628
return str;
629629
}
630630

631-
static void update_client_info(struct mux_client *client, plist_t dict)
631+
static void copy_plist_item(const char* key, plist_type type, plist_t src, plist_t dest)
632632
{
633-
plist_t node = NULL;
634-
plist_t info = plist_new_dict();
635-
636-
node = plist_dict_get_item(dict, "BundleID");
637-
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
638-
plist_dict_set_item(info, "BundleID", plist_copy(node));
639-
}
640-
641-
node = plist_dict_get_item(dict, "ClientVersionString");
642-
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
643-
plist_dict_set_item(info, "ClientVersionString", plist_copy(node));
633+
plist_t item = plist_dict_get_item(src, key);
634+
if (item && (plist_get_node_type(item) == type)) {
635+
plist_dict_set_item(dest, key, plist_copy(item));
644636
}
637+
}
645638

646-
node = plist_dict_get_item(dict, "ProgName");
647-
if (node && (plist_get_node_type(node) == PLIST_STRING)) {
648-
plist_dict_set_item(info, "ProgName", plist_copy(node));
649-
}
639+
static void update_client_info(struct mux_client *client, plist_t dict)
640+
{
641+
plist_t info = plist_new_dict();
650642

651-
node = plist_dict_get_item(dict, "kLibUSBMuxVersion");
652-
if (node && (plist_get_node_type(node) == PLIST_UINT)) {
653-
plist_dict_set_item(info, "kLibUSBMuxVersion", plist_copy(node));
654-
}
643+
copy_plist_item("BundleID", PLIST_STRING, dict, info);
644+
copy_plist_item("ClientVersionString", PLIST_STRING, dict, info);
645+
copy_plist_item("ProgName", PLIST_STRING, dict, info);
646+
copy_plist_item("kLibUSBMuxVersion", PLIST_UINT, dict, info);
655647
plist_free(client->info);
656648
client->info = info;
657649
}

0 commit comments

Comments
 (0)