oxr_verify.cpp: rewrite in C

This commit is contained in:
Drew DeVault 2019-10-08 10:40:40 -04:00 committed by Jakob Bornecrantz
parent d7debdb724
commit 7f16e7230f
3 changed files with 50 additions and 48 deletions

View file

@ -37,7 +37,7 @@ set(OXR_SOURCE_FILES
oxr_swapchain_vk.c oxr_swapchain_vk.c
oxr_system.c oxr_system.c
oxr_two_call.h oxr_two_call.h
oxr_verify.cpp oxr_verify.c
oxr_vulkan.c oxr_vulkan.c
oxr_xdev.c oxr_xdev.c
) )

View file

@ -33,7 +33,7 @@ lib_st_oxr = static_library(
'oxr_swapchain_vk.c', 'oxr_swapchain_vk.c',
'oxr_system.c', 'oxr_system.c',
'oxr_two_call.h', 'oxr_two_call.h',
'oxr_verify.cpp', 'oxr_verify.c',
'oxr_vulkan.c', 'oxr_vulkan.c',
'oxr_xdev.c', 'oxr_xdev.c',
), ),

View file

@ -9,8 +9,9 @@
* @ingroup oxr_api * @ingroup oxr_api
*/ */
#include <cstdio> #include <assert.h>
#include <cstring> #include <stdio.h>
#include <string.h>
#include "xrt/xrt_compiler.h" #include "xrt/xrt_compiler.h"
#include "util/u_debug.h" #include "util/u_debug.h"
@ -57,7 +58,7 @@ contains_zero(const char *path, uint32_t size)
return false; return false;
} }
extern "C" XrResult XrResult
oxr_verify_fixed_size_single_level_path(struct oxr_logger *log, oxr_verify_fixed_size_single_level_path(struct oxr_logger *log,
const char *path, const char *path,
uint32_t array_size, uint32_t array_size,
@ -97,7 +98,7 @@ oxr_verify_fixed_size_single_level_path(struct oxr_logger *log,
return XR_SUCCESS; return XR_SUCCESS;
} }
extern "C" XrResult XrResult
oxr_verify_localized_name(struct oxr_logger *log, oxr_verify_localized_name(struct oxr_logger *log,
const char *string, const char *string,
uint32_t array_size, uint32_t array_size,
@ -123,15 +124,15 @@ oxr_verify_localized_name(struct oxr_logger *log,
return XR_SUCCESS; return XR_SUCCESS;
} }
enum class State enum verify_state
{ {
Start, VERIFY_START,
Middle, VERIFY_MIDDLE,
Slash, VERIFY_SLASH,
SlashDots, VERIFY_SLASHDOTS,
}; };
extern "C" XrResult XrResult
oxr_verify_full_path_c(struct oxr_logger *log, oxr_verify_full_path_c(struct oxr_logger *log,
const char *path, const char *path,
const char *name) const char *name)
@ -149,13 +150,13 @@ oxr_verify_full_path_c(struct oxr_logger *log,
return oxr_verify_full_path(log, path, (uint32_t)length, name); return oxr_verify_full_path(log, path, (uint32_t)length, name);
} }
extern "C" XrResult XrResult
oxr_verify_full_path(struct oxr_logger *log, oxr_verify_full_path(struct oxr_logger *log,
const char *path, const char *path,
size_t length, size_t length,
const char *name) const char *name)
{ {
State state = State::Start; enum verify_state state = VERIFY_START;
bool valid = true; bool valid = true;
if (length >= XR_MAX_PATH_LENGTH) { if (length >= XR_MAX_PATH_LENGTH) {
@ -171,7 +172,7 @@ oxr_verify_full_path(struct oxr_logger *log,
for (uint32_t i = 0; i < length; i++) { for (uint32_t i = 0; i < length; i++) {
const char c = path[i]; const char c = path[i];
switch (state) { switch (state) {
case State::Start: case VERIFY_START:
if (c != '/') { if (c != '/') {
return oxr_error(log, return oxr_error(log,
XR_ERROR_PATH_FORMAT_INVALID, XR_ERROR_PATH_FORMAT_INVALID,
@ -179,13 +180,13 @@ oxr_verify_full_path(struct oxr_logger *log,
"fowrward slash", "fowrward slash",
name); name);
} }
state = State::Slash; state = VERIFY_SLASH;
break; break;
case State::Slash: case VERIFY_SLASH:
switch (c) { switch (c) {
case '.': case '.':
// Is valid and starts the SlashDot(s) state. // Is valid and starts the SlashDot(s) state.
state = State::SlashDots; state = VERIFY_SLASHDOTS;
break; break;
case '/': case '/':
return oxr_error( return oxr_error(
@ -193,18 +194,18 @@ oxr_verify_full_path(struct oxr_logger *log,
"(%s) '//' is not a valid in a path", name); "(%s) '//' is not a valid in a path", name);
default: default:
valid = valid_path_char(c); valid = valid_path_char(c);
state = State::Middle; state = VERIFY_MIDDLE;
} }
break; break;
case State::Middle: case VERIFY_MIDDLE:
switch (c) { switch (c) {
case '/': state = State::Slash; break; case '/': state = VERIFY_SLASH; break;
default: default:
valid = valid_path_char(c); valid = valid_path_char(c);
state = State::Middle; state = VERIFY_MIDDLE;
} }
break; break;
case State::SlashDots: case VERIFY_SLASHDOTS:
switch (c) { switch (c) {
case '/': case '/':
return oxr_error( return oxr_error(
@ -216,7 +217,7 @@ oxr_verify_full_path(struct oxr_logger *log,
break; break;
default: default:
valid = valid_path_char(c); valid = valid_path_char(c);
state = State::Middle; state = VERIFY_MIDDLE;
} }
break; break;
} }
@ -234,12 +235,12 @@ oxr_verify_full_path(struct oxr_logger *log,
} }
switch (state) { switch (state) {
case State::Start: case VERIFY_START:
// Empty string // Empty string
return oxr_error(log, XR_ERROR_PATH_FORMAT_INVALID, return oxr_error(log, XR_ERROR_PATH_FORMAT_INVALID,
"(%s) a empty string is not a valid path", "(%s) a empty string is not a valid path",
name); name);
case State::Slash: case VERIFY_SLASH:
// Is this '/foo/' or '/' // Is this '/foo/' or '/'
if (length > 1) { if (length > 1) {
// It was '/foo/' // It was '/foo/'
@ -249,13 +250,13 @@ oxr_verify_full_path(struct oxr_logger *log,
return oxr_error(log, XR_ERROR_PATH_FORMAT_INVALID, return oxr_error(log, XR_ERROR_PATH_FORMAT_INVALID,
"(%s) the string '%s' is not a valid path", "(%s) the string '%s' is not a valid path",
name, path); name, path);
case State::SlashDots: case VERIFY_SLASHDOTS:
// Does the path ends with '/..' // Does the path ends with '/..'
return oxr_error( return oxr_error(
log, XR_ERROR_PATH_FORMAT_INVALID, log, XR_ERROR_PATH_FORMAT_INVALID,
"(%s) strings ending with '/.[.]*' is not a valid", name); "(%s) strings ending with '/.[.]*' is not a valid", name);
case State::Middle: case VERIFY_MIDDLE:
// '/foo/bar' okay! // '/foo/bar' okay!
return XR_SUCCESS; return XR_SUCCESS;
default: default:
@ -277,11 +278,12 @@ oxr_verify_full_path(struct oxr_logger *log,
static XrResult static XrResult
subaction_path_no_dups(struct oxr_logger *log, subaction_path_no_dups(struct oxr_logger *log,
struct oxr_instance *inst, struct oxr_instance *inst,
struct oxr_sub_paths &sub_paths, struct oxr_sub_paths *sub_paths,
XrPath path, XrPath path,
const char *variable, const char *variable,
uint32_t index) uint32_t index)
{ {
assert(sub_paths);
bool duplicate = false; bool duplicate = false;
if (path == XR_NULL_PATH) { if (path == XR_NULL_PATH) {
@ -292,34 +294,34 @@ subaction_path_no_dups(struct oxr_logger *log,
} }
if (path == inst->path_cache.user) { if (path == inst->path_cache.user) {
if (sub_paths.user) { if (sub_paths->user) {
duplicate = true; duplicate = true;
} else { } else {
sub_paths.user = true; sub_paths->user = true;
} }
} else if (path == inst->path_cache.head) { } else if (path == inst->path_cache.head) {
if (sub_paths.head) { if (sub_paths->head) {
duplicate = true; duplicate = true;
} else { } else {
sub_paths.head = true; sub_paths->head = true;
} }
} else if (path == inst->path_cache.left) { } else if (path == inst->path_cache.left) {
if (sub_paths.left) { if (sub_paths->left) {
duplicate = true; duplicate = true;
} else { } else {
sub_paths.left = true; sub_paths->left = true;
} }
} else if (path == inst->path_cache.right) { } else if (path == inst->path_cache.right) {
if (sub_paths.right) { if (sub_paths->right) {
duplicate = true; duplicate = true;
} else { } else {
sub_paths.right = true; sub_paths->right = true;
} }
} else if (path == inst->path_cache.gamepad) { } else if (path == inst->path_cache.gamepad) {
if (sub_paths.gamepad) { if (sub_paths->gamepad) {
duplicate = true; duplicate = true;
} else { } else {
sub_paths.gamepad = true; sub_paths->gamepad = true;
} }
} else { } else {
const char *str = NULL; const char *str = NULL;
@ -347,19 +349,19 @@ subaction_path_no_dups(struct oxr_logger *log,
} }
extern "C" XrResult XrResult
oxr_verify_subaction_paths_create(struct oxr_logger *log, oxr_verify_subaction_paths_create(struct oxr_logger *log,
struct oxr_instance *inst, struct oxr_instance *inst,
uint32_t countSubactionPaths, uint32_t countSubactionPaths,
const XrPath *subactionPaths, const XrPath *subactionPaths,
const char *variable) const char *variable)
{ {
struct oxr_sub_paths sub_paths = {}; struct oxr_sub_paths sub_paths = {0};
for (uint32_t i = 0; i < countSubactionPaths; i++) { for (uint32_t i = 0; i < countSubactionPaths; i++) {
XrPath path = subactionPaths[i]; XrPath path = subactionPaths[i];
XrResult ret = subaction_path_no_dups(log, inst, sub_paths, XrResult ret = subaction_path_no_dups(log, inst, &sub_paths,
path, variable, i); path, variable, i);
if (ret != XR_SUCCESS) { if (ret != XR_SUCCESS) {
return ret; return ret;
@ -369,7 +371,7 @@ oxr_verify_subaction_paths_create(struct oxr_logger *log,
return XR_SUCCESS; return XR_SUCCESS;
} }
extern "C" XrResult XrResult
oxr_verify_subaction_path_sync(struct oxr_logger *log, oxr_verify_subaction_path_sync(struct oxr_logger *log,
struct oxr_instance *inst, struct oxr_instance *inst,
XrPath path, XrPath path,
@ -391,7 +393,7 @@ oxr_verify_subaction_path_sync(struct oxr_logger *log,
index, str); index, str);
} }
extern "C" XrResult XrResult
oxr_verify_subaction_path_get(struct oxr_logger *log, oxr_verify_subaction_path_get(struct oxr_logger *log,
struct oxr_instance *inst, struct oxr_instance *inst,
XrPath path, XrPath path,
@ -399,7 +401,7 @@ oxr_verify_subaction_path_get(struct oxr_logger *log,
struct oxr_sub_paths *out_sub_paths, struct oxr_sub_paths *out_sub_paths,
const char *variable) const char *variable)
{ {
struct oxr_sub_paths sub_paths = {}; struct oxr_sub_paths sub_paths = {0};
if (path == XR_NULL_PATH) { if (path == XR_NULL_PATH) {
sub_paths.any = true; sub_paths.any = true;
@ -452,7 +454,7 @@ oxr_verify_subaction_path_get(struct oxr_logger *log,
* *
*/ */
extern "C" XrResult XrResult
oxr_verify_XrSessionCreateInfo(struct oxr_logger *log, oxr_verify_XrSessionCreateInfo(struct oxr_logger *log,
const struct oxr_instance *inst, const struct oxr_instance *inst,
const XrSessionCreateInfo *createInfo) const XrSessionCreateInfo *createInfo)
@ -517,7 +519,7 @@ oxr_verify_XrSessionCreateInfo(struct oxr_logger *log,
#ifdef XR_USE_PLATFORM_XLIB #ifdef XR_USE_PLATFORM_XLIB
extern "C" XrResult XrResult
oxr_verify_XrGraphicsBindingOpenGLXlibKHR( oxr_verify_XrGraphicsBindingOpenGLXlibKHR(
struct oxr_logger *log, const XrGraphicsBindingOpenGLXlibKHR *next) struct oxr_logger *log, const XrGraphicsBindingOpenGLXlibKHR *next)
{ {
@ -534,7 +536,7 @@ oxr_verify_XrGraphicsBindingOpenGLXlibKHR(
#ifdef XR_USE_GRAPHICS_API_VULKAN #ifdef XR_USE_GRAPHICS_API_VULKAN
extern "C" XrResult XrResult
oxr_verify_XrGraphicsBindingVulkanKHR(struct oxr_logger *log, oxr_verify_XrGraphicsBindingVulkanKHR(struct oxr_logger *log,
const XrGraphicsBindingVulkanKHR *next) const XrGraphicsBindingVulkanKHR *next)
{ {