created why2_toml_write_preserve rust fn

same as why2_toml_write but does not remove comments
This commit is contained in:
Václav Šmejkal 2025-02-01 16:04:04 +01:00
parent 3227814edf
commit 82a572bda1
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
3 changed files with 44 additions and 0 deletions

View File

@ -40,6 +40,7 @@ void why2_chat_init_authority(void); //CREATE WHY2_CHAT_AUTHORITY_DIR
char *why2_toml_read(const char* path, const char* key); //READ key FROM path TOML FILE char *why2_toml_read(const char* path, const char* key); //READ key FROM path TOML FILE
void why2_toml_write(const char *path, const char *key, const char *value); //WRITE value AS key INTO path TOML FILE void why2_toml_write(const char *path, const char *key, const char *value); //WRITE value AS key INTO path TOML FILE
void why2_toml_write_preserve(const char *path, const char *key, const char *value); //WRITE value AS key INTO path TOML FILE WITHOUT REMOVING COMMENTS
why2_bool why2_toml_contains(const char *path, const char *key); //CHECK IF path CONTAINS key why2_bool why2_toml_contains(const char *path, const char *key); //CHECK IF path CONTAINS key
why2_bool why2_toml_equals(const char *path, const char *key, const char *value); //CHECK IF key IN path IS EQUAL TO value why2_bool why2_toml_equals(const char *path, const char *key, const char *value); //CHECK IF key IN path IS EQUAL TO value
void why2_toml_read_free(char* s); //DEALLOCATE THE READ VALUE void why2_toml_read_free(char* s); //DEALLOCATE THE READ VALUE

View File

@ -25,3 +25,4 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
toml = "*" toml = "*"
toml_edit = "*"

View File

@ -126,6 +126,48 @@ pub extern "C" fn why2_toml_write(path: *const c_char, key: *const c_char, value
} }
} }
#[no_mangle]
pub extern "C" fn why2_toml_write_preserve(path: *const c_char, key: *const c_char, value: *const c_char)
{
//CONVERT C STRINGS TO RUST STRINGS
let path_r = unsafe { CStr::from_ptr(path).to_string_lossy().into_owned() };
let key_r = unsafe { CStr::from_ptr(key).to_string_lossy().into_owned() };
let value_r = unsafe { CStr::from_ptr(value).to_string_lossy().into_owned() };
//READ FILE
let file_raw = match read_to_string(&path_r)
{
Ok(raw) => raw,
Err(e) =>
{
eprintln!("Could not read TOML config '{}': {}", path_r, e);
return;
}
};
//PARSE TO A toml_edit Document
let mut doc: toml_edit::Document = match file_raw.parse()
{
Ok(doc) => doc,
Err(e) =>
{
eprintln!("Could not parse TOML config '{}': {}", path_r, e);
return;
}
};
doc[&key_r] = toml_edit::value(value_r);
//CONVERT DOCUMENT TO STRING
let updated_data = doc.to_string();
//WRITE
if let Err(e) = write(&path_r, updated_data)
{
eprintln!("Could not write to TOML config '{}': {}", path_r, e);
}
}
#[no_mangle] #[no_mangle]
pub extern "C" fn why2_toml_contains(path: *const c_char, key: *const c_char) -> bool pub extern "C" fn why2_toml_contains(path: *const c_char, key: *const c_char) -> bool
{ {