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