?? epr_msph.c
字號:
/* * $Id: epr_msph.c,v 1.1.1.1 2003/03/05 17:36:43 hartmut Exp $ * * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation. This program is distributed in the hope it will * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include <assert.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "epr_api.h"#include "epr_core.h"#include "epr_string.h"#include "epr_ptrarray.h"#include "epr_swap.h"#include "epr_field.h"#include "epr_record.h"#include "epr_param.h"#include "epr_dsd.h"#include "epr_msph.h"#include "epr_band.h"#include "epr_bitmask.h"/** * Reads the full main product header (MPH) of the ENVISAT product file * by the given product identifier. * * @param product_id the product identifier, must not be <code>NULL</code> * @return a record representing the MPH of the specified product file * or <code>NULL</code> if an error occured. */EPR_SRecord* epr_read_mph(EPR_SProductId* product_id){ EPR_SRecord* record = NULL; char* code_block; int numread; epr_clear_err(); code_block = epr_create_string(EPR_MPH_SIZE); if (code_block == NULL) { epr_set_err(e_err_out_of_memory, "epr_read_mph: out of memory"); return NULL; } rewind(product_id->istream); numread = fread(code_block, 1, EPR_MPH_SIZE, product_id->istream); if (numread != EPR_MPH_SIZE) { epr_set_err(e_err_file_read_error, "epr_read_mph: wrong reading MPH from product data file"); return NULL; } record = epr_parse_header("mph", code_block); if (record == NULL) { epr_set_err(e_err_invalid_record, "epr_read_mph: can not recognize the correct MPH from product data file"); } else { epr_add_ptr_array_elem(product_id->record_info_cache, record->info); } epr_free_string(code_block); return record;}/** * Reads the full specific product header (SPH) of the ENVISAT product file * by the given product identifier. * * @param product_id the product identifier, must not be <code>NULL</code> * @return a record representing the MPH of the specified product file * or <code>NULL</code> if an error occured. */EPR_SRecord* epr_read_sph(EPR_SProductId* product_id){ EPR_SRecord* sph_record = NULL; const EPR_SField* field; char* code_block; int numread; ulong sph_length = 0; ulong sph_without_dsd_length = 0; ulong dsd_number = 0; epr_clear_err(); if (product_id->mph_record == NULL) { product_id->mph_record = epr_read_mph(product_id); if (product_id->mph_record == NULL) { epr_set_err(e_err_file_read_error, "epr_read_sph: wrong MPH"); return NULL; } } field = epr_get_field(product_id->mph_record, "SPH_SIZE"); sph_length = ((ulong*) field->elems)[0]; if (sph_length == 0) { epr_set_err(e_err_invalid_value, "epr_read_sph: wrong MPH: SPH_SIZE must be > 0"); return NULL; } field = epr_get_field(product_id->mph_record, "NUM_DSD"); dsd_number = ((ulong*) field->elems)[0]; if (dsd_number == 0) { epr_set_err(e_err_invalid_value, "epr_read_sph: wrong MPH: NUM_DSD must be > 0"); return NULL; } epr_api.epr_head_size = sph_length + EPR_MPH_SIZE; if (fseek(product_id->istream, EPR_MPH_SIZE, SEEK_SET) != 0) { epr_set_err(e_err_file_access_denied, "epr_read_sph: file seek failed"); return NULL; } sph_without_dsd_length = sph_length - dsd_number * EPR_DSD_SIZE; code_block = epr_create_string(sph_without_dsd_length); numread = fread(code_block, 1, sph_without_dsd_length, product_id->istream); if ((uint)numread != sph_without_dsd_length) { epr_set_err(e_err_file_read_error, "epr_read_sph: wrong reading SPH from product data file"); return NULL; } sph_record = epr_parse_header("sph", code_block); if (sph_record == NULL) { epr_set_err(e_err_invalid_record, "epr_read_sph: can not recognize the correct SPH from product data file"); } else { epr_add_ptr_array_elem(product_id->record_info_cache, sph_record->info); } epr_free_string(code_block); return sph_record;}void epr_store_header(const char* header_name, const char* ascii_source) { FILE* os; char fname[1024]; sprintf(fname, "%s.txt", header_name); os=fopen(fname, "w"); fprintf(os,"%s", ascii_source); fclose(os);}/** * Parses the header ASCII information. * * @param header_name name of the header ascii information; * @param ascii_source the header ascii information was read; * @param record the identifier of header ascii information. */EPR_SRecord* epr_parse_header(const char* header_name, const char* ascii_source){ EPR_SRecordInfo* record_info; EPR_SPtrArray* field_infos = NULL; EPR_SFieldInfo* field_info; EPR_SPtrArray* header_values = NULL; EPR_SRecord* record = NULL; EPR_EDataTypeId tp; char * code_block; char seps[] = EPR_HEADER_SEPARATOR_ARRAY; char * token_name; char * token_value; char * token_unit; char * h_name; int pos = 0; int pos_ascii = 0; uint num_bytes = 0; uint num_elems = 0; epr_clear_err(); /* uncomment for debugging purpose */ /* epr_store_header(header_name, ascii_source); */ header_values = epr_create_ptr_array(16); field_infos = epr_create_ptr_array(16); h_name = epr_clone_string(header_name); while ((code_block = epr_str_tok(ascii_source, "\n", &pos_ascii)) != NULL) { /*if EMPTY code_block*/ if ((strlen(code_block) > 0) && (code_block[0] == ' ')) { /* epr_log(e_log_info, "code_block is empty"); */ if (code_block != NULL) { epr_free_string(code_block); code_block = NULL; } continue; } /*if '=' separator*/ pos = 0; token_name = epr_str_tok(code_block, seps, &pos); if (pos == 1) { epr_set_err(e_err_invalid_keyword_name, "epr_parse_header: invalid ascii header: keyword is empty"); epr_free_string(token_name); if (code_block != NULL) { epr_free_string(code_block); code_block = NULL; } continue; } if (pos == (int)strlen(code_block) + 1) { epr_set_err(e_err_invalid_keyword_name, "epr_parse_header: invalid ascii header: keyword not found"); epr_free_string(token_name); if (code_block != NULL) { epr_free_string(code_block); code_block = NULL; } continue; } /*if STRING value*/ if (code_block[pos] == '\"') { pos ++; /* Note that strings always are considered as one single element, so we get the total number of characters from tot_size. Addidionally we reserve an extra character for the trailing zero (terminator), */ token_value = epr_strip_string_r(epr_str_tok(code_block, "\"", &pos)); token_unit = NULL; tp = e_tid_string; num_bytes = (uint)strlen(token_value); num_elems = 1; epr_add_ptr_array_elem(header_values, token_value); } else { token_value = epr_str_tok(code_block, seps, &pos); if (token_value == NULL) { epr_set_err(e_err_invalid_value, "epr_parse_header: invalid ascii header: value not found"); token_value = epr_clone_string(""); token_unit = NULL; tp = e_tid_uchar; num_bytes = 0; num_elems = 1; epr_add_ptr_array_elem(header_values, token_value); } else { /*if FLOAT-DOUBLE value*/ if (strchr(token_value, '.') != NULL || strchr(token_value, 'e') != NULL || strchr(token_value, 'E') != NULL) { epr_parse_double_token(header_values, token_value, &num_elems, &num_bytes, &tp); token_unit = epr_str_tok(code_block, seps, &pos); epr_free_string(token_value); token_value = NULL; /*if INTEGER_LONG value*/ } else if ((strlen(token_value) > 1)) { epr_parse_long_token(header_values, token_value, &num_elems, &num_bytes, &tp);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -