增加了示例fiber_id 修改了rust中关于字符串的处理

This commit is contained in:
xin
2025-06-12 09:54:15 +08:00
parent 1aba741f67
commit a500813e45
3 changed files with 19 additions and 1 deletions

View File

@ -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);

View File

@ -26,6 +26,7 @@ pub fn read_spectral_info<R: Read>(reader: &mut R) -> Result<SpectralInfo> {
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<R: Read>(reader: &mut R) -> Result<ImageInfo> {
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<R: Read>(reader: &mut R) -> Result<OtherInfo> {
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<R: Read>(reader: &mut R) -> Result<SpectralData> {
let mut data = SpectralData::new();
@ -213,10 +223,12 @@ pub fn read_spectral_data<R: Read>(reader: &mut R) -> Result<SpectralData> {
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)?;

View File

@ -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;