-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubread.cpp
62 lines (50 loc) · 1.65 KB
/
subread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <filesystem>
std::vector<std::string> args;
std::string prog_basename;
bool readsubfile(std::string fname, std::uintmax_t start_read=0, std::uintmax_t length_read=0) {
std::uintmax_t file_length=std::filesystem::file_size(std::filesystem::path{fname});
if (start_read > file_length) {
std::cerr << "Trying to read starting from character " << start_read << ", which is beyond file length of " << file_length << " characters" << std::endl;
}
std::ifstream in(fname);
if (in.is_open()) {
if (start_read > 0) in.seekg(start_read);
std::uintmax_t l=0;
char c;
while(in.good()) {
in.get(c);
std::cout << c;
if (length_read > 0 && l > length_read) break;
l++;
}
std::cout << std::endl;
in.close();
} else {
std::cerr << "Could not open " << fname << std::endl;
return false;
}
return true;
}
int main(int argc, char *argv[]) {
args=std::vector<std::string>(argv, argv + argc);
prog_basename=std::filesystem::path(args[0]).stem().string();
args.erase(args.begin());
// filename in args[0]
if (args.size() < 1) {
std::cerr << "At least filename" << std::endl;
std::cerr << "Optionnally start read and length read" << std::endl;
return 1;
}
// start char in args[1] optionnale, default 0
// read length in args[2] optionnal, default to end
std::uintmax_t start_read=0, length_read=0;
if (args.size() >= 2) start_read=std::stoi(args[1]);
if (args.size() >= 3) length_read=std::stoi(args[2]);
if (readsubfile(args[0], start_read, length_read)) return 0;
return 1;
}