Browse Source

feat: short code string convert api

master
Dnomd343 1 year ago
parent
commit
e34ec3b6f8
  1. 24
      src/klotski/ffi/codec.cc
  2. 10
      src/klotski/ffi/klotski.h
  3. 47
      src/main.c

24
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;

10
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);

47
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;
}

Loading…
Cancel
Save