core/vil/file_formats/vil_nitf2_scalar_field.cxx
Go to the documentation of this file.
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 #include <vcl_cstdlib.h>
00012 
00013 vil_nitf2_scalar_field*
00014 vil_nitf2_scalar_field::read(vil_nitf2_istream& input,
00015                              vil_nitf2_field_definition* definition,
00016                              int variable_width, bool* error)
00017 {
00018   if (error) (*error) = false;
00019   if (!definition || !definition->formatter) {
00020     vcl_cerr << "vil_nitf2_field::read(): Incomplete field definition!\n";
00021     return 0;
00022   }
00023   vil_nitf2_field_formatter* formatter = definition->formatter;
00024   // variable_width, if positive, overrides formatter field_width
00025   if (variable_width > 0) formatter->field_width = variable_width;
00026   // Parse string
00027   VIL_NITF2_LOG(log_debug) << "Reading tag " << definition->tag << ": ";
00028   bool is_blank;
00029   vil_nitf2_scalar_field* result = formatter->read_field(input, is_blank);
00030 
00031   // Set result definition, if found, and output (for debugging):
00032   //   value if computed
00033   //   warning if required field missing (scalar field or vcl_vector element)
00034   //   failed message if neither of the above apply
00035   if (result!=0) {
00036     result->m_definition = definition;
00037     VIL_NITF2_LOG(log_debug) << *result;
00038   } else if (is_blank && !definition->blanks_ok) {
00039     VIL_NITF2_LOG(log_debug) << "required field not specified!";
00040     if (error) (*error) = true;
00041   } else if (is_blank) {
00042     VIL_NITF2_LOG(log_debug) << "(unspecified)";
00043   } else {
00044     VIL_NITF2_LOG(log_debug) << "failed!";
00045     if (error) (*error) = true;
00046   }
00047   VIL_NITF2_LOG(log_debug) << vcl_endl;
00048   return result;
00049 }
00050 
00051 bool vil_nitf2_scalar_field::write(vil_nitf2_ostream& output, int variable_width) const
00052 {
00053   if (!m_definition || !m_definition->formatter) {
00054     vcl_cerr << "vil_nitf2_scalar_field::write(): Incomplete field definition!\n";
00055     return 0;
00056   }
00057   VIL_NITF2_LOG(log_debug) << "Writing tag " << m_definition->tag << ':';
00058   vil_nitf2_field_formatter* formatter = m_definition->formatter;
00059   // variable_width, if non-negative, overrides formatter's field_width
00060   if (variable_width > 0) formatter->field_width = variable_width;
00061   formatter->write_field(output, this);
00062   VIL_NITF2_LOG(log_debug) << vcl_endl;
00063   return output.ok();
00064 }
00065 
00066 vil_nitf2_field::field_tree* vil_nitf2_scalar_field::get_tree() const
00067 {
00068   //put the normal stuff in there
00069   field_tree* tr = vil_nitf2_field::get_tree();
00070   //now grab my value and put that in there
00071   vil_stream_core* str = new vil_stream_core;
00072   write( *str );
00073   vil_streampos num_to_read = str->tell();
00074   str->seek( 0 );
00075   char* buffer;
00076   buffer = (char*)vcl_malloc( (vcl_size_t) num_to_read+1 );
00077   str->read( (void*)buffer, num_to_read );
00078   buffer[(vcl_size_t) num_to_read] = 0;
00079   tr->columns.push_back( vcl_string( buffer ) );
00080   free( buffer );
00081   return tr;
00082 }