From 8157c3921287b34544ef9e5d823a635e818bf4dd Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Tue, 9 Dec 2014 09:04:43 -0600 Subject: [PATCH] added parser and filename processing --- envi/binary.h | 23 ++++++----------------- grids/grid_data.cuh | 33 --------------------------------- grids/grid_data.h | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ grids/image_stack.h | 8 ++++++++ parser/arguments.h | 436 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ parser/filename.h | 253 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ parser/parser.h | 29 +++++++++++++++++++++++++++++ parser/wildcards.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui/arguments.h | 436 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 9 files changed, 889 insertions(+), 486 deletions(-) delete mode 100644 grids/grid_data.cuh create mode 100644 grids/grid_data.h create mode 100644 parser/arguments.h create mode 100644 parser/filename.h create mode 100644 parser/parser.h create mode 100644 parser/wildcards.h delete mode 100644 ui/arguments.h diff --git a/envi/binary.h b/envi/binary.h index 883e3b6..5f934c4 100644 --- a/envi/binary.h +++ b/envi/binary.h @@ -112,21 +112,21 @@ public: } //save one band from the memory to the file - bool writeZ( T * p, unsigned int page){ + bool write_page( T * p, unsigned int page){ if(p == NULL){ std::cout<<"ERROR: unable to write into file, empty pointer"<= R[2]){ //make sure the bank number is right std::cout<<"ERROR: page out of range"<(X), h); - } - - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int h = 0){ - return open(filename, vec(X, Y), h); - } - - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int Z, unsigned int h = 0){ - return open(filename, vec(X, Y, Z), h); - }*/ }; diff --git a/grids/grid_data.cuh b/grids/grid_data.cuh deleted file mode 100644 index 4df492e..0000000 --- a/grids/grid_data.cuh +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef STIM_GRID_DATA_CUH -#define STIM_GRID_DATA_CUH - -#include -#include -#include - -#include "../cuda/threads.h" -#include "../cuda/error.h" -#include "../cuda/devices.h" - - -namespace stim{ - -//This object describes a generic D-dimensional grid containing data of type T - // data can be stored on the GPU or CPU (and transferred between the two) - // data can be loaded in the form of images - // data can be saved in the form of binary files -template -class grid_data{ - -protected: - - unsigned long R[D]; //elements in each dimension - T* ptr; //pointer to the data (on the GPU or CPU) - bool gpu; //true if the data is on the GPU - -}; - -} - - -#endif \ No newline at end of file diff --git a/grids/grid_data.h b/grids/grid_data.h new file mode 100644 index 0000000..449416e --- /dev/null +++ b/grids/grid_data.h @@ -0,0 +1,75 @@ +#ifndef STIM_GRID_DATA_CUH +#define STIM_GRID_DATA_CUH + +#include +#include +#include + +#include "../cuda/threads.h" +#include "../cuda/error.h" +#include "../cuda/devices.h" +#include "../math/vector.h" + + +namespace stim{ + +//This object describes a generic D-dimensional grid containing data of type T + // data can be stored on the GPU or CPU (and transferred between the two) + // data can be loaded in the form of images + // data can be saved in the form of binary files +template +class grid_data{ + +protected: + + stim::vector R; //elements in each dimension + T* ptr; //pointer to the data (on the GPU or CPU) + bool gpu; //true if the data is on the GPU + + //return the total number of values in the binary file + unsigned long samples(){ + + unsigned long s = 1; + for(unsigned int d = 0; d < D; d++) + s *= R[d]; + + return s; + + } + +public: + + //write data to disk + void write(std::string filename){ + + //open the file as binary for reading + file.open(filename.c_str(), std::ios::out | std::ios::binary); + + //write file to disk + file.write((char *)p, samples() * sizeof(T)); + } + + //load a binary file from disk + // header size is in bytes + void read(std::string filename, stim::vector S, unsigned long header = 0){ + + R = S; //set the sample resolution + + //open the file as binary for writing + file.open(filename.c_str(), std::ios::in | std::ios::binary); + + //seek past the header + file.seekg(header, std::ios::beg); + + //read the data + file.read((char *)p, samples() * sizeof(T)); + } + + + +}; + +} + + +#endif \ No newline at end of file diff --git a/grids/image_stack.h b/grids/image_stack.h index b1dcd95..43929d6 100644 --- a/grids/image_stack.h +++ b/grids/image_stack.h @@ -8,6 +8,14 @@ namespace stim{ template class image_stack : public virtual grid_data{ +public: + + void load(std::string wild_name){ + + } + + + }; diff --git a/parser/arguments.h b/parser/arguments.h new file mode 100644 index 0000000..a82584c --- /dev/null +++ b/parser/arguments.h @@ -0,0 +1,436 @@ +#ifndef STIM_ARGUMENTS +#define STIM_ARGUMENTS + +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +namespace stim{ + + class cmd_option + { + private: + bool ansi; + + //argument name + std::string name; + + //description of the argument + std::vector desc; + + //argument values + std::vector vals; + + //range or example + std::string range; + + //flag is true when the argument is user-specified + bool flag; + + void parse_val(const std::string &s){ + + vals.clear(); + + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, ' ')) { + vals.push_back(item); + } + } + + void parse_desc(const std::string &s){ + + desc.clear(); + + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, '\n')) { + desc.push_back(item); + } + } + + public: + void set_ansi(bool b){ ansi = b; } + //create an option with a given name, description, and default value + cmd_option(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") + { + name = _name; + parse_desc(_desc); + parse_val(_default); + + //if a default value is provided, set the flag + if(_default != "") + flag = true; + else flag = false; + + range = _range; + + + } + + int nargs() + { + return vals.size(); + } + + //return the value of a text option + std::string as_string(unsigned int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - Option requested without being set: "< n) + return vals[n]; + + else return ""; + } + + //return the value of a floating point option + float as_float(unsigned int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - option requested without being set: "< n) + { + float r; + if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; + return r; + } + + else return 0; + } + + //return the value of an integer option + int as_int(unsigned int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - option requested without being set: "< n) + { + int r; + if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; + return r; + } + + else return 0; + } + + //get the width of the left column + int col_width() + { + int n = 3; + //add the length of the option name + n += name.size(); + + //if there are any default parameters + if(vals.size() > 0) + { + //padding (parenthesis, =, etc.) + n += 6; + + //for each default option value + for(unsigned int v=0; v opts; + std::vector args; + + //column width of the longest option + int col_width; + + //list of sections + std::vector sections; + + public: + + arglist(){ + col_width = 0; + ansi = true; + } + + void set_ansi(bool b) + { + ansi = b; + for(unsigned int i=0; i(col_width, opt.col_width()); + } + + void section(std::string _name) + { + argsection s; + s.name = _name; + s.index = opts.size(); + sections.push_back(s); + } + + //output the options (generally in response to --help) + std::string str() + { + std::stringstream ss; + + int si = -1; + + if(sections.size() > 0) + si = 0; + + //for each option + for(unsigned int a=0; a= opts.size()) + return -1; + + return (int)i; + } + + void set(std::string _name, std::string _value) + { + int i = index(_name); + + if(i != -1) + { + opts[i].set(_value); + //adjust the column width if necessary + col_width = (std::max)(col_width, opts[i].col_width()); + } + else + std::cout<<"ERROR - option not recognized: "<<_name< /* defines FILENAME_MAX */ +#ifdef WINDOWS + #include + #define GetCurrentDir _getcwd + #define STIM_FILENAME_DIV '\\' +#else + #include + #define GetCurrentDir getcwd + #define STIM_FILENAME_DIV '/' + #endif + +#include +#include +#include +#include +#include + +#include "../parser/parser.h" + +#include + +namespace stim{ + +//filename class designed to work with both Windows and Unix + +class filename{ + +private: + void init(){ + + + } + +protected: + + std::string drive; //drive letter (for Windows) + std::vector path; //directory hierarchy + std::string prefix; //file prefix (without extension) + std::string ext; //file extension + + //replace any incorrect dividers with the appropriate version for the OS + std::string parse_div(std::string s) { + #ifdef _WIN32 + std::replace( s.begin(), s.end(), '/', '\\'); + #else + std::replace( s.begin(), s.end(), '\\', '/'); + #endif + + return s; + } + + //parse the file name (prefix and extension) + void parse_name(std::string fname){ + + //find the extension + size_t xpos = fname.find_last_of('.'); //find the position of the extension period (if there is one) + if(xpos != std::string::npos){ //split the prefix and extension + prefix = fname.substr(0, xpos); + ext = fname.substr(xpos + 1); + } + else + prefix = fname; + } + + //parse a file locator string + void parse(std::string loc){ + + //determine the drive (if Windows) + drive = ""; + #ifdef _WIN32 + //if the second character is a colon, the character right before it is the drive + if(loc[1] == ':'){ + drive = loc[0]; //copy the drive letter + loc = loc.substr(2); //remove the drive information from the locator string + } + #endif + + //remove the initial divider + if(loc[0] == STIM_FILENAME_DIV) + loc = loc.substr(1); + + //determine the file name + std::string fname = loc.substr( loc.find_last_of(STIM_FILENAME_DIV) + 1 ); //find the file name (including extension) + + //parse the file name + parse_name(fname); + + //find the directory hierarchy + std::string dir = loc.substr(0, loc.find_last_of(STIM_FILENAME_DIV) + 1 ); + path = stim::parser::split(dir, STIM_FILENAME_DIV); + } + +public: + + filename(){ + init(); + } + + filename(std::string loc){ + init(); + parse(loc); + } + + + + std::string get_name(){ + if(prefix == "" && ext == "") + return ""; + else + return prefix + "." + ext; + } + + std::string get_extension(){ + return ext; + } + + std::string get_prefix(){ + return prefix; + } + + std::string dir(){ + std::stringstream ss; + + //output the drive letter if in Windows + #ifdef _WIN32 + ss< get_list(){ + + boost::filesystem::path p(dir()); //create a path from the current filename + + std::vector file_list; + + if(boost::filesystem::exists(p)){ + if(boost::filesystem::is_directory(p)){ + + typedef std::vector vec; // store paths, + vec v; // so we can sort them later + + std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(v)); + + std::sort(v.begin(), v.end()); // sort, since directory iteration + // is not ordered on some file systems + + //compare file names to the current template (look for wild cards) + for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) + { + + //if the filename is a wild card *or* it matches the read file name + if( prefix == "*" || prefix == (*it).filename().stem().string()){ + + //if the extension is a wild card *or* it matches the read file extension + if( ext == "*" || "." + ext == (*it).filename().extension().string()){ + + file_list.push_back((*it).string()); //include it in the final list + } + } + + } + } + } + + return file_list; + } + + //gets the current working directory + static stim::filename cwd(){ + + + char cCurrentPath[FILENAME_MAX]; + + if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))){ + std::cout<<"ERROR getting current working directory."< rel_path = stim::parser::split(rel, STIM_FILENAME_DIV); + + //if the relative path doesn't contain a file, add a blank string to be used as the filename + if(rel[rel.size()-1] == STIM_FILENAME_DIV) + rel_path.push_back(""); + + //create a stack representing the current absolute path + std::stack > s(path); + + //for each token in the relative path + for(int d=0; d result_path(begin, end); + + //create a new path to be returned + stim::filename result; + result.path = result_path; + result.set_name(rel_path.back()); + + return result; + } + + + + + + + + +}; + + +} //end namespace stim + +#endif diff --git a/parser/parser.h b/parser/parser.h new file mode 100644 index 0000000..2583fde --- /dev/null +++ b/parser/parser.h @@ -0,0 +1,29 @@ +#ifndef STIM_PARSER_H +#define STIM_PARSER_H + +namespace stim{ + +class parser{ + +public: + + static std::vector &split(const std::string &s, char delim, std::vector &elems) { + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, delim)) { + elems.push_back(item); + } + return elems; + } + + static std::vector split(const std::string &s, char delim) { + std::vector elems; + split(s, delim, elems); + return elems; + } + +}; + +} //end namespace stim + +#endif \ No newline at end of file diff --git a/parser/wildcards.h b/parser/wildcards.h new file mode 100644 index 0000000..ac8c42d --- /dev/null +++ b/parser/wildcards.h @@ -0,0 +1,82 @@ +#ifndef STIM_WILDCARDS_H +#define STIM_WILDCARDS_H + +#include +#include + + +//#include + +namespace stim{ + +class wildcards{ +public: + //generate a sequence of strings where '?' is replaced by integer increments + static std::vector increment(std::string input, + unsigned int start, + unsigned int end, + unsigned int step){ + + size_t a = input.find_first_of('?'); //find the first ? + size_t b = input.find_last_of('?'); //find the last ? + size_t n = b - a + 1; //calculate the number of ? + + std::string str_a = input.substr(0, a); //split the strings along the wildcard + std::string str_b = input.substr(b + 1, std::string::npos); + + //create a vector to hold the output strings + std::vector out; + + //create a stringstream to handle the padding + for (unsigned int i = start; i <= end; i += step){ + + std::stringstream ss; + ss << str_a + << std::setfill('0') + << std::setw(n) + << i + << str_b; + + out.push_back(ss.str()); + } + + return out; + + + } +/* + //returns a list of files that match the specified wildcards in 'd' + static std::vector get_file_list(std::string s){ + + stim::filename f(s); + std::string target_path(f.dir()); + boost::regex filter(f.name()); + + std::vector< std::string > all_matching_files; + + boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end + for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i ) + { + // Skip if not a file + if( !boost::filesystem::is_regular_file( i->status() ) ) continue; + + boost::smatch what; + + // Skip if no match + if( !boost::regex_match( i->path().filename().string(), what, filter ) ) continue; + + // File matches, store it + all_matching_files.push_back( i->path().filename().string() ); + } + + return all_matching_files; + + } +*/ + +}; + + +} //end namespace stim + +#endif diff --git a/ui/arguments.h b/ui/arguments.h deleted file mode 100644 index a82584c..0000000 --- a/ui/arguments.h +++ /dev/null @@ -1,436 +0,0 @@ -#ifndef STIM_ARGUMENTS -#define STIM_ARGUMENTS - -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#include -#endif - -namespace stim{ - - class cmd_option - { - private: - bool ansi; - - //argument name - std::string name; - - //description of the argument - std::vector desc; - - //argument values - std::vector vals; - - //range or example - std::string range; - - //flag is true when the argument is user-specified - bool flag; - - void parse_val(const std::string &s){ - - vals.clear(); - - std::stringstream ss(s); - std::string item; - while (std::getline(ss, item, ' ')) { - vals.push_back(item); - } - } - - void parse_desc(const std::string &s){ - - desc.clear(); - - std::stringstream ss(s); - std::string item; - while (std::getline(ss, item, '\n')) { - desc.push_back(item); - } - } - - public: - void set_ansi(bool b){ ansi = b; } - //create an option with a given name, description, and default value - cmd_option(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") - { - name = _name; - parse_desc(_desc); - parse_val(_default); - - //if a default value is provided, set the flag - if(_default != "") - flag = true; - else flag = false; - - range = _range; - - - } - - int nargs() - { - return vals.size(); - } - - //return the value of a text option - std::string as_string(unsigned int n = 0) - { - if(!flag) - { - std::cout<<"ERROR - Option requested without being set: "< n) - return vals[n]; - - else return ""; - } - - //return the value of a floating point option - float as_float(unsigned int n = 0) - { - if(!flag) - { - std::cout<<"ERROR - option requested without being set: "< n) - { - float r; - if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; - return r; - } - - else return 0; - } - - //return the value of an integer option - int as_int(unsigned int n = 0) - { - if(!flag) - { - std::cout<<"ERROR - option requested without being set: "< n) - { - int r; - if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; - return r; - } - - else return 0; - } - - //get the width of the left column - int col_width() - { - int n = 3; - //add the length of the option name - n += name.size(); - - //if there are any default parameters - if(vals.size() > 0) - { - //padding (parenthesis, =, etc.) - n += 6; - - //for each default option value - for(unsigned int v=0; v opts; - std::vector args; - - //column width of the longest option - int col_width; - - //list of sections - std::vector sections; - - public: - - arglist(){ - col_width = 0; - ansi = true; - } - - void set_ansi(bool b) - { - ansi = b; - for(unsigned int i=0; i(col_width, opt.col_width()); - } - - void section(std::string _name) - { - argsection s; - s.name = _name; - s.index = opts.size(); - sections.push_back(s); - } - - //output the options (generally in response to --help) - std::string str() - { - std::stringstream ss; - - int si = -1; - - if(sections.size() > 0) - si = 0; - - //for each option - for(unsigned int a=0; a= opts.size()) - return -1; - - return (int)i; - } - - void set(std::string _name, std::string _value) - { - int i = index(_name); - - if(i != -1) - { - opts[i].set(_value); - //adjust the column width if necessary - col_width = (std::max)(col_width, opts[i].col_width()); - } - else - std::cout<<"ERROR - option not recognized: "<<_name<