Audacity 3.2.0
RequestPayload.cpp
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file RequestPayload.cpp
6 @brief Declare a class for constructing HTTP requests.
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#include "RequestPayload.h"
12
13#include <iterator>
14#include <vector>
15
16#include <wx/file.h>
17
18#include "CodeConversions.h"
19
21{
22
23namespace
24{
26 int64_t currentOffset, int64_t offset, int64_t size, RequestPayloadStream::SeekDirection direction)
27{
28 switch (direction)
29 {
31 return offset;
33 return currentOffset + offset;
35 return size - offset;
36 default:
37 return 0;
38 }
39}
40
42{
43public:
44 bool HasData() const override
45 {
46 return false;
47 }
48
49 int64_t GetDataSize() const override
50 {
51 return 0;
52 }
53
54 bool Seek(int64_t offset, SeekDirection direction) override
55 {
56 return false;
57 }
58
59 int64_t Read(void* buffer, int64_t size) override
60 {
61 return 0;
62 }
63}; // class EmptyRequestPayloadStream
64
66{
67public:
68 MemoryRequestPayloadStream(const void* data, int64_t size, bool copyData)
69 : mStreamSize { size }
70 {
71 if (copyData && data != nullptr && mStreamSize > 0)
72 {
73 mData.reserve(size);
74 std::copy_n(static_cast<const uint8_t*>(data), size, std::back_inserter(mData));
75 mDataPointer = mData.data();
76 }
77 else
78 {
79 mDataPointer = static_cast<const uint8_t*>(data);
80 }
81 }
82
83 bool HasData() const override
84 {
85 return mDataPointer != nullptr && mStreamSize > 0;
86 }
87
88 int64_t GetDataSize() const override
89 {
90 return mStreamSize;
91 }
92
93 bool Seek(int64_t offset, SeekDirection direction) override
94 {
95 const auto requestedOffset = CalculateOffset(mStreamPosition, offset, mStreamSize, direction);
96
97 if (requestedOffset < 0 || requestedOffset > mStreamSize)
98 return false;
99
100 mStreamPosition = requestedOffset;
101 return true;
102 }
103
104 int64_t Read(void* buffer, int64_t size) override
105 {
106 const auto bytesToRead = std::min(size, mStreamSize - mStreamPosition);
107
108 if (bytesToRead <= 0)
109 return 0;
110
111 std::copy_n(mDataPointer + mStreamPosition, bytesToRead, static_cast<uint8_t*>(buffer));
112 mStreamPosition += bytesToRead;
113
114 return bytesToRead;
115 }
116
117private:
118 std::vector<uint8_t> mData;
119
120 const uint8_t* mDataPointer {};
121 int64_t mStreamSize {};
122
123 int64_t mStreamPosition {};
124}; // class MemoryRequestPayloadStream
125
127{
128public:
129 FileRequestPayloadStream(const std::string& filePath)
130 : mFilePath { audacity::ToWXString(filePath) }
131 , mFile { mFilePath }
132 {
133 wxStat(mFilePath, &mFileStat);
134 }
135
136 bool HasData() const override
137 {
138 return mFile.IsOpened();
139 }
140
141 int64_t GetDataSize() const override
142 {
143 return mFileStat.st_size;
144 }
145
146 bool Seek(int64_t offset, SeekDirection direction) override
147 {
148 const auto platformSeekDirection = [&]
149 {
150 switch (direction)
151 {
152 case SeekDirection::Start:
153 return wxFromStart;
154 case SeekDirection::Current:
155 return wxFromCurrent;
156 case SeekDirection::End:
157 return wxFromEnd;
158 default:
159 return wxFromStart;
160 }
161 }();
162
163 return mFile.Seek(offset, platformSeekDirection) != wxInvalidOffset;
164 }
165
166 int64_t Read(void* buffer, int64_t size) override
167 {
168 return mFile.Read(buffer, size);
169 }
170
171private:
172 wxString mFilePath;
173 wxFile mFile;
174 wxStructStat mFileStat;
175}; // class FileRequestPayloadStream
176
177} // namespace
178
180{
181}
182
184CreateRequestPayloadStream(const void* data, int64_t size, bool copyData)
185{
186 return std::make_shared<MemoryRequestPayloadStream>(data, size, copyData);
187}
188
190{
191 return std::make_shared<EmptyRequestPayloadStream>();
192}
193
195{
196 return std::make_shared<FileRequestPayloadStream>(filePath);
197}
198
199} // namespace audacity::network_manager
Declare functions to perform UTF-8 to std::wstring conversions.
int min(int a, int b)
Declare a class for constructing HTTP requests.
int64_t Read(void *buffer, int64_t size) override
returns number of bytes read
bool Seek(int64_t offset, SeekDirection direction) override
returns true on success
int64_t GetDataSize() const override
May return 0 even if HasData() returns true.
int64_t Read(void *buffer, int64_t size) override
returns number of bytes read
int64_t GetDataSize() const override
May return 0 even if HasData() returns true.
bool Seek(int64_t offset, SeekDirection direction) override
returns true on success
int64_t GetDataSize() const override
May return 0 even if HasData() returns true.
int64_t Read(void *buffer, int64_t size) override
returns number of bytes read
bool Seek(int64_t offset, SeekDirection direction) override
returns true on success
int64_t CalculateOffset(int64_t currentOffset, int64_t offset, int64_t size, RequestPayloadStream::SeekDirection direction)
std::shared_ptr< RequestPayloadStream > RequestPayloadStreamPtr
Definition: CurlResponse.h:33
RequestPayloadStreamPtr CreateRequestPayloadStream(const void *data, int64_t size, bool copyData)
wxString ToWXString(const std::string &str)