From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 000C04603E for ; Mon, 1 May 2023 11:45:38 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BB44F68C093; Mon, 1 May 2023 14:45:35 +0300 (EEST) Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7C65668BF55 for ; Mon, 1 May 2023 14:45:29 +0300 (EEST) Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-50bc22805d3so2241932a12.1 for ; Mon, 01 May 2023 04:45:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682941528; x=1685533528; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=I1Gk0PmBb9BURtvvcjbeUZnBXxk5iurC2ff+GLKJmOA=; b=M+jUazc9A39OwJqxIoYxsc+w7TkF/WRXJlp19r1vICBSinQa9x8M/LNGSyVMBF0CnQ wEU4b3wxWE4vLzzqvUgGBovO/HV8p+E/2Y2dVGXnyrkzej1NIzW/PvSExYHAc+PBMFf0 O2BlVcZ1hd4b86rS1ns2dGMdbCNA+4kIwKIqj2p3sIMDx1Y3yyJ6plnIEudqB0yT55wp uZvKR/T900e64Fpo7x/ZXPXDxBHM6DZ/X2ux8W/JgpkIv+vcNDRa7St71TvQXA7xAqIS nEKtMK/RbRsH9LhDzRxo0mDOb/8bdNl4w1ABZjNrhPk9tpwhVMRtUhWYfvzuA16DpmTr UtiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682941528; x=1685533528; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=I1Gk0PmBb9BURtvvcjbeUZnBXxk5iurC2ff+GLKJmOA=; b=HNtUgDBt30Nk/RemmSiXExcdjqQIEbdexC0srTXF6Nqdj5hWQb9EnlFlzztV3b7ZAm z09T1SkZMtSbwGrKLg5xrB54AGPdeMvoxYPqNHkP706SP7ZwopIHKg0bWTyuTo3ejSqw 1T+UsXD9aBgRfrR6tDWVNh7xfqF2zTdA4WV8+wqxsi7jm6go83OeYxgwQv/SKJjcXDl1 rGeTDh9BYozSnBpu+66mHJOpik1BaVgjQDe2aCzI96erG/pq6dNo2L8KacOGptIVYFdu VOcbMDgCsD8KVmkIkmV6VRP8on56jNQfizh7kvjkUHJrp0DSw95J2fn11RsJG0AxrceF u5KA== X-Gm-Message-State: AC+VfDwV05MXGitaV2hKWuPM44v10Fpnfl3cOatwqhBFxJJBOOPCk1kY OVxYwN/Jo9eemSTB8avocItgZ4E+XPp11g== X-Google-Smtp-Source: ACHHUZ7tCkXL3epAzIFk+Uxdpw1I0LwjcfJH8tUfAvEHheozggjiTGacLz95jaCDZztY23nPLG2umw== X-Received: by 2002:a50:ef0a:0:b0:50b:caae:ae6e with SMTP id m10-20020a50ef0a000000b0050bcaaeae6emr563335eds.17.1682941528458; Mon, 01 May 2023 04:45:28 -0700 (PDT) Received: from MBP-von-Marvin.citadel.scalie.me (84-112-104-25.cable.dynamic.surfer.at. [84.112.104.25]) by smtp.gmail.com with ESMTPSA id f15-20020a056402150f00b00506adf55ae2sm12024942edw.6.2023.05.01.04.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 May 2023 04:45:27 -0700 (PDT) From: Marvin Scholz To: ffmpeg-devel@ffmpeg.org Date: Mon, 1 May 2023 13:44:54 +0200 Message-Id: <20230501114456.13898-1-epirat07@gmail.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avutil/dict: add av_dict_pop X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marvin Scholz Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: This new API allows to remove an entry and obtain ownership of the key/value that was associated with the removed entry. --- doc/APIchanges | 4 ++++ libavutil/dict.c | 27 +++++++++++++++++++++++++++ libavutil/dict.h | 20 ++++++++++++++++++++ libavutil/tests/dict.c | 34 ++++++++++++++++++++++++++++++++++ libavutil/version.h | 2 +- tests/ref/fate/dict | 12 ++++++++++++ 6 files changed, 98 insertions(+), 1 deletion(-) diff --git a/doc/APIchanges b/doc/APIchanges index 0b609e3d3b..5b807873b7 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,10 @@ The last version increases of all libraries were on 2023-02-09 API changes, most recent first: +2023-04-29 - xxxxxxxxxx - lavu 58.7.100 - dict.c + Add av_dict_pop() to remove an entry from a dict + and get ownership of the removed key/value. + 2023-04-10 - xxxxxxxxxx - lavu 58.6.100 - frame.h av_frame_get_plane_buffer() now accepts const AVFrame*. diff --git a/libavutil/dict.c b/libavutil/dict.c index f673977a98..ac41771994 100644 --- a/libavutil/dict.c +++ b/libavutil/dict.c @@ -173,6 +173,33 @@ int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, return av_dict_set(pm, key, valuestr, flags); } +int av_dict_pop(AVDictionary **pm, const char *key, + char **out_key, char **out_value, int flags) +{ + AVDictionary *m = *pm; + AVDictionaryEntry *entry = NULL; + entry = (AVDictionaryEntry *)av_dict_get(m, key, NULL, flags); + if (!entry) + return AVERROR(ENOENT); + + if (out_key) + *out_key = entry->key; + else + av_free(entry->key); + + if (out_value) + *out_value = entry->value; + else + av_free(entry->value); + + *entry = m->elems[--m->count]; + if (m && !m->count) { + av_freep(&m->elems); + av_freep(pm); + } + return 0; +} + static int parse_key_value_pair(AVDictionary **pm, const char **buf, const char *key_val_sep, const char *pairs_sep, int flags) diff --git a/libavutil/dict.h b/libavutil/dict.h index 713c9e361a..b2ab55a026 100644 --- a/libavutil/dict.h +++ b/libavutil/dict.h @@ -172,6 +172,26 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags */ int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags); +/** + * Remove the entry with the given key from the dictionary. + * + * Search for an entry matching `key` and remove it, if found. Optionally + * the found key and/or value can be returned using the `out_key`/`out_value` + * arguments. + * + * If more than one entry matches, only one entry is removed and returned + * on each call. Which entry is returned first in that case is undefined. + * + * @param pm Pointer to a pointer to a dictionary struct. + * @param key Entry key to match. + * @param out_key Pointer whose pointee will be set to the matched + * entry key. Must be freed by the caller. May be NULL. + * @param out_value Pointer whose pointee will be set to the matched + * entry value. Must be freed by the caller. May be NULL. + */ +int av_dict_pop(AVDictionary **pm, const char *key, + char **out_key, char **out_value, int flags); + /** * Parse the key/value pairs list and add the parsed entries to a dictionary. * diff --git a/libavutil/tests/dict.c b/libavutil/tests/dict.c index bececefb31..0652794b97 100644 --- a/libavutil/tests/dict.c +++ b/libavutil/tests/dict.c @@ -158,5 +158,39 @@ int main(void) printf("%s\n", e->value); av_dict_free(&dict); + char *key, *val = NULL; + int ret; + printf("\nTesting av_dict_pop() with existing AVDictionaryEntry.key as key\n"); + av_dict_set(&dict, "test-key", "test-value", 0); + ret = av_dict_pop(&dict, "test-key", &key, &val, 0); + printf("%s: %s (Return code: %i)\n", + (key) ? key : "(null)", + (val) ? val : "(null)", ret); + e = av_dict_get(dict, "test-key", NULL, 0); + printf("%s\n", (e) ? e->value : "(null)"); + av_freep(&key); + av_freep(&val); + + printf("\nTesting av_dict_pop() with nonexistent key\n"); + ret = av_dict_pop(&dict, "test-key", &key, &val, 0); + printf("%s: %s (Return code: %i)\n", + (key) ? key : "(null)", + (val) ? val : "(null)", ret); + e = av_dict_get(dict, "test-key", NULL, 0); + printf("%s\n", (e) ? e->value : "(null)"); + av_freep(&key); + av_freep(&val); + + printf("\nTesting av_dict_pop() with prefix key match\n"); + av_dict_set(&dict, "prefix-test-key", "test-value", 0); + ret = av_dict_pop(&dict, "prefix-test", &key, &val, AV_DICT_IGNORE_SUFFIX); + printf("%s: %s (Return code: %i)\n", + (key) ? key : "(null)", + (val) ? val : "(null)", ret); + e = av_dict_get(dict, "prefix-test", NULL, AV_DICT_IGNORE_SUFFIX); + printf("%s\n", (e) ? e->value : "(null)"); + av_freep(&key); + av_freep(&val); + return 0; } diff --git a/libavutil/version.h b/libavutil/version.h index 40f92af055..b8d1ef06a8 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 58 -#define LIBAVUTIL_VERSION_MINOR 6 +#define LIBAVUTIL_VERSION_MINOR 7 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ diff --git a/tests/ref/fate/dict b/tests/ref/fate/dict index 7205e4c845..bdb097cb03 100644 --- a/tests/ref/fate/dict +++ b/tests/ref/fate/dict @@ -48,3 +48,15 @@ Testing av_dict_set_int() Testing av_dict_set() with existing AVDictionaryEntry.key as key new val OK new val OK + +Testing av_dict_pop() with existing AVDictionaryEntry.key as key +test-key: test-value (Return code: 0) +(null) + +Testing av_dict_pop() with nonexistent key +(null): (null) (Return code: -2) +(null) + +Testing av_dict_pop() with prefix key match +prefix-test-key: test-value (Return code: 0) +(null) -- 2.37.0 (Apple Git-136) _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".