1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

Write array of strings OK

This commit is contained in:
Anthony Scemama 2023-10-12 00:09:10 +02:00
parent c5f65eefe7
commit 05f48378d5
3 changed files with 119 additions and 27 deletions

View File

@ -121,8 +121,8 @@ 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 mut data_c: {type_c} = 0{type_c};
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())
};
@ -146,9 +146,9 @@ pub fn write_{group_l}_{element_l}<T>(trex_file: File, data: T) -> Result<(), Ex
elif data[group][element][0] in [ "str" ]:
r += [ """
pub fn read_{group_l}_{element_l}(trex_file: File, capacity: usize) -> Result<String, ExitCode> {
let mut data_c = String::with_capacity(capacity);
let data_c = data_c.as_mut_ptr() as *mut c_char;
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))
};
@ -174,8 +174,8 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: &str) -> Result<(), Ex
elif data[group][element][0] in [ "dim readonly" ]:
r += [ """
pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode> {
let mut data_c: {type_c} = 0{type_c};
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())
};
@ -189,6 +189,109 @@ pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode>
.replace("{element}",element)
.replace("{element_l}",element_l) ]
# Array elements
else:
if data[group][element][0] in [ "int", "float", "dim", "index" ]:
t = [ """pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<Vec<{type_r}>, ExitCode> {
let size = 1;""" ]
t_prime = []
for dim in data[group][element][1]:
try: # A dimensioning variable
dim_group, dim_element = dim.split('.')
t_prime += [f" let size = size * read_{dim_group}_{dim_element}(trex_file).unwrap();" ]
except: # Only an integer
t_prime += [f" let size = size * {dim};"]
t += t_prime
t += [ """
let data: Vec<{type_r}> = Vec::with_capacity(size);
let data_c = data.as_ptr() as *mut {type_c};
let (rc, data) = unsafe {
let rc = c::trexio_read_safe_{group}_{element}_64(trex_file, data_c, size.try_into().unwrap());
(rc, data)
};
rc_return(rc, data)
}
""" ]
r += [ '\n'.join(t)
.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) ]
r += [ """
pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<{type_r}>) -> Result<(), ExitCode> {
let size: i64 = data.len().try_into().unwrap();
let data = data.as_ptr() as *const {type_c};
let rc = unsafe { c::trexio_write_safe_{group}_{element}_64(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 [ "str" ]:
t = [ """pub fn read_{group_l}_{element_l}(trex_file: File, capacity: usize) -> Result<Vec<String>, ExitCode> {
let size = 1;""" ]
t_prime = []
for dim in data[group][element][1]:
try: # A dimensioning variable
dim_group, dim_element = dim.split('.')
t_prime += [f" let size = size * read_{dim_group}_{dim_element}(trex_file).unwrap();" ]
except: # Only an integer
t_prime += [f" let size = size * {dim};"]
t += t_prime
t += [ """
let data = vec![ String::with_capacity(capacity) ; size ];
let data_c = data.as_ptr() as *mut *mut c_char;
let (rc, data) = unsafe {
let rc = c::trexio_read_{group}_{element}(trex_file, data_c, capacity.try_into().unwrap() );
(rc, data)
};
rc_return(rc, data)
}
""" ]
r += [ '\n'.join(t)
.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) ]
r += [ """
pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<&str>) -> Result<(), ExitCode> {
let mut size = 0;
for s in data.iter() {
let l = s.len();
size = if l>size {l} else {size};
}
size = size+1;
let data_c : Vec<std::ffi::CString> = data.iter().map(|&x| string_to_c(x)).collect::<Vec<_>>();
let data_c : Vec<*const c_char> = data_c.iter().map(|x| x.as_ptr() as *const c_char).collect::<Vec<_>>();
let size : i32 = size.try_into().unwrap();
let data_c = data_c.as_ptr() as *mut *const c_char;
let rc = unsafe { c::trexio_write_{group}_{element}(trex_file, data_c, 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 [ "float sparse" ]:
pass
with open(generated_rs,'w') as f:

View File

@ -63,14 +63,6 @@ pub fn inquire(file_name: &str) -> Result<bool, ExitCode> {
include!("generated.rs");
pub fn write_nucleus_charge(trex_file: File, data: Vec<f64>) -> Result<(), ExitCode> {
let size: i64 = data.len().try_into().unwrap();
let rc = unsafe { c::trexio_write_safe_nucleus_charge_64(trex_file, data.as_ptr() as *const f64, size) };
rc_return(rc, ())
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -40,19 +40,18 @@ pub fn test_write(file_name: &str, back_end: BackEnd) {
let basis_nucleus_index = vec!([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ]);
let label = vec![
"C ",
"Na ",
"C ",
"C 66 ",
"C ",
"C ",
"H 99 ",
"Ru ",
"H ",
"H ",
"H ",
"H " ];
let mut label = label.concat();
"C",
"Na",
"C",
"C 66",
"C",
"C",
"H 99",
"Ru",
"H",
"H",
"H",
"H" ];
let sym_str = "B3U with some comments";
@ -71,10 +70,8 @@ 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_label(trex_file, label.as_ptr(), label[0].len().try_into().unwrap()) };