1
- #include < array>
2
- #include < exception>
3
- #include < filesystem>
4
- #include < fmt/core.h>
5
- #include < fstream>
6
- #include < random>
7
- #include < vector>
1
+ #include < catify.hpp>
8
2
9
3
namespace fs = std::filesystem;
10
4
11
- std::string read_all (fs::path const & p, std::fstream& file) {
12
- std::string buffer (fs::file_size (p), ' \0 ' );
13
- file.read (buffer.data (), buffer.size ());
14
- return buffer;
15
- }
16
-
17
- int catify (fs::path const & p) {
18
- std::fstream file (p);
19
- std::string contents = read_all (p, file);
20
- using std::string_view_literals::operator " " sv;
21
-
22
- constexpr std::array endings = {" π" sv, " π" sv, " π
" sv, " π" sv, " π" sv};
23
-
24
- std::random_device rd;
25
-
26
- std::string_view ending1 = endings[rd () % endings.size ()];
27
- std::string_view ending2;
28
- do {
29
- ending2 = endings[rd () % endings.size ()];
30
- } while (ending2 == ending1);
31
-
32
- std::string_view sv = contents;
33
-
34
- for (size_t start = 0 ; start < sv.size ();) {
35
- size_t end = sv.find (' \n ' , start);
36
- auto line = sv.substr (start, end - start);
37
- if (line.size () == 0 || line[0 ] == ' #' ) {
38
- start = end + 1 ;
39
- continue ;
40
- }
41
-
42
- file.seekp (end);
43
- file << ' ' << ending1 << ending2;
44
- file << sv.substr (end);
45
- return 0 ;
46
- }
47
-
48
- fmt::print (" Could not find suitable point in file to put the cat.\n " );
49
- return 1 ;
50
- }
51
5
int main (int argc, char ** argv) {
52
-
53
-
54
6
if (argc == 1 ) {
55
7
fmt::print (" Error: no file specified.\n " );
56
8
return 1 ;
@@ -62,5 +14,5 @@ int main(int argc, char** argv) {
62
14
return 1 ;
63
15
}
64
16
65
- return catify (file);
17
+ return catify::catify (file);
66
18
}
0 commit comments