From 05f48378d503a3cc62694809a67cc00a6dfd6eb3 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Thu, 12 Oct 2023 00:09:10 +0200 Subject: [PATCH] Write array of strings OK --- rust/trexio/build.py | 111 ++++++++++++++++++++++++++++++++++++++-- rust/trexio/src/lib.rs | 8 --- rust/trexio/src/test.rs | 27 +++++----- 3 files changed, 119 insertions(+), 27 deletions(-) diff --git a/rust/trexio/build.py b/rust/trexio/build.py index e31144e..93fe918 100755 --- a/rust/trexio/build.py +++ b/rust/trexio/build.py @@ -121,8 +121,8 @@ pub fn has_{group_l}_{element_l}(trex_file: File) -> Result { 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}(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 { + 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, 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, 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 = data.iter().map(|&x| string_to_c(x)).collect::>(); + let data_c : Vec<*const c_char> = data_c.iter().map(|x| x.as_ptr() as *const c_char).collect::>(); + 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: diff --git a/rust/trexio/src/lib.rs b/rust/trexio/src/lib.rs index d23b0cc..a34921c 100644 --- a/rust/trexio/src/lib.rs +++ b/rust/trexio/src/lib.rs @@ -63,14 +63,6 @@ pub fn inquire(file_name: &str) -> Result { include!("generated.rs"); -pub fn write_nucleus_charge(trex_file: File, data: Vec) -> 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::*; diff --git a/rust/trexio/src/test.rs b/rust/trexio/src/test.rs index fc39901..5532e93 100644 --- a/rust/trexio/src/test.rs +++ b/rust/trexio/src/test.rs @@ -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()) };