Browse Source

feat: codec convert api

master
Dnomd343 2 years ago
parent
commit
a5eb0ca84f
  1. 100
      src/klotski/ffi/codec.cc
  2. 22
      src/klotski/ffi/klotski.h
  3. 22
      src/main.c

100
src/klotski/ffi/codec.cc

@ -1,26 +1,106 @@
#include "klotski.h" #include "klotski.h"
#include "all_cases.h"
#include "short_code.h" #include "short_code.h"
using klotski::RawCode; using klotski::RawCode;
using klotski::ShortCode; using klotski::ShortCode;
using klotski::CommonCode; using klotski::CommonCode;
bool raw_code_check(uint64_t code) { using klotski::AllCases;
return RawCode::check(code); using klotski::BasicRanges;
////////////////////////////////////////////////////////////////////////////////
void short_code_enable() {
ShortCode::speed_up(ShortCode::NORMAL);
} }
bool short_code_check(uint32_t code) { void short_code_enable_fast() {
return ShortCode::check(code); ShortCode::speed_up(ShortCode::FAST);
} }
bool common_code_check(uint64_t code) { bool is_short_code_available() {
return CommonCode::check(code); return BasicRanges::status() == BasicRanges::AVAILABLE;
} }
void short_code_speed_up() { bool is_short_code_available_fast() {
ShortCode::speed_up(ShortCode::NORMAL); 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;
}
////////////////////////////////////////////////////////////////////////////////

22
src/klotski/ffi/klotski.h

@ -21,12 +21,24 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern bool raw_code_check(uint64_t code); extern void short_code_enable();
extern bool short_code_check(uint32_t code); extern void short_code_enable_fast();
extern bool common_code_check(uint64_t code); 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 #ifdef __cplusplus
} }
#endif #endif

22
src/main.c

@ -6,8 +6,26 @@ int main() {
// tmain(); // tmain();
bool ret = common_code_check(0x1A9BF0C00); // bool ret = common_code_check(0x1A9BF0C00);
printf("result -> %d\n", ret); // 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"); // printf("cli exit\n");
return 0; return 0;

Loading…
Cancel
Save