1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.Util; 26 27 private import glib.Str; 28 private import glib.c.functions; 29 public import glib.c.types; 30 31 32 /** */ 33 public struct Util 34 { 35 /** 36 * Behaves exactly like g_build_filename(), but takes the path elements 37 * as a string array, instead of varargs. This function is mainly 38 * meant for language bindings. 39 * 40 * Params: 41 * args = strings containing the path elements. 42 * 43 * Return: a newly-allocated string that must be freed with g_free(). 44 * 45 * Since: 2.8 46 */ 47 public static string buildFilename(string[] firstElement ... ) 48 { 49 return Str.toString(g_build_filenamev(Str.toStringzArray(firstElement))); 50 } 51 52 /** 53 * Behaves exactly like g_build_path(), but takes the path elements 54 * as a string array, instead of varargs. This function is mainly 55 * meant for language bindings. 56 * 57 * Params: 58 * separator = a string used to separator the elements of the path. 59 * args = strings containing the path elements. 60 * 61 * Return: a newly-allocated string that must be freed with g_free(). 62 * 63 * Since: 2.8 64 */ 65 public static string buildPath(string separator, string[] firstElement ... ) 66 { 67 return Str.toString(g_build_pathv(Str.toStringz(separator), Str.toStringzArray(firstElement))); 68 } 69 70 /** 71 */ 72 73 /** 74 * Specifies a function to be called at normal program termination. 75 * 76 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor 77 * macro that maps to a call to the atexit() function in the C 78 * library. This means that in case the code that calls g_atexit(), 79 * i.e. atexit(), is in a DLL, the function will be called when the 80 * DLL is detached from the program. This typically makes more sense 81 * than that the function is called when the GLib DLL is detached, 82 * which happened earlier when g_atexit() was a function in the GLib 83 * DLL. 84 * 85 * The behaviour of atexit() in the context of dynamically loaded 86 * modules is not formally specified and varies wildly. 87 * 88 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically 89 * loaded module which is unloaded before the program terminates might 90 * well cause a crash at program exit. 91 * 92 * Some POSIX systems implement atexit() like Windows, and have each 93 * dynamically loaded module maintain an own atexit chain that is 94 * called when the module is unloaded. 95 * 96 * On other POSIX systems, before a dynamically loaded module is 97 * unloaded, the registered atexit functions (if any) residing in that 98 * module are called, regardless where the code that registered them 99 * resided. This is presumably the most robust approach. 100 * 101 * As can be seen from the above, for portability it's best to avoid 102 * calling g_atexit() (or atexit()) except in the main executable of a 103 * program. 104 * 105 * Deprecated: It is best to avoid g_atexit(). 106 * 107 * Params: 108 * func = the function to call on normal program termination. 109 */ 110 public static void atexit(GVoidFunc func) 111 { 112 g_atexit(func); 113 } 114 115 /** 116 * Gets the name of the file without any leading directory 117 * components. It returns a pointer into the given file name 118 * string. 119 * 120 * Deprecated: Use g_path_get_basename() instead, but notice 121 * that g_path_get_basename() allocates new memory for the 122 * returned string, unlike this function which returns a pointer 123 * into the argument. 124 * 125 * Params: 126 * fileName = the name of the file 127 * 128 * Returns: the name of the file without any leading 129 * directory components 130 */ 131 public static string basename(string fileName) 132 { 133 return Str.toString(g_basename(Str.toStringz(fileName))); 134 } 135 136 /** 137 * Find the position of the first bit set in @mask, searching 138 * from (but not including) @nth_bit upwards. Bits are numbered 139 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63, 140 * usually). To start searching from the 0th bit, set @nth_bit to -1. 141 * 142 * Params: 143 * mask = a #gulong containing flags 144 * nthBit = the index of the bit to start the search from 145 * 146 * Returns: the index of the first bit set which is higher than @nth_bit, or -1 147 * if no higher bits are set 148 */ 149 public static int bitNthLsf(gulong mask, int nthBit) 150 { 151 return g_bit_nth_lsf(mask, nthBit); 152 } 153 154 /** 155 * Find the position of the first bit set in @mask, searching 156 * from (but not including) @nth_bit downwards. Bits are numbered 157 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63, 158 * usually). To start searching from the last bit, set @nth_bit to 159 * -1 or GLIB_SIZEOF_LONG * 8. 160 * 161 * Params: 162 * mask = a #gulong containing flags 163 * nthBit = the index of the bit to start the search from 164 * 165 * Returns: the index of the first bit set which is lower than @nth_bit, or -1 166 * if no lower bits are set 167 */ 168 public static int bitNthMsf(gulong mask, int nthBit) 169 { 170 return g_bit_nth_msf(mask, nthBit); 171 } 172 173 /** 174 * Gets the number of bits used to hold @number, 175 * e.g. if @number is 4, 3 bits are needed. 176 * 177 * Params: 178 * number = a #guint 179 * 180 * Returns: the number of bits used to hold @number 181 */ 182 public static uint bitStorage(gulong number) 183 { 184 return g_bit_storage(number); 185 } 186 187 /** 188 * Returns the value of the environment variable @variable in the 189 * provided list @envp. 190 * 191 * Params: 192 * envp = an environment list (eg, as returned from g_get_environ()), or %NULL 193 * for an empty environment list 194 * variable = the environment variable to get 195 * 196 * Returns: the value of the environment variable, or %NULL if 197 * the environment variable is not set in @envp. The returned 198 * string is owned by @envp, and will be freed if @variable is 199 * set or unset again. 200 * 201 * Since: 2.32 202 */ 203 public static string environGetenv(string[] envp, string variable) 204 { 205 return Str.toString(g_environ_getenv(Str.toStringzArray(envp), Str.toStringz(variable))); 206 } 207 208 /** 209 * Sets the environment variable @variable in the provided list 210 * @envp to @value. 211 * 212 * Params: 213 * envp = an environment list that can be freed using g_strfreev() (e.g., as 214 * returned from g_get_environ()), or %NULL for an empty 215 * environment list 216 * variable = the environment variable to set, must not 217 * contain '=' 218 * value = the value for to set the variable to 219 * overwrite = whether to change the variable if it already exists 220 * 221 * Returns: the updated environment list. Free it using g_strfreev(). 222 * 223 * Since: 2.32 224 */ 225 public static string[] environSetenv(string[] envp, string variable, string value, bool overwrite) 226 { 227 auto retStr = g_environ_setenv(Str.toStringzArray(envp), Str.toStringz(variable), Str.toStringz(value), overwrite); 228 229 scope(exit) Str.freeStringArray(retStr); 230 return Str.toStringArray(retStr); 231 } 232 233 /** 234 * Removes the environment variable @variable from the provided 235 * environment @envp. 236 * 237 * Params: 238 * envp = an environment list that can be freed using g_strfreev() (e.g., as 239 * returned from g_get_environ()), or %NULL for an empty environment list 240 * variable = the environment variable to remove, must not 241 * contain '=' 242 * 243 * Returns: the updated environment list. Free it using g_strfreev(). 244 * 245 * Since: 2.32 246 */ 247 public static string[] environUnsetenv(string[] envp, string variable) 248 { 249 auto retStr = g_environ_unsetenv(Str.toStringzArray(envp), Str.toStringz(variable)); 250 251 scope(exit) Str.freeStringArray(retStr); 252 return Str.toStringArray(retStr); 253 } 254 255 /** 256 * Locates the first executable named @program in the user's path, in the 257 * same way that execvp() would locate it. Returns an allocated string 258 * with the absolute path name, or %NULL if the program is not found in 259 * the path. If @program is already an absolute path, returns a copy of 260 * @program if @program exists and is executable, and %NULL otherwise. 261 * 262 * On Windows, if @program does not have a file type suffix, tries 263 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in 264 * the `PATHEXT` environment variable. 265 * 266 * On Windows, it looks for the file in the same way as CreateProcess() 267 * would. This means first in the directory where the executing 268 * program was loaded from, then in the current directory, then in the 269 * Windows 32-bit system directory, then in the Windows directory, and 270 * finally in the directories in the `PATH` environment variable. If 271 * the program is found, the return value contains the full name 272 * including the type suffix. 273 * 274 * Params: 275 * program = a program name in the GLib file name encoding 276 * 277 * Returns: a newly-allocated 278 * string with the absolute path, or %NULL 279 */ 280 public static string findProgramInPath(string program) 281 { 282 auto retStr = g_find_program_in_path(Str.toStringz(program)); 283 284 scope(exit) Str.freeString(retStr); 285 return Str.toString(retStr); 286 } 287 288 /** 289 * Formats a size (for example the size of a file) into a human readable 290 * string. Sizes are rounded to the nearest size prefix (kB, MB, GB) 291 * and are displayed rounded to the nearest tenth. E.g. the file size 292 * 3292528 bytes will be converted into the string "3.2 MB". The returned string 293 * is UTF-8, and may use a non-breaking space to separate the number and units, 294 * to ensure they aren’t separated when line wrapped. 295 * 296 * The prefix units base is 1000 (i.e. 1 kB is 1000 bytes). 297 * 298 * This string should be freed with g_free() when not needed any longer. 299 * 300 * See g_format_size_full() for more options about how the size might be 301 * formatted. 302 * 303 * Params: 304 * size = a size in bytes 305 * 306 * Returns: a newly-allocated formatted string containing 307 * a human readable file size 308 * 309 * Since: 2.30 310 */ 311 public static string formatSize(ulong size) 312 { 313 auto retStr = g_format_size(size); 314 315 scope(exit) Str.freeString(retStr); 316 return Str.toString(retStr); 317 } 318 319 /** 320 * Formats a size (for example the size of a file) into a human 321 * readable string. Sizes are rounded to the nearest size prefix 322 * (KB, MB, GB) and are displayed rounded to the nearest tenth. 323 * E.g. the file size 3292528 bytes will be converted into the 324 * string "3.1 MB". 325 * 326 * The prefix units base is 1024 (i.e. 1 KB is 1024 bytes). 327 * 328 * This string should be freed with g_free() when not needed any longer. 329 * 330 * Deprecated: This function is broken due to its use of SI 331 * suffixes to denote IEC units. Use g_format_size() instead. 332 * 333 * Params: 334 * size = a size in bytes 335 * 336 * Returns: a newly-allocated formatted string 337 * containing a human readable file size 338 * 339 * Since: 2.16 340 */ 341 public static string formatSizeForDisplay(long size) 342 { 343 auto retStr = g_format_size_for_display(size); 344 345 scope(exit) Str.freeString(retStr); 346 return Str.toString(retStr); 347 } 348 349 /** 350 * Formats a size. 351 * 352 * This function is similar to g_format_size() but allows for flags 353 * that modify the output. See #GFormatSizeFlags. 354 * 355 * Params: 356 * size = a size in bytes 357 * flags = #GFormatSizeFlags to modify the output 358 * 359 * Returns: a newly-allocated formatted string 360 * containing a human readable file size 361 * 362 * Since: 2.30 363 */ 364 public static string formatSizeFull(ulong size, GFormatSizeFlags flags) 365 { 366 auto retStr = g_format_size_full(size, flags); 367 368 scope(exit) Str.freeString(retStr); 369 return Str.toString(retStr); 370 } 371 372 /** 373 * Gets a human-readable name for the application, as set by 374 * g_set_application_name(). This name should be localized if 375 * possible, and is intended for display to the user. Contrast with 376 * g_get_prgname(), which gets a non-localized name. If 377 * g_set_application_name() has not been called, returns the result of 378 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not 379 * been called). 380 * 381 * Returns: human-readable application 382 * name. May return %NULL 383 * 384 * Since: 2.2 385 */ 386 public static string getApplicationName() 387 { 388 return Str.toString(g_get_application_name()); 389 } 390 391 /** 392 * Gets the list of environment variables for the current process. 393 * 394 * The list is %NULL terminated and each item in the list is of the 395 * form 'NAME=VALUE'. 396 * 397 * This is equivalent to direct access to the 'environ' global variable, 398 * except portable. 399 * 400 * The return value is freshly allocated and it should be freed with 401 * g_strfreev() when it is no longer needed. 402 * 403 * Returns: the list of environment variables 404 * 405 * Since: 2.28 406 */ 407 public static string[] getEnviron() 408 { 409 auto retStr = g_get_environ(); 410 411 scope(exit) Str.freeStringArray(retStr); 412 return Str.toStringArray(retStr); 413 } 414 415 /** 416 * Gets the current directory. 417 * 418 * The returned string should be freed when no longer needed. 419 * The encoding of the returned string is system defined. 420 * On Windows, it is always UTF-8. 421 * 422 * Since GLib 2.40, this function will return the value of the "PWD" 423 * environment variable if it is set and it happens to be the same as 424 * the current directory. This can make a difference in the case that 425 * the current directory is the target of a symbolic link. 426 * 427 * Returns: the current directory 428 */ 429 public static string getCurrentDir() 430 { 431 auto retStr = g_get_current_dir(); 432 433 scope(exit) Str.freeString(retStr); 434 return Str.toString(retStr); 435 } 436 437 /** 438 * Gets the current user's home directory. 439 * 440 * As with most UNIX tools, this function will return the value of the 441 * `HOME` environment variable if it is set to an existing absolute path 442 * name, falling back to the `passwd` file in the case that it is unset. 443 * 444 * If the path given in `HOME` is non-absolute, does not exist, or is 445 * not a directory, the result is undefined. 446 * 447 * Before version 2.36 this function would ignore the `HOME` environment 448 * variable, taking the value from the `passwd` database instead. This was 449 * changed to increase the compatibility of GLib with other programs (and 450 * the XDG basedir specification) and to increase testability of programs 451 * based on GLib (by making it easier to run them from test frameworks). 452 * 453 * If your program has a strong requirement for either the new or the 454 * old behaviour (and if you don't wish to increase your GLib 455 * dependency to ensure that the new behaviour is in effect) then you 456 * should either directly check the `HOME` environment variable yourself 457 * or unset it before calling any functions in GLib. 458 * 459 * Returns: the current user's home directory 460 */ 461 public static string getHomeDir() 462 { 463 return Str.toString(g_get_home_dir()); 464 } 465 466 /** 467 * Return a name for the machine. 468 * 469 * The returned name is not necessarily a fully-qualified domain name, 470 * or even present in DNS or some other name service at all. It need 471 * not even be unique on your local network or site, but usually it 472 * is. Callers should not rely on the return value having any specific 473 * properties like uniqueness for security purposes. Even if the name 474 * of the machine is changed while an application is running, the 475 * return value from this function does not change. The returned 476 * string is owned by GLib and should not be modified or freed. If no 477 * name can be determined, a default fixed string "localhost" is 478 * returned. 479 * 480 * The encoding of the returned string is UTF-8. 481 * 482 * Returns: the host name of the machine. 483 * 484 * Since: 2.8 485 */ 486 public static string getHostName() 487 { 488 return Str.toString(g_get_host_name()); 489 } 490 491 /** 492 * Gets the name of the program. This name should not be localized, 493 * in contrast to g_get_application_name(). 494 * 495 * If you are using #GApplication the program name is set in 496 * g_application_run(). In case of GDK or GTK+ it is set in 497 * gdk_init(), which is called by gtk_init() and the 498 * #GtkApplication::startup handler. The program name is found by 499 * taking the last component of @argv[0]. 500 * 501 * Returns: the name of the program, 502 * or %NULL if it has not been set yet. The returned string belongs 503 * to GLib and must not be modified or freed. 504 */ 505 public static string getPrgname() 506 { 507 return Str.toString(g_get_prgname()); 508 } 509 510 /** 511 * Gets the real name of the user. This usually comes from the user's 512 * entry in the `passwd` file. The encoding of the returned string is 513 * system-defined. (On Windows, it is, however, always UTF-8.) If the 514 * real user name cannot be determined, the string "Unknown" is 515 * returned. 516 * 517 * Returns: the user's real name. 518 */ 519 public static string getRealName() 520 { 521 return Str.toString(g_get_real_name()); 522 } 523 524 /** 525 * Returns an ordered list of base directories in which to access 526 * system-wide configuration information. 527 * 528 * On UNIX platforms this is determined using the mechanisms described 529 * in the 530 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec). 531 * In this case the list of directories retrieved will be `XDG_CONFIG_DIRS`. 532 * 533 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_DIRS` is defined. 534 * If `XDG_CONFIG_DIRS` is undefined, the directory that contains application 535 * data for all users is used instead. A typical path is 536 * `C:\Documents and Settings\All Users\Application Data`. 537 * This folder is used for application data 538 * that is not user specific. For example, an application can store 539 * a spell-check dictionary, a database of clip art, or a log file in the 540 * CSIDL_COMMON_APPDATA folder. This information will not roam and is available 541 * to anyone using the computer. 542 * 543 * The return value is cached and modifying it at runtime is not supported, as 544 * it’s not thread-safe to modify environment variables at runtime. 545 * 546 * Returns: a %NULL-terminated array of strings owned by GLib that must not be 547 * modified or freed. 548 * 549 * Since: 2.6 550 */ 551 public static string[] getSystemConfigDirs() 552 { 553 return Str.toStringArray(g_get_system_config_dirs()); 554 } 555 556 /** 557 * Returns an ordered list of base directories in which to access 558 * system-wide application data. 559 * 560 * On UNIX platforms this is determined using the mechanisms described 561 * in the 562 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec) 563 * In this case the list of directories retrieved will be `XDG_DATA_DIRS`. 564 * 565 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_DIRS` is defined. 566 * If `XDG_DATA_DIRS` is undefined, 567 * the first elements in the list are the Application Data 568 * and Documents folders for All Users. (These can be determined only 569 * on Windows 2000 or later and are not present in the list on other 570 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and 571 * CSIDL_COMMON_DOCUMENTS. 572 * 573 * Then follows the "share" subfolder in the installation folder for 574 * the package containing the DLL that calls this function, if it can 575 * be determined. 576 * 577 * Finally the list contains the "share" subfolder in the installation 578 * folder for GLib, and in the installation folder for the package the 579 * application's .exe file belongs to. 580 * 581 * The installation folders above are determined by looking up the 582 * folder where the module (DLL or EXE) in question is located. If the 583 * folder's name is "bin", its parent is used, otherwise the folder 584 * itself. 585 * 586 * Note that on Windows the returned list can vary depending on where 587 * this function is called. 588 * 589 * The return value is cached and modifying it at runtime is not supported, as 590 * it’s not thread-safe to modify environment variables at runtime. 591 * 592 * Returns: a %NULL-terminated array of strings owned by GLib that must not be 593 * modified or freed. 594 * 595 * Since: 2.6 596 */ 597 public static string[] getSystemDataDirs() 598 { 599 return Str.toStringArray(g_get_system_data_dirs()); 600 } 601 602 /** 603 * Gets the directory to use for temporary files. 604 * 605 * On UNIX, this is taken from the `TMPDIR` environment variable. 606 * If the variable is not set, `P_tmpdir` is 607 * used, as defined by the system C library. Failing that, a 608 * hard-coded default of "/tmp" is returned. 609 * 610 * On Windows, the `TEMP` environment variable is used, with the 611 * root directory of the Windows installation (eg: "C:\") used 612 * as a default. 613 * 614 * The encoding of the returned string is system-defined. On Windows, 615 * it is always UTF-8. The return value is never %NULL or the empty 616 * string. 617 * 618 * Returns: the directory to use for temporary files. 619 */ 620 public static string getTmpDir() 621 { 622 return Str.toString(g_get_tmp_dir()); 623 } 624 625 /** 626 * Returns a base directory in which to store non-essential, cached 627 * data specific to particular user. 628 * 629 * On UNIX platforms this is determined using the mechanisms described 630 * in the 631 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec). 632 * In this case the directory retrieved will be `XDG_CACHE_HOME`. 633 * 634 * On Windows it follows XDG Base Directory Specification if `XDG_CACHE_HOME` is defined. 635 * If `XDG_CACHE_HOME` is undefined, the directory that serves as a common 636 * repository for temporary Internet files is used instead. A typical path is 637 * `C:\Documents and Settings\username\Local Settings\Temporary Internet Files`. 638 * See the [documentation for `CSIDL_INTERNET_CACHE`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_internet_cache). 639 * 640 * The return value is cached and modifying it at runtime is not supported, as 641 * it’s not thread-safe to modify environment variables at runtime. 642 * 643 * Returns: a string owned by GLib that 644 * must not be modified or freed. 645 * 646 * Since: 2.6 647 */ 648 public static string getUserCacheDir() 649 { 650 return Str.toString(g_get_user_cache_dir()); 651 } 652 653 /** 654 * Returns a base directory in which to store user-specific application 655 * configuration information such as user preferences and settings. 656 * 657 * On UNIX platforms this is determined using the mechanisms described 658 * in the 659 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec). 660 * In this case the directory retrieved will be `XDG_CONFIG_HOME`. 661 * 662 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_HOME` is defined. 663 * If `XDG_CONFIG_HOME` is undefined, the folder to use for local (as opposed 664 * to roaming) application data is used instead. See the 665 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata). 666 * Note that in this case on Windows it will be the same 667 * as what g_get_user_data_dir() returns. 668 * 669 * The return value is cached and modifying it at runtime is not supported, as 670 * it’s not thread-safe to modify environment variables at runtime. 671 * 672 * Returns: a string owned by GLib that 673 * must not be modified or freed. 674 * 675 * Since: 2.6 676 */ 677 public static string getUserConfigDir() 678 { 679 return Str.toString(g_get_user_config_dir()); 680 } 681 682 /** 683 * Returns a base directory in which to access application data such 684 * as icons that is customized for a particular user. 685 * 686 * On UNIX platforms this is determined using the mechanisms described 687 * in the 688 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec). 689 * In this case the directory retrieved will be `XDG_DATA_HOME`. 690 * 691 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_HOME` 692 * is defined. If `XDG_DATA_HOME` is undefined, the folder to use for local (as 693 * opposed to roaming) application data is used instead. See the 694 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata). 695 * Note that in this case on Windows it will be the same 696 * as what g_get_user_config_dir() returns. 697 * 698 * The return value is cached and modifying it at runtime is not supported, as 699 * it’s not thread-safe to modify environment variables at runtime. 700 * 701 * Returns: a string owned by GLib that must 702 * not be modified or freed. 703 * 704 * Since: 2.6 705 */ 706 public static string getUserDataDir() 707 { 708 return Str.toString(g_get_user_data_dir()); 709 } 710 711 /** 712 * Gets the user name of the current user. The encoding of the returned 713 * string is system-defined. On UNIX, it might be the preferred file name 714 * encoding, or something else, and there is no guarantee that it is even 715 * consistent on a machine. On Windows, it is always UTF-8. 716 * 717 * Returns: the user name of the current user. 718 */ 719 public static string getUserName() 720 { 721 return Str.toString(g_get_user_name()); 722 } 723 724 /** 725 * Returns a directory that is unique to the current user on the local 726 * system. 727 * 728 * This is determined using the mechanisms described 729 * in the 730 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec). 731 * This is the directory 732 * specified in the `XDG_RUNTIME_DIR` environment variable. 733 * In the case that this variable is not set, we return the value of 734 * g_get_user_cache_dir(), after verifying that it exists. 735 * 736 * The return value is cached and modifying it at runtime is not supported, as 737 * it’s not thread-safe to modify environment variables at runtime. 738 * 739 * Returns: a string owned by GLib that must not be 740 * modified or freed. 741 * 742 * Since: 2.28 743 */ 744 public static string getUserRuntimeDir() 745 { 746 return Str.toString(g_get_user_runtime_dir()); 747 } 748 749 /** 750 * Returns the full path of a special directory using its logical id. 751 * 752 * On UNIX this is done using the XDG special user directories. 753 * For compatibility with existing practise, %G_USER_DIRECTORY_DESKTOP 754 * falls back to `$HOME/Desktop` when XDG special user directories have 755 * not been set up. 756 * 757 * Depending on the platform, the user might be able to change the path 758 * of the special directory without requiring the session to restart; GLib 759 * will not reflect any change once the special directories are loaded. 760 * 761 * Params: 762 * directory = the logical id of special directory 763 * 764 * Returns: the path to the specified special directory, or 765 * %NULL if the logical id was not found. The returned string is owned by 766 * GLib and should not be modified or freed. 767 * 768 * Since: 2.14 769 */ 770 public static string getUserSpecialDir(GUserDirectory directory) 771 { 772 return Str.toString(g_get_user_special_dir(directory)); 773 } 774 775 /** 776 * Returns the value of an environment variable. 777 * 778 * On UNIX, the name and value are byte strings which might or might not 779 * be in some consistent character set and encoding. On Windows, they are 780 * in UTF-8. 781 * On Windows, in case the environment variable's value contains 782 * references to other environment variables, they are expanded. 783 * 784 * Params: 785 * variable = the environment variable to get 786 * 787 * Returns: the value of the environment variable, or %NULL if 788 * the environment variable is not found. The returned string 789 * may be overwritten by the next call to g_getenv(), g_setenv() 790 * or g_unsetenv(). 791 */ 792 public static string getenv(string variable) 793 { 794 return Str.toString(g_getenv(Str.toStringz(variable))); 795 } 796 797 /** 798 * Gets the names of all variables set in the environment. 799 * 800 * Programs that want to be portable to Windows should typically use 801 * this function and g_getenv() instead of using the environ array 802 * from the C library directly. On Windows, the strings in the environ 803 * array are in system codepage encoding, while in most of the typical 804 * use cases for environment variables in GLib-using programs you want 805 * the UTF-8 encoding that this function and g_getenv() provide. 806 * 807 * Returns: a %NULL-terminated list of strings which must be freed with 808 * g_strfreev(). 809 * 810 * Since: 2.8 811 */ 812 public static string[] listenv() 813 { 814 auto retStr = g_listenv(); 815 816 scope(exit) Str.freeStringArray(retStr); 817 return Str.toStringArray(retStr); 818 } 819 820 /** 821 * Set the pointer at the specified location to %NULL. 822 * 823 * Params: 824 * nullifyLocation = the memory address of the pointer. 825 */ 826 public static void nullifyPointer(void** nullifyLocation) 827 { 828 g_nullify_pointer(nullifyLocation); 829 } 830 831 /** 832 * Parses a string containing debugging options 833 * into a %guint containing bit flags. This is used 834 * within GDK and GTK+ to parse the debug options passed on the 835 * command line or through environment variables. 836 * 837 * If @string is equal to "all", all flags are set. Any flags 838 * specified along with "all" in @string are inverted; thus, 839 * "all,foo,bar" or "foo,bar,all" sets all flags except those 840 * corresponding to "foo" and "bar". 841 * 842 * If @string is equal to "help", all the available keys in @keys 843 * are printed out to standard error. 844 * 845 * Params: 846 * string_ = a list of debug options separated by colons, spaces, or 847 * commas, or %NULL. 848 * keys = pointer to an array of #GDebugKey which associate 849 * strings with bit flags. 850 * 851 * Returns: the combined set of bit flags. 852 */ 853 public static uint parseDebugString(string string_, GDebugKey[] keys) 854 { 855 return g_parse_debug_string(Str.toStringz(string_), keys.ptr, cast(uint)keys.length); 856 } 857 858 /** 859 * Gets the last component of the filename. 860 * 861 * If @file_name ends with a directory separator it gets the component 862 * before the last slash. If @file_name consists only of directory 863 * separators (and on Windows, possibly a drive letter), a single 864 * separator is returned. If @file_name is empty, it gets ".". 865 * 866 * Params: 867 * fileName = the name of the file 868 * 869 * Returns: a newly allocated string containing the last 870 * component of the filename 871 */ 872 public static string pathGetBasename(string fileName) 873 { 874 auto retStr = g_path_get_basename(Str.toStringz(fileName)); 875 876 scope(exit) Str.freeString(retStr); 877 return Str.toString(retStr); 878 } 879 880 /** 881 * Gets the directory components of a file name. For example, the directory 882 * component of `/usr/bin/test` is `/usr/bin`. The directory component of `/` 883 * is `/`. 884 * 885 * If the file name has no directory components "." is returned. 886 * The returned string should be freed when no longer needed. 887 * 888 * Params: 889 * fileName = the name of the file 890 * 891 * Returns: the directory components of the file 892 */ 893 public static string pathGetDirname(string fileName) 894 { 895 auto retStr = g_path_get_dirname(Str.toStringz(fileName)); 896 897 scope(exit) Str.freeString(retStr); 898 return Str.toString(retStr); 899 } 900 901 /** 902 * Returns %TRUE if the given @file_name is an absolute file name. 903 * Note that this is a somewhat vague concept on Windows. 904 * 905 * On POSIX systems, an absolute file name is well-defined. It always 906 * starts from the single root directory. For example "/usr/local". 907 * 908 * On Windows, the concepts of current drive and drive-specific 909 * current directory introduce vagueness. This function interprets as 910 * an absolute file name one that either begins with a directory 911 * separator such as "\Users\tml" or begins with the root on a drive, 912 * for example "C:\Windows". The first case also includes UNC paths 913 * such as "\\\\myserver\docs\foo". In all cases, either slashes or 914 * backslashes are accepted. 915 * 916 * Note that a file name relative to the current drive root does not 917 * truly specify a file uniquely over time and across processes, as 918 * the current drive is a per-process value and can be changed. 919 * 920 * File names relative the current directory on some specific drive, 921 * such as "D:foo/bar", are not interpreted as absolute by this 922 * function, but they obviously are not relative to the normal current 923 * directory as returned by getcwd() or g_get_current_dir() 924 * either. Such paths should be avoided, or need to be handled using 925 * Windows-specific code. 926 * 927 * Params: 928 * fileName = a file name 929 * 930 * Returns: %TRUE if @file_name is absolute 931 */ 932 public static bool pathIsAbsolute(string fileName) 933 { 934 return g_path_is_absolute(Str.toStringz(fileName)) != 0; 935 } 936 937 /** 938 * Returns a pointer into @file_name after the root component, 939 * i.e. after the "/" in UNIX or "C:\" under Windows. If @file_name 940 * is not an absolute path it returns %NULL. 941 * 942 * Params: 943 * fileName = a file name 944 * 945 * Returns: a pointer into @file_name after the 946 * root component 947 */ 948 public static string pathSkipRoot(string fileName) 949 { 950 return Str.toString(g_path_skip_root(Str.toStringz(fileName))); 951 } 952 953 /** 954 * This is just like the standard C qsort() function, but 955 * the comparison routine accepts a user data argument. 956 * 957 * This is guaranteed to be a stable sort since version 2.32. 958 * 959 * Params: 960 * pbase = start of array to sort 961 * totalElems = elements in the array 962 * size = size of each element 963 * compareFunc = function to compare elements 964 * userData = data to pass to @compare_func 965 */ 966 public static void qsortWithData(void* pbase, int totalElems, size_t size, GCompareDataFunc compareFunc, void* userData) 967 { 968 g_qsort_with_data(pbase, totalElems, size, compareFunc, userData); 969 } 970 971 /** 972 * Resets the cache used for g_get_user_special_dir(), so 973 * that the latest on-disk version is used. Call this only 974 * if you just changed the data on disk yourself. 975 * 976 * Due to thread safety issues this may cause leaking of strings 977 * that were previously returned from g_get_user_special_dir() 978 * that can't be freed. We ensure to only leak the data for 979 * the directories that actually changed value though. 980 * 981 * Since: 2.22 982 */ 983 public static void reloadUserSpecialDirsCache() 984 { 985 g_reload_user_special_dirs_cache(); 986 } 987 988 /** 989 * Sets a human-readable name for the application. This name should be 990 * localized if possible, and is intended for display to the user. 991 * Contrast with g_set_prgname(), which sets a non-localized name. 992 * g_set_prgname() will be called automatically by gtk_init(), 993 * but g_set_application_name() will not. 994 * 995 * Note that for thread safety reasons, this function can only 996 * be called once. 997 * 998 * The application name will be used in contexts such as error messages, 999 * or when displaying an application's name in the task list. 1000 * 1001 * Params: 1002 * applicationName = localized name of the application 1003 * 1004 * Since: 2.2 1005 */ 1006 public static void setApplicationName(string applicationName) 1007 { 1008 g_set_application_name(Str.toStringz(applicationName)); 1009 } 1010 1011 /** 1012 * Sets the name of the program. This name should not be localized, 1013 * in contrast to g_set_application_name(). 1014 * 1015 * If you are using #GApplication the program name is set in 1016 * g_application_run(). In case of GDK or GTK+ it is set in 1017 * gdk_init(), which is called by gtk_init() and the 1018 * #GtkApplication::startup handler. The program name is found by 1019 * taking the last component of @argv[0]. 1020 * 1021 * Note that for thread-safety reasons this function can only be called once. 1022 * 1023 * Params: 1024 * prgname = the name of the program. 1025 */ 1026 public static void setPrgname(string prgname) 1027 { 1028 g_set_prgname(Str.toStringz(prgname)); 1029 } 1030 1031 /** 1032 * Sets an environment variable. On UNIX, both the variable's name and 1033 * value can be arbitrary byte strings, except that the variable's name 1034 * cannot contain '='. On Windows, they should be in UTF-8. 1035 * 1036 * Note that on some systems, when variables are overwritten, the memory 1037 * used for the previous variables and its value isn't reclaimed. 1038 * 1039 * You should be mindful of the fact that environment variable handling 1040 * in UNIX is not thread-safe, and your program may crash if one thread 1041 * calls g_setenv() while another thread is calling getenv(). (And note 1042 * that many functions, such as gettext(), call getenv() internally.) 1043 * This function is only safe to use at the very start of your program, 1044 * before creating any other threads (or creating objects that create 1045 * worker threads of their own). 1046 * 1047 * If you need to set up the environment for a child process, you can 1048 * use g_get_environ() to get an environment array, modify that with 1049 * g_environ_setenv() and g_environ_unsetenv(), and then pass that 1050 * array directly to execvpe(), g_spawn_async(), or the like. 1051 * 1052 * Params: 1053 * variable = the environment variable to set, must not 1054 * contain '='. 1055 * value = the value for to set the variable to. 1056 * overwrite = whether to change the variable if it already exists. 1057 * 1058 * Returns: %FALSE if the environment variable couldn't be set. 1059 * 1060 * Since: 2.4 1061 */ 1062 public static bool setenv(string variable, string value, bool overwrite) 1063 { 1064 return g_setenv(Str.toStringz(variable), Str.toStringz(value), overwrite) != 0; 1065 } 1066 1067 /** 1068 * Gets the smallest prime number from a built-in array of primes which 1069 * is larger than @num. This is used within GLib to calculate the optimum 1070 * size of a #GHashTable. 1071 * 1072 * The built-in array of primes ranges from 11 to 13845163 such that 1073 * each prime is approximately 1.5-2 times the previous prime. 1074 * 1075 * Params: 1076 * num = a #guint 1077 * 1078 * Returns: the smallest prime number from a built-in array of primes 1079 * which is larger than @num 1080 */ 1081 public static uint spacedPrimesClosest(uint num) 1082 { 1083 return g_spaced_primes_closest(num); 1084 } 1085 1086 /** 1087 * Removes an environment variable from the environment. 1088 * 1089 * Note that on some systems, when variables are overwritten, the 1090 * memory used for the previous variables and its value isn't reclaimed. 1091 * 1092 * You should be mindful of the fact that environment variable handling 1093 * in UNIX is not thread-safe, and your program may crash if one thread 1094 * calls g_unsetenv() while another thread is calling getenv(). (And note 1095 * that many functions, such as gettext(), call getenv() internally.) This 1096 * function is only safe to use at the very start of your program, before 1097 * creating any other threads (or creating objects that create worker 1098 * threads of their own). 1099 * 1100 * If you need to set up the environment for a child process, you can 1101 * use g_get_environ() to get an environment array, modify that with 1102 * g_environ_setenv() and g_environ_unsetenv(), and then pass that 1103 * array directly to execvpe(), g_spawn_async(), or the like. 1104 * 1105 * Params: 1106 * variable = the environment variable to remove, must 1107 * not contain '=' 1108 * 1109 * Since: 2.4 1110 */ 1111 public static void unsetenv(string variable) 1112 { 1113 g_unsetenv(Str.toStringz(variable)); 1114 } 1115 1116 /** 1117 * Gets the canonical file name from @filename. All triple slashes are turned into 1118 * single slashes, and all `..` and `.`s resolved against @relative_to. 1119 * 1120 * Symlinks are not followed, and the returned path is guaranteed to be absolute. 1121 * 1122 * If @filename is an absolute path, @relative_to is ignored. Otherwise, 1123 * @relative_to will be prepended to @filename to make it absolute. @relative_to 1124 * must be an absolute path, or %NULL. If @relative_to is %NULL, it'll fallback 1125 * to g_get_current_dir(). 1126 * 1127 * This function never fails, and will canonicalize file paths even if they don't 1128 * exist. 1129 * 1130 * No file system I/O is done. 1131 * 1132 * Params: 1133 * filename = the name of the file 1134 * relativeTo = the relative directory, or %NULL 1135 * to use the current working directory 1136 * 1137 * Returns: a newly allocated string with the 1138 * canonical file path 1139 * 1140 * Since: 2.58 1141 */ 1142 public static string canonicalizeFilename(string filename, string relativeTo) 1143 { 1144 auto retStr = g_canonicalize_filename(Str.toStringz(filename), Str.toStringz(relativeTo)); 1145 1146 scope(exit) Str.freeString(retStr); 1147 return Str.toString(retStr); 1148 } 1149 1150 /** 1151 * Get information about the operating system. 1152 * 1153 * On Linux this comes from the `/etc/os-release` file. On other systems, it may 1154 * come from a variety of sources. You can either use the standard key names 1155 * like %G_OS_INFO_KEY_NAME or pass any UTF-8 string key name. For example, 1156 * `/etc/os-release` provides a number of other less commonly used values that may 1157 * be useful. No key is guaranteed to be provided, so the caller should always 1158 * check if the result is %NULL. 1159 * 1160 * Params: 1161 * keyName = a key for the OS info being requested, for example %G_OS_INFO_KEY_NAME. 1162 * 1163 * Returns: The associated value for the requested key or %NULL if 1164 * this information is not provided. 1165 * 1166 * Since: 2.64 1167 */ 1168 public static string getOsInfo(string keyName) 1169 { 1170 auto retStr = g_get_os_info(Str.toStringz(keyName)); 1171 1172 scope(exit) Str.freeString(retStr); 1173 return Str.toString(retStr); 1174 } 1175 }