From a5eb0ca84f4cbae4ba03b3eff07657e7522edf4a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 27 Jan 2023 17:35:16 +0800 Subject: [PATCH] feat: codec convert api --- src/klotski/ffi/codec.cc | 100 ++++++++++++++++++++++++++++++++++---- src/klotski/ffi/klotski.h | 22 +++++++-- src/main.c | 22 ++++++++- 3 files changed, 127 insertions(+), 17 deletions(-) diff --git a/src/klotski/ffi/codec.cc b/src/klotski/ffi/codec.cc index df78654..4b8d2a4 100644 --- a/src/klotski/ffi/codec.cc +++ b/src/klotski/ffi/codec.cc @@ -1,26 +1,106 @@ #include "klotski.h" +#include "all_cases.h" #include "short_code.h" using klotski::RawCode; using klotski::ShortCode; using klotski::CommonCode; -bool raw_code_check(uint64_t code) { - return RawCode::check(code); +using klotski::AllCases; +using klotski::BasicRanges; + +//////////////////////////////////////////////////////////////////////////////// + +void short_code_enable() { + ShortCode::speed_up(ShortCode::NORMAL); } -bool short_code_check(uint32_t code) { - return ShortCode::check(code); +void short_code_enable_fast() { + ShortCode::speed_up(ShortCode::FAST); } -bool common_code_check(uint64_t code) { - return CommonCode::check(code); +bool is_short_code_available() { + return BasicRanges::status() == BasicRanges::AVAILABLE; } -void short_code_speed_up() { - ShortCode::speed_up(ShortCode::NORMAL); +bool is_short_code_available_fast() { + return AllCases::status() == AllCases::AVAILABLE; } -void short_code_speed_up_fast() { - ShortCode::speed_up(ShortCode::FAST); +//////////////////////////////////////////////////////////////////////////////// + +bool raw_code_check(uint64_t raw_code) { + return RawCode::check(raw_code); } + +bool short_code_check(uint32_t short_code) { + return ShortCode::check(short_code); +} + +bool common_code_check(uint64_t common_code) { + return CommonCode::check(common_code); +} + +//////////////////////////////////////////////////////////////////////////////// + +bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) { + if (!RawCode::check(raw_code)) { + return false; + } + *short_code = ShortCode::from_common_code( + CommonCode::from_raw_code(RawCode::unsafe_create(raw_code)) + ).unwrap(); + return true; +} + +bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) { + if (!ShortCode::check(short_code)) { + return false; + } + *raw_code = RawCode::from_common_code( + CommonCode::from_short_code(ShortCode::unsafe_create(short_code)) + ).unwrap(); + return true; +} + +bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) { + if (!RawCode::check(raw_code)) { + return false; + } + *common_code = CommonCode::from_raw_code( + RawCode::unsafe_create(raw_code) + ).unwrap(); + return true; +} + +bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) { + if (!CommonCode::check(common_code)) { + return false; + } + *raw_code = RawCode::from_common_code( + CommonCode::unsafe_create(common_code) + ).unwrap(); + return true; +} + +bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) { + if (!ShortCode::check(short_code)) { + return false; + } + *common_code = CommonCode::from_short_code( + ShortCode::unsafe_create(short_code) + ).unwrap(); + return true; +} + +bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) { + if (!CommonCode::check(common_code)) { + return false; + } + *short_code = ShortCode::from_common_code( + CommonCode::unsafe_create(common_code) + ).unwrap(); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/klotski/ffi/klotski.h b/src/klotski/ffi/klotski.h index 0c40edc..6d79e0f 100644 --- a/src/klotski/ffi/klotski.h +++ b/src/klotski/ffi/klotski.h @@ -21,12 +21,24 @@ extern "C" { #ifdef __cplusplus extern "C" { #endif - extern bool raw_code_check(uint64_t code); - extern bool short_code_check(uint32_t code); - extern bool common_code_check(uint64_t code); + extern void short_code_enable(); + extern void short_code_enable_fast(); + extern bool is_short_code_available(); + extern bool is_short_code_available_fast(); + + extern bool raw_code_check(uint64_t raw_code); + extern bool short_code_check(uint32_t short_code); + extern bool common_code_check(uint64_t common_code); + + // TODO: string code convert + + extern bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code); + extern bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code); + extern bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code); + extern bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code); + extern bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code); + extern bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code); - extern void short_code_speed_up(); - extern void short_code_speed_up_fast(); #ifdef __cplusplus } #endif diff --git a/src/main.c b/src/main.c index 96a29de..5aeb451 100644 --- a/src/main.c +++ b/src/main.c @@ -6,8 +6,26 @@ int main() { // tmain(); - bool ret = common_code_check(0x1A9BF0C00); - printf("result -> %d\n", ret); +// bool ret = common_code_check(0x1A9BF0C00); +// printf("result -> %d\n", ret); + + uint64_t raw_code; + uint32_t short_code; + uint64_t common_code = 0x1A9BF0C00; + + short_code_enable_fast(); + + if (!common_code_to_raw_code(common_code, &raw_code)) { + printf("error\n"); + } else { + printf("raw code -> %015lX\n", raw_code); + } + + if (!common_code_to_short_code(common_code, &short_code)) { + printf("error\n"); + } else { + printf("short code -> %d\n", short_code); + } // printf("cli exit\n"); return 0;