From patchwork Sat Sep 24 14:36:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marvin Scholz X-Patchwork-Id: 38210 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp1156549pzh; Sat, 24 Sep 2022 07:37:21 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6x7zUVTnsY+PUcJp8d6aIg4+2lYYNSmpqNJbOZX4BTYfz8Ks/H5QN4KDUdYmpTDda9I8X4 X-Received: by 2002:a17:907:7629:b0:776:a147:8524 with SMTP id jy9-20020a170907762900b00776a1478524mr11628923ejc.632.1664030241700; Sat, 24 Sep 2022 07:37:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664030241; cv=none; d=google.com; s=arc-20160816; b=DRoI4XfUxzFRnOItaRV6SMC8AmUeTYROQMGsYOzrs/F7LG6pymtLFPxXaTVJCLsMlc cotzXD0FfWlPZe6colc6uF8N3FIU+bvpzKRwrs/sn0aEn4U1qnhPWeGNx5Avr+e6wO71 NT0iPCUgFj3uUQEGisCdAwbYUX3QDmQ7/3solQL661B8RKMPHJcxd7ByEu0Mq79b/zzv jvUqIyVRgs1FPVGOSwwpQtTX07BjjISOohdK2fsyECcNScN/sKZvZZ3k8U08MoJojRmU iWMTqAgfvfNcFlOotuTgQtd2YfwCToU/f214wI58tom+CxNE1uMKoDTIhjlC9ZEKvAE2 LVyg== 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=sp4Xn9VpvgKC1+gT+I6n4iduEexynkaArupuP/+7mT8=; b=dYkPgD81ft9RW4ZYXDZZ9Cq4f75rwyvu9U01+DEnZNIout3UPZNVHU998p9UX3+IE/ EOlZRNF9tdNbPw4ZKsmoSQKIjglPrr96SJafh+h9TKS1P44HtjtFSBghSGlbgkY5dhrB kXUVHF0RXhoy7Tnx2LT2EGwWXrGDQpfjHqXShFWr7D+h+4cnd1t3iQbJM59EOSWb0R/C 1+Vu0LnYMsV+iVG6TN2yr/X9s9vbuEtcqF1s7r8rTFxd9U4gaX4RoVtA82Nw2oijIXKR 2+kwghmkLUlyqYO4YLBR/ElBCDizq5yd68Yd/5x8MpDwmvUneQ5vQ4cruyK+m1KkG4Yo uZOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=VcHUKaTS; 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 sa27-20020a1709076d1b00b007825337afeesi7486988ejc.273.2022.09.24.07.37.20; Sat, 24 Sep 2022 07:37:21 -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=20210112 header.b=VcHUKaTS; 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 128FB68B8FD; Sat, 24 Sep 2022 17:37:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4286268B411 for ; Sat, 24 Sep 2022 17:37:10 +0300 (EEST) Received: by mail-ed1-f43.google.com with SMTP id x21so3624358edd.11 for ; Sat, 24 Sep 2022 07:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=UkuEQkGu+bvxY35c3Y82bttZvxcY/TmDGlsNt3T/fbg=; b=VcHUKaTSyX2VJzyZlvlSGVq260b06G9ziqdjXA+mAaG3jQ45LiqcBwHKq+6My1oY+k YKTroa7qor1ma3HQsHr2ZZDJUaCsiYfTrKT8JUNRbtuUaMTDpTrfNCpTtfwgPKyuVmiC DkoNRx9RlEsQNMF2GHGdT0/78YstNr2jzgIsOSsCOtcCzZekt0+cpTkHHSfPcVo9vntl 3GLMC74Qm+Tv2OdjBR9BB0Gd7rJilGqZdvDED/QsflQQusBnkgqadGZowGFQRNfD7/tm Mq9x9rxlMy369X3bUnY2dSVhLbLQdWJY80nGPkmQOXczbYtcYUrjf4c5JcgtELlUHIOL 4ZvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=UkuEQkGu+bvxY35c3Y82bttZvxcY/TmDGlsNt3T/fbg=; b=he2Grm6byTEArZGeEDpHRihUGzxY7Unt6LCdJvDjn5CqBx9341TB8aiWRIlZsNB+D/ 7MFKN20hP9y/nTT3dOZEOFm2+v2gtm60DgXRt1JMQLHwSlQ9AcYjU+Vdj1P7FLZ9unlC DUG+6tbUFbJCGMBYZR/j+G8uPEUOcjxohqSr26WjFfeBF6y1kOBxG42F1XaxleFnzfMf hwfMk+K7XqehiiRsiXIzoFOE58vceq9Rn5oCp7L+JLeLgeuEqgx0dBEpBrmdgKfLMbI8 t7wJoAWSUyh9Fj3NQqUJMUS1SSyRx7JhWd974N8X7ay3f5lZ+hD4eTWsCNZs4/SsOK0b No8g== X-Gm-Message-State: ACrzQf3Yx9O5vvffHkMxlW23hRq6FE20LA3078QcxYUIlSJiXR9zLM36 5saT/2IxukKTt3k90WhnoN6GRonhj2Ff2g== X-Received: by 2002:aa7:cb0b:0:b0:456:e744:79e5 with SMTP id s11-20020aa7cb0b000000b00456e74479e5mr4953656edt.191.1664030229578; Sat, 24 Sep 2022 07:37:09 -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 u17-20020a50c2d1000000b0044e8d0682b2sm7970504edf.71.2022.09.24.07.37.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Sep 2022 07:37:09 -0700 (PDT) From: Marvin Scholz To: ffmpeg-devel@ffmpeg.org Date: Sat, 24 Sep 2022 16:36:55 +0200 Message-Id: <20220924143659.74756-1-epirat07@gmail.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20220922020216.46589-1-epirat07@gmail.com> References: <20220922020216.46589-1-epirat07@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/5] avutil/dict: Add av_dict_iterate 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: fmJxy0C1KP5k This is a more explicit iteration API rather than using the "magic" av_dict_get(d, "", t, AV_DICT_IGNORE_SUFFIX) which is not really trivial to grasp what it does when casually reading through code. --- libavutil/dict.c | 19 +++++++++++++++++++ libavutil/dict.h | 46 +++++++++++++++++++++++++++++++++++---------- libavutil/version.h | 4 ++-- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/libavutil/dict.c b/libavutil/dict.c index 14ad780a79..ee059d712c 100644 --- a/libavutil/dict.c +++ b/libavutil/dict.c @@ -20,6 +20,7 @@ #include +#include "avassert.h" #include "avstring.h" #include "dict.h" #include "dict_internal.h" @@ -38,6 +39,24 @@ int av_dict_count(const AVDictionary *m) return m ? m->count : 0; } +const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m, + const AVDictionaryEntry *prev) +{ + int i = 0; + + if (!m) + return NULL; + + if (prev) + i = prev - m->elems + 1; + + av_assert2(i >= 0); + if (i >= m->count) + return NULL; + + return &m->elems[i]; +} + AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags) { diff --git a/libavutil/dict.h b/libavutil/dict.h index 0d1afc6c64..344afb452b 100644 --- a/libavutil/dict.h +++ b/libavutil/dict.h @@ -32,6 +32,8 @@ #include +#include "attributes.h" + /** * @addtogroup lavu_dict AVDictionary * @ingroup lavu_data @@ -43,9 +45,9 @@ * an AVDictionary, simply pass an address of a NULL pointer to * av_dict_set(). NULL can be used as an empty dictionary wherever * a pointer to an AVDictionary is required. - * Use av_dict_get() to retrieve an entry or iterate over all - * entries and finally av_dict_free() to free the dictionary - * and all its contents. + * Use av_dict_get() to retrieve an entry and av_dict_iterate() to + * iterate over all entries and finally av_dict_free() to free the + * dictionary and all its contents. * @code AVDictionary *d = NULL; // "create" an empty dictionary @@ -57,8 +59,8 @@ char *v = av_strdup("value"); // you can avoid copying them like this av_dict_set(&d, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); - while (t = av_dict_get(d, "", t, AV_DICT_IGNORE_SUFFIX)) { - <....> // iterate over all entries in d + while (t = av_dict_iterate(d, t)) { + <....> // iterate over all entries in d } av_dict_free(&d); @endcode @@ -89,9 +91,6 @@ typedef struct AVDictionary AVDictionary; * The returned entry key or value must not be changed, or it will * cause undefined behavior. * - * To iterate through all the dictionary entries, you can set the matching key - * to the null string "" and set the AV_DICT_IGNORE_SUFFIX flag. - * * @param prev Set to the previous matching element to find the next. * If set to NULL the first matching element is returned. * @param key matching key @@ -101,6 +100,33 @@ typedef struct AVDictionary AVDictionary; AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags); +/** + * Iterate over a dictionary + * + * Iterates through all entries in the dictionary. + * + * @warning The returned AVDictionaryEntry key/value must not be changed. + * + * @warning As av_dict_set() invalidates all previous entries returned + * by this function, it must not be called while iterating over the dict. + * + * Typical usage: + * @code + * const AVDictionaryEntry *e = NULL; + * while ((e = av_dict_iterate(m, e))) { + * // ... + * } + * @endcode + * + * @param m The dictionary to iterate over + * @param prev Pointer to the previous AVDictionaryEntry, NULL initially + * + * @retval AVDictionaryEntry* The next element in the dictionary + * @retval NULL No more elements in the dictionary + */ +const AVDictionaryEntry *av_dict_iterate(const AVDictionary *m, + const AVDictionaryEntry *prev); + /** * Get number of entries in dictionary. * @@ -115,8 +141,8 @@ int av_dict_count(const AVDictionary *m); * Note: If AV_DICT_DONT_STRDUP_KEY or AV_DICT_DONT_STRDUP_VAL is set, * these arguments will be freed on error. * - * Warning: Adding a new entry to a dictionary invalidates all existing entries - * previously returned with av_dict_get. + * @warning Adding a new entry to a dictionary invalidates all existing entries + * previously returned with av_dict_get() or av_dict_iterate(). * * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL * a dictionary struct is allocated and put in *pm. diff --git a/libavutil/version.h b/libavutil/version.h index 0585fa7b80..9c44cef6aa 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,8 +79,8 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 36 -#define LIBAVUTIL_VERSION_MICRO 102 +#define LIBAVUTIL_VERSION_MINOR 37 +#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ LIBAVUTIL_VERSION_MINOR, \