:right-sidebar: True PathBuf =================================================================== .. currentmodule:: gi.repository.GLib .. versionadded:: 2.76 .. class:: PathBuf(*args, **kwargs) :no-contents-entry: ``GPathBuf`` is a helper type that allows you to easily build paths from individual elements, using the platform specific conventions for path separators. .. code-block:: c :dedent: g_auto (GPathBuf) path; g_path_buf_init (&path); g_path_buf_push (&path, "usr"); g_path_buf_push (&path, "bin"); g_path_buf_push (&path, "echo"); g_autofree char *echo = g_path_buf_to_path (&path); g_assert_cmpstr (echo, ==, "/usr/bin/echo"); You can also load a full path and then operate on its components: .. code-block:: c :dedent: g_auto (GPathBuf) path; g_path_buf_init_from_path (&path, "/usr/bin/echo"); g_path_buf_pop (&path); g_path_buf_push (&path, "sh"); g_autofree char *sh = g_path_buf_to_path (&path); g_assert_cmpstr (sh, ==, "/usr/bin/sh"); Methods ------- .. rst-class:: interim-class .. class:: PathBuf :no-index: .. method:: clear() -> None Clears the contents of the path buffer. This function should be use to free the resources in a stack-allocated ``GPathBuf`` initialized using :func:`~gi.repository.GLib.PathBuf.init` or :func:`~gi.repository.GLib.PathBuf.init_from_path`. .. versionadded:: 2.76 .. method:: clear_to_path() -> str | None Clears the contents of the path buffer and returns the built path. This function returns ``NULL`` if the ``GPathBuf`` is empty. See also: :func:`~gi.repository.GLib.PathBuf.to_path` .. versionadded:: 2.76 .. classmethod:: equal(v2: ~typing.Any) -> bool Compares two path buffers for equality and returns ``TRUE`` if they are equal. The path inside the paths buffers are not going to be normalized, so ``X/Y/Z/A/..``\, ``X/./Y/Z`` and ``X/Y/Z`` are not going to be considered equal. This function can be passed to :func:`~gi.repository.GLib.HashTable.new` as the ``key_equal_func`` parameter. .. versionadded:: 2.76 :param v2: a path buffer to compare .. method:: free() -> None Frees a ``GPathBuf`` allocated by :func:`~gi.repository.GLib.PathBuf.new`. .. versionadded:: 2.76 .. method:: free_to_path() -> str | None Frees a ``GPathBuf`` allocated by :func:`~gi.repository.GLib.PathBuf.new`, and returns the path inside the buffer. This function returns ``NULL`` if the ``GPathBuf`` is empty. See also: :func:`~gi.repository.GLib.PathBuf.to_path` .. versionadded:: 2.76 .. method:: init() -> ~gi.repository.GLib.PathBuf Initializes a ``GPathBuf`` instance. .. versionadded:: 2.76 .. method:: init_from_path(path: str | None = None) -> ~gi.repository.GLib.PathBuf Initializes a ``GPathBuf`` instance with the given path. .. versionadded:: 2.76 :param path: a file system path .. method:: pop() -> bool Removes the last element of the path buffer. If there is only one element in the path buffer (for example, ``/`` on Unix-like operating systems or the drive on Windows systems), it will not be removed and :const:`False` will be returned instead. .. code-block:: C :dedent: GPathBuf buf, cmp; g_path_buf_init_from_path (&buf, "/bin/sh"); g_path_buf_pop (&buf); g_path_buf_init_from_path (&cmp, "/bin"); g_assert_true (g_path_buf_equal (&buf, &cmp)); g_path_buf_clear (&cmp); g_path_buf_pop (&buf); g_path_buf_init_from_path (&cmp, "/"); g_assert_true (g_path_buf_equal (&buf, &cmp)); g_path_buf_clear (&cmp); g_path_buf_clear (&buf); .. versionadded:: 2.76 .. method:: push(path: str) -> ~gi.repository.GLib.PathBuf Extends the given path buffer with ``path``\. If ``path`` is absolute, it replaces the current path. If ``path`` contains a directory separator, the buffer is extended by as many elements the path provides. On Windows, both forward slashes and backslashes are treated as directory separators. On other platforms, :const:`~gi.repository.GLib.DIR_SEPARATOR_S` is the only directory separator. .. code-block:: C :dedent: GPathBuf buf, cmp; g_path_buf_init_from_path (&buf, "/tmp"); g_path_buf_push (&buf, ".X11-unix/X0"); g_path_buf_init_from_path (&cmp, "/tmp/.X11-unix/X0"); g_assert_true (g_path_buf_equal (&buf, &cmp)); g_path_buf_clear (&cmp); g_path_buf_push (&buf, "/etc/locale.conf"); g_path_buf_init_from_path (&cmp, "/etc/locale.conf"); g_assert_true (g_path_buf_equal (&buf, &cmp)); g_path_buf_clear (&cmp); g_path_buf_clear (&buf); .. versionadded:: 2.76 :param path: a path .. method:: set_extension(extension: str | None = None) -> bool Adds an extension to the file name in the path buffer. If ``extension`` is ``NULL``\, the extension will be unset. If the path buffer does not have a file name set, this function returns ``FALSE`` and leaves the path buffer unmodified. .. versionadded:: 2.76 :param extension: the file extension .. method:: set_filename(file_name: str) -> bool Sets the file name of the path. If the path buffer is empty, the filename is left unset and this function returns ``FALSE``\. If the path buffer only contains the root element (on Unix-like operating systems) or the drive (on Windows), this is the equivalent of pushing the new ``file_name``\. If the path buffer contains a path, this is the equivalent of popping the path buffer and pushing ``file_name``\, creating a sibling of the original path. .. code-block:: C :dedent: GPathBuf buf, cmp; g_path_buf_init_from_path (&buf, "/"); g_path_buf_set_filename (&buf, "bar"); g_path_buf_init_from_path (&cmp, "/bar"); g_assert_true (g_path_buf_equal (&buf, &cmp)); g_path_buf_clear (&cmp); g_path_buf_set_filename (&buf, "baz.txt"); g_path_buf_init_from_path (&cmp, "/baz.txt"); g_assert_true (g_path_buf_equal (&buf, &cmp); g_path_buf_clear (&cmp); g_path_buf_clear (&buf); .. versionadded:: 2.76 :param file_name: the file name in the path .. method:: to_path() -> str | None Retrieves the built path from the path buffer. On Windows, the result contains backslashes as directory separators, even if forward slashes were used in input. If the path buffer is empty, this function returns ``NULL``\. .. versionadded:: 2.76 Fields ------ .. rst-class:: interim-class .. class:: PathBuf :no-index: .. attribute:: dummy