From 08473a97df058f89664ce3700b36900b390087a8 Mon Sep 17 00:00:00 2001 From: Conlan Wesson Date: Sun, 14 Apr 2024 10:34:13 -0700 Subject: [PATCH] Add TIME fingerprint --- README.md | 4 ++ src/FingerprintStrategy.cpp | 2 + src/fingerprint/FingerprintTIME.cpp | 68 +++++++++++++++++++++++ src/fingerprint/include/FingerprintTIME.h | 26 +++++++++ 4 files changed, 100 insertions(+) create mode 100644 src/fingerprint/FingerprintTIME.cpp create mode 100644 src/fingerprint/include/FingerprintTIME.h diff --git a/README.md b/README.md index 3f9415a..941a434 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,12 @@ Instructions from dynamic Funges run with the same stack as the IP that called t `TERM` [Terminal extension](http://www.rcfunge98.com/rcfunge2_manual.html#TERM). +`TIME` [Time and Date functions](http://www.rcfunge98.com/rcfunge2_manual.html#TIME). + `TOYS` [Standard Toys](https://github.com/catseye/Funge-98/blob/master/library/TOYS.markdown). +`TRDS`[IP travel in time and space](http://www.rcfunge98.com/rcfunge2_manual.html#TRDS), backward time travel not supported. + ## Debugger The Funge++ debugger, known as defunge, can be run on any Befunge program by specifying the `-g` command line argument. diff --git a/src/FingerprintStrategy.cpp b/src/FingerprintStrategy.cpp index c8b6459..94c397f 100644 --- a/src/FingerprintStrategy.cpp +++ b/src/FingerprintStrategy.cpp @@ -32,6 +32,7 @@ #include "FingerprintREFC.h" #include "FingerprintSTRN.h" #include "FingerprintSUBR.h" +#include "FingerprintTIME.h" #include "FingerprintTOYS.h" #include "FingerprintTRDS.h" #include "FungeUniverse.h" @@ -91,6 +92,7 @@ Fingerprint* FingerprintStrategy::loadBuiltin(uint64_t fingerprint){ case 0x52454643: fing = new FingerprintREFC(runner); break; case 0x5354524E: fing = new FingerprintSTRN(runner); break; case 0x53554252: fing = new FingerprintSUBR(runner); break; + case 0x54494D45: fing = new FingerprintTIME(runner); break; case 0x544F5953: fing = new FingerprintTOYS(runner); break; case 0x54524453: fing = new FingerprintTRDS(runner); break; case FingerprintDynamic::ID: diff --git a/src/fingerprint/FingerprintTIME.cpp b/src/fingerprint/FingerprintTIME.cpp new file mode 100644 index 0000000..52d25d4 --- /dev/null +++ b/src/fingerprint/FingerprintTIME.cpp @@ -0,0 +1,68 @@ +/** + * @file FingerprintTIME.cpp + * Time and Date functions. + * @author Conlan Wesson + */ + +#include "FingerprintTIME.h" +#include + +namespace Funge { + +FingerprintTIME::FingerprintTIME(FungeRunner& r) : + Fingerprint(r, {'D', 'F', 'G', 'H', 'L', 'M', 'O', 'S', 'W', 'Y'}), + gmt(false) +{} + +FungeError FingerprintTIME::execute(inst_t cmd){ + const std::tm* t = getTime(); + if(t == nullptr){ + return ERROR_UNSPEC; + } + switch(cmd){ + case 'D': + stack.top().push(t->tm_mday); + break; + case 'F': + stack.top().push(t->tm_yday); + break; + case 'G': + gmt = true; + break; + case 'H': + stack.top().push(t->tm_hour); + break; + case 'L': + gmt = false; + break; + case 'M': + stack.top().push(t->tm_min); + break; + case 'O': + stack.top().push(t->tm_mon+1); + break; + case 'S': + stack.top().push(t->tm_sec); + break; + case 'W': + stack.top().push(t->tm_wday+1); + break; + case 'Y': + stack.top().push(t->tm_year+1900); + break; + default: + return ERROR_UNIMP; + } + return ERROR_NONE; +} + +const std::tm* FingerprintTIME::getTime(){ + std::time_t t = std::time(nullptr); + if(gmt){ + return std::gmtime(&t); + }else{ + return std::localtime(&t); + } +} + +} diff --git a/src/fingerprint/include/FingerprintTIME.h b/src/fingerprint/include/FingerprintTIME.h new file mode 100644 index 0000000..a8b2c19 --- /dev/null +++ b/src/fingerprint/include/FingerprintTIME.h @@ -0,0 +1,26 @@ +/** + * @file FingerprintTIME.h + * Time and Date functions. + * @author Conlan Wesson + */ + +#pragma once + +#include "Fingerprint.h" + +namespace Funge { + +class FingerprintTIME : public Fingerprint { + public: + explicit FingerprintTIME(FungeRunner& r); + virtual ~FingerprintTIME() = default; + + virtual FungeError execute(inst_t cmd) override; + + private: + bool gmt; + + const std::tm* getTime(); +}; + +}