Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support Windows x64 MSVC openssl build #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
381 changes: 221 additions & 160 deletions CMakeLists.txt

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions ansi_terminal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
// Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif

static HANDLE stdoutHandle, stdinHandle;
static DWORD outModeInit, inModeInit;

void setupConsole(void) {
DWORD outMode = 0, inMode = 0;
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
stdinHandle = GetStdHandle(STD_INPUT_HANDLE);

if(stdoutHandle == INVALID_HANDLE_VALUE || stdinHandle == INVALID_HANDLE_VALUE) {
exit(GetLastError());
}

if(!GetConsoleMode(stdoutHandle, &outMode) || !GetConsoleMode(stdinHandle, &inMode)) {
exit(GetLastError());
}

outModeInit = outMode;
inModeInit = inMode;

// Enable ANSI escape codes
outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

// Set stdin as no echo and unbuffered
inMode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);

if(!SetConsoleMode(stdoutHandle, outMode) || !SetConsoleMode(stdinHandle, inMode)) {
exit(GetLastError());
}
}

void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");

// Reset console mode
if(!SetConsoleMode(stdoutHandle, outModeInit) || !SetConsoleMode(stdinHandle, inModeInit)) {
exit(GetLastError());
}
}
#else

static struct termios orig_term;
static struct termios new_term;

void setupConsole(void) {
tcgetattr(STDIN_FILENO, &orig_term);
new_term = orig_term;

new_term.c_lflag &= ~(ICANON | ECHO);

tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
}

void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");

// Reset console mode
tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
}
#endif

void getCursorPosition(int *row, int *col) {
printf("\x1b[6n");
char buff[128];
int indx = 0;
for(;;) {
int cc = getchar();
buff[indx] = (char)cc;
indx++;
if(cc == 'R') {
buff[indx + 1] = '\0';
break;
}
}
sscanf(buff, "\x1b[%d;%dR", row, col);
fseek(stdin, 0, SEEK_END);
}
13 changes: 13 additions & 0 deletions ansi_terminal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* adopted from https://github.com/sol-prog/ansi-escape-codes-windows-posix-terminals-c-programming-examples */

#define cRED "\033[1;31m"
#define cDRED "\033[0;31m"
#define cGREEN "\033[1;32m"
#define cDGREEN "\033[0;32m"
#define cBLUE "\033[1;34m"
#define cDBLUE "\033[0;34m"
#define cNORM "\033[m"

void setupConsole(void);
void restoreConsole(void);
void getCursorPosition(int *row, int *col);
36 changes: 36 additions & 0 deletions benchmark/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#ifdef _MSC_VER
/** init the clock */
#define TIMER_INIT \
LARGE_INTEGER frequency; \
LARGE_INTEGER t1,t2; \
double elapsedTime; \
QueryPerformanceFrequency(&frequency);

#define TIMER_INIT_EX(CT) TIMER_INIT
/** start the performance timer */
#define TIMER_START QueryPerformanceCounter(&t1);
/** stop the performance timer and store the result in elapsedTime. */
#define TIMER_STOP \
QueryPerformanceCounter(&t2); \
elapsedTime=(double)(t2.QuadPart-t1.QuadPart)* 1000000 /frequency.QuadPart;

#else
#include <sys/time.h>
#include <time.h>

#define TIMER_INIT \
struct timespec ts1, ts2; \
struct timeval tv1, tv2, delta; \
double elapsedTime; \
clockid_t clock_type = CLOCK_MONOTONIC;

#define TIMER_INIT_EX(CT) TIMER_INIT clock_type = CT;
#define TIMER_START clock_gettime(clock_type, &ts1);
#define TIMER_STOP clock_gettime(clock_type, &ts2);\
TIMESPEC_TO_TIMEVAL(&tv1, &ts1);\
TIMESPEC_TO_TIMEVAL(&tv2, &ts2);\
timersub(&tv2, &tv1, &delta); \
elapsedTime = (double)delta.tv_sec * 1000000 + (double)delta.tv_usec ;

#endif
Loading