00001 #ifndef mbl_read_props_h 00002 #define mbl_read_props_h 00003 //: 00004 // \file 00005 // \author Ian Scott 00006 // \date 14-Aug-2001 00007 // \brief Load properties from text files 00008 // Along with mbl_parse_block, the functions in this file allow you to quickly 00009 // parse hierarchical property files that look like: 00010 // \verbatim 00011 // { 00012 // n_disp_dx: 3 00013 // n_synth_targets: 30 00014 // parameter_displacements: 00015 // { 00016 // 0 1 2 3 00017 // } 00018 // rfunc: m32_simple_residual_function 00019 // { 00020 // model: 00021 // { 00022 // apm_filepath: ../hip_cropped_lres.apm 00023 // background_filepath: gen:100x50x80:vxl_int_32:0 00024 // foobar_modifier: foobar_strategy3 00025 // } 00026 // comparator: 00027 // { 00028 // type: comparator_strategy2 00029 // initial_geometry: 00030 // { 00031 // centre_of_source: 0 -1000 0 00032 // centre_of_image_plane: 0 300 0 00033 // } 00034 // } 00035 // comparator: 00036 // { 00037 // type: comparator_strategy2 00038 // initial_geometry: 00039 // { 00040 // centre_of_source: 860.365 -1228.73 0 00041 // centre_of_image_plane: -172.073 245.746 0 00042 // } 00043 // } 00044 // } 00045 // } 00046 // \endverbatim 00047 00048 00049 #include <vcl_map.h> 00050 #include <vcl_iosfwd.h> 00051 #include <vcl_string.h> 00052 00053 //: The type of the property dictionary 00054 class mbl_read_props_type : public vcl_map<vcl_string, vcl_string > 00055 { 00056 public: 00057 00058 //: Return the contents for a given property prop. 00059 // prop is removed from the property list. 00060 // \throws mbl_exception_missing_property if prop doesn't exist 00061 vcl_string get_required_property(const vcl_string &prop); 00062 00063 //: Return the contents for a given property prop. 00064 // prop is removed from the property list. 00065 // Returns def_value, or empty string if prop doesn't exist. 00066 // \note This is a fairly trivial function, intended simply as a 00067 // convenient analogue to get_required_property(). 00068 vcl_string get_optional_property(const vcl_string &prop, const vcl_string &def_value = ""); 00069 }; 00070 00071 00072 //: Read properties from a text stream. 00073 // The function will terminate on an eof. If one of 00074 // the opening lines contains an opening brace '{', then the function 00075 // will also stop reading the stream after finding a line containing 00076 // a closing brace '}' 00077 // 00078 // There should be one property per line, with a colon ':' after 00079 // each property label. Each property label should not contain 00080 // any whitespace. However, property values may contain white space 00081 // they are assumed to be terminated by an end-of-line. 00082 // 00083 // Differs from mbl_read_props_ws(afs) in that it treats everything 00084 // after the label up to the end-of-line as the value. 00085 // Thus it treats the single line "a: a b: b" as 00086 // containing a single label ("a:") with value "a b: b". 00087 // If you want such a line treated as two label-values, use mbl_read_props_ws() 00088 mbl_read_props_type mbl_read_props(vcl_istream &afs); 00089 00090 00091 //: Read properties from a text stream. 00092 // The function will terminate on an eof. If one of 00093 // the opening lines contains an opening brace '{', then the function 00094 // will also stop reading the stream after finding a line containing 00095 // a closing brace '}' 00096 // 00097 // Every property label ends in ":", and should not contain 00098 // any whitespace. 00099 // If there is a brace after the first string following the label, 00100 // the following text up to matching 00101 // braces is included in the property value. 00102 // 00103 // Differs from mbl_read_props(afs) in that all whitespace is treated 00104 // as a separator. Thus it treats the single line "a: a b: b" as 00105 // containing two separate label-value pairs. 00106 mbl_read_props_type mbl_read_props_ws(vcl_istream &afs); 00107 00108 00109 //: Print a list of properties for debugging purposes. 00110 void mbl_read_props_print(vcl_ostream &afs, mbl_read_props_type props); 00111 00112 //: Print a list of properties for debugging purposes. Limit each property value length to \p max_chars 00113 // Useful for preventing diagnostic output from being flooded by large properties. 00114 void mbl_read_props_print(vcl_ostream &afs, mbl_read_props_type props, unsigned max_chars); 00115 00116 //: merge two property sets. 00117 // \param first_overrides 00118 // properties in "first" will override identically named properties in "second" 00119 mbl_read_props_type mbl_read_props_merge( 00120 const mbl_read_props_type& first, 00121 const mbl_read_props_type& second, 00122 bool first_overrides=true); 00123 00124 00125 //: Throw error if there are any keys in props that aren't in ignore. 00126 // \throw mbl_exception_unused_props 00127 void mbl_read_props_look_for_unused_props( 00128 const vcl_string & function_name, 00129 const mbl_read_props_type &props, 00130 const mbl_read_props_type &ignore); 00131 00132 00133 //: Throw error if there are any keys in props. 00134 // \throw mbl_exception_unused_props 00135 inline void mbl_read_props_look_for_unused_props( 00136 const vcl_string & function_name, 00137 const mbl_read_props_type &props) 00138 { 00139 mbl_read_props_look_for_unused_props(function_name, props, 00140 mbl_read_props_type()); 00141 } 00142 00143 00144 #endif // mbl_read_props_h