diff --git a/rust/iris_rust/src/examples.rs b/rust/iris_rust/src/examples.rs index 41f1622..cdb31e5 100644 --- a/rust/iris_rust/src/examples.rs +++ b/rust/iris_rust/src/examples.rs @@ -105,6 +105,7 @@ pub fn read_iris_file_example() { // Write to a temporary file let path = "iris_data_example.iris"; + // let path = "output_iris_data.iris"; // { // let file = std::fs::File::create(path).unwrap(); // let mut writer = std::io::BufWriter::new(file); diff --git a/rust/iris_rust/src/read.rs b/rust/iris_rust/src/read.rs index 316fd4d..c3e5a50 100644 --- a/rust/iris_rust/src/read.rs +++ b/rust/iris_rust/src/read.rs @@ -26,6 +26,7 @@ pub fn read_spectral_info(reader: &mut R) -> Result { let mut sensor_buf = [0u8; 50]; reader.read_exact(&mut sensor_buf)?; info.sensor_id = String::from_utf8_lossy(&sensor_buf).trim_end_matches('\0').to_string(); + info.sensor_id = remove_after_null_split_once( info.sensor_id); // Read wave coefficients for i in 0..4 { @@ -49,6 +50,7 @@ pub fn read_image_info(reader: &mut R) -> Result { let mut name_buf = [0u8; 100]; reader.read_exact(&mut name_buf)?; info.name = String::from_utf8_lossy(&name_buf).trim_end_matches('\0').to_string(); + info.name = remove_after_null_split_once(info.name); // Read collection time info.collection_time = read_time(reader)?; @@ -202,7 +204,15 @@ pub fn read_other_info(reader: &mut R) -> Result { Ok(info) } - +fn remove_after_null_split_once(s: String) -> String { + if let Some((before_null, _after_null)) = s.split_once('\0') { + // 返回 \0 之前的部分 + before_null.to_string() + } else { + // 如果没有找到 \0,就返回原始 String + s + } +} pub fn read_spectral_data(reader: &mut R) -> Result { let mut data = SpectralData::new(); @@ -213,10 +223,12 @@ pub fn read_spectral_data(reader: &mut R) -> Result { name_buf[99] = 0; // Ensure null termination let temp= String::from_utf8_lossy(&name_buf); data.name = temp.trim_end_matches('\0').to_string(); + data.name = remove_after_null_split_once(data.name); let mut sensor_buf = [0u8; 50]; reader.read_exact(&mut sensor_buf)?; data.sensor_id = String::from_utf8_lossy(&sensor_buf).trim_end_matches('\0').to_string(); + data.sensor_id = remove_after_null_split_once(data.sensor_id); let mut uint8_buf = [0u8; 1]; reader.read_exact(&mut uint8_buf)?; diff --git a/source/iris_format/iris_deffine.cpp b/source/iris_format/iris_deffine.cpp index 720f4a7..e016c5d 100644 --- a/source/iris_format/iris_deffine.cpp +++ b/source/iris_format/iris_deffine.cpp @@ -56,6 +56,7 @@ void IRIS_DATA_example() { tempspectradata.SpectralDataAddress = (uint8_t *)plot1; std::string temp="HH3_IRIS"; strcpy(tempspectradata.SensorId, temp.c_str()); + strcpy(tempspectradata.Name, "HH3_IRIS_Spectral_Data"); // 光谱数据名称 ///赋值第二个光谱数据 One_Spectral_Data_Struct &tempspectradata1= mydata.SepctralDataSection.SectionContent.SepctralDataAddressList[1]; tempspectradata1.CollectionTime= time; // 采集时间 @@ -66,7 +67,9 @@ void IRIS_DATA_example() { tempspectradata1.SpectralDataAddress = (uint8_t *)plot2; std::string temp1="IS3_IRIS"; + strcpy(tempspectradata1.SensorId, temp1.c_str()); + strcpy(tempspectradata1.Name, "IS3_IRIS_Spectral_Data"); // 光谱数据名称 ///赋值第三个光谱数据 One_Spectral_Data_Struct &tempspectradata2= mydata.SepctralDataSection.SectionContent.SepctralDataAddressList[2]; tempspectradata2.CollectionTime= time; // 采集时间 @@ -77,6 +80,7 @@ void IRIS_DATA_example() { tempspectradata2.SpectralDataAddress = (uint8_t *)plot3; std::string temp2="IS2_IRIS"; strcpy(tempspectradata2.SensorId, temp2.c_str()); + strcpy(tempspectradata2.Name, "IS2_IRIS_Spectral_Data"); // 光谱数据名称 ///赋值第二个光谱数据 One_Spectral_Data_Struct &tempspectradata3= mydata.SepctralDataSection.SectionContent.SepctralDataAddressList[3]; tempspectradata3.CollectionTime= time; // 采集时间 @@ -87,6 +91,7 @@ void IRIS_DATA_example() { tempspectradata3.SpectralDataAddress = (uint8_t *)plot4; strcpy(tempspectradata3.SensorId, temp1.c_str()); + strcpy(tempspectradata3.Name, "IS2_IRIS_Spectral_Data"); // 光谱数据名称 ///光谱信息区域构造 mydata.SepctralInfoSection.SectionContent.SepctralInfoNumber = 3;