1. 4FipS.com
  2. Photos
  3. Videos
  4. Code
  5. Forums
  6. pfQuizzz
  7. About
/*
(c) 2004-14 Filip Stoklas, aka FipS, www.4FipS.com
THIS CODE IS FREE - LICENSED UNDER THE MIT LICENSE
*/

#include "fs_common_os.h"
#include "fs_common.h"

#include <cstdarg> // va_list, ...

#if defined(FS_PLATFORM_WIN)
#   define WIN32_LEAN_AND_MEAN
#   include <windows.h>
#endif

#if defined(FS_PLATFORM_OSX)
#   include <CoreFoundation/CFUserNotification.h>
#endif

namespace fs { namespace os {

void debug_print(const char *text) FS_NOEXCEPT
{
#if defined(FS_PLATFORM_WIN)
    OutputDebugStringA(text);
#else
    printf("%s", text);
#endif
}

bool show_yes_no_dialog(const char *title, const char *text, bool def_ans) FS_NOEXCEPT
{
#if defined(FS_PLATFORM_WIN)

    return MessageBoxA(nullptr, text, title, MB_ICONQUESTION | MB_YESNO) == IDYES;

#elif defined(FS_PLATFORM_OSX)

    CFStringRef title_ref = CFStringCreateWithCString(nullptr, title, kCFStringEncodingASCII);
    CFStringRef text_ref = CFStringCreateWithCString(nullptr, text, kCFStringEncodingASCII);

    CFOptionFlags res = 0;

    CFUserNotificationDisplayAlert(
     0.0, // timeout
     kCFUserNotificationNoteAlertLevel, // flags
     nullptr, // iconURL
     nullptr, // soundURL
     nullptr, // localizationURL
     title_ref, // alertHeader,
     text_ref, // alertMessage,
     CFSTR("Yes"), // defaultButtonTitle,
     CFSTR("No"), // alternateButtonTitle,
     nullptr, // otherButtonTitle,
     &res
    );

    CFRelease(title_ref);
    CFRelease(text_ref);

    return res == kCFUserNotificationDefaultResponse;

#else

    FS_TRACE(("[!] show_yes_no_dialog() -> %d", int(def_ans)));
    return def_ans;

#endif
}

}} // namespace fs::os

namespace fs { namespace str {

void vsnprintf_trunc(char *buf, size_t buf_size, const char *fmt, va_list args) FS_NOEXCEPT
{
#if defined(FS_PLATFORM_WIN)
    // if truncated, '\0' is automatically appended
    _vsnprintf_s(buf, buf_size, _TRUNCATE, fmt, args);
#else
    vsnprintf(buf, buf_size, fmt, args); // TODO
#endif
}

void snprintf_trunc(char *buf, size_t buf_size, const char *fmt, ...) FS_NOEXCEPT
{
    va_list args;
    va_start(args, fmt);
    vsnprintf_trunc(buf, buf_size, fmt, args);
    va_end(args);
}

}} // namespace fs::str