Class PathBuf
- All Implemented Interfaces:
Proxy
GPathBuf is a helper type that allows you to easily build paths from
individual elements, using the platform specific conventions for path
separators.
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:
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");
- Since:
- 2.76
-
Constructor Summary
ConstructorsConstructorDescriptionPathBuf()Allocate a new PathBuf.Allocate a new PathBuf.PathBuf(MemorySegment address) Create a PathBuf proxy instance for the provided memory address.PathBuf(MemorySegment[] dummy) Allocate a new PathBuf with the fields set to the provided values.PathBuf(MemorySegment[] dummy, Arena arena) Allocate a new PathBuf with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the contents of the path buffer.@Nullable StringClears the contents of the path buffer and returns the built path.copy()Copies the contents of a path buffer into a newGPathBuf.static booleanequal(MemorySegment v1, MemorySegment v2) Compares two path buffers for equality and returnsTRUEif they are equal.voidfree()Frees aGPathBufallocated by g_path_buf_new().@Nullable StringFrees aGPathBufallocated by g_path_buf_new(), and returns the path inside the buffer.static MemoryLayoutThe memory layout of the native struct.init()Initializes aGPathBufinstance.initFromPath(@Nullable String path) Initializes aGPathBufinstance with the given path.static PathBufnew_()Allocates a newGPathBuf.static PathBufnewFromPath(@Nullable String path) Allocates a newGPathBufwith the givenpath.booleanpop()Removes the last element of the path buffer.Extends the given path buffer withpath.@Nullable MemorySegment @Nullable []Read the value of the fielddummy.booleansetExtension(@Nullable String extension) Adds an extension to the file name in the path buffer.booleansetFilename(String fileName) Sets the file name of the path.@Nullable StringtoPath()Retrieves the built path from the path buffer.voidwriteDummy(@Nullable MemorySegment @Nullable [] dummy, Arena _arena) Write a value in the fielddummy.Methods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
PathBuf
Create a PathBuf proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
PathBuf
Allocate a new PathBuf.- Parameters:
arena- to control the memory allocation scope
-
PathBuf
public PathBuf()Allocate a new PathBuf. The memory is allocated withArena.ofAuto(). -
PathBuf
Allocate a new PathBuf with the fields set to the provided values.- Parameters:
dummy- value for the fielddummyarena- to control the memory allocation scope
-
PathBuf
Allocate a new PathBuf with the fields set to the provided values. The memory is allocated withArena.ofAuto().- Parameters:
dummy- value for the fielddummy
-
-
Method Details
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readDummy
Read the value of the fielddummy.- Returns:
- The value of the field
dummy
-
writeDummy
Write a value in the fielddummy.- Parameters:
dummy- The new value for the fielddummy
-
equal
Compares two path buffers for equality and returnsTRUEif they are equal.The paths inside the path buffers are not going to be normalized, so
X/Y/Z/A/..,X/./Y/ZandX/Y/Zare not going to be considered equal.This function can be passed to g_hash_table_new() as the
key_equal_funcparameter.- Parameters:
v1- a path buffer to comparev2- a path buffer to compare- Returns:
TRUEif the two path buffers are equal, andFALSEotherwise- Since:
- 2.76
-
new_
Allocates a newGPathBuf.- Returns:
- the newly allocated path buffer
- Since:
- 2.76
-
newFromPath
-
clear
public void clear()Clears the contents of the path buffer.This function should be use to free the resources in a stack-allocated
GPathBufinitialized using g_path_buf_init() or g_path_buf_init_from_path().- Since:
- 2.76
-
clearToPath
Clears the contents of the path buffer and returns the built path.This function returns
NULLif theGPathBufis empty.See also: g_path_buf_to_path()
- Returns:
- the built path
- Since:
- 2.76
-
copy
Copies the contents of a path buffer into a newGPathBuf.- Returns:
- the newly allocated path buffer
- Since:
- 2.76
-
free
public void free()Frees aGPathBufallocated by g_path_buf_new().- Since:
- 2.76
-
freeToPath
Frees aGPathBufallocated by g_path_buf_new(), and returns the path inside the buffer.This function returns
NULLif theGPathBufis empty.See also: g_path_buf_to_path()
- Returns:
- the path
- Since:
- 2.76
-
init
Initializes aGPathBufinstance.- Returns:
- the initialized path builder
- Since:
- 2.76
-
initFromPath
-
pop
public boolean pop()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 andfalsewill be returned instead.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);- Returns:
TRUEif the buffer was modified andFALSEotherwise- Since:
- 2.76
-
push
Extends the given path buffer withpath.If
pathis absolute, it replaces the current path.If
pathcontains 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,
G_DIR_SEPARATOR_Sis the only directory separator.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);- Parameters:
path- a path- Returns:
- the same pointer to
buf,for convenience - Since:
- 2.76
-
setExtension
Adds an extension to the file name in the path buffer.If
extensionisNULL, the extension will be unset.If the path buffer does not have a file name set, this function returns
FALSEand leaves the path buffer unmodified.- Parameters:
extension- the file extension- Returns:
TRUEif the extension was replaced, andFALSEotherwise- Since:
- 2.76
-
setFilename
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
fileName.If the path buffer contains a path, this is the equivalent of popping the path buffer and pushing
fileName,creating a sibling of the original path.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);- Parameters:
fileName- the file name in the path- Returns:
TRUEif the file name was replaced, andFALSEotherwise- Since:
- 2.76
-
toPath
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.- Returns:
- the path
- Since:
- 2.76
-