3#include "../conversion/stringconversion.h"
8std::ostream &
operator<<(std::ostream &out,
const std::wstring &s)
11#ifdef CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN
16 (
reinterpret_cast<const char *
>(s.data()), s.size() * (
sizeof(std::wstring::value_type) /
sizeof(
char)));
17 out.write(utf8.first.get(),
static_cast<std::streamsize
>(utf8.second));
21#include "../conversion/conversionexception.h"
22#include "../conversion/stringbuilder.h"
24#include "../io/ansiescapecodes.h"
25#include "../io/binaryreader.h"
26#include "../io/binarywriter.h"
27#include "../io/bitreader.h"
28#include "../io/buffersearch.h"
29#include "../io/copy.h"
30#include "../io/inifile.h"
31#include "../io/misc.h"
32#include "../io/nativefilestream.h"
33#include "../io/path.h"
35#include <cppunit/TestFixture.h>
36#include <cppunit/extensions/HelperMacros.h>
43#ifdef PLATFORM_WINDOWS
55using namespace CPPUNIT_NS;
73#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
74 CPPUNIT_TEST(testNativeFileStream);
76 CPPUNIT_TEST_SUITE_END();
79 void setUp()
override;
93#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
94 void testNativeFileStream();
115 testFile.exceptions(ios_base::failbit | ios_base::badbit);
116 testFile.open(
testFilePath(
"some_data"), ios_base::in | ios_base::binary);
118 CPPUNIT_ASSERT_EQUAL(
static_cast<istream::pos_type
>(398), reader.
readStreamsize());
119 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint16_t
>(0x0102u), reader.
readUInt16LE());
120 CPPUNIT_ASSERT_EQUAL(
static_cast<istream::pos_type
>(396), reader.
readRemainingBytes());
121 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint16_t
>(0x0102u), reader.
readUInt16BE());
124 CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.
readUInt32LE());
125 CPPUNIT_ASSERT_EQUAL(0x01020304u, reader.
readUInt32BE());
126 CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.
readUInt40LE());
127 CPPUNIT_ASSERT_EQUAL(0x0102030405u, reader.
readUInt40BE());
128 CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.
readUInt56LE());
129 CPPUNIT_ASSERT_EQUAL(0x01020304050607u, reader.
readUInt56BE());
130 CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.
readUInt64LE());
131 CPPUNIT_ASSERT_EQUAL(0x0102030405060708u, reader.
readUInt64BE());
133 CPPUNIT_ASSERT_EQUAL(reader.
readInt16LE(),
static_cast<std::int16_t
>(0x0102));
134 CPPUNIT_ASSERT_EQUAL(reader.
readInt16BE(),
static_cast<std::int16_t
>(0x0102));
135 CPPUNIT_ASSERT_EQUAL(0x010203, reader.
readInt24LE());
136 CPPUNIT_ASSERT_EQUAL(0x010203, reader.
readInt24BE());
137 CPPUNIT_ASSERT_EQUAL(0x01020304, reader.
readInt32LE());
138 CPPUNIT_ASSERT_EQUAL(0x01020304, reader.
readInt32BE());
139 CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.
readInt40LE());
140 CPPUNIT_ASSERT_EQUAL(0x0102030405, reader.
readInt40BE());
141 CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.
readInt56LE());
142 CPPUNIT_ASSERT_EQUAL(0x01020304050607, reader.
readInt56BE());
143 CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.
readInt64LE());
144 CPPUNIT_ASSERT_EQUAL(0x0102030405060708, reader.
readInt64BE());
149 CPPUNIT_ASSERT_EQUAL(
false, reader.
readBool());
150 CPPUNIT_ASSERT_EQUAL(
true, reader.
readBool());
151 CPPUNIT_ASSERT_EQUAL(
"abc"s, reader.
readString(3));
153 CPPUNIT_ASSERT_EQUAL(
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
154 "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
155 "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
156 "678901234567890123456789"s,
159 testFile.seekg(-4, ios_base::cur);
162 CPPUNIT_ASSERT_MESSAGE(
"pos in stream not advanced on conversion error", reader.
readByte() == 0);
181 testFile.exceptions(ios_base::failbit | ios_base::badbit);
182 testFile.open(
testFilePath(
"some_data"), ios_base::in | ios_base::binary);
185 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
186 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
188 outputStream.rdbuf()->pubsetbuf(testData,
sizeof(testData));
206 for (
char c : testData) {
207 CPPUNIT_ASSERT(c ==
static_cast<char>(testFile.get()));
208 if (testFile.tellg() >= 58) {
213 outputStream.seekp(0);
236 writer.
writeLengthPrefixedString(
"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
237 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
238 "234567890123456789012345678901234567890123456789012345678901234567890123456789");
242 for (
char c : testData) {
243 CPPUNIT_ASSERT(c ==
static_cast<char>(testFile.get()));
261 const std::uint8_t testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
262 BitReader reader(
reinterpret_cast<const char *
>(testData),
sizeof(testData));
263 CPPUNIT_ASSERT(reader.
readBit() == 1);
265 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint8_t
>(3), reader.
showBits<std::uint8_t>(2));
266 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint8_t
>(3), reader.
readBits<std::uint8_t>(2));
267 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint32_t
>(0x103C4428 << 1), reader.
readBits<std::uint32_t>(32));
269 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint8_t
>(0x44), reader.
readBits<std::uint8_t>(8));
272 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint8_t
>(0), reader.
readBit());
273 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint8_t
>(0), reader.
readBit());
276 CPPUNIT_ASSERT_EQUAL(
static_cast<std::uint8_t
>(0xA), reader.
readBits<std::uint8_t>(4));
277 CPPUNIT_ASSERT_THROW(reader.
readBit(), std::ios_base::failure);
278 CPPUNIT_ASSERT_THROW(reader.
skipBits(1), std::ios_base::failure);
279 reader.
reset(
reinterpret_cast<const char *
>(testData),
sizeof(testData));
280 CPPUNIT_ASSERT_EQUAL(
static_cast<std::size_t
>(8 *
sizeof(testData)), reader.
bitsAvailable());
289 auto expectedResult = std::string();
290 auto hasResult =
false;
292 CPPUNIT_ASSERT_EQUAL(expectedResult, result);
293 CPPUNIT_ASSERT_MESSAGE(
"callback only invoked once", !hasResult);
298 char buffer[30] = { 0 };
300 CPPUNIT_ASSERT(!hasResult);
301 std::strcpy(buffer,
"Starting Updated");
302 bs(std::string_view(buffer, 16));
303 CPPUNIT_ASSERT(!hasResult);
304 std::strcpy(buffer,
" version: some ");
306 CPPUNIT_ASSERT(!hasResult);
307 expectedResult =
"some version number";
308 std::strcpy(buffer,
"version number\emore chars");
310 CPPUNIT_ASSERT(hasResult);
312 std::strcpy(buffer,
"... Starting build ...");
314 CPPUNIT_ASSERT(!hasResult);
322 CPPUNIT_ASSERT_EQUAL(
"libc++utilities.so"s,
fileName(
"C:\\libs\\libc++utilities.so"));
323 CPPUNIT_ASSERT_EQUAL(
"libc++utilities.so"s,
fileName(
"C:\\libs/libc++utilities.so"));
324 CPPUNIT_ASSERT_EQUAL(
"libc++utilities.so"s,
fileName(
"/usr/lib/libc++utilities.so"));
325 CPPUNIT_ASSERT_EQUAL(
"libc++utilities.so"s,
fileName(
"libc++utilities.so"));
326 CPPUNIT_ASSERT_EQUAL(
"/usr/lib/"s,
directory(
"/usr/lib/libc++utilities.so"));
327 CPPUNIT_ASSERT_EQUAL(
string(),
directory(
"libc++utilities.so"));
328 CPPUNIT_ASSERT_EQUAL(
"C:\\libs\\"s,
directory(
"C:\\libs\\libc++utilities.so"));
329 CPPUNIT_ASSERT_EQUAL(
"C:\\libs/"s,
directory(
"C:\\libs/libc++utilities.so"));
330 string invalidPath(
"lib/c++uti*lities.so?");
332 CPPUNIT_ASSERT(invalidPath ==
"libc++utilities.so");
334 const auto input = std::string_view(
"some/path/täst");
335#ifdef PLATFORM_WINDOWS
336 const auto expected = std::wstring(L
"some/path/täst");
338 const auto expected = input;
341 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"makeNativePath()", expected, output);
351 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
355 ini.
parse(inputFile);
356 const auto globalScope = ini.
data().at(0);
357 const auto scope1 = ini.
data().at(1);
358 const auto scope2 = ini.
data().at(2);
359 CPPUNIT_ASSERT(globalScope.first.empty());
360 CPPUNIT_ASSERT(globalScope.second.find(
"key0") != globalScope.second.cend());
361 CPPUNIT_ASSERT(globalScope.second.find(
"key0")->second ==
"value 0");
362 CPPUNIT_ASSERT(globalScope.second.find(
"key1") == globalScope.second.cend());
363 CPPUNIT_ASSERT(scope1.first ==
"scope 1");
364 CPPUNIT_ASSERT(scope1.second.find(
"key1") != scope1.second.cend());
365 CPPUNIT_ASSERT(scope1.second.find(
"key1")->second ==
"value 1");
366 CPPUNIT_ASSERT(scope1.second.find(
"key2") != scope1.second.cend());
367 CPPUNIT_ASSERT(scope1.second.find(
"key2")->second ==
"value=2");
368 CPPUNIT_ASSERT(scope2.first ==
"scope 2");
369 CPPUNIT_ASSERT(scope2.second.find(
"key5") == scope2.second.cend());
373 outputFile.exceptions(ios_base::failbit | ios_base::badbit);
374 outputFile.open(
workingCopyPath(
"output.ini", WorkingCopyMode::NoCopy), ios_base::out | ios_base::trunc);
375 ini.
make(outputFile);
379 outputFile.open(
workingCopyPath(
"output.ini", WorkingCopyMode::NoCopy), ios_base::in);
381 ini2.
parse(outputFile);
382 CPPUNIT_ASSERT(ini.
data() == ini2.
data());
392 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
393 inputFile.open(
testFilePath(
"pacman.conf"), ios_base::in);
397 ini.
parse(inputFile);
400 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"5 scopes (taking implicit empty section at the end into account)", 5_st, ini.
sections.size());
404 "comment block before section",
"# Based on.*\n.*# GENERAL OPTIONS\n#\n"s, std::regex::extended, options->precedingCommentBlock);
405 CPPUNIT_ASSERT_EQUAL(7_st, options->fields.size());
406 CPPUNIT_ASSERT_EQUAL(
"HoldPkg"s, options->fields[0].key);
407 CPPUNIT_ASSERT_EQUAL(
"pacman glibc"s, options->fields[0].value);
408 CPPUNIT_ASSERT_MESSAGE(
"value present", options->fields[0].flags & IniFileFieldFlags::HasValue);
410 "# The following paths are.*\n.*#HookDir = /etc/pacman\\.d/hooks/\n"s, std::regex::extended, options->fields[0].precedingCommentBlock);
411 CPPUNIT_ASSERT_EQUAL(
""s, options->fields[0].followingInlineComment);
412 CPPUNIT_ASSERT_EQUAL(
"Foo"s, options->fields[1].key);
413 CPPUNIT_ASSERT_EQUAL(
"bar"s, options->fields[1].value);
414 CPPUNIT_ASSERT_MESSAGE(
"value present", options->fields[1].flags & IniFileFieldFlags::HasValue);
415 TESTUTILS_ASSERT_LIKE_FLAGS(
"comment block between fields",
"#XferCommand.*\n.*#CleanMethod = KeepInstalled\n"s, std::regex::extended,
416 options->fields[1].precedingCommentBlock);
417 CPPUNIT_ASSERT_EQUAL(
"# inline comment"s, options->fields[1].followingInlineComment);
418 CPPUNIT_ASSERT_EQUAL(
"CheckSpace"s, options->fields[3].key);
419 CPPUNIT_ASSERT_EQUAL(
""s, options->fields[3].value);
420 CPPUNIT_ASSERT_MESSAGE(
"no value present", !(options->fields[3].flags & IniFileFieldFlags::HasValue));
421 TESTUTILS_ASSERT_LIKE_FLAGS(
"empty lines in comments preserved",
"\n# Pacman.*\n.*\n\n#NoUpgrade =\n.*#TotalDownload\n"s, std::regex::extended,
422 options->fields[3].precedingCommentBlock);
423 CPPUNIT_ASSERT_EQUAL(
""s, options->fields[3].followingInlineComment);
424 auto extraScope = ini.
findSection(options,
"extra");
425 CPPUNIT_ASSERT(extraScope != ini.
sectionEnd());
426 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"comment block which is only an empty line",
"\n"s, extraScope->precedingCommentBlock);
427 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"inline comment after scope",
"# an inline comment after a scope name"s, extraScope->followingInlineComment);
428 CPPUNIT_ASSERT_EQUAL(1_st, extraScope->fields.size());
429 CPPUNIT_ASSERT(ini.
sections.back().flags & IniFileSectionFlags::Implicit);
430 TESTUTILS_ASSERT_LIKE_FLAGS(
"comment block after last field present in implicitly added last scope",
"\n# If you.*\n.*custompkgs\n"s,
431 std::regex::extended, ini.
sections.back().precedingCommentBlock);
434 const auto *
const constIniFile = &ini;
435 auto includeField = constIniFile->
findField(
"extra",
"Include");
436 CPPUNIT_ASSERT(includeField.has_value());
437 CPPUNIT_ASSERT_EQUAL(
"Include"s, includeField.value()->key);
438 CPPUNIT_ASSERT_EQUAL(
"/etc/pacman.d/mirrorlist"s, includeField.value()->value);
439 CPPUNIT_ASSERT_MESSAGE(
"field not present", !constIniFile->findField(
"extra",
"Includ").has_value());
440 CPPUNIT_ASSERT_MESSAGE(
"scope not present", !constIniFile->findField(
"extr",
"Includ").has_value());
444 std::stringstream newFile;
446 std::string originalContents;
448 inputFile.seekg(std::ios_base::beg);
449 originalContents.assign((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
450 CPPUNIT_ASSERT_EQUAL(originalContents, newFile.str());
460 testFile.exceptions(ios_base::failbit | ios_base::badbit);
461 testFile.open(
testFilePath(
"some_data"), ios_base::in | ios_base::binary);
462 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
463 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
467 copyHelper.
copy(testFile, outputStream, 50);
471 for (
auto i = 0;
i < 50; ++
i) {
472 CPPUNIT_ASSERT(testFile.get() == outputStream.get());
483 CPPUNIT_ASSERT_EQUAL(
"# file for testing INI parser\n"
487 "key1=value 1 # comment\n"
498 CPPUNIT_ASSERT_THROW(
readFile(iniFilePath, 10), std::ios_base::failure);
501#if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
502 CPPUNIT_ASSERT_EQUAL(
"file with non-ASCII character 'ä' in its name\n"s,
readFile(
testFilePath(
"täst.txt")));
511 const string path(
workingCopyPath(
"test.ini", WorkingCopyMode::NoCopy));
513 CPPUNIT_ASSERT_EQUAL(
"some contents"s,
readFile(path));
523 ss1 << EscapeCodes::Phrases::Error <<
"some error" << EscapeCodes::Phrases::End;
524 ss1 << EscapeCodes::Phrases::Warning <<
"some warning" << EscapeCodes::Phrases::End;
525 ss1 << EscapeCodes::Phrases::Info <<
"some info" << EscapeCodes::Phrases::End;
526 ss1 << EscapeCodes::Phrases::ErrorMessage <<
"Arch-style error" << EscapeCodes::Phrases::End;
527 ss1 << EscapeCodes::Phrases::WarningMessage <<
"Arch-style warning" << EscapeCodes::Phrases::End;
528 ss1 << EscapeCodes::Phrases::PlainMessage <<
"Arch-style message" << EscapeCodes::Phrases::End;
529 ss1 << EscapeCodes::Phrases::SuccessMessage <<
"Arch-style success" << EscapeCodes::Phrases::End;
530 ss1 << EscapeCodes::Phrases::SubMessage <<
"Arch-style sub-message" << EscapeCodes::Phrases::End;
531 ss1 <<
EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
532 <<
"blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset <<
'\n';
533 cout <<
"\noutput for formatting with ANSI escape codes:\n" << ss1.str() <<
"---------------------------------------------\n";
534 fstream(
"/tmp/test.txt", ios_base::out | ios_base::trunc) << ss1.str();
535 CPPUNIT_ASSERT_EQUAL(
"\e[1;31mError: \e[0m\e[1msome error\e[0m\n"
536 "\e[1;33mWarning: \e[0m\e[1msome warning\e[0m\n"
537 "\e[1;34mInfo: \e[0m\e[1msome info\e[0m\n"
538 "\e[1;31m==> ERROR: \e[0m\e[1mArch-style error\e[0m\n"
539 "\e[1;33m==> WARNING: \e[0m\e[1mArch-style warning\e[0m\n"
540 " \e[0m\e[1mArch-style message\e[0m\n"
541 "\e[1;32m==> \e[0m\e[1mArch-style success\e[0m\n"
542 "\e[1;32m -> \e[0m\e[1mArch-style sub-message\e[0m\n"
543 "\e[5;34;41mblue, blinking text on red background\e[0m\n"s,
548 ss2 << EscapeCodes::Phrases::Info <<
"some info" << EscapeCodes::Phrases::End;
549 CPPUNIT_ASSERT_EQUAL(
"Info: some info\n"s, ss2.str());
552#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
556void IoTests::testNativeFileStream()
561 fileStream.exceptions(ios_base::badbit | ios_base::failbit);
562 CPPUNIT_ASSERT(!fileStream.is_open());
563 fileStream.open(txtFilePath, ios_base::in);
564 CPPUNIT_ASSERT(fileStream.is_open());
565#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
566 CPPUNIT_ASSERT(fileStream.fileHandle() !=
nullptr);
568 CPPUNIT_ASSERT(fileStream.fileDescriptor() != -1);
570 CPPUNIT_ASSERT_EQUAL(
static_cast<char>(fileStream.get()),
'f');
571 fileStream.seekg(0, ios_base::end);
572 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(),
static_cast<NativeFileStream::pos_type
>(47));
574 CPPUNIT_ASSERT(!fileStream.is_open());
576 fileStream.open(
"non existing file", ios_base::in | ios_base::out | ios_base::binary);
577 CPPUNIT_FAIL(
"expected exception");
578 }
catch (
const std::ios_base::failure &failure) {
579#ifdef PLATFORM_WINDOWS
580#ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
592#ifndef PLATFORM_WINDOWS
593 auto readWriteFileDescriptor = open(txtFilePath.data(), O_RDWR);
594 CPPUNIT_ASSERT(readWriteFileDescriptor);
595 fileStream.open(readWriteFileDescriptor, ios_base::in | ios_base::out | ios_base::binary);
596 CPPUNIT_ASSERT(fileStream.is_open());
597 CPPUNIT_ASSERT_EQUAL(
static_cast<char>(fileStream.get()),
'f');
598 fileStream.seekg(0, ios_base::end);
599 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(),
static_cast<NativeFileStream::pos_type
>(47));
602 CPPUNIT_ASSERT(!fileStream.is_open());
605 fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
607 CPPUNIT_FAIL(
"expected exception");
608 }
catch (
const std::ios_base::failure &failure) {
609#ifndef PLATFORM_WINDOWS
611 "expected error message",
"(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s,
string(failure.what()));
620 fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
621 fileStream2.open(txtFilePath, ios_base::in | ios_base::out | ios_base::app);
622 CPPUNIT_ASSERT(fileStream2.is_open());
623 fileStream2 <<
"foo";
626 CPPUNIT_ASSERT(!fileStream2.is_open());
627 CPPUNIT_ASSERT_EQUAL(
"file with non-ASCII character 'ä' in its name\nfoo"s,
readFile(txtFilePath, 50));
630 fileStream2.open(txtFilePath, ios_base::out | ios_base::trunc);
631 CPPUNIT_ASSERT(fileStream2.is_open());
632 fileStream2 <<
"bar";
634 CPPUNIT_ASSERT(!fileStream2.is_open());
635 CPPUNIT_ASSERT_EQUAL(
"bar"s,
readFile(txtFilePath, 4));
638#ifdef PLATFORM_WINDOWS
639 const auto wideTxtFilePath = NativeFileStream::makeWidePath(txtFilePath);
640 const auto appendFileHandle = _wfopen(wideTxtFilePath.get(), L
"a+");
642 const auto appendFileHandle = fopen(txtFilePath.data(),
"a");
644 CPPUNIT_ASSERT(appendFileHandle);
645 fileStream2.open(fileno(appendFileHandle), ios_base::out | ios_base::app);
646 CPPUNIT_ASSERT(fileStream2.is_open());
647 fileStream2 <<
"foo";
649 CPPUNIT_ASSERT(!fileStream2.is_open());
650 CPPUNIT_ASSERT_EQUAL(
"barfoo"s,
readFile(txtFilePath, 7));
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
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.
void reset(const char *buffer, std::size_t bufferSize)
Resets the reader.
std::uint8_t readBit()
Reads the one bit from the buffer advancing the current position by one bit.
void skipBits(std::size_t bitCount)
Skips the specified number of bits without reading it.
void align()
Re-establishes alignment.
std::size_t bitsAvailable()
Returns the number of bits which are still available to read.
intType readSignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (signed).
intType readUnsignedExpGolombCodedBits()
Reads "Exp-Golomb coded" bits (unsigned).
intType readBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer advancing the current position by bitCount bits.
intType showBits(std::uint8_t bitCount)
Reads the specified number of bits from the buffer without advancing the current position.
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.
void copy(std::istream &input, std::ostream &output, std::size_t count)
Copies count bytes from input to output.
The IniFile class allows parsing and writing INI files.
void parse(std::istream &inputStream)
Parses all data from the specified inputStream.
ScopeList & data()
Returns the data of the file.
void make(std::ostream &outputStream)
Write the current data to the specified outputStream.
The IoTests class tests classes and functions provided by the files inside the io directory.
void testBufferSearch()
Tests the BufferSearch class.
void testBinaryReader()
Tests the most important methods of the BinaryReader.
void testAnsiEscapeCodes()
Tests formatting functions of CppUtilities::EscapeCodes namespace.
void testIniFile()
Tests IniFile.
void testBitReader()
Tests the BitReader class.
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
void testCopy()
Tests CopyHelper.
void testPathUtilities()
Tests fileName() and removeInvalidChars().
void testWriteFile()
Tests writeFile().
void testReadFile()
Tests readFile().
void testAdvancedIniFile()
Tests AdvancedIniFile.
CPPUNIT_TEST_SUITE_REGISTRATION(IoTests)
std::ostream & operator<<(std::ostream &out, const std::wstring &s)
Allows printing std::wstring using CPPUNIT_ASSERT_EQUAL.
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.
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.
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
Convenience function to invoke TestApplication::testFilePath().
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
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 StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
std::string_view makeNativePath(std::string_view path)
Returns path in the platform's native encoding.
CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents)
Writes all contents to the specified file in a single call.
std::fstream NativeFileStream
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
The AdvancedIniFile class allows parsing and writing INI files.
void make(std::ostream &outputStream, IniFileMakeOptions options=IniFileMakeOptions::None)
Write the current data to the specified outputStream.
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.
void parse(std::istream &inputStream, IniFileParseOptions options=IniFileParseOptions::None)
Parses all data from the specified inputStream.
SectionList::iterator sectionEnd()
Returns an iterator that points one past the last section.
SectionList::iterator findSection(std::string_view sectionName)
Returns an iterator to the first section with the name sectionName.
#define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString)
Asserts whether the specified string matches the specified regex.
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.