[FFmpeg-devel] [PATCH v20 1/5] libavutil: Add wchartoutf8(), wchartoansi(), utf8toansi(), getenv_utf8(), freeenv_utf8() and getenv_dup()

Martin Storsjö martin at martin.st
Tue Jun 21 13:33:21 EEST 2022


On Mon, 20 Jun 2022, Martin Storsjö wrote:

> On Mon, 20 Jun 2022, Nil Admirari wrote:
>
>> wchartoutf8() converts strings returned by WinAPI into UTF-8,
>> which is FFmpeg's preffered encoding.
>> 
>> Some external dependencies, such as AviSynth, are still
>> not Unicode-enabled. utf8toansi() converts UTF-8 strings
>> into ANSI in two steps: UTF-8 -> wchar_t -> ANSI.
>> wchartoansi() is responsible for the second step of the conversion.
>> Conversion in just one step is not supported by WinAPI.
>> 
>> Since these character converting functions allocate the buffer
>> of necessary size, they also facilitate the removal of MAX_PATH limit
>> in places where fixed-size ANSI/WCHAR strings were used
>> as filename buffers.
>> 
>> On Windows, getenv_utf8() wraps _wgetenv() converting its input from
>> and its output to UTF-8. Strings returned by getenv_utf8()
>> must be freed by freeenv_utf8().
>> 
>> On all other platforms getenv_utf8() is a wrapper around getenv(),
>> and freeenv_utf8() is a no-op.
>> 
>> The value returned by plain getenv() cannot be modified;
>> av_strdup() is usually used when modifications are required.
>> However, on Windows, av_strdup() after getenv_utf8() leads to
>> unnecessary allocation. getenv_dup() is introduced to avoid
>> such an allocation. Value returned by getenv_dup() must be freed
>> by av_free().
>> 
>> Because of cleanup complexities, in places that only test the existence
>> of an environment variable or compare its value with a string
>> consisting entirely of ASCII characters, the use of plain getenv()
>> is still preferred. (libavutil/log.c check_color_terminal()
>> is an example of such a place.)
>> 
>> Plain getenv() is also preffered in UNIX-only code,
>> such as bktr.c, fbdev_common.c, oss.c in libavdevice
>> or af_ladspa.c in libavfilter.
>> ---
>> configure                  |  1 +
>> libavutil/getenv_utf8.h    | 86 ++++++++++++++++++++++++++++++++++++++
>> libavutil/wchar_filename.h | 53 +++++++++++++++++++++++
>> 3 files changed, 140 insertions(+)
>> create mode 100644 libavutil/getenv_utf8.h
>
> This set looks good to me, thanks! Will push tomorrow if there's nothing more 
> to add on it.

Pushed now, thanks!

// Martin


More information about the ffmpeg-devel mailing list