core/vil/file_formats/vil_nitf2_field_formatter.h
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 #ifndef VIL_NITF2_FIELD_FORMATTER_H
00006 #define VIL_NITF2_FIELD_FORMATTER_H
00007 
00008 #include <vcl_string.h>
00009 #include <vcl_istream.h>
00010 #include <vcl_ostream.h>
00011 
00012 #include "vil_nitf2.h"
00013 class vil_nitf2_field;
00014 class vil_nitf2_field_definition;
00015 class vil_nitf2_date_time;
00016 class vil_nitf2_location;
00017 class vil_nitf2_scalar_field;
00018 class vil_nitf2_array_field;
00019 
00020 //-----------------------------------------------------------------------------
00021 // A subclass of NITF field formatter exists for each NITF field data type.
00022 // These are used to define record extensions (as opposed to representing
00023 // values); they are used to read and write individual fields of the NITF
00024 // stream.
00025 
00026 // All formatters definitions include a field width, typically a vcl_fixed width,
00027 // unless a length functor is specified which is evaluated when the file is
00028 // read.
00029 //
00030 // Some fields include "precision" (the length of mantissa). For some numeric
00031 // fields, NITF allows blanks in lieu of insignificant decimal values.
00032 // For this reason, the precision stored in a field may (once implemented)
00033 // be less than the Formatter's precision.
00034 
00035 // Base class for NITF field formatters
00036 //
00037 class vil_nitf2_field_formatter
00038 {
00039  public:
00040   // Field types currently supported
00041 
00042   vil_nitf2::enum_field_type field_type;
00043   int field_width;
00044 
00045   vil_nitf2_field_formatter(vil_nitf2::enum_field_type field_type, int field_width)
00046     : field_type(field_type), field_width(field_width) {}
00047 
00048   // Destructor
00049   virtual ~vil_nitf2_field_formatter() {}
00050 
00051   // Virtual copy method
00052   virtual vil_nitf2_field_formatter* copy() const = 0;
00053 
00054   // Attempts to read scalar field from stream. Returns a new instance of field if
00055   // success and 0 otherwise. Sets out_blank to whether input was entirely blank
00056   // (in which case 0 is returned).
00057   virtual vil_nitf2_scalar_field* read_field(
00058     vil_nitf2_istream& input, bool& out_blank) = 0;
00059 
00060   // Initializes a vector field of specified dimensionality
00061   virtual vil_nitf2_array_field* create_array_field(
00062     int num_dimensions, vil_nitf2_field_definition*) = 0;
00063 
00064   virtual bool write_field(vil_nitf2_ostream& output, const vil_nitf2_scalar_field* field) = 0;
00065 
00066   // Writes a blank instance of field value to output stream. Returns
00067   // true iff successfully written. No need to overload this method.
00068   bool write_blank(vcl_ostream& output);
00069 
00070   // Same as above, but writes to a vil_stream.
00071   bool write_blank(vil_stream& output);
00072 
00073   // Helper function which reads the specified number characters into
00074   // a string (if possible), which it returns as a null-terminated C string,
00075   // which the caller owns. The length of the C string reflects the number
00076   // of characters read.
00077   static char* read_char_array(vcl_istream& input, int length);
00078   static bool read_c_str(vcl_istream& input, int length,
00079                          char*& out_cstr, bool& all_blank);
00080   // Same as above, but returns a vcl_string.
00081   static vcl_string read_string(vcl_istream& input, int length);
00082   // Same as above, but takes a vil_stream as input
00083   static vcl_string read_string(vil_stream& input, int length);
00084 
00085   // Helper function to test presence of number sign. Returns true
00086   // iff first character is consistent with flag show_sign.
00087   static bool check_sign(const char* cstr, bool show_sign);
00088 
00089   // Helper function to test whether the null-terminated C vcl_string contains
00090   // all blanks
00091   static bool is_all_blank(const char* cstr);
00092 };
00093 
00094 #endif // VIL_NITF2_FIELD_FORMATTER_H