From patchwork Sun Jun 25 10:49:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marvin Scholz X-Patchwork-Id: 42294 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3805:b0:126:ac68:d900 with SMTP id p5csp486588pzf; Sun, 25 Jun 2023 03:49:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7fyJDG2Ge+5aUoGFrMm9SJOSA/O4ahG81j/xvGnQX105aB23QKC7lC9iNh140m1xlA64PN X-Received: by 2002:a17:907:3f87:b0:97d:9b73:690b with SMTP id hr7-20020a1709073f8700b0097d9b73690bmr26196975ejc.59.1687690167146; Sun, 25 Jun 2023 03:49:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687690167; cv=none; d=google.com; s=arc-20160816; b=o0xLbHumJdZzXWezJ10Q7PF8+R6LPH7J4NuJn3OHoYdxQkIQQibtlGwvN60uVfwyc3 M9t7QrQxfQH+J2jatfqUPCjlks0N9P2x+lvm/v3BCpkabr79Vvo8OkhfL+gIQJIqjpSX MHZSqhpUc+d1K/zpI8v+OyJaDkTe/+4rTy9wCSF7rolqvm/tLo73myaeezNwenr/JPZn J6DRaVHTttHMjf5whoXX612rgawWdQl51FoHXCcTNhRbbNsS/EtfHTRUyx8BjJrnIGD3 gxnsi+EouxOjdtAPmbtJD5tpzaExY7t85nVko9i5VkbKNVbwt0pkd6QgIoKc1byX2Zmj VrTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=ifoIp5p8aKZBn3TpmZ/8AcOxogIqk+BKs46S20gJ70A=; fh=mLkBQceLb8D9f+BOlqd6YzMp0Ug6ToY5UXClJOqpwTc=; b=vsl9bwlPm8T0MbLZiLSICPW4evt2xtfceI9cLDJu0OlBSSEWsRio45vanVXCgiIAm7 MK1mx7iJcQYpTxBkb+n944s47O3ECfXK4xPr9mrgQNCu2kFnoSg1SjHHXbmBFZnxhkl7 Ro62u1rqV8USEdHze92kEFVyHinsHWF4BD93gqGqFz1GxWJ/fcrxmg49QcVUZjmpnFcF qDlqJBgoQhGzqJzmFAYNZywbyfiVfP7YFPqvlY+Q28GLtWkH2vzaTS7HE+YhVozfqlqy MGhN+uHz5DL6e0epXXBMa4+uKtzJYDfRWC7FIarauLVIIYsSGpcS+j3nKa+QA1YutxbA NPFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=RXzpPNSL; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id pk15-20020a170906d7af00b009873cfd4239si1500195ejb.391.2023.06.25.03.49.26; Sun, 25 Jun 2023 03:49:27 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=RXzpPNSL; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D4ACB68C198; Sun, 25 Jun 2023 13:49:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D69BD68BF6E for ; Sun, 25 Jun 2023 13:49:16 +0300 (EEST) Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-9875c2d949eso340588566b.0 for ; Sun, 25 Jun 2023 03:49:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687690156; x=1690282156; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cXuPNS2214y+4ditSLmNCI8iJvUaJZRGhSYbH+QH6D0=; b=RXzpPNSLDLxoiW7WOFjXNSDK/PQD2GpocDsxoAZ+Th/dDuwsPmDPYaa+yPbvQkNF39 18v+M12Ab7ELPh4jgIUN6q3GjrsHe189hKcR8SYKCk0Fi+Dhi5NxwDZZ/bXn8nu24BO2 d3EUQnjAhNTdHD15TZUai/1DQOfOi/Zd1ZvpL/Bh6mpcd8rcSRKFlnoRzSlWR6oG9z33 4oT+w4G4+DOlpGM2NeQLtL3DTqnxkKlqOeMkjdeRRRq5LrD8XW+DkLOOr96gzBTpKw8U WqPIs3QJ6+hKJLFydRyFC5/afuYNkMKtKpIywdnwgyBiz7xmM9e8G/vzwTl/CpL9+u8R rX0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687690156; x=1690282156; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cXuPNS2214y+4ditSLmNCI8iJvUaJZRGhSYbH+QH6D0=; b=Is5HcMum2KuZafuCGgspNtKupVUSDeCOjzWTyIIsmwXIvOp95ekvTv+VTf6EZctQ3m 1Y+WmYbacB4yBht5VHXO5y7OJu5NOCK9kp6SBwIt8qbu6ZX8P7o0rcv9O8rPuHcsMBN+ KwHoaIpJWNaV1NNXlACeL0f3pucDcc1cE2wLnf6vSF1HJpG/h3Pl3+3Xd5scfPL1FY9T /aGRS9qyCnLgglM9q0AQ8cYd3utHIDbGMaiuyl/47l50NwEvXf4EMTvxgLvlj4Tj/3ML 7KZeeQ3YM2vP1ORCE2KtOAjTJvPWijW4i8Y4K/7s/IwlCK4A3d4IX80FjLtOuL5vxZJ2 FyLQ== X-Gm-Message-State: AC+VfDz3+5R1G8Yoq/M/jydLSsC1/bPMMwi7g2VGg1sTSjsqTjcmRQ/g TXoB2NiSKF5X5A7BAOCt4DhecEqtlLUYjw== X-Received: by 2002:a17:907:2d88:b0:98d:4e03:1d55 with SMTP id gt8-20020a1709072d8800b0098d4e031d55mr8309161ejc.76.1687690155752; Sun, 25 Jun 2023 03:49:15 -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 br26-20020a170906d15a00b0096f7cf96525sm1952877ejb.146.2023.06.25.03.49.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 03:49:14 -0700 (PDT) From: Marvin Scholz To: ffmpeg-devel@ffmpeg.org Date: Sun, 25 Jun 2023 12:49:05 +0200 Message-Id: <20230625104907.53071-1-epirat07@gmail.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20230501114456.13898-1-epirat07@gmail.com> References: <20230501114456.13898-1-epirat07@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: rmOIX9UMXKHr This new API allows to remove an entry and obtain ownership of the key/value that was associated with the removed entry. --- Changes since v1: - Clarify documentation about av_free having to be used. - Fix fate test to not rely on specific error code value doc/APIchanges | 4 ++++ libavutil/dict.c | 27 +++++++++++++++++++++++++++ libavutil/dict.h | 26 ++++++++++++++++++++++++++ libavutil/tests/dict.c | 38 ++++++++++++++++++++++++++++++++++++++ libavutil/version.h | 4 ++-- tests/ref/fate/dict | 12 ++++++++++++ 6 files changed, 109 insertions(+), 2 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index f040211f7d..d55821f682 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-06-02 - xxxxxxxxxx - lavu 58.14.100 - dict.h + Add av_dict_pop() to remove an entry from a dict + and get ownership of the removed key/value. + 2023-05-29 - xxxxxxxxxx - lavc 60.16.100 - avcodec.h codec_id.h Add AV_CODEC_ID_EVC, FF_PROFILE_EVC_BASELINE, and FF_PROFILE_EVC_MAIN. 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..31d38dabec 100644 --- a/libavutil/dict.h +++ b/libavutil/dict.h @@ -172,6 +172,32 @@ 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 @p key and remove it, if found. Optionally + * the found key and/or value can be returned using the @p out_key and + * @p 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 using av_dict_free() by + * the caller. May be NULL. + * @param out_value Pointer whose pointee will be set to the matched + * entry value. Must be freed using av_dict_free() by + * the caller. May be NULL. + * + * @retval 0 Success + * @retval AVERROR(ENOENT) No item for the given key found + * @retval "Other (negative) AVERROR" Other failure + */ +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..06d94ecc9a 100644 --- a/libavutil/tests/dict.c +++ b/libavutil/tests/dict.c @@ -158,5 +158,43 @@ 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 ", + (key) ? key : "(null)", + (val) ? val : "(null)"); + if (ret == AVERROR(ENOENT)) + printf("(Return code: ENOENT)\n"); + else + printf("(Return code: Unexpected error: %i)\n", 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 17a6d296a6..24af520e08 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,8 +79,8 @@ */ #define LIBAVUTIL_VERSION_MAJOR 58 -#define LIBAVUTIL_VERSION_MINOR 13 -#define LIBAVUTIL_VERSION_MICRO 101 +#define LIBAVUTIL_VERSION_MINOR 14 +#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ LIBAVUTIL_VERSION_MINOR, \ diff --git a/tests/ref/fate/dict b/tests/ref/fate/dict index 7205e4c845..afa87aca5f 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: ENOENT) +(null) + +Testing av_dict_pop() with prefix key match +prefix-test-key: test-value (Return code: 0) +(null)