luaur_reduce_cli/methods/
reducer_read_line.rs1use crate::records::reducer::Reducer;
2use alloc::string::String;
3use core::ffi::{c_char, c_int, CStr};
4
5extern "C" {
6 fn fgets(s: *mut c_char, n: c_int, stream: *mut core::ffi::c_void) -> *mut c_char;
7}
8
9impl Reducer {
10 pub fn read_line(&self, f: *mut core::ffi::c_void) -> String {
11 let mut line = String::new();
12 let mut buffer = [0 as c_char; 256];
13
14 unsafe {
15 while !fgets(buffer.as_mut_ptr(), buffer.len() as c_int, f).is_null() {
16 let c_str = CStr::from_ptr(buffer.as_ptr());
17 let bytes = c_str.to_bytes();
18 let len = bytes.len();
19
20 if let Ok(s) = core::str::from_utf8(bytes) {
21 line.push_str(s);
22 }
23
24 if len > 0 && buffer[len - 1] == b'\n' as c_char {
25 break;
26 }
27 }
28 }
29
30 line
31 }
32}
33
34pub fn reducer_read_line(this: &Reducer, f: *mut core::ffi::c_void) -> String {
35 this.read_line(f)
36}