vil_nitf2_scalar_field.cxx
Go to the documentation of this file.
1 // vil_nitf2: Written by Harry Voorhees (hlv@) and Rob Radtke (rob@) of
2 // Stellar Science Ltd. Co. (stellarscience.com) for
3 // Air Force Research Laboratory, 2005.
4 
5 #include <cstddef>
6 #include <cstdlib>
8 #include "vil_nitf2.h"
11 #include <vil/vil_stream_core.h>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 
18  vil_nitf2_field_definition* definition,
19  int variable_width, bool* error)
20 {
21  if (error) (*error) = false;
22  if (!definition || !definition->formatter) {
23  std::cerr << "vil_nitf2_field::read(): Incomplete field definition!\n";
24  return nullptr;
25  }
26  vil_nitf2_field_formatter* formatter = definition->formatter;
27  // variable_width, if positive, overrides formatter field_width
28  if (variable_width > 0) formatter->field_width = variable_width;
29  // Parse string
30  VIL_NITF2_LOG(log_debug) << "Reading tag " << definition->tag << ": ";
31  bool is_blank;
32  vil_nitf2_scalar_field* result = formatter->read_field(input, is_blank);
33 
34  // Set result definition, if found, and output (for debugging):
35  // value if computed
36  // warning if required field missing (scalar field or std::vector element)
37  // failed message if neither of the above apply
38  if (result!=nullptr) {
39  result->m_definition = definition;
40  VIL_NITF2_LOG(log_debug) << *result;
41  } else if (is_blank && !definition->blanks_ok) {
42  VIL_NITF2_LOG(log_debug) << "required field not specified!";
43  if (error) (*error) = true;
44  } else if (is_blank) {
45  VIL_NITF2_LOG(log_debug) << "(unspecified)";
46  } else {
47  VIL_NITF2_LOG(log_debug) << "failed!";
48  if (error) (*error) = true;
49  }
50  VIL_NITF2_LOG(log_debug) << std::endl;
51  return result;
52 }
53 
54 bool vil_nitf2_scalar_field::write(vil_nitf2_ostream& output, int variable_width) const
55 {
57  std::cerr << "vil_nitf2_scalar_field::write(): Incomplete field definition!\n";
58  return false;
59  }
60  VIL_NITF2_LOG(log_debug) << "Writing tag " << m_definition->tag << ':';
62  // variable_width, if non-negative, overrides formatter's field_width
63  if (variable_width > 0) formatter->field_width = variable_width;
64  formatter->write_field(output, this);
65  VIL_NITF2_LOG(log_debug) << std::endl;
66  return output.ok();
67 }
68 
70 {
71  //put the normal stuff in there
73  //now grab my value and put that in there
74  auto* str = new vil_stream_core;
75  write( *str );
76  vil_streampos num_to_read = str->tell();
77  str->seek( 0 );
78  char* buffer;
79  buffer = (char*)std::malloc( (std::size_t) num_to_read+1 );
80  str->read( (void*)buffer, num_to_read );
81  buffer[(std::size_t) num_to_read] = 0;
82  tr->columns.emplace_back( buffer );
83  free( buffer );
84  return tr;
85 }
virtual field_tree * get_tree() const
std::vector< std::string > columns
virtual vil_nitf2_scalar_field * read_field(vil_nitf2_istream &input, bool &out_blank)=0
#define VIL_NITF2_LOG(LEVEL)
Definition: vil_nitf2.h:63
virtual bool write_field(vil_nitf2_ostream &output, const vil_nitf2_scalar_field *field)=0
bool write(vil_nitf2_ostream &output, int variable_width=-1) const
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
field_tree * get_tree() const override
An in-core vil_stream implementation.
vil_nitf2_field_definition * m_definition
vxl_int_32 vil_streampos
Definition: vil_stream.h:16
vil_nitf2_field_formatter * formatter
virtual std::ostream & output(std::ostream &os) const =0
static vil_nitf2_scalar_field * read(vil_nitf2_istream &input, vil_nitf2_field_definition *definition, int variable_width=-1, bool *error=nullptr)
An in-core vil_stream implementation.