Changed uiInit() to return bool instead of int, now that we've settled on using bool.

This commit is contained in:
Pietro Gagliardi 2019-05-29 21:10:44 -04:00
parent edfd5e9157
commit 19ad0d33a3
7 changed files with 30 additions and 30 deletions

View File

@ -9,7 +9,7 @@
#include "ui.h" #include "ui.h"
#include "uipriv.h" #include "uipriv.h"
static int initialized = 0; static bool initialized = false;
#define errAlreadyInitialized "libui already initialized" #define errAlreadyInitialized "libui already initialized"
#define errOptionsMustBeNULL "options parameter to uiInit() must be NULL" #define errOptionsMustBeNULL "options parameter to uiInit() must be NULL"
@ -20,33 +20,33 @@ static const char *commonInitErrors[] = {
NULL, NULL,
}; };
static int checkInitErrorLengths(uiInitError *err, const char **initErrors) static bool checkInitErrorLengths(uiInitError *err, const char **initErrors)
{ {
const char **p; const char **p;
if (initErrors == NULL) if (initErrors == NULL)
return 1; return true;
for (p = initErrors; *p != NULL; p++) for (p = initErrors; *p != NULL; p++)
if (strlen(*p) > 255) { if (strlen(*p) > 255) {
strcpy(err->Message, "[INTERNAL] uiInit() error too long: "); strcpy(err->Message, "[INTERNAL] uiInit() error too long: ");
strncat(err->Message, *p, 32); strncat(err->Message, *p, 32);
strcat(err->Message, "..."); strcat(err->Message, "...");
return 0; return false;
} }
return 1; return true;
} }
int uiInit(void *options, uiInitError *err) bool uiInit(void *options, uiInitError *err)
{ {
if (err == NULL) if (err == NULL)
return 0; return false;
if (err->Size != sizeof (uiInitError)) if (err->Size != sizeof (uiInitError))
return 0; return false;
if (!checkInitErrorLengths(err, commonInitErrors)) if (!checkInitErrorLengths(err, commonInitErrors))
return 0; return false;
if (!checkInitErrorLengths(err, uiprivSysInitErrors())) if (!checkInitErrorLengths(err, uiprivSysInitErrors()))
return 0; return false;
if (initialized) if (initialized)
return uiprivInitReturnError(err, errAlreadyInitialized); return uiprivInitReturnError(err, errAlreadyInitialized);
@ -55,19 +55,19 @@ int uiInit(void *options, uiInitError *err)
return uiprivInitReturnError(err, errOptionsMustBeNULL); return uiprivInitReturnError(err, errOptionsMustBeNULL);
if (!uiprivSysInit(options, err)) if (!uiprivSysInit(options, err))
return 0; return false;
initialized = 1; initialized = true;
return 1; return true;
} }
int uiprivInitReturnError(uiInitError *err, const char *msg) bool uiprivInitReturnError(uiInitError *err, const char *msg)
{ {
// checkInitErrorLengths() above ensures that err->Message[255] will always be '\0' // checkInitErrorLengths() above ensures that err->Message[255] will always be '\0'
strncpy(err->Message, msg, 256); strncpy(err->Message, msg, 256);
return 0; return false;
} }
int uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...) bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...)
{ {
va_list ap; va_list ap;
@ -75,7 +75,7 @@ int uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...)
va_start(ap, msg); va_start(ap, msg);
vsnprintf(err->Message, 256, msg, ap); vsnprintf(err->Message, 256, msg, ap);
va_end(ap); va_end(ap);
return 0; return false;
} }
bool uiprivCheckInitializedAndThreadImpl(const char *func) bool uiprivCheckInitializedAndThreadImpl(const char *func)

View File

@ -21,9 +21,9 @@ extern "C" {
// init.c // init.c
extern const char **uiprivSysInitErrors(void); extern const char **uiprivSysInitErrors(void);
extern int uiprivSysInit(void *options, uiInitError *err); extern bool uiprivSysInit(void *options, uiInitError *err);
extern int uiprivInitReturnError(uiInitError *err, const char *msg); extern bool uiprivInitReturnError(uiInitError *err, const char *msg);
extern int uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...); extern bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...);
extern bool uiprivCheckInitializedAndThreadImpl(const char *func); extern bool uiprivCheckInitializedAndThreadImpl(const char *func);
#define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc) #define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc)
extern bool uiprivSysCheckThread(void); extern bool uiprivSysCheckThread(void);

View File

@ -51,7 +51,7 @@ const char **uiprivSysInitErrors(void)
static pthread_t mainThread; static pthread_t mainThread;
static BOOL initialized = NO; // TODO deduplicate this from common/init.c static BOOL initialized = NO; // TODO deduplicate this from common/init.c
int uiprivSysInit(void *options, uiInitError *err) bool uiprivSysInit(void *options, uiInitError *err)
{ {
uiprivApp = [uiprivApplication sharedApplication]; uiprivApp = [uiprivApplication sharedApplication];
if (![NSApp isKindOfClass:[uiprivApplication class]]) if (![NSApp isKindOfClass:[uiprivApplication class]])
@ -66,7 +66,7 @@ int uiprivSysInit(void *options, uiInitError *err)
mainThread = pthread_self(); mainThread = pthread_self();
initialized = YES; initialized = YES;
return 1; return true;
} }
void uiMain(void) void uiMain(void)

View File

@ -78,10 +78,10 @@ TODO timers
### `uiInit()` ### `uiInit()`
```c ```c
int uiInit(void *options, uiInitError *err); bool uiInit(void *options, uiInitError *err);
``` ```
`uiInit()` initializes libui. It returns nonzero on success and zero on failure; in the event of a failure, `err` is filled with relevant information explaining the failure. `uiInit()` initializes libui. It returns `true` on success and `false` on failure; in the event of a failure, `err` is filled with relevant information explaining the failure.
`err` is required and must be properly initialized. If `err` is `NULL` or `err->Size` does not match `sizeof (uiError)`, `uiInit()` immediately returns zero without doing anything. If any of the other fields of `err` are not zero-initialized as with `memset(0)`, the behavior is undefined. `err` is required and must be properly initialized. If `err` is `NULL` or `err->Size` does not match `sizeof (uiError)`, `uiInit()` immediately returns zero without doing anything. If any of the other fields of `err` are not zero-initialized as with `memset(0)`, the behavior is undefined.

2
ui.h
View File

@ -38,7 +38,7 @@ struct uiInitError {
char Message[256]; char Message[256];
}; };
uiprivExtern int uiInit(void *options, uiInitError *err); uiprivExtern bool uiInit(void *options, uiInitError *err);
uiprivExtern void uiMain(void); uiprivExtern void uiMain(void);
uiprivExtern void uiQuit(void); uiprivExtern void uiQuit(void);
uiprivExtern void uiQueueMain(void (*f)(void *data), void *data); uiprivExtern void uiQueueMain(void (*f)(void *data), void *data);

View File

@ -9,7 +9,7 @@ const char **uiprivSysInitErrors(void)
static pthread_t mainThread; static pthread_t mainThread;
static gboolean initialized = FALSE; // TODO deduplicate this from common/init.c static gboolean initialized = FALSE; // TODO deduplicate this from common/init.c
int uiprivSysInit(void *options, uiInitError *err) bool uiprivSysInit(void *options, uiInitError *err)
{ {
GError *gerr = NULL; GError *gerr = NULL;
@ -17,11 +17,11 @@ int uiprivSysInit(void *options, uiInitError *err)
// TODO make sure this is safe // TODO make sure this is safe
strncpy(err->Message, gerr->message, 255); strncpy(err->Message, gerr->message, 255);
g_error_free(gerr); g_error_free(gerr);
return 0; return false;
} }
mainThread = pthread_self(); mainThread = pthread_self();
initialized = TRUE; initialized = TRUE;
return 1; return true;
} }
void uiMain(void) void uiMain(void)

View File

@ -69,7 +69,7 @@ const char **uiprivSysInitErrors(void)
static DWORD mainThread; static DWORD mainThread;
static BOOL initialized = FALSE; // TODO deduplicate this from common/init.c static BOOL initialized = FALSE; // TODO deduplicate this from common/init.c
int uiprivSysInit(void *options, uiInitError *err) bool uiprivSysInit(void *options, uiInitError *err)
{ {
STARTUPINFOW si; STARTUPINFOW si;
HICON hDefaultIcon; HICON hDefaultIcon;
@ -115,7 +115,7 @@ int uiprivSysInit(void *options, uiInitError *err)
*/ */
mainThread = GetCurrentThreadId(); mainThread = GetCurrentThreadId();
initialized = TRUE; initialized = TRUE;
return 1; return true;
} }
void uiMain(void) void uiMain(void)