Skip to content

Commit 163442d

Browse files
committed
Refactor storage APIs and add preferences/secure storage C API
Split preferences and secure storage C APIs into separate files, replacing storage_c.cpp/h with preferences_c.cpp/h and secure_storage_c.cpp/h. Updated includes and usage throughout the codebase. Refactored string allocation to use to_c_str/free_c_str for consistency and safety. Improved memory management and error handling in menu and window C APIs.
1 parent 2677c55 commit 163442d

17 files changed

+486
-509
lines changed

include/nativeapi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
#include "../src/capi/image_c.h"
3737
#include "../src/capi/keyboard_monitor_c.h"
3838
#include "../src/capi/menu_c.h"
39+
#include "../src/capi/preferences_c.h"
3940
#include "../src/capi/run_example_app_c.h"
40-
#include "../src/capi/storage_c.h"
41+
#include "../src/capi/secure_storage_c.h"
4142
#include "../src/capi/string_utils_c.h"
4243
#include "../src/capi/tray_icon_c.h"
4344
#include "../src/capi/tray_manager_c.h"

src/capi/menu_c.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../global_registry.h"
1010
#include "../image.h"
1111
#include "../menu.h"
12+
#include "string_utils_c.h"
1213

1314
using namespace nativeapi;
1415

@@ -243,13 +244,7 @@ char* native_menu_item_get_label(native_menu_item_t item) {
243244
}
244245

245246
const std::string& label = labelOpt.value();
246-
247-
// Allocate C string and copy content
248-
char* result = static_cast<char*>(malloc(label.length() + 1));
249-
if (result) {
250-
strcpy(result, label.c_str());
251-
}
252-
return result;
247+
return to_c_str(label);
253248
} catch (...) {
254249
return nullptr;
255250
}
@@ -321,13 +316,7 @@ char* native_menu_item_get_tooltip(native_menu_item_t item) {
321316
}
322317

323318
const std::string& tooltip = tooltipOpt.value();
324-
325-
// Allocate C string and copy content
326-
char* result = static_cast<char*>(malloc(tooltip.length() + 1));
327-
if (result) {
328-
strcpy(result, tooltip.c_str());
329-
}
330-
return result;
319+
return to_c_str(tooltip);
331320
} catch (...) {
332321
return nullptr;
333322
}
@@ -993,13 +982,7 @@ char* native_keyboard_accelerator_to_string(const native_keyboard_accelerator_t*
993982
try {
994983
KeyboardAccelerator cpp_accelerator = convert_keyboard_accelerator(accelerator);
995984
std::string str = cpp_accelerator.ToString();
996-
997-
// Allocate C string and copy content
998-
char* result = static_cast<char*>(malloc(str.length() + 1));
999-
if (result) {
1000-
strcpy(result, str.c_str());
1001-
}
1002-
return result;
985+
return to_c_str(str);
1003986
} catch (...) {
1004987
return nullptr;
1005988
}

src/capi/preferences_c.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include "preferences_c.h"
2+
#include "../preferences.h"
3+
#include "string_utils_c.h"
4+
5+
using namespace nativeapi;
6+
7+
// Preferences C API Implementation
8+
9+
native_preferences_t native_preferences_create(void) {
10+
try {
11+
auto* prefs = new Preferences();
12+
return static_cast<void*>(prefs);
13+
} catch (...) {
14+
return nullptr;
15+
}
16+
}
17+
18+
native_preferences_t native_preferences_create_with_scope(const char* scope) {
19+
if (!scope) {
20+
return nullptr;
21+
}
22+
23+
try {
24+
auto* prefs = new Preferences(scope);
25+
return static_cast<void*>(prefs);
26+
} catch (...) {
27+
return nullptr;
28+
}
29+
}
30+
31+
void native_preferences_destroy(native_preferences_t prefs) {
32+
if (prefs) {
33+
delete static_cast<Preferences*>(prefs);
34+
}
35+
}
36+
37+
bool native_preferences_set(native_preferences_t prefs, const char* key, const char* value) {
38+
if (!prefs || !key || !value) {
39+
return false;
40+
}
41+
42+
try {
43+
auto* preferences = static_cast<Preferences*>(prefs);
44+
return preferences->Set(key, value);
45+
} catch (...) {
46+
return false;
47+
}
48+
}
49+
50+
char* native_preferences_get(native_preferences_t prefs,
51+
const char* key,
52+
const char* default_value) {
53+
if (!prefs || !key) {
54+
return default_value ? to_c_str(std::string(default_value)) : nullptr;
55+
}
56+
57+
try {
58+
auto* preferences = static_cast<Preferences*>(prefs);
59+
std::string result = preferences->Get(key, default_value ? default_value : "");
60+
return to_c_str(result);
61+
} catch (...) {
62+
return default_value ? to_c_str(std::string(default_value)) : nullptr;
63+
}
64+
}
65+
66+
bool native_preferences_remove(native_preferences_t prefs, const char* key) {
67+
if (!prefs || !key) {
68+
return false;
69+
}
70+
71+
try {
72+
auto* preferences = static_cast<Preferences*>(prefs);
73+
return preferences->Remove(key);
74+
} catch (...) {
75+
return false;
76+
}
77+
}
78+
79+
bool native_preferences_clear(native_preferences_t prefs) {
80+
if (!prefs) {
81+
return false;
82+
}
83+
84+
try {
85+
auto* preferences = static_cast<Preferences*>(prefs);
86+
return preferences->Clear();
87+
} catch (...) {
88+
return false;
89+
}
90+
}
91+
92+
bool native_preferences_contains(native_preferences_t prefs, const char* key) {
93+
if (!prefs || !key) {
94+
return false;
95+
}
96+
97+
try {
98+
auto* preferences = static_cast<Preferences*>(prefs);
99+
return preferences->Contains(key);
100+
} catch (...) {
101+
return false;
102+
}
103+
}
104+
105+
bool native_preferences_get_keys(native_preferences_t prefs, char*** out_keys, size_t* out_count) {
106+
if (!prefs || !out_keys || !out_count) {
107+
return false;
108+
}
109+
110+
try {
111+
auto* preferences = static_cast<Preferences*>(prefs);
112+
auto keys = preferences->GetKeys();
113+
114+
*out_count = keys.size();
115+
*out_keys = new char*[keys.size()];
116+
117+
for (size_t i = 0; i < keys.size(); ++i) {
118+
(*out_keys)[i] = to_c_str(keys[i]);
119+
}
120+
121+
return true;
122+
} catch (...) {
123+
*out_keys = nullptr;
124+
*out_count = 0;
125+
return false;
126+
}
127+
}
128+
129+
size_t native_preferences_get_size(native_preferences_t prefs) {
130+
if (!prefs) {
131+
return 0;
132+
}
133+
134+
try {
135+
auto* preferences = static_cast<Preferences*>(prefs);
136+
return preferences->GetSize();
137+
} catch (...) {
138+
return 0;
139+
}
140+
}
141+
142+
char* native_preferences_get_scope(native_preferences_t prefs) {
143+
if (!prefs) {
144+
return nullptr;
145+
}
146+
147+
try {
148+
auto* preferences = static_cast<Preferences*>(prefs);
149+
return to_c_str(preferences->GetScope());
150+
} catch (...) {
151+
return nullptr;
152+
}
153+
}
154+
155+
void native_preferences_free_string(char* str) {
156+
free_c_str(str);
157+
}
158+
159+
void native_preferences_free_string_array(char** strings, size_t count) {
160+
if (strings) {
161+
for (size_t i = 0; i < count; ++i) {
162+
free_c_str(strings[i]);
163+
}
164+
delete[] strings;
165+
}
166+
}

src/capi/preferences_c.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#include <stdbool.h>
8+
#include <stddef.h>
9+
10+
// Opaque handle type
11+
typedef void* native_preferences_t;
12+
13+
// Preferences API
14+
15+
/**
16+
* @brief Create a preferences storage with default scope.
17+
* @return Handle to preferences storage, or NULL on failure
18+
*/
19+
native_preferences_t native_preferences_create(void);
20+
21+
/**
22+
* @brief Create a preferences storage with custom scope.
23+
* @param scope Scope for isolating preferences
24+
* @return Handle to preferences storage, or NULL on failure
25+
*/
26+
native_preferences_t native_preferences_create_with_scope(const char* scope);
27+
28+
/**
29+
* @brief Destroy a preferences storage instance.
30+
* @param prefs Handle to preferences storage
31+
*/
32+
void native_preferences_destroy(native_preferences_t prefs);
33+
34+
/**
35+
* @brief Set a key-value pair.
36+
* @param prefs Handle to preferences storage
37+
* @param key The key to set
38+
* @param value The value to store
39+
* @return true if successful, false otherwise
40+
*/
41+
bool native_preferences_set(native_preferences_t prefs, const char* key, const char* value);
42+
43+
/**
44+
* @brief Get the value for a given key.
45+
* @param prefs Handle to preferences storage
46+
* @param key The key to retrieve
47+
* @param default_value Default value if key doesn't exist
48+
* @return The stored value or default_value. Caller must free the returned string.
49+
*/
50+
char* native_preferences_get(native_preferences_t prefs,
51+
const char* key,
52+
const char* default_value);
53+
54+
/**
55+
* @brief Remove a key-value pair.
56+
* @param prefs Handle to preferences storage
57+
* @param key The key to remove
58+
* @return true if successful, false if key doesn't exist
59+
*/
60+
bool native_preferences_remove(native_preferences_t prefs, const char* key);
61+
62+
/**
63+
* @brief Clear all key-value pairs.
64+
* @param prefs Handle to preferences storage
65+
* @return true if successful, false otherwise
66+
*/
67+
bool native_preferences_clear(native_preferences_t prefs);
68+
69+
/**
70+
* @brief Check if a key exists.
71+
* @param prefs Handle to preferences storage
72+
* @param key The key to check
73+
* @return true if key exists, false otherwise
74+
*/
75+
bool native_preferences_contains(native_preferences_t prefs, const char* key);
76+
77+
/**
78+
* @brief Get all keys.
79+
* @param prefs Handle to preferences storage
80+
* @param out_keys Pointer to array of keys (allocated by function)
81+
* @param out_count Pointer to receive number of keys
82+
* @return true if successful, false otherwise. Caller must free each key and the array.
83+
*/
84+
bool native_preferences_get_keys(native_preferences_t prefs, char*** out_keys, size_t* out_count);
85+
86+
/**
87+
* @brief Get the number of stored items.
88+
* @param prefs Handle to preferences storage
89+
* @return Number of key-value pairs
90+
*/
91+
size_t native_preferences_get_size(native_preferences_t prefs);
92+
93+
/**
94+
* @brief Get the scope.
95+
* @param prefs Handle to preferences storage
96+
* @return The scope. Caller must free the returned string.
97+
*/
98+
char* native_preferences_get_scope(native_preferences_t prefs);
99+
100+
/**
101+
* @brief Free a string returned by preferences functions.
102+
* @param str String to free
103+
*/
104+
void native_preferences_free_string(char* str);
105+
106+
/**
107+
* @brief Free a string array returned by preferences functions.
108+
* @param strings Array of strings to free
109+
* @param count Number of strings in the array
110+
*/
111+
void native_preferences_free_string_array(char** strings, size_t count);
112+
113+
#ifdef __cplusplus
114+
}
115+
#endif

0 commit comments

Comments
 (0)