C++ Utilities 5.26.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
iotests.cpp
Go to the documentation of this file.
1#include "./testutils.h"
2
3using namespace CppUtilities;
4
6
10std::ostream &operator<<(std::ostream &out, const std::wstring &s)
11{
12 const auto utf8 = CppUtilities::
13#ifdef CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN
15#else
17#endif
18 (reinterpret_cast<const char *>(s.data()), s.size() * (sizeof(std::wstring::value_type) / sizeof(char)));
19 out.write(utf8.first.get(), static_cast<std::streamsize>(utf8.second));
20 return out;
21}
22
25
27#include "../io/binaryreader.h"
28#include "../io/binarywriter.h"
29#include "../io/bitreader.h"
30#include "../io/buffersearch.h"
31#include "../io/copy.h"
32#include "../io/inifile.h"
33#include "../io/misc.h"
35#include "../io/path.h"
36
37#ifdef CPP_UTILITIES_USE_LIBARCHIVE
38#include "../io/archive.h"
39#endif
40
41#include <cppunit/TestFixture.h>
42#include <cppunit/extensions/HelperMacros.h>
43
44#include <algorithm>
45#include <cstddef>
46#include <fstream>
47#include <regex>
48#include <sstream>
49
50#ifdef PLATFORM_WINDOWS
51#include <cstdio>
52#endif
53
54#ifdef PLATFORM_UNIX
55#include <sys/fcntl.h>
56#include <sys/types.h>
57#endif
58
59using namespace std;
60using namespace CppUtilities::Literals;
61using namespace CPPUNIT_NS;
62
66class IoTests : public TestFixture {
67 CPPUNIT_TEST_SUITE(IoTests);
68 CPPUNIT_TEST(testBinaryReader);
69 CPPUNIT_TEST(testBinaryWriter);
70 CPPUNIT_TEST(testBitReader);
71 CPPUNIT_TEST(testBufferSearch);
72 CPPUNIT_TEST(testPathUtilities);
73 CPPUNIT_TEST(testIniFile);
74 CPPUNIT_TEST(testAdvancedIniFile);
75 CPPUNIT_TEST(testCopy);
76 CPPUNIT_TEST(testCopyWithNativeFileStream);
77 CPPUNIT_TEST(testReadFile);
78 CPPUNIT_TEST(testWriteFile);
79 CPPUNIT_TEST(testAnsiEscapeCodes);
80#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
81 CPPUNIT_TEST(testNativeFileStream);
82#endif
83#ifdef CPP_UTILITIES_USE_LIBARCHIVE
84 CPPUNIT_TEST(testExtractingArchive);
85#endif
86 CPPUNIT_TEST_SUITE_END();
87
88public:
89 void setUp() override;
90 void tearDown() override;
91
92 void testBinaryReader();
93 void testBinaryWriter();
94 void testBitReader();
95 void testBufferSearch();
96 void testPathUtilities();
97 void testIniFile();
99 void testCopy();
101 void testReadFile();
102 void testWriteFile();
103 void testAnsiEscapeCodes();
104#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
105 void testNativeFileStream();
106#endif
107#ifdef CPP_UTILITIES_USE_LIBARCHIVE
108 void testExtractingArchive();
109#endif
110};
111
113
115{
116}
117
119{
120}
121
126{
127 // read test file
128 fstream testFile;
129 testFile.exceptions(ios_base::failbit | ios_base::badbit);
130 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
131 BinaryReader reader(&testFile);
132 CPPUNIT_ASSERT_EQUAL(static_cast<istream::pos_type>(398), reader.readStreamsize());
133 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint16_t>(0x0102u), reader.readUInt16LE());
134 CPPUNIT_ASSERT_EQUAL(static_cast<istream::pos_type>(396), reader.readRemainingBytes());
135 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint16_t>(0x0102u), reader.readUInt16BE());
136 CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24LE());
137 CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24BE());
138 CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.readUInt32LE());
139 CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.readUInt32BE());
140 CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.readUInt40LE());
141 CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.readUInt40BE());
142 CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.readUInt56LE());
143 CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.readUInt56BE());
144 CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.readUInt64LE());
145 CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.readUInt64BE());
146 testFile.seekg(0);
147 CPPUNIT_ASSERT_EQUAL(reader.readInt16LE(), static_cast<std::int16_t>(0x0102));
148 CPPUNIT_ASSERT_EQUAL(reader.readInt16BE(), static_cast<std::int16_t>(0x0102));
149 CPPUNIT_ASSERT_EQUAL(0x010203, reader.readInt24LE());
150 CPPUNIT_ASSERT_EQUAL(0x010203, reader.readInt24BE());
151 CPPUNIT_ASSERT_EQUAL(0x01020304, reader.readInt32LE());
152 CPPUNIT_ASSERT_EQUAL(0x01020304, reader.readInt32BE());
153 CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.readInt40LE());
154 CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.readInt40BE());
155 CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.readInt56LE());
156 CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.readInt56BE());
157 CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.readInt64LE());
158 CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.readInt64BE());
159 CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32LE());
160 CPPUNIT_ASSERT_EQUAL(1.625, reader.readFloat64LE());
161 CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32BE());
162 CPPUNIT_ASSERT_EQUAL(reader.readFloat64BE(), 1.625);
163 CPPUNIT_ASSERT_EQUAL(false, reader.readBool());
164 CPPUNIT_ASSERT_EQUAL(true, reader.readBool());
165 CPPUNIT_ASSERT_EQUAL("abc"s, reader.readString(3));
166 CPPUNIT_ASSERT_EQUAL(reader.readLengthPrefixedString(), "ABC"s);
167 CPPUNIT_ASSERT_EQUAL("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
168 "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
169 "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
170 "678901234567890123456789"s,
171 reader.readLengthPrefixedString());
172 CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString());
173 testFile.seekg(-4, ios_base::cur);
174 CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString(5, 0));
175 CPPUNIT_ASSERT_THROW(reader.readLengthPrefixedString(), ConversionException);
176 CPPUNIT_ASSERT_MESSAGE("pos in stream not advanced on conversion error", reader.readByte() == 0);
177
178 // test ownership
179 reader.setStream(nullptr, true);
180 reader.setStream(new fstream(), true);
181 BinaryReader reader2(reader);
182 CPPUNIT_ASSERT(reader2.stream() == reader.stream());
183 CPPUNIT_ASSERT(!reader2.hasOwnership());
184 reader.setStream(&testFile, false);
185 reader.setStream(new fstream(), true);
186}
187
192{
193 // prepare reading expected data
194 fstream testFile;
195 testFile.exceptions(ios_base::failbit | ios_base::badbit);
196 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
197
198 // prepare output stream
199 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
200 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
201 char testData[397];
202#if defined(__GLIBCXX__) && !defined(_LIBCPP_VERSION)
203#define USE_RDBUF_DIRECTLY
204 outputStream.rdbuf()->pubsetbuf(testData, sizeof(testData));
205#endif
206
207 // write test data
208 BinaryWriter writer(&outputStream);
209 writer.writeUInt16LE(0x0102u);
210 writer.writeUInt16BE(0x0102u);
211 writer.writeUInt24LE(0x010203u);
212 writer.writeUInt24BE(0x010203u);
213 writer.writeUInt32LE(0x01020304u);
214 writer.writeUInt32BE(0x01020304u);
215 writer.writeUInt40LE(0x0102030405u);
216 writer.writeUInt40BE(0x0102030405u);
217 writer.writeUInt56LE(0x01020304050607u);
218 writer.writeUInt56BE(0x01020304050607u);
219 writer.writeUInt64LE(0x0102030405060708u);
220 writer.writeUInt64BE(0x0102030405060708u);
221
222#ifndef USE_RDBUF_DIRECTLY
223 outputStream.seekg(0);
224 outputStream.read(testData, 58);
225#endif
226
227 // test written values
228 for (char c : testData) {
229 const auto pos = static_cast<std::size_t>(testFile.tellg());
230 if (pos >= 58) {
231 break;
232 }
233 char expected;
234 testFile.read(&expected, 1);
235 CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("offset ", pos), asHexNumber(expected), asHexNumber(c));
236 }
237 testFile.seekg(0);
238 outputStream.seekp(0);
239
240 // write more test data
241 writer.writeInt16LE(0x0102);
242 writer.writeInt16BE(0x0102);
243 writer.writeInt24LE(0x010203);
244 writer.writeInt24BE(0x010203);
245 writer.writeInt32LE(0x01020304);
246 writer.writeInt32BE(0x01020304);
247 writer.writeInt40LE(0x0102030405);
248 writer.writeInt40BE(0x0102030405);
249 writer.writeInt56LE(0x01020304050607);
250 writer.writeInt56BE(0x01020304050607);
251 writer.writeInt64LE(0x0102030405060708);
252 writer.writeInt64BE(0x0102030405060708);
253 writer.writeFloat32LE(1.125);
254 writer.writeFloat64LE(1.625);
255 writer.writeFloat32BE(1.125);
256 writer.writeFloat64BE(1.625);
257 writer.writeBool(false);
258 writer.writeBool(true);
259 writer.writeString("abc");
260 writer.writeLengthPrefixedString("ABC");
261 writer.writeLengthPrefixedString("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
262 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
263 "234567890123456789012345678901234567890123456789012345678901234567890123456789");
264 writer.writeTerminatedString("def");
265
266#ifndef USE_RDBUF_DIRECTLY
267 outputStream.seekg(0);
268 outputStream.read(testData, 58);
269#endif
270
271 // test written values
272 for (char c : testData) {
273 const auto pos = static_cast<std::size_t>(testFile.tellg());
274 if (pos >= 58) {
275 break;
276 }
277 char expected;
278 testFile.read(&expected, 1);
279 CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("offset ", pos), asHexNumber(expected), asHexNumber(c));
280 }
281
282 // test ownership
283 writer.setStream(nullptr, true);
284 writer.setStream(new fstream(), true);
285 BinaryWriter writer2(writer);
286 CPPUNIT_ASSERT(writer2.stream() == writer.stream());
287 CPPUNIT_ASSERT(!writer2.hasOwnership());
288 writer.setStream(&testFile, false);
289 writer.setStream(new fstream(), true);
290}
291
296{
297 const std::uint8_t testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
298 BitReader reader(reinterpret_cast<const char *>(testData), sizeof(testData));
299 CPPUNIT_ASSERT(reader.readBit() == 1);
300 reader.skipBits(6);
301 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.showBits<std::uint8_t>(2));
302 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.readBits<std::uint8_t>(2));
303 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(0x103C4428 << 1), reader.readBits<std::uint32_t>(32));
304 reader.align();
305 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0x44), reader.readBits<std::uint8_t>(8));
306 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(7), reader.readUnsignedExpGolombCodedBits<std::uint8_t>());
307 CPPUNIT_ASSERT_EQUAL(static_cast<std::int8_t>(4), reader.readSignedExpGolombCodedBits<std::int8_t>());
308 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
309 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
310 reader.skipBits(8 + 4);
311 CPPUNIT_ASSERT_EQUAL(4_st, reader.bitsAvailable());
312 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0xA), reader.readBits<std::uint8_t>(4));
313 CPPUNIT_ASSERT_THROW(reader.readBit(), std::ios_base::failure);
314 CPPUNIT_ASSERT_THROW(reader.skipBits(1), std::ios_base::failure);
315 reader.reset(reinterpret_cast<const char *>(testData), sizeof(testData));
316 CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(8 * sizeof(testData)), reader.bitsAvailable());
317}
318
323{
324 // setup search to test
325 auto expectedResult = std::string();
326 auto hasResult = false;
327 auto bs = BufferSearch("Updated version: ", "\t\n", "Starting build", [&](BufferSearch &, std::string &&result) {
328 CPPUNIT_ASSERT_EQUAL(expectedResult, result);
329 CPPUNIT_ASSERT_MESSAGE("callback only invoked once", !hasResult);
330 hasResult = true;
331 });
332
333 // feed data into the search
334 char buffer[30] = { 0 };
335 bs(buffer, 0);
336 CPPUNIT_ASSERT(!hasResult);
337 std::strcpy(buffer, "Starting Updated");
338 CPPUNIT_ASSERT(bs.process(std::string_view(buffer, 16)) == nullptr);
339 CPPUNIT_ASSERT(!hasResult);
340 std::strcpy(buffer, " version: some ");
341 bs(buffer, 15);
342 CPPUNIT_ASSERT(!hasResult);
343 expectedResult = "some version number";
344 std::strcpy(buffer, "version number\tmore chars");
345 CPPUNIT_ASSERT_EQUAL(static_cast<std::ptrdiff_t>(14), bs.process(buffer, 25) - buffer);
346 CPPUNIT_ASSERT(hasResult);
347 hasResult = false;
348 std::strcpy(buffer, "... Starting build ...");
349 bs(buffer, 22);
350 CPPUNIT_ASSERT(!hasResult);
351}
352
357{
358 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs\\libc++utilities.so"));
359 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs/libc++utilities.so"));
360 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("/usr/lib/libc++utilities.so"));
361 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("libc++utilities.so"));
362 CPPUNIT_ASSERT_EQUAL("/usr/lib/"s, directory("/usr/lib/libc++utilities.so"));
363 CPPUNIT_ASSERT_EQUAL(string(), directory("libc++utilities.so"));
364 CPPUNIT_ASSERT_EQUAL("C:\\libs\\"s, directory("C:\\libs\\libc++utilities.so"));
365 CPPUNIT_ASSERT_EQUAL("C:\\libs/"s, directory("C:\\libs/libc++utilities.so"));
366 string invalidPath("lib/c++uti*lities.so?");
367 removeInvalidChars(invalidPath);
368 CPPUNIT_ASSERT(invalidPath == "libc++utilities.so");
369
370 const auto input = std::string_view("some/path/täst");
371 const auto expected = input;
372 const auto output = makeNativePath(input);
373#ifdef PLATFORM_WINDOWS
374 const auto outputAsUtf8 = convertUtf16LEToUtf8(reinterpret_cast<const char *>(output.data()), output.size() * 2);
375 const auto outputView = std::string_view(outputAsUtf8.first.get(), outputAsUtf8.second);
376 CPPUNIT_ASSERT_EQUAL_MESSAGE("makeNativePath()", expected, outputView);
377#else
378 CPPUNIT_ASSERT_EQUAL_MESSAGE("makeNativePath()", expected, output);
379#endif
380}
381
386{
387 // prepare reading test file
388 fstream inputFile;
389 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
390 inputFile.open(testFilePath("test.ini"), ios_base::in);
391
392 IniFile ini;
393 ini.parse(inputFile);
394 const auto globalScope = ini.data().at(0);
395 const auto scope1 = ini.data().at(1);
396 const auto scope2 = ini.data().at(2);
397 CPPUNIT_ASSERT(globalScope.first.empty());
398 CPPUNIT_ASSERT(globalScope.second.find("key0") != globalScope.second.cend());
399 CPPUNIT_ASSERT(globalScope.second.find("key0")->second == "value 0");
400 CPPUNIT_ASSERT(globalScope.second.find("key1") == globalScope.second.cend());
401 CPPUNIT_ASSERT(scope1.first == "scope 1");
402 CPPUNIT_ASSERT(scope1.second.find("key1") != scope1.second.cend());
403 CPPUNIT_ASSERT(scope1.second.find("key1")->second == "value 1");
404 CPPUNIT_ASSERT(scope1.second.find("key2") != scope1.second.cend());
405 CPPUNIT_ASSERT(scope1.second.find("key2")->second == "value=2");
406 CPPUNIT_ASSERT(scope2.first == "scope 2");
407 CPPUNIT_ASSERT(scope2.second.find("key5") == scope2.second.cend());
408
409 // write values to another file
410 fstream outputFile;
411 outputFile.exceptions(ios_base::failbit | ios_base::badbit);
412 outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::out | ios_base::trunc);
413 ini.make(outputFile);
414
415 // parse written values (again)
416 outputFile.close();
417 outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::in);
418 IniFile ini2;
419 ini2.parse(outputFile);
420 CPPUNIT_ASSERT(ini.data() == ini2.data());
421}
422
427{
428 // prepare reading test file
429 fstream inputFile;
430 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
431 inputFile.open(testFilePath("pacman.conf"), ios_base::in);
432
433 // parse the test file
434 AdvancedIniFile ini;
435 ini.parse(inputFile);
436
437 // check whether scope data is as expected
438 CPPUNIT_ASSERT_EQUAL_MESSAGE("5 scopes (taking implicit empty section at the end into account)", 5_st, ini.sections.size());
439 auto options = ini.findSection("options");
440 CPPUNIT_ASSERT(options != ini.sectionEnd());
441#if !defined(PLATFORM_WINDOWS) || defined(PLATFORM_MINGW) || defined(PLATFORM_CYGWIN)
442#define STD_REGEX_WORKS // the parsed data looks good, apparently std::regex behaves differently on MSVC
444 "comment block before section", "# Based on.*\n.*# GENERAL OPTIONS\n#\n"s, std::regex::extended, options->precedingCommentBlock);
445#endif
446 CPPUNIT_ASSERT_EQUAL(7_st, options->fields.size());
447 CPPUNIT_ASSERT_EQUAL("HoldPkg"s, options->fields[0].key);
448 CPPUNIT_ASSERT_EQUAL("pacman glibc"s, options->fields[0].value);
449 CPPUNIT_ASSERT_MESSAGE("value present", options->fields[0].flags & IniFileFieldFlags::HasValue);
450#ifdef STD_REGEX_WORKS
451 TESTUTILS_ASSERT_LIKE_FLAGS("comment block between section header and first field",
452 "# The following paths are.*\n.*#HookDir = /etc/pacman\\.d/hooks/\n"s, std::regex::extended, options->fields[0].precedingCommentBlock);
453#endif
454 CPPUNIT_ASSERT_EQUAL(""s, options->fields[0].followingInlineComment);
455 CPPUNIT_ASSERT_EQUAL("Foo"s, options->fields[1].key);
456 CPPUNIT_ASSERT_EQUAL("bar"s, options->fields[1].value);
457 CPPUNIT_ASSERT_MESSAGE("value present", options->fields[1].flags & IniFileFieldFlags::HasValue);
458#ifdef STD_REGEX_WORKS
459 TESTUTILS_ASSERT_LIKE_FLAGS("comment block between fields", "#XferCommand.*\n.*#CleanMethod = KeepInstalled\n"s, std::regex::extended,
460 options->fields[1].precedingCommentBlock);
461#endif
462 CPPUNIT_ASSERT_EQUAL("# inline comment"s, options->fields[1].followingInlineComment);
463 CPPUNIT_ASSERT_EQUAL("CheckSpace"s, options->fields[3].key);
464 CPPUNIT_ASSERT_EQUAL(""s, options->fields[3].value);
465 CPPUNIT_ASSERT_MESSAGE("no value present", !(options->fields[3].flags & IniFileFieldFlags::HasValue));
466#ifdef STD_REGEX_WORKS
467 TESTUTILS_ASSERT_LIKE_FLAGS("empty lines in comments preserved", "\n# Pacman.*\n.*\n\n#NoUpgrade =\n.*#TotalDownload\n"s, std::regex::extended,
468 options->fields[3].precedingCommentBlock);
469#endif
470 CPPUNIT_ASSERT_EQUAL(""s, options->fields[3].followingInlineComment);
471 auto extraScope = ini.findSection(options, "extra");
472 CPPUNIT_ASSERT(extraScope != ini.sectionEnd());
473 CPPUNIT_ASSERT_EQUAL_MESSAGE("comment block which is only an empty line", "\n"s, extraScope->precedingCommentBlock);
474 CPPUNIT_ASSERT_EQUAL_MESSAGE("inline comment after scope", "# an inline comment after a scope name"s, extraScope->followingInlineComment);
475 CPPUNIT_ASSERT_EQUAL(1_st, extraScope->fields.size());
476 CPPUNIT_ASSERT(ini.sections.back().flags & IniFileSectionFlags::Implicit);
477#ifdef STD_REGEX_WORKS
478 TESTUTILS_ASSERT_LIKE_FLAGS("comment block after last field present in implicitly added last scope", "\n# If you.*\n.*custompkgs\n"s,
479 std::regex::extended, ini.sections.back().precedingCommentBlock);
480#endif
481
482 // test finding a field from file level and const access
483 const auto *const constIniFile = &ini;
484 auto includeField = constIniFile->findField("extra", "Include");
485 CPPUNIT_ASSERT(includeField.has_value());
486 CPPUNIT_ASSERT_EQUAL("Include"s, includeField.value()->key);
487 CPPUNIT_ASSERT_EQUAL("/etc/pacman.d/mirrorlist"s, includeField.value()->value);
488 CPPUNIT_ASSERT_MESSAGE("field not present", !constIniFile->findField("extra", "Includ").has_value());
489 CPPUNIT_ASSERT_MESSAGE("scope not present", !constIniFile->findField("extr", "Includ").has_value());
490
491 // write values again; there shouldn't be a difference as the parser and the writer are supposed to
492 // preserve the order of all elements and comments
493 std::stringstream newFile;
494 ini.make(newFile);
495 std::string originalContents;
496 inputFile.clear();
497 inputFile.seekg(std::ios_base::beg);
498 // ignore warning about null pointer dereference from GCC 12 for now (which is *likely* not correct)
499#ifdef __GNUC__
500#pragma GCC diagnostic push
501#pragma GCC diagnostic ignored "-Wnull-dereference"
502#endif
503 originalContents.assign((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
504#ifdef __GNUC__
505#pragma GCC diagnostic pop
506#endif
507 CPPUNIT_ASSERT_EQUAL(originalContents, newFile.str());
508}
509
514{
515 // prepare streams
516 auto testFile = std::fstream();
517 testFile.exceptions(ios_base::failbit | ios_base::badbit);
518 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
519 auto outputStream = std::stringstream(ios_base::in | ios_base::out | ios_base::binary);
520 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
521
522 // copy
523 auto copyHelper = CopyHelper<13>();
524 copyHelper.copy(testFile, outputStream, 50);
525 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(0), outputStream.tellg());
526 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellp());
527 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellg());
528 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellp());
529
530 // verify
531 testFile.seekg(0);
532 for (auto i = 0; i < 50; ++i) {
533 CPPUNIT_ASSERT_EQUAL(testFile.get(), outputStream.get());
534 }
535}
536
541{
542 // prepare streams
543 auto testFile = NativeFileStream();
544 testFile.exceptions(ios_base::failbit | ios_base::badbit);
545 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
546 auto outputPath = workingCopyPath("copied_data", WorkingCopyMode::Cleanup);
547 auto outputStream = NativeFileStream();
548 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
549 outputStream.open(outputPath, ios_base::out | ios_base::binary);
550
551 // copy
552 auto copyHelper = CopyHelper<13>();
553 copyHelper.copy(testFile, outputStream, 50);
554 CPPUNIT_ASSERT(outputStream.is_open());
555 CPPUNIT_ASSERT(testFile.is_open());
556
557 // test seek positions (the expected values are from a run with normal std::fstream)
558 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellg());
559 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellp());
560 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellg());
561 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellp());
562
563 // add a few more bytes to output stream (to be sure it is still usable) and re-open for reading
564 const auto aFewMoreBytes = "a few more bytes"sv;
565 outputStream << aFewMoreBytes;
566 outputStream.close();
567 outputStream.open(outputPath, ios_base::in | ios_base::binary);
568
569 // verify
570 testFile.seekg(0);
571 for (auto i = 0; i < 50; ++i) {
572 CPPUNIT_ASSERT_EQUAL(testFile.get(), outputStream.get());
573 }
574 auto tail = std::string(aFewMoreBytes.size(), '0');
575 outputStream.read(tail.data(), static_cast<std::streamsize>(tail.size()));
576 CPPUNIT_ASSERT_EQUAL(aFewMoreBytes, std::string_view(tail.data(), tail.size()));
577
578 // repeat with callback version
579 auto percentage = 0.0;
580 const auto isAborted = [] { return false; };
581 const auto callback = [&percentage](double p) { percentage = p; };
582 testFile.seekg(0);
583 outputStream.close();
584 outputStream.open(outputPath, ios_base::out | ios_base::trunc | ios_base::binary);
585 copyHelper.callbackCopy(testFile, outputStream, 50, isAborted, callback);
586 CPPUNIT_ASSERT_EQUAL(1.0, percentage);
587
588 // verify again
589 CPPUNIT_ASSERT(outputStream.is_open());
590 CPPUNIT_ASSERT(testFile.is_open());
591 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellg());
592 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), outputStream.tellp());
593 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellg());
594 CPPUNIT_ASSERT_EQUAL(static_cast<std::fstream::pos_type>(50), testFile.tellp());
595 outputStream << aFewMoreBytes;
596 outputStream.close();
597 outputStream.open(outputPath, ios_base::in | ios_base::binary);
598 testFile.seekg(0);
599 for (auto i = 0; i < 50; ++i) {
600 CPPUNIT_ASSERT_EQUAL(testFile.get(), outputStream.get());
601 }
602 tail.assign(aFewMoreBytes.size(), '0');
603 outputStream.read(tail.data(), static_cast<std::streamsize>(tail.size()));
604 CPPUNIT_ASSERT_EQUAL(aFewMoreBytes, std::string_view(tail.data(), tail.size()));
605}
606
611{
612 // read a file successfully
613 const string iniFilePath(testFilePath("test.ini"));
614 CPPUNIT_ASSERT_EQUAL("# file for testing INI parser\n"
615 "key0=value 0\n"
616 "\n"
617 "[scope 1]\n"
618 "key1=value 1 # comment\n"
619 "key2=value=2\n"
620 "key3=value 3\n"
621 "\n"
622 "[scope 2]\n"
623 "key4=value 4\n"
624 "#key5=value 5\n"
625 "key6=value 6\n"s,
626 readFile(iniFilePath));
627
628 // fail by exceeding max size
629 CPPUNIT_ASSERT_THROW(readFile(iniFilePath, 10), std::ios_base::failure);
630
631 // handle UTF-8 in path and file contents correctly via NativeFileStream
632#if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
633 CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'ä' in its name\n"s, readFile(testFilePath("täst.txt")));
634#endif
635}
636
641{
642 const string path(workingCopyPath("test.ini", WorkingCopyMode::NoCopy));
643 writeFile(path, "some contents");
644 CPPUNIT_ASSERT_EQUAL("some contents"s, readFile(path));
645}
646
651{
652 stringstream ss1;
654 ss1 << EscapeCodes::Phrases::Error << "some error" << EscapeCodes::Phrases::End;
655 ss1 << EscapeCodes::Phrases::Warning << "some warning" << EscapeCodes::Phrases::End;
656 ss1 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
657 ss1 << EscapeCodes::Phrases::ErrorMessage << "Arch-style error" << EscapeCodes::Phrases::End;
658 ss1 << EscapeCodes::Phrases::WarningMessage << "Arch-style warning" << EscapeCodes::Phrases::End;
659 ss1 << EscapeCodes::Phrases::PlainMessage << "Arch-style message" << EscapeCodes::Phrases::End;
660 ss1 << EscapeCodes::Phrases::SuccessMessage << "Arch-style success" << EscapeCodes::Phrases::End;
661 ss1 << EscapeCodes::Phrases::SubMessage << "Arch-style sub-message" << EscapeCodes::Phrases::End;
662 ss1 << EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
663 << "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n';
664 cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n";
665 CPPUNIT_ASSERT_EQUAL("\033[1;31mError: \033[0m\033[1msome error\033[0m\n"
666 "\033[1;33mWarning: \033[0m\033[1msome warning\033[0m\n"
667 "\033[1;34mInfo: \033[0m\033[1msome info\033[0m\n"
668 "\033[1;31m==> ERROR: \033[0m\033[1mArch-style error\033[0m\n"
669 "\033[1;33m==> WARNING: \033[0m\033[1mArch-style warning\033[0m\n"
670 " \033[0m\033[1mArch-style message\033[0m\n"
671 "\033[1;32m==> \033[0m\033[1mArch-style success\033[0m\n"
672 "\033[1;32m -> \033[0m\033[1mArch-style sub-message\033[0m\n"
673 "\033[5;34;41mblue, blinking text on red background\033[0m\n"s,
674 ss1.str());
675
676 stringstream ss2;
677 EscapeCodes::enabled = false;
678 ss2 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
679 CPPUNIT_ASSERT_EQUAL("Info: some info\n"s, ss2.str());
680}
681
682#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
686void IoTests::testNativeFileStream()
687{
688 return;
689 // open file by path
690 const auto txtFilePath(workingCopyPath("täst.txt"));
691 NativeFileStream fileStream;
692 fileStream.exceptions(ios_base::badbit | ios_base::failbit);
693 CPPUNIT_ASSERT(!fileStream.is_open());
694 fileStream.open(txtFilePath, ios_base::in);
695 CPPUNIT_ASSERT(fileStream.is_open());
696#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
697 CPPUNIT_ASSERT(fileStream.fileHandle() != nullptr);
698#else
699 CPPUNIT_ASSERT(fileStream.fileDescriptor() != -1);
700#endif
701 CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
702 fileStream.seekg(0, ios_base::end);
703 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
704 fileStream.close();
705 CPPUNIT_ASSERT(!fileStream.is_open());
706 try {
707 fileStream.open("non existing file", ios_base::in | ios_base::out | ios_base::binary);
708 CPPUNIT_FAIL("expected exception");
709 } catch (const std::ios_base::failure &failure) {
710#ifdef PLATFORM_WINDOWS
711#ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
712 TESTUTILS_ASSERT_LIKE("expected error with some message", "CreateFileW failed: .+", failure.what());
713#else
714 TESTUTILS_ASSERT_LIKE("expected error with some message", "_wopen failed: .+", failure.what());
715#endif
716#else
717 TESTUTILS_ASSERT_LIKE("expected error with some message", "open failed: .+", failure.what());
718#endif
719 }
720 fileStream.clear();
721
722 // open file from file descriptor
723#ifndef PLATFORM_WINDOWS
724 auto readWriteFileDescriptor = open(txtFilePath.data(), O_RDWR);
725 CPPUNIT_ASSERT(readWriteFileDescriptor);
726 fileStream.open(readWriteFileDescriptor, ios_base::in | ios_base::out | ios_base::binary);
727 CPPUNIT_ASSERT(fileStream.is_open());
728 CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
729 fileStream.seekg(0, ios_base::end);
730 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
731 fileStream.flush();
732 fileStream.close();
733 CPPUNIT_ASSERT(!fileStream.is_open());
734#endif
735 try {
736 fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
737 fileStream.get();
738 CPPUNIT_FAIL("expected exception");
739 } catch (const std::ios_base::failure &failure) {
740#ifndef PLATFORM_WINDOWS
742 "expected error message", "(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s, string(failure.what()));
743#else
744 CPP_UTILITIES_UNUSED(failure)
745#endif
746 }
747 fileStream.clear();
748
749 // append + write file via path
750 NativeFileStream fileStream2;
751 fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
752 fileStream2.open(txtFilePath, ios_base::in | ios_base::out | ios_base::app);
753 CPPUNIT_ASSERT(fileStream2.is_open());
754 fileStream2 << "foo";
755 fileStream2.flush();
756 fileStream2.close();
757 CPPUNIT_ASSERT(!fileStream2.is_open());
758 CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'ä' in its name\nfoo"s, readFile(txtFilePath, 50));
759
760 // truncate + write file via path
761 fileStream2.open(txtFilePath, ios_base::out | ios_base::trunc);
762 CPPUNIT_ASSERT(fileStream2.is_open());
763 fileStream2 << "bar";
764 fileStream2.close();
765 CPPUNIT_ASSERT(!fileStream2.is_open());
766 CPPUNIT_ASSERT_EQUAL("bar"s, readFile(txtFilePath, 4));
767
768 // append + write via file descriptor from file handle
769#ifdef PLATFORM_WINDOWS
770 const auto wideTxtFilePath = NativeFileStream::makeWidePath(txtFilePath);
771 const auto appendFileHandle = _wfopen(wideTxtFilePath.get(), L"a+");
772#else
773 const auto appendFileHandle = fopen(txtFilePath.data(), "a");
774#endif
775 CPPUNIT_ASSERT(appendFileHandle);
776 fileStream2.open(fileno(appendFileHandle), ios_base::out | ios_base::app);
777 CPPUNIT_ASSERT(fileStream2.is_open());
778 fileStream2 << "foo";
779 fileStream2.close();
780 CPPUNIT_ASSERT(!fileStream2.is_open());
781 CPPUNIT_ASSERT_EQUAL("barfoo"s, readFile(txtFilePath, 7));
782}
783#endif
784
785#ifdef CPP_UTILITIES_USE_LIBARCHIVE
786void IoTests::testExtractingArchive()
787{
788 const auto archivePath = testFilePath("archive-test.zip");
789 const auto archiveContents = extractFiles(archivePath);
790 const auto &root = archiveContents.at(std::string());
791 const auto &subdir = archiveContents.at("subdir");
792 const auto &subsubdir = archiveContents.at("subdir/foo");
793
794 CPPUNIT_ASSERT_EQUAL(1_st, root.size());
795 CPPUNIT_ASSERT_EQUAL("test.txt"s, root.at(0).name);
796 CPPUNIT_ASSERT_EQUAL(ArchiveFileType::Regular, root.at(0).type);
797 CPPUNIT_ASSERT_EQUAL(DateTime::fromDate(1970, 1, 1), root.at(0).creationTime);
798 CPPUNIT_ASSERT_EQUAL(DateTime::fromDateAndTime(2024, 3, 3, 19, 46, 42), root.at(0).modificationTime);
799 CPPUNIT_ASSERT_EQUAL("testfile\n"s, root.at(0).content);
800
801 CPPUNIT_ASSERT_EQUAL(1_st, subdir.size());
802 CPPUNIT_ASSERT_EQUAL("nested-testfile.txt"s, subdir.at(0).name);
803 CPPUNIT_ASSERT_EQUAL(ArchiveFileType::Regular, subdir.at(0).type);
804 CPPUNIT_ASSERT_EQUAL("some file\n"s, subdir.at(0).content);
805
806 CPPUNIT_ASSERT_EQUAL(1_st, subsubdir.size());
807 CPPUNIT_ASSERT_EQUAL("bar"s, subsubdir.at(0).name);
808 CPPUNIT_ASSERT_EQUAL(ArchiveFileType::Regular, subsubdir.at(0).type);
809 CPPUNIT_ASSERT_EQUAL(std::string(), subsubdir.at(0).content);
810}
811#endif
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Definition global.h:92
Reads primitive data types from a std::istream.
std::int64_t readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
float readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
std::string readTerminatedString(std::uint8_t termination=0)
Reads a terminated string from the current stream.
std::string readString(std::size_t length)
Reads a string from the current stream of the given length from the stream and advances the current p...
std::uint64_t readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
std::int64_t readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
std::int32_t readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
std::int32_t readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
std::uint32_t readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint16_t readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
void setStream(std::istream *stream, bool giveOwnership=false)
Assigns the stream the reader will read from when calling one of the read-methods.
std::uint64_t readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
std::uint32_t readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
std::int32_t readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
std::int16_t readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
std::uint32_t readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
std::int64_t readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
std::istream::pos_type readRemainingBytes()
Returns the number of remaining bytes in the stream from the current offset.
std::uint64_t readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
std::uint64_t readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint64_t readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint16_t readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
std::int32_t readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
std::int64_t readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
double readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
std::uint32_t readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
std::int64_t readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
std::int16_t readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
double readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
std::int64_t readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
std::istream::pos_type readStreamsize()
Returns the size of the assigned stream.
std::uint64_t readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
float readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
std::uint8_t readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Writes primitive data types to a std::ostream.
void writeFloat32LE(float value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
void writeUInt40BE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt64LE(std::uint64_t value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
void writeInt40LE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
void writeUInt40LE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt64BE(std::int64_t value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt32LE(std::int32_t value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
void writeUInt24LE(std::uint32_t value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
void writeFloat32BE(float value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
void writeInt24BE(std::int32_t value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
void writeUInt56BE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt32BE(std::uint32_t value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
void writeFloat64BE(double value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
void writeFloat64LE(double value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
void writeString(const std::string &value)
Writes a string to the current stream and advances the current position of the stream by the length o...
void writeUInt56LE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt56LE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
void writeUInt16BE(std::uint16_t value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt64BE(std::uint64_t value)
Writes a 64-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt56BE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt16LE(std::int16_t value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
void writeUInt32LE(std::uint32_t value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
void writeUInt16LE(std::uint16_t value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
void writeInt16BE(std::int16_t value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
void writeInt24LE(std::int32_t value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
void writeUInt24BE(std::uint32_t value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
void writeTerminatedString(const std::string &value)
Writes a terminated string to the current stream and advances the current position of the stream by t...
void writeInt64LE(std::int64_t value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
void writeInt32BE(std::int32_t value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt40BE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
The BitReader class provides bitwise reading of buffered data.
Definition bitreader.h:13
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
Definition bitreader.h:148
std::uint8_t readBit()
Reads the one bit from the buffer advancing the current position by one bit.
Definition bitreader.h:89
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
Definition bitreader.cpp:43
void align()
Re-establishes alignment.
Definition bitreader.h:171
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
Definition bitreader.h:137
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
Definition bitreader.h:119
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
Definition bitreader.h:102
intType readBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits.
Definition bitreader.h:67
intType showBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
Definition bitreader.h:128
The BufferSearch struct invokes a callback if an initially given search term occurs in consecutively ...
The ConversionException class is thrown by the various conversion functions of this library when a co...
The CopyHelper class helps to copy bytes from one stream to another.
Definition copy.h:30
static DateTime fromDateAndTime(int year=1, int month=1, int day=1, int hour=0, int minute=0, int second=0, double millisecond=0.0)
Constructs a DateTime to the specified year, month, day, hour, minute, second and millisecond.
Definition datetime.h:230
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
Definition datetime.h:211
The IniFile class allows parsing and writing INI files.
Definition inifile.h:16
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
Definition inifile.cpp:40
ScopeList & data()
Returns the data of the file.
Definition inifile.h:46
void make(std::ostream &outputStream)
Write the current data to the specified outputStream.
Definition inifile.cpp:159
The IoTests class tests classes and functions provided by the files inside the io directory.
Definition iotests.cpp:66
void testBufferSearch()
Tests the BufferSearch class.
Definition iotests.cpp:322
void testBinaryReader()
Tests the most important methods of the BinaryReader.
Definition iotests.cpp:125
void testAnsiEscapeCodes()
Tests formatting functions of CppUtilities::EscapeCodes namespace.
Definition iotests.cpp:650
void testIniFile()
Tests IniFile.
Definition iotests.cpp:385
void testBitReader()
Tests the BitReader class.
Definition iotests.cpp:295
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
Definition iotests.cpp:191
void testCopyWithNativeFileStream()
Tests CopyHelper in combination with NativeFileStream.
Definition iotests.cpp:540
void setUp() override
Definition iotests.cpp:114
void testCopy()
Tests CopyHelper.
Definition iotests.cpp:513
void testPathUtilities()
Tests fileName() and removeInvalidChars().
Definition iotests.cpp:356
void tearDown() override
Definition iotests.cpp:118
void testWriteFile()
Tests writeFile().
Definition iotests.cpp:640
void testReadFile()
Tests readFile().
Definition iotests.cpp:610
void testAdvancedIniFile()
Tests AdvancedIniFile.
Definition iotests.cpp:426
CPPUNIT_TEST_SUITE_REGISTRATION(IoTests)
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition testutils.h:368
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT std::string readFile(const std::string &path, std::string::size_type maxSize=std::string::npos)
Reads all contents of the specified file in a single call.
Definition misc.cpp:17
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
Convenience function to invoke TestApplication::testFilePath().
Definition testutils.h:161
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
Definition testutils.h:179
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
CPP_UTILITIES_EXPORT FileMap extractFiles(std::string_view archivePath, const FilePredicate &isFileRelevant=FilePredicate())
Extracts the specified archive.
Definition archive.cpp:259
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
std::fstream NativeFileStream
StringType argsToString(Args &&...args)
AsHexNumber< T > asHexNumber(const T &value)
Wraps a value to be printed using the hex system in the error case when asserted with cppunit (or sim...
Definition testutils.h:258
CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents)
Writes all contents to the specified file in a single call.
Definition misc.cpp:58
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
Definition path.cpp:75
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
Definition path.cpp:17
NativePathStringView makeNativePath(PathStringView path)
Returns path in the platform's native encoding.
Definition path.h:82
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
Definition path.cpp:25
STL namespace.
The AdvancedIniFile class allows parsing and writing INI files.
Definition inifile.h:79
void make(std::ostream &outputStream, IniFileMakeOptions options=IniFileMakeOptions::None)
Write the current data to the specified outputStream.
Definition inifile.cpp:412
std::optional< FieldList::iterator > findField(std::string_view sectionName, std::string_view key)
Returns an iterator to the first field within the first section with matching sectionName and key.
Definition inifile.h:170
void parse(std::istream &inputStream, IniFileParseOptions options=IniFileParseOptions::None)
Parses all data from the specified inputStream.
Definition inifile.cpp:217
SectionList::iterator sectionEnd()
Returns an iterator that points one past the last section.
Definition inifile.h:154
SectionList::iterator findSection(std::string_view sectionName)
Returns an iterator to the first section with the name sectionName.
Definition inifile.h:122
#define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString)
Asserts whether the specified string matches the specified regex.
Definition testutils.h:328
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.
Definition testutils.h:338
constexpr int i