The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

u/file: Add file helpers

authored by

Jakob Bornecrantz and committed by
Jakob Bornecrantz
b5dd07f2 c02f89dc

+154
+1
doc/changes/aux/mr.266.1.md
··· 1 + u/file: Add file helpers to load files from config directory.
+2
src/xrt/auxiliary/CMakeLists.txt
··· 77 77 util/u_distortion_mesh.c 78 78 util/u_distortion_mesh.h 79 79 util/u_documentation.h 80 + util/u_file.c 81 + util/u_file.h 80 82 util/u_format.c 81 83 util/u_format.h 82 84 util/u_frame.c
+2
src/xrt/auxiliary/meson.build
··· 15 15 'util/u_distortion_mesh.c', 16 16 'util/u_distortion_mesh.h', 17 17 'util/u_documentation.h', 18 + 'util/u_file.c', 19 + 'util/u_file.h', 18 20 'util/u_format.c', 19 21 'util/u_format.h', 20 22 'util/u_frame.c',
+113
src/xrt/auxiliary/util/u_file.c
··· 1 + // Copyright 2019-2020, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Very simple file opening functions. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @author Pete Black <pblack@collabora.com> 8 + * @ingroup aux_util 9 + */ 10 + 11 + #include "util/u_file.h" 12 + 13 + #include <errno.h> 14 + #include <stdio.h> 15 + #include <stdlib.h> 16 + #include <string.h> 17 + #include <sys/stat.h> 18 + #include <linux/limits.h> 19 + 20 + 21 + static int 22 + mkpath(const char *path) 23 + { 24 + char tmp[PATH_MAX]; 25 + char *p = NULL; 26 + size_t len; 27 + 28 + snprintf(tmp, sizeof(tmp), "%s", path); 29 + len = strlen(tmp) - 1; 30 + if (tmp[len] == '/') { 31 + tmp[len] = 0; 32 + } 33 + 34 + for (p = tmp + 1; *p; p++) { 35 + if (*p == '/') { 36 + *p = 0; 37 + if (mkdir(tmp, S_IRWXU) < 0 && errno != EEXIST) { 38 + return -1; 39 + } 40 + *p = '/'; 41 + } 42 + } 43 + 44 + if (mkdir(tmp, S_IRWXU) < 0 && errno != EEXIST) { 45 + return -1; 46 + } 47 + 48 + return 0; 49 + } 50 + 51 + ssize_t 52 + u_file_get_config_dir(char *out_path, size_t out_path_size) 53 + { 54 + const char *xgd_home = getenv("XDG_CONFIG_HOME"); 55 + const char *home = getenv("HOME"); 56 + if (xgd_home != NULL) { 57 + return snprintf(out_path, out_path_size, "%s/monado", xgd_home); 58 + } else if (home != NULL) { 59 + return snprintf(out_path, out_path_size, "%s/.config/monado", 60 + home); 61 + } else { 62 + fprintf(stderr, 63 + "Could not create config file no $HOME or " 64 + "$XDG_CONFIG_HOME env variables defined\n"); 65 + return -1; 66 + } 67 + } 68 + 69 + ssize_t 70 + u_file_get_path_in_config_dir(const char *filename, 71 + char *out_path, 72 + size_t out_path_size) 73 + { 74 + char tmp[PATH_MAX]; 75 + ssize_t i = u_file_get_config_dir(tmp, sizeof(tmp)); 76 + if (i <= 0) { 77 + return -1; 78 + } 79 + 80 + return snprintf(out_path, out_path_size, "%s/%s", tmp, filename); 81 + } 82 + 83 + FILE * 84 + u_file_open_file_in_config_dir(const char *filename, const char *mode) 85 + { 86 + char tmp[PATH_MAX]; 87 + ssize_t i = u_file_get_config_dir(tmp, sizeof(tmp)); 88 + if (i <= 0) { 89 + return NULL; 90 + } 91 + 92 + char file_str[PATH_MAX + 15]; 93 + i = snprintf(file_str, sizeof(file_str), "%s/%s", tmp, filename); 94 + if (i <= 0) { 95 + return NULL; 96 + } 97 + 98 + FILE *file = fopen(file_str, mode); 99 + if (file != NULL) { 100 + return file; 101 + } 102 + 103 + // Try creating the path. 104 + mkpath(tmp); 105 + 106 + file = fopen(file_str, mode); 107 + if (file == NULL) { 108 + fprintf(stderr, "Could not open or create file '%s'\n", 109 + file_str); 110 + } 111 + 112 + return file; 113 + }
+36
src/xrt/auxiliary/util/u_file.h
··· 1 + // Copyright 2019-2020, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Very simple file opening functions. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_util 8 + */ 9 + 10 + #pragma once 11 + 12 + #include "xrt/xrt_compiler.h" 13 + 14 + #include <stdio.h> 15 + 16 + 17 + #ifdef __cplusplus 18 + extern "C" { 19 + #endif 20 + 21 + 22 + ssize_t 23 + u_file_get_config_dir(char *out_path, size_t out_path_size); 24 + 25 + ssize_t 26 + u_file_get_path_in_config_dir(const char *suffix, 27 + char *out_path, 28 + size_t out_path_size); 29 + 30 + FILE * 31 + u_file_open_file_in_config_dir(const char *filename, const char *mode); 32 + 33 + 34 + #ifdef __cplusplus 35 + } 36 + #endif