|
| 1 | +#include <nativeapi.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +void demo_preferences() { |
| 6 | + printf("=== Preferences C API Demo ===\n"); |
| 7 | + |
| 8 | + // Create preferences with custom scope |
| 9 | + native_preferences_t prefs = native_preferences_create_with_scope("my_c_app"); |
| 10 | + if (!prefs) { |
| 11 | + printf("Failed to create preferences\n"); |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + // Store some values |
| 16 | + native_preferences_set(prefs, "username", "alice"); |
| 17 | + native_preferences_set(prefs, "theme", "light"); |
| 18 | + native_preferences_set(prefs, "font_size", "12"); |
| 19 | + |
| 20 | + // Retrieve values |
| 21 | + char* username = native_preferences_get(prefs, "username", ""); |
| 22 | + char* theme = native_preferences_get(prefs, "theme", ""); |
| 23 | + char* font_size = native_preferences_get(prefs, "font_size", ""); |
| 24 | + |
| 25 | + printf("Username: %s\n", username); |
| 26 | + printf("Theme: %s\n", theme); |
| 27 | + printf("Font size: %s\n", font_size); |
| 28 | + |
| 29 | + native_preferences_free_string(username); |
| 30 | + native_preferences_free_string(theme); |
| 31 | + native_preferences_free_string(font_size); |
| 32 | + |
| 33 | + // Check if key exists |
| 34 | + if (native_preferences_contains(prefs, "language")) { |
| 35 | + char* language = native_preferences_get(prefs, "language", ""); |
| 36 | + printf("Language: %s\n", language); |
| 37 | + native_preferences_free_string(language); |
| 38 | + } else { |
| 39 | + char* default_lang = native_preferences_get(prefs, "language", "en"); |
| 40 | + printf("Language not set, using default: %s\n", default_lang); |
| 41 | + native_preferences_free_string(default_lang); |
| 42 | + } |
| 43 | + |
| 44 | + // Get all keys |
| 45 | + char** keys = NULL; |
| 46 | + size_t count = 0; |
| 47 | + if (native_preferences_get_keys(prefs, &keys, &count)) { |
| 48 | + printf("\nAll keys (%zu):\n", count); |
| 49 | + for (size_t i = 0; i < count; i++) { |
| 50 | + char* value = native_preferences_get(prefs, keys[i], ""); |
| 51 | + printf(" - %s: %s\n", keys[i], value); |
| 52 | + native_preferences_free_string(value); |
| 53 | + } |
| 54 | + native_preferences_free_string_array(keys, count); |
| 55 | + } |
| 56 | + |
| 57 | + // Get size |
| 58 | + size_t size = native_preferences_get_size(prefs); |
| 59 | + printf("Total items: %zu\n", size); |
| 60 | + |
| 61 | + // Remove a key |
| 62 | + printf("\nRemoving 'font_size'...\n"); |
| 63 | + native_preferences_remove(prefs, "font_size"); |
| 64 | + printf("Size after removal: %zu\n", native_preferences_get_size(prefs)); |
| 65 | + |
| 66 | + // Get scope |
| 67 | + char* scope = native_preferences_get_scope(prefs); |
| 68 | + printf("Scope: %s\n", scope); |
| 69 | + native_preferences_free_string(scope); |
| 70 | + |
| 71 | + // Clean up |
| 72 | + native_preferences_destroy(prefs); |
| 73 | + |
| 74 | + printf("\n"); |
| 75 | +} |
| 76 | + |
| 77 | +void demo_secure_storage() { |
| 78 | + printf("=== Secure Storage C API Demo ===\n"); |
| 79 | + |
| 80 | + // Check if secure storage is available |
| 81 | + if (!native_secure_storage_is_available()) { |
| 82 | + printf("Secure storage is not available on this platform\n"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Create secure storage with custom scope |
| 87 | + native_secure_storage_t storage = native_secure_storage_create_with_scope("my_c_app_secure"); |
| 88 | + if (!storage) { |
| 89 | + printf("Failed to create secure storage\n"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // Store sensitive data |
| 94 | + native_secure_storage_set(storage, "api_key", "sk-c-api-1234567890"); |
| 95 | + native_secure_storage_set(storage, "secret", "my_secret_value"); |
| 96 | + native_secure_storage_set(storage, "token", "bearer_token_xyz"); |
| 97 | + |
| 98 | + // Retrieve sensitive data |
| 99 | + char* api_key = native_secure_storage_get(storage, "api_key", ""); |
| 100 | + char* secret = native_secure_storage_get(storage, "secret", ""); |
| 101 | + |
| 102 | + printf("API Key: %s\n", api_key); |
| 103 | + printf("Secret: %s\n", secret); |
| 104 | + |
| 105 | + native_secure_storage_free_string(api_key); |
| 106 | + native_secure_storage_free_string(secret); |
| 107 | + |
| 108 | + // Get all keys |
| 109 | + char** keys = NULL; |
| 110 | + size_t count = 0; |
| 111 | + if (native_secure_storage_get_keys(storage, &keys, &count)) { |
| 112 | + printf("\nStored secure items (%zu):\n", count); |
| 113 | + for (size_t i = 0; i < count; i++) { |
| 114 | + printf(" - %s: [encrypted]\n", keys[i]); |
| 115 | + } |
| 116 | + native_secure_storage_free_string_array(keys, count); |
| 117 | + } |
| 118 | + |
| 119 | + // Check existence |
| 120 | + if (native_secure_storage_contains(storage, "api_key")) { |
| 121 | + printf("\nAPI key is securely stored\n"); |
| 122 | + } |
| 123 | + |
| 124 | + // Get size |
| 125 | + size_t size = native_secure_storage_get_size(storage); |
| 126 | + printf("Total secure items: %zu\n", size); |
| 127 | + |
| 128 | + // Remove sensitive data |
| 129 | + printf("\nRemoving 'token'...\n"); |
| 130 | + native_secure_storage_remove(storage, "token"); |
| 131 | + printf("Size after removal: %zu\n", native_secure_storage_get_size(storage)); |
| 132 | + |
| 133 | + // Get scope |
| 134 | + char* storage_scope = native_secure_storage_get_scope(storage); |
| 135 | + printf("Scope: %s\n", storage_scope); |
| 136 | + native_secure_storage_free_string(storage_scope); |
| 137 | + |
| 138 | + // Clean up (optional: clear all for this demo) |
| 139 | + // native_secure_storage_clear(storage); |
| 140 | + |
| 141 | + native_secure_storage_destroy(storage); |
| 142 | + |
| 143 | + printf("\n"); |
| 144 | +} |
| 145 | + |
| 146 | +int main() { |
| 147 | + printf("Storage C API Example\n"); |
| 148 | + printf("=====================\n\n"); |
| 149 | + |
| 150 | + // Demo Preferences (plain text storage) |
| 151 | + demo_preferences(); |
| 152 | + |
| 153 | + // Demo SecureStorage (encrypted storage) |
| 154 | + demo_secure_storage(); |
| 155 | + |
| 156 | + printf("Done! Check your system's storage locations:\n"); |
| 157 | + printf(" - macOS: NSUserDefaults & Keychain\n"); |
| 158 | + printf(" - Windows: Registry & DPAPI\n"); |
| 159 | + printf(" - Linux: ~/.config/nativeapi & ~/.local/share/nativeapi\n"); |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
0 commit comments