Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
#ifndef DEFAULTINPUT_H_INCLUDED__
#define DEFAULTINPUT_H_INCLUDED__
char GetDefaultInput(const char *prompt, char defaultchar, unsigned timeout);
#endif // DEFAULTINPUT_H_INCLUDED__
#include "defaultinput.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
// The calling convention of thread routines on Windows is required to be
// __stdcall.
#if defined(_WIN32) || \
defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
#define BASICTHREAD __stdcall
#else // POSIX
#define BASICTHREAD
#endif
typedef void *basic_thread_t;
typedef void *(BASICTHREAD *basic_thread_routine_t)(void *);
bool CreateBasicThread(basic_thread_t *const thread_ptr,
basic_thread_routine_t start_routine, void *arg_ptr);
int FastestBasicThread(basic_thread_t thread_array[2]);
void SleepSec(unsigned s);
void *BASICTHREAD Timeout(void *arg);
void *BASICTHREAD ReadInput(void *arg);
char GetDefaultInput(const char *prompt, char defaultchar, unsigned timeout) {
char ch = 0;
basic_thread_t thread_ids[2] = {0};
fputs(prompt, stdout);
fflush(stdout);
CreateBasicThread(&thread_ids[0], Timeout, &timeout);
CreateBasicThread(&thread_ids[1], ReadInput, &ch);
if (FastestBasicThread(thread_ids) == 0) {
printf("%c\n", defaultchar);
return defaultchar;
}
return ch;
}
void *BASICTHREAD Timeout(void *arg) {
unsigned *parg = arg;
SleepSec(*parg);
return NULL;
}
void *BASICTHREAD ReadInput(void *arg) {
char *parg = arg;
*parg = getchar();
while (*parg != '\n' && getchar() != '\n')
;
return NULL;
}
#if defined(_WIN32) || \
defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x601
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
bool CreateBasicThread(basic_thread_t *const thread_ptr,
basic_thread_routine_t start_routine, void *arg_ptr) {
return (*thread_ptr = (basic_thread_t)CreateThread(
NULL, 0u, (LPTHREAD_START_ROUTINE)start_routine, arg_ptr, 0u,
NULL)) != NULL;
}
int FastestBasicThread(basic_thread_t thread_array[2]) {
int found_thread = -1;
do {
Sleep(1u);
for (int i = 0; i < 2; ++i) {
if (WaitForSingleObject(thread_array[i], 0u) == 0u) {
found_thread = i;
break;
}
}
} while (found_thread == -1);
TerminateThread(thread_array[!found_thread], 0);
for (int i = 0; i < 2; ++i)
CloseHandle(thread_array[i]);
return found_thread;
}
void SleepSec(unsigned s) { Sleep(s * (DWORD)1000U); }
#else // POSIX
#include <unistd.h>
#if defined(_POSIX_THREADS) && _POSIX_THREADS == 200809L
#include <pthread.h>
#include <signal.h>
#else
#error POSIX Threads Not Supported.
#endif
bool CreateBasicThread(basic_thread_t *const thread_ptr,
basic_thread_routine_t start_routine, void *arg_ptr) {
pthread_t thread = 0;
bool ret = pthread_create(&thread, NULL, start_routine, arg_ptr) == 0;
*thread_ptr = (basic_thread_t)(uintptr_t)thread;
return ret;
}
int FastestBasicThread(basic_thread_t thread_array[2]) {
int found_thread = -1;
do {
usleep(1000u);
for (int i = 0; i < 2; ++i) {
if (pthread_kill((pthread_t)(uintptr_t)thread_array[i], 0) != 0) {
found_thread = i;
break;
}
}
} while (found_thread == -1);
pthread_join((pthread_t)(uintptr_t)thread_array[found_thread], NULL);
pthread_cancel((pthread_t)(uintptr_t)thread_array[!found_thread]);
return found_thread;
}
void SleepSec(unsigned s) { sleep(s); }
#endif // platforms
#include "defaultinput.h"
#include <stdio.h>
int main(void) {
char ch = GetDefaultInput("Ja oder Nein? [j/n]: ", 'j', 2);
printf("%c\n", ch);
return 0;
}
Vermutlich zu kompliziert, sollte aber funktionieren.
Ich finde, hier wäre aber ein enthusiastischeres, dickes Dankeschön fällig gewesen. Just my 2 cents...Jo, läuft. Von der Idee her nicht schlecht.
Ich finde, hier wäre aber ein enthusiastischeres, dickes Dankeschön fällig gewesen. Just my 2 cents...
Ja nee is klar. Wenn ich nicht wüsste von wem das kommt ...kann er froh sein das er überhaupt
Ja nee is klar. Wenn ich nicht wüsste von wem das kommt ...
ist das einzige Benefit was ich ziehen kann eine Rückmeldung ob das was ich da verbrochen habe irgend einen Sinn macht.
0 Feedback --> Ignore List fürs nächste mal
die ganze Unzulänglichkeiten um die Ohren wirft
Ne Dankesrede brauch ich allerdings tatsächlich nicht