1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-06-02 03:15:40 +02:00

Scalars OK

This commit is contained in:
Anthony Scemama 2023-10-11 22:18:06 +02:00
parent 0ad057f77b
commit c5f65eefe7
4 changed files with 54 additions and 22 deletions

View File

@ -663,6 +663,7 @@ def main():
data = json.load(f)
for group in data:
for element in data[group]:
print(f"{group}_{element}")
if data[group][element][0] == "str":
data[group][element][0] = "string"

View File

@ -70,7 +70,7 @@ def make_functions():
"dim" : "usize",
"dim readonly" : "usize",
"index" : "usize",
"string" : "string"}
"str" : "str"}
convert_c = { "int": "i64",
"int special" : "i64",
@ -80,7 +80,7 @@ def make_functions():
"dim" : "i64",
"dim readonly" : "i64",
"index" : "i64",
"string" : "string"}
"str" : "str"}
r = []
for group in data:
@ -121,10 +121,10 @@ pub fn has_{group_l}_{element_l}(trex_file: File) -> Result<bool, ExitCode> {
if data[group][element][0] in [ "int", "float", "dim", "index" ]:
r += [ """
pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode> {
let data = unsafe {
let data_c: {type_c};
let rc = c::trexio_read_{group}_{element}_64(trex_file, &data);
data_c.try_into().unwrap();
let (rc, data) = unsafe {
let mut data_c: {type_c} = 0{type_c};
let rc = c::trexio_read_{group}_{element}_64(trex_file, &mut data_c);
(rc, data_c.try_into().unwrap())
};
rc_return(rc, data)
}
@ -137,24 +137,53 @@ pub fn write_{group_l}_{element_l}<T>(trex_file: File, data: T) -> Result<(), Ex
}
"""
.replace("{type_c}",type_c)
.replace("{type_r}",type_r)
.replace("{group}",group)
.replace("{group_l}",group_l)
.replace("{element}",element)
.replace("{element_l}",element_l) ]
elif data[group][element][0] in [ "string" ]:
pass # TODO
elif data[group][element][0] in [ "dim readonly" ]:
elif data[group][element][0] in [ "str" ]:
r += [ """
pub fn write_{group_l}_{element_l}<T>(trex_file: File, data: T) -> Result<(), ExitCode>
where {type_c}: From<T> {
let data: {type_c} = data.try_into().unwrap();
let rc = unsafe { c::trexio_write_{group}_{element}_64(trex_file, data) };
pub fn read_{group_l}_{element_l}(trex_file: File, capacity: usize) -> Result<String, ExitCode> {
let (rc, data) = unsafe {
let mut data_c = String::with_capacity(capacity);
let data_c = data_c.as_mut_ptr() as *mut c_char;
let rc = c::trexio_read_{group}_{element}(trex_file, data_c, capacity.try_into().unwrap());
(rc, String::from_raw_parts(data_c as *mut u8, capacity, capacity))
};
rc_return(rc, data)
}
pub fn write_{group_l}_{element_l}(trex_file: File, data: &str) -> Result<(), ExitCode> {
let size : i32 = data.len().try_into().unwrap();
let data = string_to_c(data);
let data = data.as_ptr() as *const c_char;
let rc = unsafe { c::trexio_write_{group}_{element}(trex_file, data, size) };
rc_return(rc, ())
}
"""
.replace("{type_c}",type_c)
.replace("{type_r}",type_r)
.replace("{group}",group)
.replace("{group_l}",group_l)
.replace("{element}",element)
.replace("{element_l}",element_l) ]
elif data[group][element][0] in [ "dim readonly" ]:
r += [ """
pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode> {
let (rc, data) = unsafe {
let mut data_c: {type_c} = 0{type_c};
let rc = c::trexio_read_{group}_{element}_64(trex_file, &mut data_c);
(rc, data_c.try_into().unwrap())
};
rc_return(rc, data)
}
"""
.replace("{type_r}",type_r)
.replace("{type_c}",type_c)
.replace("{group}",group)
.replace("{group_l}",group_l)
.replace("{element}",element)

View File

@ -24,8 +24,8 @@ fn rc_return<T>(rc : c::trexio_exit_code, result: T) -> Result<T,ExitCode> {
}
}
fn file_name_to_c(file_name: &str) -> std::ffi::CString {
std::ffi::CString::new(file_name).unwrap()
fn string_to_c(s: &str) -> std::ffi::CString {
std::ffi::CString::new(s).unwrap()
}
@ -33,7 +33,7 @@ fn file_name_to_c(file_name: &str) -> std::ffi::CString {
pub fn open(file_name: &str, mode: char, back_end: BackEnd) -> Result<File, ExitCode> {
let file_name_c = file_name_to_c(file_name);
let file_name_c = string_to_c(file_name);
let file_name_c = file_name_c.as_ptr() as *const c_char;
let mode = mode as c_char;
let back_end = back_end.to_c();
@ -49,7 +49,7 @@ pub fn close(file: File) -> Result<(), ExitCode> {
}
pub fn inquire(file_name: &str) -> Result<bool, ExitCode> {
let file_name_c = file_name_to_c(file_name);
let file_name_c = string_to_c(file_name);
let file_name_c = file_name_c.as_ptr() as *const c_char;
let rc = unsafe { c::trexio_inquire(file_name_c) };
match ExitCode::from(rc) {

View File

@ -53,7 +53,7 @@ pub fn test_write(file_name: &str, back_end: BackEnd) {
"H ",
"H " ];
let mut label = label.concat();
let sym_str = b"B3U with some comments";
let sym_str = "B3U with some comments";
println!("{}", file_name);
@ -70,11 +70,13 @@ pub fn test_write(file_name: &str, back_end: BackEnd) {
trexio::write_nucleus_num(trex_file, nucleus_num).unwrap();
trexio::write_nucleus_charge(trex_file, charge).unwrap();
trexio::write_nucleus_point_group(trex_file, sym_str).unwrap();
/*
trexio::write_nucleus_coord(trex_file, coord).unwrap();
trexio::write_nucleus_label(trex_file, label).unwrap();
*/
/*
let rc = unsafe { trexio_write_nucleus_coord(trex_file, coord.as_ptr() as *const f64) };
assert!(rc == TREXIO_SUCCESS);
let rc = unsafe { trexio_write_nucleus_label(trex_file, label.as_ptr(), label[0].len().try_into().unwrap()) };
assert!(rc == TREXIO_SUCCESS);