Browse Source

feat: common code string convert api

master
Dnomd343 1 year ago
parent
commit
6b59f64eaa
  1. 30
      src/klotski/ffi/codec.cc
  2. 9
      src/klotski/ffi/klotski.h
  3. 31
      src/main.c

30
src/klotski/ffi/codec.cc

@ -1,3 +1,4 @@
#include <cstring>
#include "klotski.h"
#include "all_cases.h"
#include "short_code.h"
@ -104,3 +105,32 @@ bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) {
}
////////////////////////////////////////////////////////////////////////////////
bool common_code_to_string(uint64_t common_code, char common_code_str[]) {
if (!CommonCode::check(common_code)) {
return false;
}
std::string str = CommonCode::unsafe_create(common_code).to_string(false);
strcpy(common_code_str, str.c_str());
return true;
}
bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) {
if (!CommonCode::check(common_code)) {
return false;
}
std::string str = CommonCode::unsafe_create(common_code).to_string(true);
strcpy(common_code_str, str.c_str());
return true;
}
bool common_code_from_string(const char common_code_str[], uint64_t *common_code) {
try {
*common_code = CommonCode::from_string(common_code_str).unwrap();
} catch (...) {
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////

9
src/klotski/ffi/klotski.h

@ -30,8 +30,6 @@ extern "C" {
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);
@ -39,6 +37,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 10-bytes
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);
#ifdef __cplusplus
}
#endif

31
src/main.c

@ -13,18 +13,35 @@ int main() {
uint32_t short_code;
uint64_t common_code = 0x1A9BF0C00;
short_code_enable_fast();
if (!common_code_to_raw_code(common_code, &raw_code)) {
// 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];
if (!common_code_to_string(common_code, common_code_str)) {
printf("error\n");
} else {
printf("raw code -> %015lX\n", raw_code);
printf("string -> %s\n", common_code_str);
}
if (!common_code_to_short_code(common_code, &short_code)) {
if (!common_code_to_string_shorten(common_code, common_code_str)) {
printf("error\n");
} else {
printf("string shorten -> %s\n", common_code_str);
}
if (!common_code_from_string(common_code_str, &common_code)) {
printf("error\n");
} else {
printf("short code -> %d\n", short_code);
printf("common code -> %09lX\n", common_code);
}
// printf("cli exit\n");

Loading…
Cancel
Save