123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- /**
- * @file
- * @author Caleb Fangmeier <caleb@fangmeier.tech>
- * @version 0.1
- *
- * @section LICENSE
- *
- *
- * MIT License
- *
- * Copyright (c) 2017 Caleb Fangmeier
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- * @section DESCRIPTION
- */
- #ifndef datafile_hpp
- #define datafile_hpp
- #include <vector>
- #include <string>
- #include <fstream>
- #include <iostream>
- namespace fv::util{
- /**
- * Splits input string into substrings based on provided sep char.
- * For example, split("1,2,3", ',') -> {"1","2","3"}.
- */
- std::vector<std::string> split(const std::string& to_split, char sep){
- std::vector<std::string> substrings;
- std::string substring;
- for(const char &c : to_split){
- if(c == sep){
- substrings.push_back(substring);
- substring.clear();
- } else{
- substring.push_back(c);
- }
- }
- if(substring.size() > 0){
- substrings.push_back(substring);
- }
- return substrings;
- }
- /**
- * Removes leading and trailing whitespace from s
- */
- std::string& strip(std::string& s){
- int idx;
- //strip left side whitespace
- for(idx=0; idx<s.size(); idx++){
- char& c = s[idx];
- if(c == ' ' || c == '\t') continue;
- break;
- }
- s.erase(0,idx);
- //strip right side whitespace
- for(idx=s.size()-1; idx>=0; idx--){
- char& c = s[idx];
- if(c == ' ' || c == '\t') continue;
- break;
- }
- s.erase(idx+1,s.size()-idx-1);
- return s;
- }
- struct DataFileDescriptor{
- std::string filename;
- std::string label;
- std::string category;
- int file_number;
- DataFileDescriptor():filename(""), label(""),category(""),file_number(-1) { }
- DataFileDescriptor(const std::string filename, const std::string label, const std::string category, const int file_number)
- :filename(filename), label(label),category(category),file_number(file_number) { }
- DataFileDescriptor(const std::string filename, const std::string label, const std::string category)
- :filename(filename), label(label),category(category),file_number(-1) { }
- DataFileDescriptor(const std::string line)
- :filename(""),label(""),category(""),file_number(-1){
- auto fields = split(line, ':');
- filename = strip(fields[0]);
- if(fields.size()>1) label=strip(fields[1]);
- if(fields.size()>2) label=strip(fields[2]);
- }
- };
- // input list should be a file containing line-delineated filename for all Data
- // Files with an optional label separated by a comma.
- std::vector<DataFileDescriptor> read_input_list(const std::string &filename){
- std::ifstream istrm(filename, std::ios::in);
- std::string line;
- std::vector<DataFileDescriptor> dfds;
- std::string prefix = filename.substr(0, filename.find_last_of("/")+1);
- while(std::getline(istrm, line)){
- strip(line);
- if(line[0] != '#')
- dfds.push_back(DataFileDescriptor(prefix+line));
- }
- return dfds;
- }
- }
- #endif // datafile_hpp
|