| 1 | = Finding a Directory's Path = |
| 2 | When referencing paths, it is best to use Haiku's Find Directory functionality. The system defines numerous key directories in the [http://dev.haiku-os.org/browser/haiku/trunk/headers/os/storage/FindDirectory.h os/storage/FindDirectory.h] header. In addition to C/C++, a command line utility `finddir` can be used with the same variable names. |
| 3 | |
| 4 | == Terminal == |
| 5 | {{{ |
| 6 | export USER_SETTINGS_DIRECTORY=`/bin/finddir B_USER_SETTINGS_DIRECTORY` |
| 7 | }}} |
| 8 | To test this, try {{{ finddir B_USER_SETTINGS_DIRECTORY }}} |
| 9 | |
| 10 | == Shell Script == |
| 11 | {{{ |
| 12 | APPS_DIR=`finddir B_APPS_DIRECTORY` |
| 13 | echo ${APPS_DIR} |
| 14 | }}} |
| 15 | |
| 16 | == Makefile == |
| 17 | # Assuming the makefile sets |
| 18 | {{{ |
| 19 | APPS_DIR:=$(shell finddir B_APPS_DIRECTORY) |
| 20 | }}} |
| 21 | |
| 22 | == C == |
| 23 | {{{ |
| 24 | #!rst |
| 25 | diff -urN tuxpaint-0.9.21/src/fonts.c tuxpaint-0.9.21-haiku/src/fonts.c |
| 26 | --- tuxpaint-0.9.21/src/fonts.c 2009-06-06 18:22:00.000000000 +0000 |
| 27 | +++ tuxpaint-0.9.21-haiku/src/fonts.c 2009-10-07 07:39:18.000000000 +0000 |
| 28 | @@ -67,6 +67,11 @@ |
| 29 | #include "win32_print.h" |
| 30 | #endif |
| 31 | |
| 32 | +#ifdef __HAIKU__ |
| 33 | +#include <FindDirectory.h> |
| 34 | +#include <fs_info.h> |
| 35 | +#endif |
| 36 | + |
| 37 | #ifdef __APPLE__ |
| 38 | #include "wrapperdata.h" |
| 39 | extern WrapperData macosx; |
| 40 | @@ -699,6 +704,14 @@ |
| 41 | loadfonts(screen, "/boot/home/config/font/ttffonts"); |
| 42 | loadfonts(screen, "/usr/share/fonts"); |
| 43 | loadfonts(screen, "/usr/X11R6/lib/X11/fonts"); |
| 44 | +#elif defined(__HAIKU__) |
| 45 | + dev_t volume = dev_for_path("/boot"); |
| 46 | + char buffer[B_PATH_NAME_LENGTH+B_FILE_NAME_LENGTH]; |
| 47 | + status_t result; |
| 48 | + result = find_directory(B_SYSTEM_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); |
| 49 | + loadfonts(screen, buffer); |
| 50 | + result = find_directory(B_COMMON_FONTS_DIRECTORY, volume, false, buffer, sizeof(buffer)); |
| 51 | + loadfonts(screen, buffer); |
| 52 | #elif defined(__APPLE__) |
| 53 | loadfonts(screen, "/System/Library/Fonts"); |
| 54 | loadfonts(screen, "/Library/Fonts"); |
| 55 | }}} |
| 56 | |
| 57 | == C++ == |
| 58 | {{{ |
| 59 | #!cpp |
| 60 | // Note: This is untested! |
| 61 | |
| 62 | #ifdef __HAIKU__ |
| 63 | #include <FindDirectory.h> |
| 64 | #include <fs_info.h> |
| 65 | #endif |
| 66 | |
| 67 | // ... |
| 68 | |
| 69 | BPath dir; |
| 70 | result = find_directory(B_SYSTEM_FONTS_DIRECTORY, &dir); |
| 71 | loadfonts(screen, dir.Path()); |
| 72 | result = find_directory(B_COMMON_FONTS_DIRECTORY, &dir); |
| 73 | loadfonts(screen, dir.Path()); |
| 74 | }}} |
| 75 | |
| 76 | == Python == |
| 77 | {{{ |
| 78 | #!python |
| 79 | #!/bin/env python |
| 80 | |
| 81 | import commands |
| 82 | |
| 83 | # Creating a utility function |
| 84 | def FindDirectory(aDir): |
| 85 | result = commands.getoutput('finddir %s' % aDir) |
| 86 | return result |
| 87 | |
| 88 | |
| 89 | if __name__ == "__main__": |
| 90 | # Using the utility function ... |
| 91 | B_APPS_DIRECTORY = FindDirectory('B_APPS_DIRECTORY') |
| 92 | print 'Using utility function:' |
| 93 | print B_APPS_DIRECTORY |
| 94 | |
| 95 | # Set a baloney value, to display functionality |
| 96 | B_APPS_DIRECTORY='Some Text to display the value being set properly' |
| 97 | print 'After setting a baloney value:' |
| 98 | print B_APPS_DIRECTORY |
| 99 | |
| 100 | # As a one liner ... |
| 101 | B_APPS_DIRECTORY = commands.getoutput('finddir B_APPS_DIRECTORY') |
| 102 | print 'Using a one liner:' |
| 103 | print B_APPS_DIRECTORY |
| 104 | }}} |