00001 // vil_nitf2: Written by Harry Voorhees (hlv@) and Rob Radtke (rob@) of 00002 // Stellar Science Ltd. Co. (stellarscience.com) for 00003 // Air Force Research Laboratory, 2005. 00004 00005 #include "vil_nitf2_scalar_field.h" 00006 #include "vil_nitf2.h" 00007 #include "vil_nitf2_field_definition.h" 00008 #include "vil_nitf2_field_formatter.h" 00009 #include <vil/vil_stream_core.h> 00010 #include <vcl_cstddef.h> // for size_t 00011 00012 vil_nitf2_scalar_field* 00013 vil_nitf2_scalar_field::read(vil_nitf2_istream& input, 00014 vil_nitf2_field_definition* definition, 00015 int variable_width, bool* error) 00016 { 00017 if (error) (*error) = false; 00018 if (!definition || !definition->formatter) { 00019 vcl_cerr << "vil_nitf2_field::read(): Incomplete field definition!\n"; 00020 return 0; 00021 } 00022 vil_nitf2_field_formatter* formatter = definition->formatter; 00023 // variable_width, if positive, overrides formatter field_width 00024 if (variable_width > 0) formatter->field_width = variable_width; 00025 // Parse string 00026 VIL_NITF2_LOG(log_debug) << "Reading tag " << definition->tag << ": "; 00027 bool is_blank; 00028 vil_nitf2_scalar_field* result = formatter->read_field(input, is_blank); 00029 00030 // Set result definition, if found, and output (for debugging): 00031 // value if computed 00032 // warning if required field missing (scalar field or vcl_vector element) 00033 // failed message if neither of the above apply 00034 if (result!=0) { 00035 result->m_definition = definition; 00036 VIL_NITF2_LOG(log_debug) << *result; 00037 } else if (is_blank && !definition->blanks_ok) { 00038 VIL_NITF2_LOG(log_debug) << "required field not specified!"; 00039 if (error) (*error) = true; 00040 } else if (is_blank) { 00041 VIL_NITF2_LOG(log_debug) << "(unspecified)"; 00042 } else { 00043 VIL_NITF2_LOG(log_debug) << "failed!"; 00044 if (error) (*error) = true; 00045 } 00046 VIL_NITF2_LOG(log_debug) << vcl_endl; 00047 return result; 00048 } 00049 00050 bool vil_nitf2_scalar_field::write(vil_nitf2_ostream& output, int variable_width) const 00051 { 00052 if (!m_definition || !m_definition->formatter) { 00053 vcl_cerr << "vil_nitf2_scalar_field::write(): Incomplete field definition!\n"; 00054 return 0; 00055 } 00056 VIL_NITF2_LOG(log_debug) << "Writing tag " << m_definition->tag << ':'; 00057 vil_nitf2_field_formatter* formatter = m_definition->formatter; 00058 // variable_width, if non-negative, overrides formatter's field_width 00059 if (variable_width > 0) formatter->field_width = variable_width; 00060 formatter->write_field(output, this); 00061 VIL_NITF2_LOG(log_debug) << vcl_endl; 00062 return output.ok(); 00063 } 00064 00065 vil_nitf2_field::field_tree* vil_nitf2_scalar_field::get_tree() const 00066 { 00067 //put the normal stuff in there 00068 field_tree* tr = vil_nitf2_field::get_tree(); 00069 //now grab my value and put that in there 00070 vil_stream_core* str = new vil_stream_core; 00071 write( *str ); 00072 vil_streampos num_to_read = str->tell(); 00073 str->seek( 0 ); 00074 char* buffer; 00075 buffer = (char*)malloc( (vcl_size_t) num_to_read+1 ); 00076 str->read( (void*)buffer, num_to_read ); 00077 buffer[(vcl_size_t) num_to_read] = 0; 00078 tr->columns.push_back( vcl_string( buffer ) ); 00079 free( buffer ); 00080 return tr; 00081 }
1.4.4