datafile.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. */
  33. #ifndef datafile_hpp
  34. #define datafile_hpp
  35. #include <vector>
  36. #include <string>
  37. #include <fstream>
  38. #include <iostream>
  39. namespace fv::util{
  40. /**
  41. * Splits input string into substrings based on provided sep char.
  42. * For example, split("1,2,3", ',') -> {"1","2","3"}.
  43. */
  44. std::vector<std::string> split(const std::string& to_split, char sep){
  45. std::vector<std::string> substrings;
  46. std::string substring;
  47. for(const char &c : to_split){
  48. if(c == sep){
  49. substrings.push_back(substring);
  50. substring.clear();
  51. } else{
  52. substring.push_back(c);
  53. }
  54. }
  55. if(substring.size() > 0){
  56. substrings.push_back(substring);
  57. }
  58. return substrings;
  59. }
  60. /**
  61. * Removes leading and trailing whitespace from s
  62. */
  63. std::string& strip(std::string& s){
  64. int idx;
  65. //strip left side whitespace
  66. for(idx=0; idx<s.size(); idx++){
  67. char& c = s[idx];
  68. if(c == ' ' || c == '\t') continue;
  69. break;
  70. }
  71. s.erase(0,idx);
  72. //strip right side whitespace
  73. for(idx=s.size()-1; idx>=0; idx--){
  74. char& c = s[idx];
  75. if(c == ' ' || c == '\t') continue;
  76. break;
  77. }
  78. s.erase(idx+1,s.size()-idx-1);
  79. return s;
  80. }
  81. struct DataFileDescriptor{
  82. std::string filename;
  83. std::string label;
  84. std::string category;
  85. int file_number;
  86. DataFileDescriptor():filename(""), label(""),category(""),file_number(-1) { }
  87. DataFileDescriptor(const std::string filename, const std::string label, const std::string category, const int file_number)
  88. :filename(filename), label(label),category(category),file_number(file_number) { }
  89. DataFileDescriptor(const std::string filename, const std::string label, const std::string category)
  90. :filename(filename), label(label),category(category),file_number(-1) { }
  91. DataFileDescriptor(const std::string line)
  92. :filename(""),label(""),category(""),file_number(-1){
  93. auto fields = split(line, ':');
  94. filename = strip(fields[0]);
  95. if(fields.size()>1) label=strip(fields[1]);
  96. if(fields.size()>2) label=strip(fields[2]);
  97. }
  98. };
  99. // input list should be a file containing line-delineated filename for all Data
  100. // Files with an optional label separated by a comma.
  101. std::vector<DataFileDescriptor> read_input_list(const std::string &filename){
  102. std::ifstream istrm(filename, std::ios::in);
  103. std::string line;
  104. std::vector<DataFileDescriptor> dfds;
  105. std::string prefix = filename.substr(0, filename.find_last_of("/")+1);
  106. while(std::getline(istrm, line)){
  107. strip(line);
  108. if(line[0] != '#')
  109. dfds.push_back(DataFileDescriptor(prefix+line));
  110. }
  111. return dfds;
  112. }
  113. }
  114. #endif // datafile_hpp