AmendHub

Download:

cyberslak

/

lightsout

/

amendments

/

3

util: add strlcpy, win_center, win_is_dialog


cyberslak made amendment 3 about 1 month ago
--- util.c Mon Mar 3 21:44:26 2025 +++ util.c Sun Mar 9 23:44:12 2025 @@ -7,6 +7,17 @@ #define kMessageAlert 128 +void win_center(WindowPtr win) +{ + short barHeight = GetMBarHeight() * 2; // menu + title bar + short screenWidth = rect_width(&qd.screenBits.bounds); + short screenHeight = rect_height(&qd.screenBits.bounds) - barHeight; + short winWidth = rect_width(&win->portRect); + short winHeight = rect_height(&win->portRect); + + MoveWindow(win, (screenWidth - winWidth) / 2, barHeight + (screenHeight - winHeight) / 2, false); +} + void* xmalloc(size_t sz) { void* p = malloc(sz); @@ -77,6 +88,22 @@ void toolbox_init(void) TEInit(); InitDialogs(nil); InitCursor(); +} + +size_t lo_strlcpy(char* dest, const char* src, size_t len) +{ + size_t srclen = strlen(src); + size_t copylen; + + if (len == 0) + return srclen; + + len--; // null terminator + + copylen = (srclen > len) ? len : srclen; + memcpy(dest, src, copylen); + dest[copylen] = 0; + return srclen; } char* lo_strdup(const char* s) --- util.h Mon Mar 3 21:45:23 2025 +++ util.h Sun Mar 9 23:43:39 2025 @@ -12,6 +12,14 @@ typedef struct void *addr; } def_jmp_t; +inline bool win_is_dialog(WindowPtr win) +{ + WindowPeek wPeek = (WindowPeek)win; + return (wPeek->windowKind == dialogKind); +} + +void win_center(WindowPtr win); + // expand a Rect by npixels on each side inline void rect_expand(Rect *r, short npixels) { @@ -38,6 +46,8 @@ void die(const char*, ...); void warn(const char*, ...); void info(const char*, ...); +size_t lo_strlcpy(char* dest, const char* src, size_t len); + char* lo_strdup(const char* s); short lo_snprintf(char* s, size_t size, @@ -48,6 +58,7 @@ short lo_vsnprintf(char* s, size_t size, #ifndef _UTIL_IMPL_ +#define strlcpy lo_strlcpy #define vsnprintf lo_vsnprintf #define snprintf lo_snprintf #define strdup lo_strdup