Last active
November 11, 2023 07:29
-
-
Save KiJeong-Lim/1840989b185544d0dc4fa817c539a184 to your computer and use it in GitHub Desktop.
scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "scratch.h" | |
int delta(const char *const msg) | |
{ | |
char lhs[32] = {}; | |
float rhs = 0.0; | |
printf("\n[ECHO] %s\n", msg); | |
sscanf(msg, "%[a-zA-Z0-9] = %f", lhs, &rhs); | |
printf("lhs: %s, rhs: %f", lhs, rhs); | |
return 0; | |
} | |
int main(void) | |
{ | |
IO comm; | |
comm.set_delta(delta); | |
comm.run_delta(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "scratch.h" | |
IO::IO() | |
{ | |
delta = NULL; | |
clear(); | |
} | |
IO::IO(const IO &other) | |
{ | |
for (int i = 0; i < len(buffer); i++) { | |
buffer[i] = other.buffer[i]; | |
} | |
cursor = other.cursor; | |
result = other.result; | |
theend = other.theend; | |
delta = other.delta; | |
} | |
IO::~IO() | |
{ | |
} | |
void IO::clear() | |
{ | |
for (int i = 0; i < len(buffer); i++) { | |
buffer[i] = '\0'; | |
} | |
cursor = 0; | |
theend = 0; | |
} | |
bool IO::put_char(const int ch) | |
{ | |
switch (ch) { | |
default: | |
if (cursor < 0) { | |
result = NULL; | |
return false; | |
} | |
if (cursor > theend) { | |
result = NULL; | |
return false; | |
} | |
if (theend + 1 >= len(buffer)) { | |
result = NULL; | |
return false; | |
} | |
for (int i = theend; i >= cursor; i--) { | |
buffer[i + 1] = buffer[i]; | |
} | |
buffer[cursor++] = ch; | |
buffer[++theend] = '\0'; | |
print(); | |
result = NULL; | |
return false; | |
case '\b': | |
if (cursor > theend) { | |
result = NULL; | |
return false; | |
} | |
if (theend + 1 >= len(buffer)) { | |
result = NULL; | |
return false; | |
} | |
if (cursor <= 0) { | |
result = NULL; | |
return false; | |
} | |
for (int i = --cursor; i < theend; i++) { | |
buffer[i] = buffer[i + 1]; | |
} | |
if (theend > 0) { | |
buffer[theend--] = '\0'; | |
} | |
print(); | |
result = NULL; | |
return false; | |
case '\n': | |
case '\r': | |
result = buffer; | |
return true; | |
case '\0': | |
return false; | |
case ESC: | |
clear(); | |
printf("\n"); | |
result = NULL; | |
return true; | |
case 224: | |
if (theend + 1 >= len(buffer)) { | |
result = NULL; | |
return false; | |
} | |
switch (getch()) { | |
case LEFT_DIRECTION: | |
if (cursor > 0) { | |
cursor--; | |
} | |
print(); | |
result = NULL; | |
return false; | |
case RIGHT_DIRECTION: | |
if (cursor < theend) { | |
cursor++; | |
} | |
print(); | |
result = NULL; | |
return false; | |
} | |
} | |
return false; | |
} | |
void IO::sync(char *&msg) | |
{ | |
msg = result; | |
} | |
void IO::set_delta(int (*const delta)(const char *msg)) | |
{ | |
this->delta = delta; | |
} | |
int IO::run_delta() | |
{ | |
char *msg = NULL; | |
int ch = '\0'; | |
bool entered = false; | |
int result = 0; | |
while ((ch = getch()) != ESC) { | |
entered = put_char(ch); | |
if (entered) { | |
sync(msg); | |
result = delta(msg); | |
clear(); | |
break; | |
} | |
} | |
return result; | |
} | |
void IO::print() | |
{ | |
int i = 0; | |
printf("\r"); | |
for (i = 0; i < len(buffer); i++) { | |
printf(" "); | |
} | |
printf("\r"); | |
for (i = 0; i < cursor; i++) { | |
printf(" "); | |
} | |
for (i = cursor; i < theend; i++) { | |
printf("%c", buffer[i]); | |
} | |
buffer[i] = '\0'; | |
printf("\r"); | |
for (int i = 0; i < cursor; i++) { | |
printf("%c", buffer[i]); | |
} | |
fflush(stdout); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SCRATCH_H | |
#define SCRATCH_H | |
#include <iostream> | |
#include <cstdlib> | |
#include <cstring> | |
#include <conio.h> | |
#define len(xs) (sizeof(xs)/sizeof(*(xs))) | |
#define ESC 27 | |
#define LEFT_DIRECTION 75 | |
#define RIGHT_DIRECTION 77 | |
class IO { | |
char buffer[64]; | |
int cursor; | |
int theend; | |
char *result; | |
int (*delta)(const char *msg); | |
public: | |
IO(); | |
IO(const IO &other); | |
~IO(); | |
void clear(void); | |
bool put_char(int ch); | |
void sync(char *&msg); | |
void set_delta(int (*delta)(const char *msg)); | |
int run_delta(void); | |
private: | |
void print(void); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment