Skip to content

Commit

Permalink
chore(core): remove km_core_keyboard_load API
Browse files Browse the repository at this point in the history
Fixes: #12497
  • Loading branch information
mcdurdin committed Dec 4, 2024
1 parent 4ddcfc9 commit 4c3b213
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 188 deletions.
2 changes: 1 addition & 1 deletion core/docs/api/background.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ typedef enum { KM_CORE_FALSE = 0, KM_CORE_TRUE = 1 } km_core_bool;
[km_core_keyboard_attrs]: keyboards#km_core_keyboard_attrs "km_core_keyboard_attrs struct"
[km_core_keyboard_key]: keyboards#km_core_keyboard_key "km_core_keyboard_key struct"
[km_core_keyboard_imx]: keyboards#km_core_keyboard_imx "km_core_keyboard_imx struct"
[km_core_keyboard_load]: keyboards#km_core_keyboard_load "km_core_keyboard_load function"
[km_core_keyboard_load_from_blob]: keyboards#km_core_keyboard_load_from_blob "km_core_keyboard_load_from_blob function"
[km_core_keyboard_dispose]: keyboards#km_core_keyboard_dispose "km_core_keyboard_dispose function"
[km_core_keyboard_get_attrs]: keyboards#km_core_keyboard_get_attrs "km_core_keyboard_get_attrs function"
[km_core_keyboard_get_key_list]: keyboards#km_core_keyboard_get_key_list "km_core_keyboard_get_key_list function"
Expand Down
2 changes: 1 addition & 1 deletion core/docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Caps Lock.
[km_core_keyboard_attrs]: keyboards#km_core_keyboard_attrs "km_core_keyboard_attrs struct"
[km_core_keyboard_key]: keyboards#km_core_keyboard_key "km_core_keyboard_key struct"
[km_core_keyboard_imx]: keyboards#km_core_keyboard_imx "km_core_keyboard_imx struct"
[km_core_keyboard_load]: keyboards#km_core_keyboard_load "km_core_keyboard_load function"
[km_core_keyboard_load_from_blob]: keyboards#km_core_keyboard_load_from_blob "km_core_keyboard_load_from_blob function"
[km_core_keyboard_dispose]: keyboards#km_core_keyboard_dispose "km_core_keyboard_dispose function"
[km_core_keyboard_get_attrs]: keyboards#km_core_keyboard_get_attrs "km_core_keyboard_get_attrs function"
[km_core_keyboard_get_key_list]: keyboards#km_core_keyboard_get_key_list "km_core_keyboard_get_key_list function"
Expand Down
27 changes: 17 additions & 10 deletions core/docs/api/keyboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,35 @@ typedef struct {

-------------------------------------------------------------------------------

# km_core_keyboard_load() {#km_core_keyboard_load}
# km_core_keyboard_load_from_blob() {#km_core_keyboard_load_from_blob}

## Description

Parse and load keyboard from the supplied path and a pointer to the loaded keyboard
into the out paramter.
Parse and load keyboard from the supplied blob and size and a pointer to the
loaded keyboard into the out paramter.

## Specification

```c
KMN_API
km_core_status
km_core_keyboard_load(km_core_path_name kb_path,
km_core_keyboard **keyboard);
km_core_keyboard_load_from_blob(const km_core_path_name kb_name,
const void* blob,
const size_t blob_size,
km_core_keyboard** keyboard);

```
## Parameters
`kb_path`
: On Windows, a UTF-16 string; on other platforms, a C string:
contains a valid path to the keyboard file.
`kb_name`
: a string with the name of the keyboard.
`blob`
: a byte array containing the content of a KMX/KMX+ file.
`blob_size`
: a size_t variable with the size of the blob in bytes.
`keyboard`
: A pointer to result variable: A pointer to the opaque keyboard
Expand Down Expand Up @@ -154,7 +161,7 @@ km_core_keyboard_load(km_core_path_name kb_path,
## Description
Free the allocated memory belonging to an opaque keyboard object previously
returned by [km_core_keyboard_load].
returned by [km_core_keyboard_load_from_blob].
## Specification
Expand Down Expand Up @@ -637,7 +644,7 @@ km_core_state_to_json(km_core_state const *state,
[km_core_keyboard_attrs]: keyboards#km_core_keyboard_attrs "km_core_keyboard_attrs struct"
[km_core_keyboard_key]: keyboards#km_core_keyboard_key "km_core_keyboard_key struct"
[km_core_keyboard_imx]: keyboards#km_core_keyboard_imx "km_core_keyboard_imx struct"
[km_core_keyboard_load]: keyboards#km_core_keyboard_load "km_core_keyboard_load function"
[km_core_keyboard_load_from_blob]: keyboards#km_core_keyboard_load_from_blob "km_core_keyboard_load_from_blob function"
[km_core_keyboard_dispose]: keyboards#km_core_keyboard_dispose "km_core_keyboard_dispose function"
[km_core_keyboard_get_attrs]: keyboards#km_core_keyboard_get_attrs "km_core_keyboard_get_attrs function"
[km_core_keyboard_get_key_list]: keyboards#km_core_keyboard_get_key_list "km_core_keyboard_get_key_list function"
Expand Down
2 changes: 1 addition & 1 deletion core/docs/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ km_core_state_options_to_json(km_core_state const *state,
[km_core_keyboard_attrs]: keyboards#km_core_keyboard_attrs "km_core_keyboard_attrs struct"
[km_core_keyboard_key]: keyboards#km_core_keyboard_key "km_core_keyboard_key struct"
[km_core_keyboard_imx]: keyboards#km_core_keyboard_imx "km_core_keyboard_imx struct"
[km_core_keyboard_load]: keyboards#km_core_keyboard_load "km_core_keyboard_load function"
[km_core_keyboard_load_from_blob]: keyboards#km_core_keyboard_load_from_blob "km_core_keyboard_load_from_blob function"
[km_core_keyboard_dispose]: keyboards#km_core_keyboard_dispose "km_core_keyboard_dispose function"
[km_core_keyboard_get_attrs]: keyboards#km_core_keyboard_get_attrs "km_core_keyboard_get_attrs function"
[km_core_keyboard_get_key_list]: keyboards#km_core_keyboard_get_key_list "km_core_keyboard_get_key_list function"
Expand Down
2 changes: 1 addition & 1 deletion core/docs/api/processor.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ enum km_core_event_code {
[km_core_keyboard_attrs]: keyboards#km_core_keyboard_attrs "km_core_keyboard_attrs struct"
[km_core_keyboard_key]: keyboards#km_core_keyboard_key "km_core_keyboard_key struct"
[km_core_keyboard_imx]: keyboards#km_core_keyboard_imx "km_core_keyboard_imx struct"
[km_core_keyboard_load]: keyboards#km_core_keyboard_load "km_core_keyboard_load function"
[km_core_keyboard_load_from_blob]: keyboards#km_core_keyboard_load_from_blob "km_core_keyboard_load_from_blob function"
[km_core_keyboard_dispose]: keyboards#km_core_keyboard_dispose "km_core_keyboard_dispose function"
[km_core_keyboard_get_attrs]: keyboards#km_core_keyboard_get_attrs "km_core_keyboard_get_attrs function"
[km_core_keyboard_get_key_list]: keyboards#km_core_keyboard_get_key_list "km_core_keyboard_get_key_list function"
Expand Down
2 changes: 1 addition & 1 deletion core/docs/api/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ An opaque pointer to a state object.
[km_core_keyboard_attrs]: keyboards#km_core_keyboard_attrs "km_core_keyboard_attrs struct"
[km_core_keyboard_key]: keyboards#km_core_keyboard_key "km_core_keyboard_key struct"
[km_core_keyboard_imx]: keyboards#km_core_keyboard_imx "km_core_keyboard_imx struct"
[km_core_keyboard_load]: keyboards#km_core_keyboard_load "km_core_keyboard_load function"
[km_core_keyboard_load_from_blob]: keyboards#km_core_keyboard_load_from_blob "km_core_keyboard_load_from_blob function"
[km_core_keyboard_dispose]: keyboards#km_core_keyboard_dispose "km_core_keyboard_dispose function"
[km_core_keyboard_get_attrs]: keyboards#km_core_keyboard_get_attrs "km_core_keyboard_get_attrs function"
[km_core_keyboard_get_key_list]: keyboards#km_core_keyboard_get_key_list "km_core_keyboard_get_key_list function"
Expand Down
54 changes: 1 addition & 53 deletions core/include/keyman/keyman_core_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1096,58 +1096,6 @@ typedef struct {
-------------------------------------------------------------------------------
# km_core_keyboard_load()
## Description
DEPRECATED: use [km_core_keyboard_load_from_blob] instead.
Parse and load keyboard from the supplied path and a pointer to the loaded keyboard
into the out parameter.
## Specification
```c */
// TODO-web-core: Deprecate this function (#12497)
// KMN_DEPRECATED_API
KMN_API
km_core_status
km_core_keyboard_load(km_core_path_name kb_path,
km_core_keyboard **keyboard);

/*
```
## Parameters
`kb_path`
: On Windows, a UTF-16 string; on other platforms, a C string:
contains a valid path to the keyboard file.
`keyboard`
: A pointer to result variable: A pointer to the opaque keyboard
object returned by the Processor. This memory must be freed with a
call to [km_core_keyboard_dispose].
## Returns
`KM_CORE_STATUS_OK`
: On success.
`KM_CORE_STATUS_NO_MEM`
: In the event an internal memory allocation fails.
`KM_CORE_STATUS_IO_ERROR`
: In the event the keyboard file is unparseable for any reason
`KM_CORE_STATUS_INVALID_ARGUMENT`
: In the event the file doesn't exist or is inaccesible or `keyboard` is null.
`KM_CORE_STATUS_OS_ERROR`
: Bit 31 (high bit) set, bits 0-30 are an OS-specific error code.
-------------------------------------------------------------------------------
# km_core_keyboard_load_from_blob()
## Description
Expand Down Expand Up @@ -1207,7 +1155,7 @@ km_core_status km_core_keyboard_load_from_blob(const km_core_path_name kb_name,
## Description
Free the allocated memory belonging to an opaque keyboard object previously
returned by [km_core_keyboard_load].
returned by [km_core_keyboard_load_from_blob].
## Specification
Expand Down
90 changes: 5 additions & 85 deletions core/src/km_core_keyboard_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ namespace
} // namespace

km_core_status
keyboard_load_from_blob_internal(
km_core_keyboard_load_from_blob(
const km_core_path_name kb_name,
const std::vector<uint8_t> & buf,
const void* blob,
const size_t blob_size,
km_core_keyboard** keyboard
) {
assert(keyboard);
if (!keyboard) {
if (!keyboard || !blob) {
return KM_CORE_STATUS_INVALID_ARGUMENT;
}

std::vector<uint8_t> buf((uint8_t*)blob, (uint8_t*)blob + blob_size);
*keyboard = nullptr;
try {
abstract_processor* kp = processor_factory(kb_name, buf);
Expand All @@ -65,88 +67,6 @@ keyboard_load_from_blob_internal(
return KM_CORE_STATUS_OK;
}

km_core_status
km_core_keyboard_load_from_blob(
const km_core_path_name kb_name,
const void* blob,
const size_t blob_size,
km_core_keyboard** keyboard
) {
assert(keyboard);
if (!keyboard || !blob) {
return KM_CORE_STATUS_INVALID_ARGUMENT;
}

std::vector<uint8_t> buf((uint8_t*)blob, (uint8_t*)blob + blob_size);
return keyboard_load_from_blob_internal(kb_name, buf, keyboard);
}

// TODO-web-core: Remove this code when we remove the deprecated km_core_keyboard_load method
// BEGIN DEPRECATED
#include <fstream>
std::vector<uint8_t> load_kmx_file(path const& kb_path) {
std::vector<uint8_t> data;
std::ifstream file(static_cast<std::string>(kb_path), std::ios::binary | std::ios::ate);
if (!file.good()) {
return std::vector<uint8_t>();
}
const std::streamsize size = file.tellg();
if (size >= KMX_MAX_ALLOWED_FILE_SIZE) {
return std::vector<uint8_t>();
}

file.seekg(0, std::ios::beg);

data.resize((size_t)size);
if (!file.read((char*)data.data(), size)) {
return std::vector<uint8_t>();
}

file.close();
return data;
}

KMN_DEPRECATED_API
km_core_status
km_core_keyboard_load(km_core_path_name kb, km_core_keyboard **keyboard)
{
assert(keyboard);
if (!keyboard || !kb) {
return KM_CORE_STATUS_INVALID_ARGUMENT;
}

path const kb_path(kb);
try
{
abstract_processor* kp = nullptr;
km_core_status status = KM_CORE_STATUS_OK;
// Some legacy packages may include upper-case file extensions
if (kb_path.suffix() == ".kmx" || kb_path.suffix() == ".KMX") {
std::vector<uint8_t> buf = load_kmx_file(kb_path);
status = keyboard_load_from_blob_internal(kb_path.stem().c_str(), buf, (km_core_keyboard**)&kp);
if (status != KM_CORE_STATUS_OK) {
return status;
}
} else if (kb_path.suffix() == ".mock") {
kp = new mock_processor(kb_path);
} else {
kp = new null_processor();
}
status = kp->validate();
if (status != KM_CORE_STATUS_OK) {
delete kp;
return status;
}
*keyboard = static_cast<km_core_keyboard *>(kp);
}
catch (std::bad_alloc &)
{
return KM_CORE_STATUS_NO_MEM;
}
return KM_CORE_STATUS_OK;
}
// END DEPRECATED

void
km_core_keyboard_dispose(km_core_keyboard *keyboard)
{
Expand Down
40 changes: 11 additions & 29 deletions core/tests/unit/km_core_keyboard_api.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@

km::core::path test_dir;

// TODO-web-core: Remove this code when we remove the deprecated km_core_keyboard_load method
// BEGIN DEPRECATED
#if defined(__GNUC__) || defined(__clang__)
#define PRAGMA(X) _Pragma(#X)
#define DISABLE_WARNING_PUSH PRAGMA(GCC diagnostic push)
#define DISABLE_WARNING_POP PRAGMA(GCC diagnostic pop)
#define DISABLE_WARNING(W) PRAGMA(GCC diagnostic ignored #W)
#define DISABLE_WARNING_DEPRECATED_DECLARATIONS DISABLE_WARNING(-Wdeprecated-declarations)
#else
#define DISABLE_WARNING_PUSH
#define DISABLE_WARNING_POP
#define DISABLE_WARNING_DEPRECATED_DECLARATIONS
#endif
// #if defined(__GNUC__) || defined(__clang__)
// #define PRAGMA(X) _Pragma(#X)
// #define DISABLE_WARNING_PUSH PRAGMA(GCC diagnostic push)
// #define DISABLE_WARNING_POP PRAGMA(GCC diagnostic pop)
// #define DISABLE_WARNING(W) PRAGMA(GCC diagnostic ignored #W)
// #define DISABLE_WARNING_DEPRECATED_DECLARATIONS DISABLE_WARNING(-Wdeprecated-declarations)
// #else
// #define DISABLE_WARNING_PUSH
// #define DISABLE_WARNING_POP
// #define DISABLE_WARNING_DEPRECATED_DECLARATIONS
// #endif

class KmCoreKeyboardApiTests : public testing::Test {
protected:
Expand All @@ -35,22 +33,6 @@ class KmCoreKeyboardApiTests : public testing::Test {
}
};

TEST_F(KmCoreKeyboardApiTests, LoadFromFile) {
// Setup
km::core::path kmxfile = km::core::path(test_dir / "kmx/k_020___deadkeys_and_backspace.kmx");

// Execute
DISABLE_WARNING_PUSH
DISABLE_WARNING_DEPRECATED_DECLARATIONS
auto status = km_core_keyboard_load(kmxfile.c_str(), &this->keyboard);
DISABLE_WARNING_POP

// Verify
EXPECT_EQ(status, KM_CORE_STATUS_OK);
EXPECT_TRUE(this->keyboard != nullptr);
}
// END DEPRECATED

TEST_F(KmCoreKeyboardApiTests, LoadFromBlob) {
// Setup
km::core::path kmxfile = km::core::path(test_dir / "kmx/k_020___deadkeys_and_backspace.kmx");
Expand Down
2 changes: 1 addition & 1 deletion core/tests/unit/ldml/core_ldml_min.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int main(int argc, const char *argv[]) {
auto blob = km::tests::load_kmx_file(nowhere);
status = km_core_keyboard_load_from_blob(nowhere, blob.data(), blob.size(), &test_kb);

std::cerr << "null km_core_keyboard_load = " << status << std::endl;
std::cerr << "null km_core_keyboard_load_from_blob = " << status << std::endl;
test_assert(status == KM_CORE_STATUS_INVALID_ARGUMENT);
test_assert(test_kb == nullptr);
km_core_keyboard_dispose(test_kb);
Expand Down
1 change: 0 additions & 1 deletion linux/debian/libkeymancore2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ libkeymancore.so.2 libkeymancore2 #MINVER#
km_core_keyboard_get_key_list@Base 17.0.195
km_core_keyboard_imx_list_dispose@Base 17.0.195
km_core_keyboard_key_list_dispose@Base 17.0.195
km_core_keyboard_load@Base 17.0.195
km_core_keyboard_load_from_blob@Base 18.0.101
km_core_options_list_size@Base 17.0.195
km_core_process_event@Base 17.0.195
Expand Down
4 changes: 2 additions & 2 deletions linux/debian/tests/test-build
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
km_core_option_item opts[] = {KM_CORE_OPTIONS_END};
km_core_keyboard *kb = NULL;
km_core_state *state = NULL;
km_core_keyboard_load(NULL, &kb);
km_core_keyboard_load_from_blob(NULL, NULL, 0, &kb);
km_core_state_create(kb, opts, &state);
km_core_actions const *a = km_core_state_get_actions(state);
}
Expand All @@ -62,7 +62,7 @@ echo "build 3: OK"
# km_core_option_item opts[] = {KM_CORE_OPTIONS_END};
# km_core_keyboard *kb = NULL;
# km_core_state *state = NULL;
# km_core_keyboard_load(NULL, &kb);
# km_core_keyboard_load_from_blob(NULL, NULL, 0, &kb);
# km_core_state_create(kb, opts, &state);
# km_core_actions const *a = km_core_state_get_actions(state);
# }
Expand Down
2 changes: 1 addition & 1 deletion windows/src/engine/keyman32/K32_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ BOOL LoadlpKeyboard(int i)
free(buffer);

if (err_status != KM_CORE_STATUS_OK) {
SendDebugMessageFormat("km_core_keyboard_load failed for %ls with error status [%d]", keyboardPath, err_status);
SendDebugMessageFormat("km_core_keyboard_load_from_blob failed for %ls with error status [%d]", keyboardPath, err_status);
goto ExitError;
}
delete[] keyboardPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST(KEYBOARDOPTIONS, SetupCoreEnvironment) {
km_core_path_name dummyPath = L"dummyActions.mock";

EXPECT_EQ(SetupCoreEnvironment(&core_env_opts), TRUE);
EXPECT_EQ(km_core_keyboard_load(dummyPath, &kp->lpCoreKeyboard), KM_CORE_STATUS_OK);
EXPECT_EQ(km_core_keyboard_load_from_blob(dummyPath, (void *)"MOCK", 4, &kp->lpCoreKeyboard), KM_CORE_STATUS_OK);
EXPECT_EQ(km_core_state_create(kp->lpCoreKeyboard, core_env_opts, &kp->lpCoreKeyboardState), KM_CORE_STATUS_OK);

km_core_option_item *expected_items = new km_core_option_item[5];
Expand Down

0 comments on commit 4c3b213

Please sign in to comment.