Atlas Game Manager
A game manager for f95 and dlsite written in c++
Loading...
Searching...
No Matches
FileScanner.hpp
Go to the documentation of this file.
1//
2// Created by kj16609 on 4/9/23.
3//
4
5#pragma once
6#ifndef ATLASGAMEMANAGER_FILESCANNER_HPP
7#define ATLASGAMEMANAGER_FILESCANNER_HPP
8
9#include <coroutine>
10#include <filesystem>
11#include <string>
12#include <vector>
13
14#include "core/exceptions.hpp"
16
17namespace atlas::utils
18{
19
20 struct FileInfo
21 {
22 std::string filename { "" };
23 std::string ext { "" };
24 std::filesystem::path path { "" };
25 std::size_t size { 0 };
26 std::uint8_t depth { 0 };
27 std::filesystem::path relative { "" };
28
29 FileInfo() = delete;
30
32 std::filesystem::path path_in,
33 const std::filesystem::path& source,
34 const std::size_t filesize,
35 const std::uint8_t file_depth ) :
36 filename( path_in.filename().string() ),
37 ext( path_in.extension().string() ),
38 path( path_in ),
39 size( filesize ),
40 depth( file_depth ),
41 relative( std::filesystem::relative( std::move( path_in ), source ) )
42 {}
43 };
44
46 {
47 struct promise_type;
48 using handle_type = std::coroutine_handle< promise_type >;
49
51 {
52 std::optional< FileInfo > value { std::nullopt };
53 std::exception_ptr exception { nullptr };
54
56 {
57 return FileScannerGenerator( handle_type::from_promise( *this ) );
58 }
59
60 std::suspend_always initial_suspend() noexcept { return {}; }
61
62 std::suspend_always final_suspend() noexcept { return {}; }
63
64 void unhandled_exception() { std::rethrow_exception( exception ); }
65
66 void return_value( FileInfo&& from )
67 {
68 if ( from.filename == "" )
69 throw AtlasException( "FileScannerGenerator: return value had no filename!" );
70
71 value = std::move( from );
72 }
73
74 std::suspend_always yield_value( FileInfo from )
75 {
76 if ( from.filename == "" )
77 throw AtlasException( "FromScannerGenerator:: yield value had no filename!" );
78
79 value = std::move( from );
80 return {};
81 }
82 };
83
85
87
88 ~FileScannerGenerator() { m_h.destroy(); }
89
91 };
92
94 {
95 private:
96
97 std::filesystem::path m_path;
99 std::vector< FileInfo > files;
100 const FileInfo& at( std::size_t index );
101
102 friend class iterator;
103
105 {
106 std::size_t m_idx { 0 };
108
109 public:
110
111 iterator( const std::size_t idx, FileScanner& scanner ) : m_idx( idx ), m_scanner( scanner ) {}
112
114 {
115 ++m_idx;
116 return *this;
117 }
118
119 // Operator != required to check for end I assume. Where if the this returns true then we are good to continue
120 // So instead we can just return the state of the scanner. And if the scanner is complete then we'll return false here.
121 //bool operator !=
122 bool operator==( const std::unreachable_sentinel_t ) const;
123
124 // Required for the for loop
125 const FileInfo& operator*() { return m_scanner.at( m_idx ); }
126 };
127
128 public:
129
130 FileScanner( const std::filesystem::path& path );
131
132 iterator begin() { return iterator( 0, *this ); }
133
134 //This *probably* isn't required(?) but the for loop will want it anyways. So we can just return literaly anything here since it's not used anyways.
135 std::unreachable_sentinel_t end() { return {}; }
136
137 std::filesystem::path path() const { return m_path; }
138 };
139} // namespace atlas::utils
140#endif //ATLASGAMEMANAGER_FILESCANNER_HPP
Definition FileScanner.hpp:105
FileScanner & m_scanner
Definition FileScanner.hpp:107
iterator(const std::size_t idx, FileScanner &scanner)
Definition FileScanner.hpp:111
std::size_t m_idx
Definition FileScanner.hpp:106
const FileInfo & operator*()
Definition FileScanner.hpp:125
FileScanner::iterator & operator++()
Definition FileScanner.hpp:113
bool operator==(const std::unreachable_sentinel_t) const
Definition FileScanner.cpp:141
std::filesystem::path m_path
Definition FileScanner.hpp:97
FileScanner(const std::filesystem::path &path)
Definition FileScanner.cpp:111
friend class iterator
Definition FileScanner.hpp:102
const FileInfo & at(std::size_t index)
Definition FileScanner.cpp:117
FileScannerGenerator file_scanner
Definition FileScanner.hpp:98
std::unreachable_sentinel_t end()
Definition FileScanner.hpp:135
std::filesystem::path path() const
Definition FileScanner.hpp:137
std::vector< FileInfo > files
Definition FileScanner.hpp:99
iterator begin()
Definition FileScanner.hpp:132
Definition engineDetection.hpp:15
Definition exceptions.hpp:17
Definition FileScanner.hpp:21
std::string filename
Definition FileScanner.hpp:22
std::filesystem::path path
Definition FileScanner.hpp:24
std::string ext
Definition FileScanner.hpp:23
std::uint8_t depth
Definition FileScanner.hpp:26
FileInfo(std::filesystem::path path_in, const std::filesystem::path &source, const std::size_t filesize, const std::uint8_t file_depth)
Definition FileScanner.hpp:31
std::size_t size
Definition FileScanner.hpp:25
std::filesystem::path relative
Definition FileScanner.hpp:27
std::suspend_always final_suspend() noexcept
Definition FileScanner.hpp:62
void return_value(FileInfo &&from)
Definition FileScanner.hpp:66
std::suspend_always initial_suspend() noexcept
Definition FileScanner.hpp:60
std::optional< FileInfo > value
Definition FileScanner.hpp:52
FileScannerGenerator get_return_object()
Definition FileScanner.hpp:55
void unhandled_exception()
Definition FileScanner.hpp:64
std::suspend_always yield_value(FileInfo from)
Definition FileScanner.hpp:74
std::exception_ptr exception
Definition FileScanner.hpp:53
Definition FileScanner.hpp:46
~FileScannerGenerator()
Definition FileScanner.hpp:88
FileScannerGenerator(handle_type h)
Definition FileScanner.hpp:86
handle_type m_h
Definition FileScanner.hpp:84
std::coroutine_handle< promise_type > handle_type
Definition FileScanner.hpp:48
FileInfo operator()()
Definition FileScanner.cpp:17