8000 File: Remove dependency from Containers library · Pagghiu/SaneCppLibraries@7167f4d · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 7167f4d

Browse files
committed
File: Remove dependency from Containers library
Replacing 8000 all {Small}Vector<char> with {Small}Buffer
1 parent f1a7105 commit 7167f4d

File tree

15 files changed

+21
-70
lines changed

15 files changed

+21
-70
lines changed

Examples/SCExample/Examples/SerializationExample/SerializationExample.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ struct SC::SerializationExampleModel
167167

168168
Result loadFromBinaryFile(const StringView fileName)
169169
{
170-
Vector<uint8_t> buffer;
170+
Buffer buffer;
171171
SC_TRY(FileSystem().read(fileName, buffer));
172-
return loadFromBinary(buffer.toSpanConst());
172+
return loadFromBinary(buffer.toSpanConst().reinterpret_as_array_of<const uint8_t>());
173173
}
174174

175175
Result saveToJSONFile(StringView jsonPath)

Libraries/AsyncStreams/Tests/AsyncRequestStreamsTest.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../../Async/Async.h"
66
#include "../../AsyncStreams/AsyncStreams.h"
77
#include "../../AsyncStreams/ZLibTransformStreams.h"
8+
#include "../../Containers/Vector.h"
89
#include "../../File/File.h"
910
#include "../../FileSystem/FileSystem.h"
1011
#include "../../FileSystem/Path.h"
@@ -159,7 +160,7 @@ void SC::AsyncRequestStreamsTest::fileToFile()
159160
SC_TEST_EXPECT(readDescriptor.close());
160161

161162
// Final Check
162-
Vector<char> writableData;
163+
Buffer writableData;
163164
SC_TEST_EXPECT(fs.read(writeablePath.view(), writableData));
164165

165166
Span<const uint64_t> writtenData = writableData.toSpanConst().reinterpret_as_array_of<const uint64_t>();
@@ -336,7 +337,7 @@ void SC::AsyncRequestStreamsTest::fileToSocketToFile()
336337
SC_TEST_EXPECT(not client[1].isValid());
337338

338339
// Check written file content against source file
339-
Vector<char> destination;
340+
Buffer destination;
340341
SC_TEST_EXPECT(destination.reserve(source.size() * sizeof(uint64_t)));
341342
SC_TEST_EXPECT(fs.read("destination.txt", destination));
342343
SC_TEST_EXPECT(destination.size() == source.size() * sizeof(uint64_t));

Libraries/AsyncStreams/Tests/AsyncStreamsTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44
#include "../../AsyncStreams/AsyncStreams.h"
55
#include "../../Async/Async.h"
6+
#include "../../Containers/Vector.h"
67
#include "../../FileSystem/FileSystem.h"
78
#include "../../FileSystem/Path.h"
89
#include "../../Foundation/Buffer.h"

Libraries/File/File.cpp

-19
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ SC::Result SC::FileDescriptor::write(Span<const uint8_t> data)
5858
//-------------------------------------------------------------------------------------------------------
5959
SC::Result SC::File::open(StringView path, OpenMode mode) { return open(path, mode, OpenOptions()); }
6060

61-
template <typename T>
62-
SC::Result SC::File::readUntilEOFTemplate(Vector<T>& destination)
63-
{
64-
T buffer[1024];
65-
SC_TRY(fd.isValid());
66-
ReadResult readResult;
67-
FileDescriptor::Handle fileDescriptor;
68-
SC_TRY(fd.get(fileDescriptor, Result::Error("FileDescriptor::readAppend - Invalid Handle")));
69-
while (not readResult.isEOF)
70-
{
71-
SC_TRY(Internal::readAppend<T>(fileDescriptor, destination, {buffer, sizeof(buffer)}, readResult));
72-
}
73-
return Result(true);
74-
}
75-
7661
SC::Result SC::File::readUntilEOFTemplate(Buffer& destination)
7762
{
7863
char buffer[1024];
@@ -87,10 +72,6 @@ SC::Result SC::File::readUntilEOFTemplate(Buffer& destination)
8772
return Result(true);
8873
}
8974

90-
SC::Result SC::File::readUntilEOF(Vector<char>& destination) { return readUntilEOFTemplate(destination); }
91-
92-
SC::Result SC::File::readUntilEOF(Vector<uint8_t>& destination) { return readUntilEOFTemplate(destination); }
93-
9475
SC::Result SC::File::readUntilEOF(Buffer& destination) { return readUntilEOFTemplate(destination); }
9576

9677
SC::Result SC::File::readUntilEOF(String& destination)

Libraries/File/File.h

+1-17
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ namespace SC
88
{
99
struct Buffer;
1010
struct String;
11-
template <typename T>
12-
struct Vector;
1311
} // namespace SC
1412
//! @addtogroup group_file
1513
//! @{
1614

17-
/// @brief Wraps a SC::FileDescriptor to open it and use strings / containers.
15+
/// @brief Wraps a SC::FileDescriptor to open it and use strings / buffers.
1816
/// Example usage:
1917
/// \snippet Libraries/File/Tests/FileTest.cpp FileSnippet
2018
struct SC::File
@@ -52,18 +50,6 @@ struct SC::File
5250
/// @return Valid Result if file is opened successfully
5351
[[nodiscard]] Result open(StringView path, OpenMode mode, OpenOptions options);
5452

55-
/// @brief Reads into a given dynamic buffer until End of File (EOF) is signaled.
56-
/// It works also for non-seekable file descriptors (stdout / in / err).
57-
/// @param destination A destination buffer to write to (it will be resized as needed)
58-
/// @return Valid result if read succeeded until EOF
59-
[[nodiscard]] Result readUntilEOF(Vector<char>& destination);
60-
61-
/// @brief Reads into a given dynamic buffer until End of File (EOF) is signaled.
62-
/// It works also for non-seekable file descriptors (stdout / in / err).
63-
/// @param destination A destination buffer to write to (it will be resized as needed)
64-
/// @return Valid result if read succeeded until EOF
65-
[[nodiscard]] Result readUntilEOF(Vector<uint8_t>& destination);
66-
6753
/// @brief Reads into a given dynamic buffer until End of File (EOF) is signaled.
6854
/// It works also for non-seekable file descriptors (stdout / in / err).
6955
/// @param destination A destination buffer to write to (it will be resized as needed)
@@ -79,8 +65,6 @@ struct SC::File
7965
private:
8066
struct Internal;
8167
struct ReadResult;
82-
template <typename T>
83-
Result readUntilEOFTemplate(Vector<T>& destination);
8468
Result readUntilEOFTemplate(Buffer& destination);
8569
};
8670

Libraries/File/FileDescriptor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct SC::detail::FileDescriptorDefinition
4747
//! @addtogroup group_file
4848
//! @{
4949

50-
/// @brief File Descriptor (use SC::File to open and use it with strings and containers).
50+
/// @brief File Descriptor (use SC::File to open and use it with strings and buffers).
5151
struct SC::FileDescriptor : public SC::UniqueHandle<SC::detail::FileDescriptorDefinition>
5252
{
5353
using UniqueHandle::UniqueHandle;

Libraries/File/Internal/FilePosix.inl

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Stefano Cristiano
22
// SPDX-License-Identifier: MIT
3-
#include "../../Containers/Vector.h"
43
#include "../../Strings/String.h"
54
#include "../../Strings/StringConverter.h"
65
#include "../File.h"

Libraries/File/Internal/FileWindows.inl

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Stefano Cristiano
22
// SPDX-License-Identifier: MIT
3-
#include "../../Containers/Vector.h"
43
#include "../../Strings/String.h"
54
#include "../../Strings/StringConverter.h"
65
#include "../File.h"

Libraries/FileSystem/FileSystem.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,7 @@ SC::Result SC::FileSystem::write(StringView path, Span<const uint8_t> data)
108108
return write(path, {reinterpret_cast<const char*>(data.data()), data.sizeInBytes()});
109109
}
110110

111-
SC::Result SC::FileSystem::read(StringView path, Vector<char>& data)
112-
{
113-
StringView encodedPath;
114-
SC_TRY(convert(path, fileFormatBuffer1, &encodedPath));
115-
FileDescriptor fd;
116-
File file(fd);
117-
SC_TRY(file.open(encodedPath, File::ReadOnly));
118-
return file.readUntilEOF(data);
119-
}
120-
121-
SC::Result SC::FileSystem::read(StringView path, Vector<uint8_t>& data)
111+
SC::Result SC::FileSystem::read(StringView path, Buffer& data)
122112
{
123113
StringView encodedPath;
124114
SC_TRY(convert(path, fileFormatBuffer1, &encodedPath));

Libraries/FileSystem/FileSystem.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Stefano Cristiano
22
// SPDX-License-Identifier: MIT
33
#pragma once
4-
#include "../Containers/Vector.h"
4+
#include "../Foundation/Buffer.h"
55
#include "../Foundation/Result.h"
66
#include "../Strings/String.h"
77
#include "../Time/Time.h"
@@ -269,13 +269,12 @@ struct SC::FileSystem
269269
[[nodiscard]] Result write(StringView file, Span<const char> data);
270270
[[nodiscard]] Result write(StringView file, Span<const uint8_t> data);
271271

272-
/// @brief Reads contents of a file into a SC::Vector buffer
272+
/// @brief Reads contents of a file into a SC::Buffer
273273
/// @param file Path to the file to read
274274
/// @param[out] data Destination buffer that will receive data
275275
/// @return Valid Result if file was fully read successfully
276276
/// @see write (for an usage example)
277-
[[nodiscard]] Result read(StringView file, Vector<char>& data);
278-
[[nodiscard]] Result read(StringView file, Vector<uint8_t>& data);
277+
[[nodiscard]] Result read(StringView file, Buffer& data);
279278

280279
/// @brief Replace the entire content of a file with the provided StringView
281280
/// @param file Path to the file that is meant to be written

Libraries/FileSystemWatcher/FileSystemWatcher.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#pragma once
44

55
#include "../Async/Async.h" // AsyncLoopWakeUp
6-
#include "../Containers/Array.h"
76
#include "../Containers/IntrusiveDoubleLinkedList.h"
8-
#include "../Containers/Vector.h"
97
#include "../Foundation/Function.h"
108
#include "../Foundation/OpaqueObject.h"
119
#include "../Foundation/Result.h"
@@ -69,8 +67,8 @@ struct SC::FileSystemWatcher
6967
static constexpr int MaxChangesBufferSize = 1024;
7068
static constexpr int Windows =
7169
MaxChangesBufferSize + sizeof(void*) + sizeof(FileDescriptor) + sizeof(AsyncFilePoll);
72-
static constexpr int Apple = sizeof(void*);
73-
static constexpr int Linux = sizeof(Array<int64_t, MaxNumberOfSubdirs>) + sizeof(Vector<char>) + sizeof(void*);
70+
static constexpr int Apple = sizeof(void*);
71+
static constexpr int Linux = 1048;
7472
static constexpr int Default = Linux;
7573

7674
static constexpr size_t Alignment = alignof(void*);

Libraries/FileSystemWatcher/Internal/FileSystemWatcherLinux.inl

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <sys/stat.h> // fstat
99
#include <unistd.h> // read
1010

11+
#include "../../Containers/Array.h"
1112
#include "../../File/File.h"
1213
#include "../../FileSystemIterator/FileSystemIterator.h"
1314
#include "../../Strings/String.h"

Libraries/Http/HttpWebServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ SC::Result SC::HttpWebServer::Internal::readFile(StringView directory, HttpReque
5151
SC_TRY(fileSystem.getFileStat(url, fileStat));
5252
StringView name, extension;
5353
SC_TRY(Path::parseNameExtension(url, name, extension));
54-
Vector<char> data;
54+
Buffer data;
5555
SC_TRY(fileSystem.read(url, data));
5656
SC_TRY(response.startResponse(200));
5757
SC_TRY(response.addHeader("Connection", "Closed"));

Libraries/Process/Process.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Stefano Cristiano
22
// SPDX-License-Identifier: MIT
33
#include "Process.h"
4-
#include "../Containers/Vector.h"
54
#include "../File/File.h"
65
#include "../Strings/StringConverter.h"
76

@@ -168,7 +167,7 @@ SC::Result SC::Process::launch(const StdOut& stdOutput, const StdIn& stdInput, c
168167
case StdStream::Operation::ExternalPipe: break;
169168
case StdStream::Operation::FileDescriptor: break;
170169
case StdStream::Operation::Vector: {
171-
SC_TRY(stdinPipe.writePipe.write(stdInput.vector->toSpan()));
170+
SC_TRY(stdinPipe.writePipe.write(stdInput.buffer->toSpan()));
172171
SC_TRY(stdinPipe.writePipe.close());
173172
}
174173
break;
@@ -197,7 +196,7 @@ SC::Result SC::Process::launch(const StdOut& stdOutput, const StdIn& stdInput, c
197196
case StdStream::Operation::ExternalPipe: break;
198197
case StdStream::Operation::FileDescriptor: break;
199198
case StdStream::Operation::Vector: {
200-
SC_TRY(File(pipe.readPipe).readUntilEOF(*outputObject.vector));
199+
SC_TRY(File(pipe.readPipe).readUntilEOF(*outputObject.buffer));
201200
return pipe.close();
202201
}
203202
break;

Libraries/Process/Process.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#pragma once
44

55
#include "../Containers/IntrusiveDoubleLinkedList.h"
6-
#include "../Containers/Vector.h"
76
#include "../File/FileDescriptor.h"
87
#include "../Strings/String.h"
98
#include "ProcessDescriptor.h"
@@ -76,7 +75,7 @@ struct SC::Process
7675
StdStream(String& externalString) { operation = Operation::String; string = &externalString;}
7776

7877
/// @brief Read the process standard output/error into the given Vector
79-
StdStream(Vector<char>& externalVector) { operation = Operation::Vector; vector = &externalVector; }
78+
StdStream(Buffer& externalBuffer) { operation = Operation::Vector; buffer = &externalBuffer; }
8079
// clang-format on
8180

8281
/// @brief Redirects child process standard output/error to a given file descriptor
@@ -119,8 +118,8 @@ struct SC::Process
119118
Span<const char> readableSpan;
120119
Span<char> writableSpan;
121120

122-
String* string;
123-
Vector<char>* vector;
121+
String* string;
122+
Buffer* buffer;
124123

125124
FileDescriptor::Handle fileDescriptor;
126125

0 commit comments

Comments
 (0)
0