Line data Source code
1 : ///
2 : /// @file ar/file.cpp
3 : /// @copyright (c) 2015 by Petr Zemek (s3rvac@gmail.com) and contributors
4 : /// @license MIT, see the @c LICENSE file for more details
5 : /// @brief Implementation of the representation and factory for files.
6 : ///
7 :
8 : #include "ar/file.h"
9 : #include "ar/internal/files/filesystem_file.h"
10 : #include "ar/internal/files/string_file.h"
11 :
12 : using namespace ar::internal;
13 :
14 : namespace ar {
15 :
16 : File::File() = default;
17 :
18 : File::~File() = default;
19 :
20 : /// @fn File::getName()
21 : ///
22 : /// Returns the name of the file.
23 : ///
24 : /// When the file has no name, the empty string is returned.
25 : ///
26 :
27 : /// @fn File::getContent()
28 : ///
29 : /// Returns the content of the file.
30 : ///
31 :
32 : /// @fn File::saveCopyTo(const std::string& directoryPath)
33 : ///
34 : /// Stores a copy of the file into the given directory.
35 : ///
36 :
37 : /// @fn File::saveCopyTo(const std::string& directoryPath,
38 : /// const std::string& name)
39 : ///
40 : /// Stores a copy of the file into the given directory under the given name.
41 : ///
42 :
43 : ///
44 : /// Returns a file containing the given content with the given name.
45 : ///
46 : /// @param[in] content Content of the file.
47 : /// @param[in] name Name of the file.
48 : ///
49 12 : std::unique_ptr<File> File::fromContentWithName(const std::string& content,
50 : const std::string& name) {
51 12 : return std::make_unique<StringFile>(content, name);
52 : }
53 :
54 : ///
55 : /// Returns a file from the given path.
56 : ///
57 : /// @param[in] path Path to the file.
58 : ///
59 : /// The name of the file is obtained automatically.
60 : ///
61 1 : std::unique_ptr<File> File::fromFilesystem(const std::string& path) {
62 1 : return std::make_unique<FilesystemFile>(path);
63 : }
64 :
65 : ///
66 : /// Returns a file from the given path, but with a custom name.
67 : ///
68 : /// @param[in] path Path to the file.
69 : /// @param[in] name Name to be used as the file's name.
70 : ///
71 : /// Use this function only if you want to choose a different name for the file
72 : /// than the one it already has.
73 : ///
74 1 : std::unique_ptr<File> File::fromFilesystemWithOtherName(
75 : const std::string& path, const std::string& name) {
76 1 : return std::make_unique<FilesystemFile>(path, name);
77 : }
78 :
79 : ///
80 : /// Constructs an empty container (without files).
81 : ///
82 : Files::Files() = default;
83 :
84 1 : Files::Files(Files&& other): files{std::move(other.files)} {}
85 :
86 : Files::~Files() = default;
87 :
88 : ///
89 : /// Returns a reference to the first file in the container.
90 : ///
91 : /// Calling this function on an empty container is undefined.
92 : ///
93 7 : auto Files::front() -> reference {
94 7 : return files.front();
95 : }
96 :
97 : ///
98 : /// Returns a reference to the last file in the container.
99 : ///
100 : /// Calling this function on an empty container is undefined.
101 : ///
102 3 : auto Files::back() -> reference {
103 3 : return files.back();
104 : }
105 :
106 1 : auto Files::begin() noexcept -> iterator {
107 1 : return files.begin();
108 : }
109 :
110 1 : auto Files::end() noexcept -> iterator {
111 1 : return files.end();
112 : }
113 :
114 : ///
115 : /// Is the container empty?
116 : ///
117 4 : bool Files::empty() const noexcept {
118 4 : return files.empty();
119 : }
120 :
121 : ///
122 : /// Returns the number of files in the container.
123 : ///
124 6 : auto Files::size() const noexcept -> size_type {
125 6 : return files.size();
126 : }
127 :
128 : ///
129 : /// Appends the given file to the end of the container.
130 : ///
131 12 : void Files::push_back(value_type file) {
132 12 : files.push_back(std::move(file));
133 12 : }
134 :
135 : } // namespace ar
|