contrib/mul/mbl/mbl_read_multi_props.h
Go to the documentation of this file.
00001 #ifndef mbl_read_multi_props_h
00002 #define mbl_read_multi_props_h
00003 //:
00004 // \file
00005 // \author Ian Scott
00006 // \date 21-Nov-2005
00007 // \brief Load properties with repeated labels from text files
00008 
00009 #include <vcl_map.h>
00010 #include <vcl_iosfwd.h>
00011 #include <vcl_string.h>
00012 #include <vcl_vector.h>
00013 
00014 
00015 //: The type of the property dictionary
00016 class mbl_read_multi_props_type : public vcl_multimap<vcl_string, vcl_string>
00017 {
00018  public:
00019 
00020   //: Return a single expected value of the given property \a label.
00021   // The matching entry is removed from the property list.
00022   // \throws mbl_exception_missing_property if \a label doesn't exist.
00023   // \throws mbl_exception_read_props_parse_error if there are two or more values of \a label.
00024   vcl_string get_required_property(const vcl_string& label);
00025 
00026   //: Return a single value of the given property \a label.
00027   // The matching entry is removed from the property list.
00028   // returns empty string or \a default_prop if \a label doesn't exist.
00029   // \throws mbl_exception_read_props_parse_error if there are two or more values of \a label.
00030   vcl_string get_optional_property(const vcl_string& label,
00031                                    const vcl_string& default_prop="");
00032 
00033   //: Return a vector of all values for a given property label \a label.
00034   // All entries of \a label are removed from the property list.
00035   // \throws mbl_exception_missing_property if \a label doesn't occur at least once.
00036   // \param nmax The maximum number of permitted occurrences of this label (default=max<unsigned>).
00037   // \param nmin The minimum number of permitted occurrences of this label (default=1).
00038   // \throws mbl_exception_read_props_parse_error if \a label occurs an invalid number of times.
00039   void get_required_properties(const vcl_string& label,
00040                                vcl_vector<vcl_string>& values,
00041                                const unsigned nmax= (unsigned)(-1), //=max<unsigned>
00042                                const unsigned nmin=1);
00043 
00044   //: Return a vector of all values for a given property label \a label.
00045   // All entries of \a label are removed from the property list.
00046   // Returns an empty vector if \a label doesn't occur at least once.
00047   // \param nmax The maximum number of permitted occurrences of this label (default=max<unsigned>).
00048   // \throws mbl_exception_read_props_parse_error if \a label occurs too many times.
00049   void get_optional_properties(const vcl_string& label,
00050                                vcl_vector<vcl_string>& values,
00051                                const unsigned nmax= (unsigned)(-1)); //=max<unsigned>
00052 };
00053 
00054 
00055 //: Read properties with repeated labels from a text stream.
00056 // The function will terminate on an eof. If one of
00057 // the opening lines contains an opening brace '{', then the function
00058 // will also stop reading the stream after finding a line containing
00059 // a closing brace '}'
00060 //
00061 // Every property label ends in ":", and should not contain
00062 // any whitespace.
00063 // If there is a brace after the first string following the label,
00064 // the following text up to matching
00065 // braces is included in the property value.
00066 //
00067 // Differs from mbl_read_props_ws(afs) in that a property label can be repeated,
00068 // and the all the repeated values will be returned.
00069 mbl_read_multi_props_type mbl_read_multi_props_ws(vcl_istream &afs);
00070 
00071 
00072 //: Print a list of properties for debugging purposes.
00073 void mbl_read_multi_props_print(vcl_ostream &afs, mbl_read_multi_props_type props);
00074 
00075 //: Print a list of properties for debugging purposes. Limit each property value length to \p max_chars
00076 // Useful for preventing diagnostic output from being flooded by large properties.
00077 void mbl_read_multi_props_print(vcl_ostream &afs, mbl_read_multi_props_type props, unsigned max_chars);
00078 
00079 //: Throw error if there are any keys in props that aren't in ignore.
00080 // \throw mbl_exception_unused_props
00081 void mbl_read_multi_props_look_for_unused_props(
00082   const vcl_string & function_name,
00083   const mbl_read_multi_props_type &props,
00084   const mbl_read_multi_props_type &ignore);
00085 
00086 //: Throw error if there are any keys in props.
00087 // \throw mbl_exception_unused_props
00088 inline void mbl_read_multi_props_look_for_unused_props(
00089   const vcl_string & function_name,
00090   const mbl_read_multi_props_type &props)
00091 {
00092   mbl_read_multi_props_look_for_unused_props(function_name, props,
00093                                              mbl_read_multi_props_type());
00094 }
00095 
00096 
00097 #endif // mbl_read_multi_props_h