/src/settings.h

https://gitlab.com/adam.lukaitis/rufus · C Header · 136 lines · 99 code · 12 blank · 25 comment · 19 complexity · b3b5b2c8077a572cd85a86aec9876d20 MD5 · raw file

  1. /*
  2. * Rufus: The Reliable USB Formatting Utility
  3. * Settings access, through either registry or INI file
  4. * Copyright © 2015 Pete Batard <pete@akeo.ie>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <windows.h>
  20. #include <stdint.h>
  21. #include "rufus.h"
  22. #include "registry.h"
  23. #pragma once
  24. extern char* ini_file;
  25. /*
  26. * List of setting names used by this application
  27. */
  28. #define SETTING_VERBOSE_UPDATES "VerboseUpdateCheck"
  29. #define SETTING_LAST_UPDATE "LastUpdateCheck"
  30. #define SETTING_UPDATE_INTERVAL "UpdateCheckInterval"
  31. #define SETTING_INCLUDE_BETAS "CheckForBetas"
  32. #define SETTING_COMM_CHECK "CommCheck"
  33. #define SETTING_LOCALE "Locale"
  34. #define SETTING_DISABLE_LGP "DisableLGP"
  35. #define SETTING_ADVANCED_MODE "AdvancedMode"
  36. #define SETTING_PRESERVE_TIMESTAMPS "PreserveTimestamps"
  37. #define SETTING_USE_PROPER_SIZE_UNITS "UseProperSizeUnits"
  38. #define SETTING_ENABLE_USB_DEBUG "EnableUsbDebug"
  39. #define SETTING_DISABLE_FAKE_DRIVES_CHECK "DisableFakeDrivesCheck"
  40. #define SETTING_ENABLE_WIN_DUAL_EFI_BIOS "EnableWindowsDualUefiBiosMode"
  41. #define SETTING_FORCE_LARGE_FAT32_FORMAT "ForceLargeFat32Formatting"
  42. #define SETTING_ENABLE_VMDK_DETECTION "EnableVmdkDetection"
  43. static __inline BOOL CheckIniKey(const char* key) {
  44. char* str = get_token_data_file(key, ini_file);
  45. BOOL ret = (str != NULL);
  46. safe_free(str);
  47. return ret;
  48. }
  49. #define CheckIniKey64 CheckIniKey
  50. #define CheckIniKey32 CheckIniKey
  51. #define CheckIniKeyBool CheckIniKey
  52. #define CheckIniKeyStr CheckIniKey
  53. static __inline int64_t ReadIniKey64(const char* key) {
  54. int64_t val = 0;
  55. char* str = get_token_data_file(key, ini_file);
  56. if (str != NULL) {
  57. val = _strtoi64(str, NULL, 0);
  58. free(str);
  59. }
  60. return val;
  61. }
  62. static __inline BOOL WriteIniKey64(const char* key, int64_t val) {
  63. char str[24];
  64. static_sprintf(str, "%" PRIi64, val);
  65. return (set_token_data_file(key, str, ini_file) != NULL);
  66. }
  67. static __inline int32_t ReadIniKey32(const char* key) {
  68. int32_t val = 0;
  69. char* str = get_token_data_file(key, ini_file);
  70. if (str != NULL) {
  71. val = strtol(str, NULL, 0);
  72. free(str);
  73. }
  74. return val;
  75. }
  76. static __inline BOOL WriteIniKey32(const char* key, int32_t val) {
  77. char str[12];
  78. static_sprintf(str, "%d", val);
  79. return (set_token_data_file(key, str, ini_file) != NULL);
  80. }
  81. static __inline char* ReadIniKeyStr(const char* key) {
  82. static char str[512];
  83. char* val;
  84. str[0] = 0;
  85. val = get_token_data_file(key, ini_file);
  86. if (val != NULL) {
  87. safe_strcpy(str, sizeof(str), val);
  88. free(val);
  89. }
  90. return str;
  91. }
  92. static __inline BOOL WriteIniKeyStr(const char* key, const char* val) {
  93. return (set_token_data_file(key, val, ini_file) != NULL);
  94. }
  95. /* Helpers for boolean operations */
  96. #define ReadIniKeyBool(key) (ReadIniKey32(key) != 0)
  97. #define WriteIniKeyBool(key, b) WriteIniKey32(key, (b)?1:0)
  98. /*
  99. * Read and store settings from/to ini file or registry
  100. */
  101. static __inline int64_t ReadSetting64(const char* key) {
  102. return (ini_file != NULL)?ReadIniKey64(key):ReadRegistryKey64(REGKEY_HKCU, key);
  103. }
  104. static __inline BOOL WriteSetting64(const char* key, int64_t val) {
  105. return (ini_file != NULL)?WriteIniKey64(key, val):WriteRegistryKey64(REGKEY_HKCU, key, val);
  106. }
  107. static __inline int32_t ReadSetting32(const char* key) {
  108. return (ini_file != NULL)?ReadIniKey32(key):ReadRegistryKey32(REGKEY_HKCU, key);
  109. }
  110. static __inline BOOL WriteSetting32(const char* key, int32_t val) {
  111. return (ini_file != NULL)?WriteIniKey32(key, val):WriteRegistryKey32(REGKEY_HKCU, key, val);
  112. }
  113. static __inline BOOL ReadSettingBool(const char* key) {
  114. return (ini_file != NULL)?ReadIniKeyBool(key):ReadRegistryKeyBool(REGKEY_HKCU, key);
  115. }
  116. static __inline BOOL WriteSettingBool(const char* key, BOOL val) {
  117. return (ini_file != NULL)?WriteIniKeyBool(key, val):WriteRegistryKeyBool(REGKEY_HKCU, key, val);
  118. }
  119. static __inline char* ReadSettingStr(const char* key) {
  120. return (ini_file != NULL)?ReadIniKeyStr(key):ReadRegistryKeyStr(REGKEY_HKCU, key);
  121. }
  122. static __inline BOOL WriteSettingStr(const char* key, char* val) {
  123. return (ini_file != NULL)?WriteIniKeyStr(key, val):WriteRegistryKeyStr(REGKEY_HKCU, key, val);
  124. }