From e34ec3b6f8fb98e8788cdd12a60501888e75d8a3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 27 Jan 2023 18:05:09 +0800 Subject: [PATCH] feat: short code string convert api --- src/klotski/ffi/codec.cc | 24 ++++++++++++++++++++ src/klotski/ffi/klotski.h | 10 +++++++-- src/main.c | 47 +++++++++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/klotski/ffi/codec.cc b/src/klotski/ffi/codec.cc index b859223..8606ef2 100644 --- a/src/klotski/ffi/codec.cc +++ b/src/klotski/ffi/codec.cc @@ -106,6 +106,30 @@ bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) { //////////////////////////////////////////////////////////////////////////////// +const uint32_t short_code_string_size = 6; + +bool short_code_to_string(uint32_t short_code, char short_code_str[]) { + if (!ShortCode::check(short_code)) { + return false; + } + std::string str = ShortCode::unsafe_create(short_code).to_string(); + strcpy(short_code_str, str.c_str()); + return true; +} + +bool short_code_from_string(const char short_code_str[], uint32_t *short_code) { + try { + *short_code = ShortCode::from_string(short_code_str).unwrap(); + } catch (...) { + return false; + } + return true; +} + +//////////////////////////////////////////////////////////////////////////////// + +const uint32_t common_code_string_size = 10; + bool common_code_to_string(uint64_t common_code, char common_code_str[]) { if (!CommonCode::check(common_code)) { return false; diff --git a/src/klotski/ffi/klotski.h b/src/klotski/ffi/klotski.h index b48b4dd..ec2d715 100644 --- a/src/klotski/ffi/klotski.h +++ b/src/klotski/ffi/klotski.h @@ -17,10 +17,12 @@ extern "C" { } #endif -/// klotski codec interface +/// -------------------------------- klotski codec interface -------------------------------- + #ifdef __cplusplus extern "C" { #endif + /// short code warm up api extern void short_code_enable(); extern void short_code_enable_fast(); extern bool is_short_code_available(); @@ -37,9 +39,13 @@ extern "C" { 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); - // TODO: short code string convert + /// output char[] buffer at least 6-bytes + extern const uint32_t short_code_string_size; + extern bool short_code_to_string(uint32_t short_code, char short_code_str[]); + extern bool short_code_from_string(const char short_code_str[], uint32_t *short_code); /// output char[] buffer at least 10-bytes + extern const uint32_t common_code_string_size; extern bool common_code_to_string(uint64_t common_code, char common_code_str[]); extern bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]); extern bool common_code_from_string(const char common_code_str[], uint64_t *common_code); diff --git a/src/main.c b/src/main.c index ae5d66f..43e4d6c 100644 --- a/src/main.c +++ b/src/main.c @@ -11,23 +11,24 @@ int main() { 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); -// } - - char common_code_str[10]; +// uint64_t common_code = 0x1A9BF0C00; + uint64_t common_code = 0x4FEA13400; + + 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); + } + + char common_code_str[common_code_string_size]; if (!common_code_to_string(common_code, common_code_str)) { printf("error\n"); } else { @@ -44,6 +45,18 @@ int main() { printf("common code -> %09lX\n", common_code); } + char short_code_str[short_code_string_size]; + if (!short_code_to_string(short_code, short_code_str)) { + printf("error\n"); + } else { + printf("string -> %s\n", short_code_str); + } + if (!short_code_from_string(short_code_str, &short_code)) { + printf("error\n"); + } else { + printf("short code -> %d\n", short_code); + } + // printf("cli exit\n"); return 0; }