Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include <vcl_string.h>
00007 #include <vcl_cstddef.h>
00008 #include <vcl_iostream.h>
00009 #include <vcl_fstream.h>
00010 #include <vcl_sstream.h>
00011 #include <vcl_algorithm.h>
00012 #include <vul/vul_arg.h>
00013 #include <vcl_exception.h>
00014 #include <mbl/mbl_mask.h>
00015
00016
00017 bool load_vals(vcl_vector<vcl_string> & values, const vcl_string & filename);
00018 bool load_vals(vcl_vector<vcl_string> & values, const vcl_string & filename, const vcl_string & delim);
00019 void write_vals(const vcl_vector<vcl_string> & values, vcl_ostream & os);
00020
00021
00022 int main(int argc, char **argv)
00023 {
00024 vul_arg<vcl_string> mask_filename(0, "Input mask file");
00025 vul_arg<vcl_string> values_filename(0, "Input values file");
00026 vul_arg<vcl_string> output_filename("-out", "Output values file - write to standard out if not set");
00027 vul_arg<vcl_string> delim("-delim", "Delimiter character for values file - one entry per line if not set");
00028 vul_arg_parse(argc, argv);
00029
00030 if (delim.set() && delim().length() != 1)
00031 {
00032 vcl_cout << "User-defined delimiter should be one character" << vcl_endl;
00033 return 1;
00034 }
00035
00036 mbl_mask mask;
00037 mbl_load_mask(mask, mask_filename().c_str());
00038
00039 vcl_vector<vcl_string> values;
00040 bool loaded_vals;
00041 if (delim.set())
00042 loaded_vals = load_vals(values, values_filename(), delim());
00043 else
00044 loaded_vals = load_vals(values, values_filename());
00045
00046 if (!loaded_vals)
00047 {
00048 vcl_cout << "Unable to load input data from " << values_filename() << vcl_endl;
00049 return 1;
00050 }
00051
00052 try { mbl_apply_mask(mask, values); }
00053 catch (vcl_exception & e)
00054 {
00055 vcl_cout << "An error occurred while applying the mask.\n" << e.what() << vcl_endl;
00056 return 1;
00057 }
00058 catch (...)
00059 {
00060 vcl_cout << "An unknown error occurred while applying the mask." << vcl_endl;
00061 return 1;
00062 }
00063
00064 if (output_filename.set())
00065 {
00066 vcl_ofstream val_out(output_filename().c_str());
00067 if (!val_out)
00068 {
00069 vcl_cout << "Unable to save output data to " << output_filename() << vcl_endl;
00070 return 1;
00071 }
00072 write_vals(values, val_out);
00073 val_out.close();
00074 }
00075 else write_vals(values, vcl_cout);
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 vcl_string trim(const vcl_string & s)
00086 {
00087 vcl_size_t start = s.find_first_not_of(" ");
00088 if (start == vcl_string::npos) return "";
00089 unsigned end = s.find_last_not_of(" ");
00090 return s.substr(start, 1+end-start);
00091 }
00092
00093 void split_and_add(vcl_vector<vcl_string> & values, const vcl_string & string, const vcl_string & delim)
00094 {
00095 vcl_size_t start, next = vcl_string::npos;
00096 while (start = next+1, next = string.find_first_of(delim, start), vcl_string::npos != next)
00097 {
00098 vcl_string token = string.substr(start, next-start);
00099 values.push_back(trim(token));
00100 }
00101 vcl_string last = trim(string.substr(start));
00102 if (last.length() != 0) values.push_back(last);
00103
00104 }
00105
00106 bool load_vals(vcl_vector<vcl_string> & values, const vcl_string & filename, const vcl_string & delim)
00107 {
00108 vcl_ifstream fin(filename.c_str());
00109 if (!fin) return false;
00110
00111 values.clear();
00112
00113 vcl_string line;
00114 while (vcl_getline(fin, line))
00115 {
00116 line = trim(line);
00117 if (line.length() == 0) continue;
00118 split_and_add(values, line, delim);
00119 }
00120
00121 return true;
00122 }
00123
00124 bool load_vals(vcl_vector<vcl_string> & values, const vcl_string & filename)
00125 {
00126 vcl_ifstream fin(filename.c_str());
00127 if (!fin) return false;
00128
00129 values.clear();
00130
00131 vcl_string line;
00132 while (vcl_getline(fin, line))
00133 {
00134 line = trim(line);
00135 if (line.length() == 0) continue;
00136 values.push_back(line);
00137 }
00138
00139 return true;
00140 }
00141
00142 void write_vals(const vcl_vector<vcl_string> & values, vcl_ostream & os)
00143 {
00144 vcl_copy(values.begin(), values.end(), vcl_ostream_iterator<vcl_string>(os, "\n"));
00145 }