C++ Utilities 5.21.0
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
3#include "../conversion/stringconversion.h"
4
8std::ostream &operator<<(std::ostream &out, const std::wstring &s)
9{
10 const auto utf8 = CppUtilities::
11#ifdef CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN
13#else
15#endif
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));
18 return out;
19}
20
21#include "../conversion/conversionexception.h"
22#include "../conversion/stringbuilder.h"
23
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"
34
35#include <cppunit/TestFixture.h>
36#include <cppunit/extensions/HelperMacros.h>
37
38#include <algorithm>
39#include <fstream>
40#include <regex>
41#include <sstream>
42
43#ifdef PLATFORM_WINDOWS
44#include <cstdio>
45#endif
46
47#ifdef PLATFORM_UNIX
48#include <sys/fcntl.h>
49#include <sys/types.h>
50#endif
51
52using namespace std;
53using namespace CppUtilities;
54using namespace CppUtilities::Literals;
55using namespace CPPUNIT_NS;
56
60class IoTests : public TestFixture {
61 CPPUNIT_TEST_SUITE(IoTests);
62 CPPUNIT_TEST(testBinaryReader);
63 CPPUNIT_TEST(testBinaryWriter);
64 CPPUNIT_TEST(testBitReader);
65 CPPUNIT_TEST(testBufferSearch);
66 CPPUNIT_TEST(testPathUtilities);
67 CPPUNIT_TEST(testIniFile);
68 CPPUNIT_TEST(testAdvancedIniFile);
69 CPPUNIT_TEST(testCopy);
70 CPPUNIT_TEST(testReadFile);
71 CPPUNIT_TEST(testWriteFile);
72 CPPUNIT_TEST(testAnsiEscapeCodes);
73#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
74 CPPUNIT_TEST(testNativeFileStream);
75#endif
76 CPPUNIT_TEST_SUITE_END();
77
78public:
79 void setUp() override;
80 void tearDown() override;
81
82 void testBinaryReader();
83 void testBinaryWriter();
84 void testBitReader();
85 void testBufferSearch();
86 void testPathUtilities();
87 void testIniFile();
89 void testCopy();
90 void testReadFile();
91 void testWriteFile();
93#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
94 void testNativeFileStream();
95#endif
96};
97
99
101{
102}
103
105{
106}
107
112{
113 // read test file
114 fstream testFile;
115 testFile.exceptions(ios_base::failbit | ios_base::badbit);
116 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
117 BinaryReader reader(&testFile);
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());
122 CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24LE());
123 CPPUNIT_ASSERT_EQUAL(0x010203u, reader.readUInt24BE());
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());
132 testFile.seekg(0);
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());
145 CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32LE());
146 CPPUNIT_ASSERT_EQUAL(1.625, reader.readFloat64LE());
147 CPPUNIT_ASSERT_EQUAL(1.125f, reader.readFloat32BE());
148 CPPUNIT_ASSERT_EQUAL(reader.readFloat64BE(), 1.625);
149 CPPUNIT_ASSERT_EQUAL(false, reader.readBool());
150 CPPUNIT_ASSERT_EQUAL(true, reader.readBool());
151 CPPUNIT_ASSERT_EQUAL("abc"s, reader.readString(3));
152 CPPUNIT_ASSERT_EQUAL(reader.readLengthPrefixedString(), "ABC"s);
153 CPPUNIT_ASSERT_EQUAL("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
154 "23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
155 "45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
156 "678901234567890123456789"s,
157 reader.readLengthPrefixedString());
158 CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString());
159 testFile.seekg(-4, ios_base::cur);
160 CPPUNIT_ASSERT_EQUAL("def"s, reader.readTerminatedString(5, 0));
161 CPPUNIT_ASSERT_THROW(reader.readLengthPrefixedString(), ConversionException);
162 CPPUNIT_ASSERT_MESSAGE("pos in stream not advanced on conversion error", reader.readByte() == 0);
163
164 // test ownership
165 reader.setStream(nullptr, true);
166 reader.setStream(new fstream(), true);
167 BinaryReader reader2(reader);
168 CPPUNIT_ASSERT(reader2.stream() == reader.stream());
169 CPPUNIT_ASSERT(!reader2.hasOwnership());
170 reader.setStream(&testFile, false);
171 reader.setStream(new fstream(), true);
172}
173
178{
179 // prepare reading expected data
180 fstream testFile;
181 testFile.exceptions(ios_base::failbit | ios_base::badbit);
182 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
183
184 // prepare output stream
185 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
186 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
187 char testData[397];
188#if defined(__GLIBCXX__) && !defined(_LIBCPP_VERSION)
189#define USE_RDBUF_DIRECTLY
190 outputStream.rdbuf()->pubsetbuf(testData, sizeof(testData));
191#endif
192
193 // write test data
194 BinaryWriter writer(&outputStream);
195 writer.writeUInt16LE(0x0102u);
196 writer.writeUInt16BE(0x0102u);
197 writer.writeUInt24LE(0x010203u);
198 writer.writeUInt24BE(0x010203u);
199 writer.writeUInt32LE(0x01020304u);
200 writer.writeUInt32BE(0x01020304u);
201 writer.writeUInt40LE(0x0102030405u);
202 writer.writeUInt40BE(0x0102030405u);
203 writer.writeUInt56LE(0x01020304050607u);
204 writer.writeUInt56BE(0x01020304050607u);
205 writer.writeUInt64LE(0x0102030405060708u);
206 writer.writeUInt64BE(0x0102030405060708u);
207
208#ifndef USE_RDBUF_DIRECTLY
209 outputStream.seekg(0);
210 outputStream.read(testData, 58);
211#endif
212
213 // test written values
214 for (char c : testData) {
215 const auto pos = static_cast<std::size_t>(testFile.tellg());
216 if (pos >= 58) {
217 break;
218 }
219 char expected;
220 testFile.read(&expected, 1);
221 CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("offset ", pos), asHexNumber(expected), asHexNumber(c));
222 }
223 testFile.seekg(0);
224 outputStream.seekp(0);
225
226 // write more test data
227 writer.writeInt16LE(0x0102);
228 writer.writeInt16BE(0x0102);
229 writer.writeInt24LE(0x010203);
230 writer.writeInt24BE(0x010203);
231 writer.writeInt32LE(0x01020304);
232 writer.writeInt32BE(0x01020304);
233 writer.writeInt40LE(0x0102030405);
234 writer.writeInt40BE(0x0102030405);
235 writer.writeInt56LE(0x01020304050607);
236 writer.writeInt56BE(0x01020304050607);
237 writer.writeInt64LE(0x0102030405060708);
238 writer.writeInt64BE(0x0102030405060708);
239 writer.writeFloat32LE(1.125);
240 writer.writeFloat64LE(1.625);
241 writer.writeFloat32BE(1.125);
242 writer.writeFloat64BE(1.625);
243 writer.writeBool(false);
244 writer.writeBool(true);
245 writer.writeString("abc");
246 writer.writeLengthPrefixedString("ABC");
247 writer.writeLengthPrefixedString("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
248 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
249 "234567890123456789012345678901234567890123456789012345678901234567890123456789");
250 writer.writeTerminatedString("def");
251
252#ifndef USE_RDBUF_DIRECTLY
253 outputStream.seekg(0);
254 outputStream.read(testData, 58);
255#endif
256
257 // test written values
258 for (char c : testData) {
259 const auto pos = static_cast<std::size_t>(testFile.tellg());
260 if (pos >= 58) {
261 break;
262 }
263 char expected;
264 testFile.read(&expected, 1);
265 CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("offset ", pos), asHexNumber(expected), asHexNumber(c));
266 }
267
268 // test ownership
269 writer.setStream(nullptr, true);
270 writer.setStream(new fstream(), true);
271 BinaryWriter writer2(writer);
272 CPPUNIT_ASSERT(writer2.stream() == writer.stream());
273 CPPUNIT_ASSERT(!writer2.hasOwnership());
274 writer.setStream(&testFile, false);
275 writer.setStream(new fstream(), true);
276}
277
282{
283 const std::uint8_t testData[] = { 0x81, 0x90, 0x3C, 0x44, 0x28, 0x00, 0x44, 0x10, 0x20, 0xFF, 0xFA };
284 BitReader reader(reinterpret_cast<const char *>(testData), sizeof(testData));
285 CPPUNIT_ASSERT(reader.readBit() == 1);
286 reader.skipBits(6);
287 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.showBits<std::uint8_t>(2));
288 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(3), reader.readBits<std::uint8_t>(2));
289 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(0x103C4428 << 1), reader.readBits<std::uint32_t>(32));
290 reader.align();
291 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0x44), reader.readBits<std::uint8_t>(8));
292 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(7), reader.readUnsignedExpGolombCodedBits<std::uint8_t>());
293 CPPUNIT_ASSERT_EQUAL(static_cast<std::int8_t>(4), reader.readSignedExpGolombCodedBits<std::int8_t>());
294 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
295 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0), reader.readBit());
296 reader.skipBits(8 + 4);
297 CPPUNIT_ASSERT_EQUAL(4_st, reader.bitsAvailable());
298 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint8_t>(0xA), reader.readBits<std::uint8_t>(4));
299 CPPUNIT_ASSERT_THROW(reader.readBit(), std::ios_base::failure);
300 CPPUNIT_ASSERT_THROW(reader.skipBits(1), std::ios_base::failure);
301 reader.reset(reinterpret_cast<const char *>(testData), sizeof(testData));
302 CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(8 * sizeof(testData)), reader.bitsAvailable());
303}
304
309{
310 // setup search to test
311 auto expectedResult = std::string();
312 auto hasResult = false;
313 auto bs = BufferSearch("Updated version: ", "\t\n", "Starting build", [&](BufferSearch &, std::string &&result) {
314 CPPUNIT_ASSERT_EQUAL(expectedResult, result);
315 CPPUNIT_ASSERT_MESSAGE("callback only invoked once", !hasResult);
316 hasResult = true;
317 });
318
319 // feed data into the search
320 char buffer[30] = { 0 };
321 bs(buffer, 0);
322 CPPUNIT_ASSERT(!hasResult);
323 std::strcpy(buffer, "Starting Updated");
324 bs(std::string_view(buffer, 16));
325 CPPUNIT_ASSERT(!hasResult);
326 std::strcpy(buffer, " version: some ");
327 bs(buffer, 15);
328 CPPUNIT_ASSERT(!hasResult);
329 expectedResult = "some version number";
330 std::strcpy(buffer, "version number\tmore chars");
331 bs(buffer, 25);
332 CPPUNIT_ASSERT(hasResult);
333 hasResult = false;
334 std::strcpy(buffer, "... Starting build ...");
335 bs(buffer, 22);
336 CPPUNIT_ASSERT(!hasResult);
337}
338
343{
344 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs\\libc++utilities.so"));
345 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("C:\\libs/libc++utilities.so"));
346 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("/usr/lib/libc++utilities.so"));
347 CPPUNIT_ASSERT_EQUAL("libc++utilities.so"s, fileName("libc++utilities.so"));
348 CPPUNIT_ASSERT_EQUAL("/usr/lib/"s, directory("/usr/lib/libc++utilities.so"));
349 CPPUNIT_ASSERT_EQUAL(string(), directory("libc++utilities.so"));
350 CPPUNIT_ASSERT_EQUAL("C:\\libs\\"s, directory("C:\\libs\\libc++utilities.so"));
351 CPPUNIT_ASSERT_EQUAL("C:\\libs/"s, directory("C:\\libs/libc++utilities.so"));
352 string invalidPath("lib/c++uti*lities.so?");
353 removeInvalidChars(invalidPath);
354 CPPUNIT_ASSERT(invalidPath == "libc++utilities.so");
355
356 const auto input = std::string_view("some/path/täst");
357 const auto expected = input;
358 const auto output = makeNativePath(input);
359#ifdef PLATFORM_WINDOWS
360 const auto outputAsUtf8 = convertUtf16LEToUtf8(reinterpret_cast<const char *>(output.data()), output.size() * 2);
361 const auto outputView = std::string_view(outputAsUtf8.first.get(), outputAsUtf8.second);
362 CPPUNIT_ASSERT_EQUAL_MESSAGE("makeNativePath()", expected, outputView);
363#else
364 CPPUNIT_ASSERT_EQUAL_MESSAGE("makeNativePath()", expected, output);
365#endif
366}
367
372{
373 // prepare reading test file
374 fstream inputFile;
375 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
376 inputFile.open(testFilePath("test.ini"), ios_base::in);
377
378 IniFile ini;
379 ini.parse(inputFile);
380 const auto globalScope = ini.data().at(0);
381 const auto scope1 = ini.data().at(1);
382 const auto scope2 = ini.data().at(2);
383 CPPUNIT_ASSERT(globalScope.first.empty());
384 CPPUNIT_ASSERT(globalScope.second.find("key0") != globalScope.second.cend());
385 CPPUNIT_ASSERT(globalScope.second.find("key0")->second == "value 0");
386 CPPUNIT_ASSERT(globalScope.second.find("key1") == globalScope.second.cend());
387 CPPUNIT_ASSERT(scope1.first == "scope 1");
388 CPPUNIT_ASSERT(scope1.second.find("key1") != scope1.second.cend());
389 CPPUNIT_ASSERT(scope1.second.find("key1")->second == "value 1");
390 CPPUNIT_ASSERT(scope1.second.find("key2") != scope1.second.cend());
391 CPPUNIT_ASSERT(scope1.second.find("key2")->second == "value=2");
392 CPPUNIT_ASSERT(scope2.first == "scope 2");
393 CPPUNIT_ASSERT(scope2.second.find("key5") == scope2.second.cend());
394
395 // write values to another file
396 fstream outputFile;
397 outputFile.exceptions(ios_base::failbit | ios_base::badbit);
398 outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::out | ios_base::trunc);
399 ini.make(outputFile);
400
401 // parse written values (again)
402 outputFile.close();
403 outputFile.open(workingCopyPath("output.ini", WorkingCopyMode::NoCopy), ios_base::in);
404 IniFile ini2;
405 ini2.parse(outputFile);
406 CPPUNIT_ASSERT(ini.data() == ini2.data());
407}
408
413{
414 // prepare reading test file
415 fstream inputFile;
416 inputFile.exceptions(ios_base::failbit | ios_base::badbit);
417 inputFile.open(testFilePath("pacman.conf"), ios_base::in);
418
419 // parse the test file
420 AdvancedIniFile ini;
421 ini.parse(inputFile);
422
423 // check whether scope data is as expected
424 CPPUNIT_ASSERT_EQUAL_MESSAGE("5 scopes (taking implicit empty section at the end into account)", 5_st, ini.sections.size());
425 auto options = ini.findSection("options");
426 CPPUNIT_ASSERT(options != ini.sectionEnd());
427#if !defined(PLATFORM_WINDOWS) || defined(PLATFORM_MINGW) || defined(PLATFORM_CYGWIN)
428#define STD_REGEX_WORKS // the parsed data looks good, apparently std::regex behaves differently on MSVC
430 "comment block before section", "# Based on.*\n.*# GENERAL OPTIONS\n#\n"s, std::regex::extended, options->precedingCommentBlock);
431#endif
432 CPPUNIT_ASSERT_EQUAL(7_st, options->fields.size());
433 CPPUNIT_ASSERT_EQUAL("HoldPkg"s, options->fields[0].key);
434 CPPUNIT_ASSERT_EQUAL("pacman glibc"s, options->fields[0].value);
435 CPPUNIT_ASSERT_MESSAGE("value present", options->fields[0].flags & IniFileFieldFlags::HasValue);
436#ifdef STD_REGEX_WORKS
437 TESTUTILS_ASSERT_LIKE_FLAGS("comment block between section header and first field",
438 "# The following paths are.*\n.*#HookDir = /etc/pacman\\.d/hooks/\n"s, std::regex::extended, options->fields[0].precedingCommentBlock);
439#endif
440 CPPUNIT_ASSERT_EQUAL(""s, options->fields[0].followingInlineComment);
441 CPPUNIT_ASSERT_EQUAL("Foo"s, options->fields[1].key);
442 CPPUNIT_ASSERT_EQUAL("bar"s, options->fields[1].value);
443 CPPUNIT_ASSERT_MESSAGE("value present", options->fields[1].flags & IniFileFieldFlags::HasValue);
444#ifdef STD_REGEX_WORKS
445 TESTUTILS_ASSERT_LIKE_FLAGS("comment block between fields", "#XferCommand.*\n.*#CleanMethod = KeepInstalled\n"s, std::regex::extended,
446 options->fields[1].precedingCommentBlock);
447#endif
448 CPPUNIT_ASSERT_EQUAL("# inline comment"s, options->fields[1].followingInlineComment);
449 CPPUNIT_ASSERT_EQUAL("CheckSpace"s, options->fields[3].key);
450 CPPUNIT_ASSERT_EQUAL(""s, options->fields[3].value);
451 CPPUNIT_ASSERT_MESSAGE("no value present", !(options->fields[3].flags & IniFileFieldFlags::HasValue));
452#ifdef STD_REGEX_WORKS
453 TESTUTILS_ASSERT_LIKE_FLAGS("empty lines in comments preserved", "\n# Pacman.*\n.*\n\n#NoUpgrade =\n.*#TotalDownload\n"s, std::regex::extended,
454 options->fields[3].precedingCommentBlock);
455#endif
456 CPPUNIT_ASSERT_EQUAL(""s, options->fields[3].followingInlineComment);
457 auto extraScope = ini.findSection(options, "extra");
458 CPPUNIT_ASSERT(extraScope != ini.sectionEnd());
459 CPPUNIT_ASSERT_EQUAL_MESSAGE("comment block which is only an empty line", "\n"s, extraScope->precedingCommentBlock);
460 CPPUNIT_ASSERT_EQUAL_MESSAGE("inline comment after scope", "# an inline comment after a scope name"s, extraScope->followingInlineComment);
461 CPPUNIT_ASSERT_EQUAL(1_st, extraScope->fields.size());
462 CPPUNIT_ASSERT(ini.sections.back().flags & IniFileSectionFlags::Implicit);
463#ifdef STD_REGEX_WORKS
464 TESTUTILS_ASSERT_LIKE_FLAGS("comment block after last field present in implicitly added last scope", "\n# If you.*\n.*custompkgs\n"s,
465 std::regex::extended, ini.sections.back().precedingCommentBlock);
466#endif
467
468 // test finding a field from file level and const access
469 const auto *const constIniFile = &ini;
470 auto includeField = constIniFile->findField("extra", "Include");
471 CPPUNIT_ASSERT(includeField.has_value());
472 CPPUNIT_ASSERT_EQUAL("Include"s, includeField.value()->key);
473 CPPUNIT_ASSERT_EQUAL("/etc/pacman.d/mirrorlist"s, includeField.value()->value);
474 CPPUNIT_ASSERT_MESSAGE("field not present", !constIniFile->findField("extra", "Includ").has_value());
475 CPPUNIT_ASSERT_MESSAGE("scope not present", !constIniFile->findField("extr", "Includ").has_value());
476
477 // write values again; there shouldn't be a difference as the parser and the writer are supposed to
478 // preserve the order of all elements and comments
479 std::stringstream newFile;
480 ini.make(newFile);
481 std::string originalContents;
482 inputFile.clear();
483 inputFile.seekg(std::ios_base::beg);
484 // ignore warning about null pointer dereference from GCC 12 for now (which is *likely* not correct)
485#ifdef __GNUC__
486#pragma GCC diagnostic push
487#pragma GCC diagnostic ignored "-Wnull-dereference"
488#endif
489 originalContents.assign((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
490#ifdef __GNUC__
491#pragma GCC diagnostic pop
492#endif
493 CPPUNIT_ASSERT_EQUAL(originalContents, newFile.str());
494}
495
500{
501 // prepare streams
502 fstream testFile;
503 testFile.exceptions(ios_base::failbit | ios_base::badbit);
504 testFile.open(testFilePath("some_data"), ios_base::in | ios_base::binary);
505 stringstream outputStream(ios_base::in | ios_base::out | ios_base::binary);
506 outputStream.exceptions(ios_base::failbit | ios_base::badbit);
507
508 // copy
509 CopyHelper<13> copyHelper;
510 copyHelper.copy(testFile, outputStream, 50);
511
512 // test
513 testFile.seekg(0);
514 for (auto i = 0; i < 50; ++i) {
515 CPPUNIT_ASSERT(testFile.get() == outputStream.get());
516 }
517}
518
523{
524 // read a file successfully
525 const string iniFilePath(testFilePath("test.ini"));
526 CPPUNIT_ASSERT_EQUAL("# file for testing INI parser\n"
527 "key0=value 0\n"
528 "\n"
529 "[scope 1]\n"
530 "key1=value 1 # comment\n"
531 "key2=value=2\n"
532 "key3=value 3\n"
533 "\n"
534 "[scope 2]\n"
535 "key4=value 4\n"
536 "#key5=value 5\n"
537 "key6=value 6\n"s,
538 readFile(iniFilePath));
539
540 // fail by exceeding max size
541 CPPUNIT_ASSERT_THROW(readFile(iniFilePath, 10), std::ios_base::failure);
542
543 // handle UTF-8 in path and file contents correctly via NativeFileStream
544#if !defined(PLATFORM_WINDOWS) || defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
545 CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'ä' in its name\n"s, readFile(testFilePath("täst.txt")));
546#endif
547}
548
553{
554 const string path(workingCopyPath("test.ini", WorkingCopyMode::NoCopy));
555 writeFile(path, "some contents");
556 CPPUNIT_ASSERT_EQUAL("some contents"s, readFile(path));
557}
558
563{
564 stringstream ss1;
566 ss1 << EscapeCodes::Phrases::Error << "some error" << EscapeCodes::Phrases::End;
567 ss1 << EscapeCodes::Phrases::Warning << "some warning" << EscapeCodes::Phrases::End;
568 ss1 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
569 ss1 << EscapeCodes::Phrases::ErrorMessage << "Arch-style error" << EscapeCodes::Phrases::End;
570 ss1 << EscapeCodes::Phrases::WarningMessage << "Arch-style warning" << EscapeCodes::Phrases::End;
571 ss1 << EscapeCodes::Phrases::PlainMessage << "Arch-style message" << EscapeCodes::Phrases::End;
572 ss1 << EscapeCodes::Phrases::SuccessMessage << "Arch-style success" << EscapeCodes::Phrases::End;
573 ss1 << EscapeCodes::Phrases::SubMessage << "Arch-style sub-message" << EscapeCodes::Phrases::End;
574 ss1 << EscapeCodes::color(EscapeCodes::Color::Blue, EscapeCodes::Color::Red, EscapeCodes::TextAttribute::Blink)
575 << "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n';
576 cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n";
577 fstream("/tmp/test.txt", ios_base::out | ios_base::trunc) << ss1.str();
578 CPPUNIT_ASSERT_EQUAL("\e[1;31mError: \e[0m\e[1msome error\e[0m\n"
579 "\e[1;33mWarning: \e[0m\e[1msome warning\e[0m\n"
580 "\e[1;34mInfo: \e[0m\e[1msome info\e[0m\n"
581 "\e[1;31m==> ERROR: \e[0m\e[1mArch-style error\e[0m\n"
582 "\e[1;33m==> WARNING: \e[0m\e[1mArch-style warning\e[0m\n"
583 " \e[0m\e[1mArch-style message\e[0m\n"
584 "\e[1;32m==> \e[0m\e[1mArch-style success\e[0m\n"
585 "\e[1;32m -> \e[0m\e[1mArch-style sub-message\e[0m\n"
586 "\e[5;34;41mblue, blinking text on red background\e[0m\n"s,
587 ss1.str());
588
589 stringstream ss2;
590 EscapeCodes::enabled = false;
591 ss2 << EscapeCodes::Phrases::Info << "some info" << EscapeCodes::Phrases::End;
592 CPPUNIT_ASSERT_EQUAL("Info: some info\n"s, ss2.str());
593}
594
595#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
599void IoTests::testNativeFileStream()
600{
601 return;
602 // open file by path
603 const auto txtFilePath(workingCopyPath("täst.txt"));
604 NativeFileStream fileStream;
605 fileStream.exceptions(ios_base::badbit | ios_base::failbit);
606 CPPUNIT_ASSERT(!fileStream.is_open());
607 fileStream.open(txtFilePath, ios_base::in);
608 CPPUNIT_ASSERT(fileStream.is_open());
609#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
610 CPPUNIT_ASSERT(fileStream.fileHandle() != nullptr);
611#else
612 CPPUNIT_ASSERT(fileStream.fileDescriptor() != -1);
613#endif
614 CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
615 fileStream.seekg(0, ios_base::end);
616 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
617 fileStream.close();
618 CPPUNIT_ASSERT(!fileStream.is_open());
619 try {
620 fileStream.open("non existing file", ios_base::in | ios_base::out | ios_base::binary);
621 CPPUNIT_FAIL("expected exception");
622 } catch (const std::ios_base::failure &failure) {
623#ifdef PLATFORM_WINDOWS
624#ifdef CPP_UTILITIES_USE_BOOST_IOSTREAMS
625 TESTUTILS_ASSERT_LIKE("expected error with some message", "CreateFileW failed: .+", failure.what());
626#else
627 TESTUTILS_ASSERT_LIKE("expected error with some message", "_wopen failed: .+", failure.what());
628#endif
629#else
630 TESTUTILS_ASSERT_LIKE("expected error with some message", "open failed: .+", failure.what());
631#endif
632 }
633 fileStream.clear();
634
635 // open file from file descriptor
636#ifndef PLATFORM_WINDOWS
637 auto readWriteFileDescriptor = open(txtFilePath.data(), O_RDWR);
638 CPPUNIT_ASSERT(readWriteFileDescriptor);
639 fileStream.open(readWriteFileDescriptor, ios_base::in | ios_base::out | ios_base::binary);
640 CPPUNIT_ASSERT(fileStream.is_open());
641 CPPUNIT_ASSERT_EQUAL(static_cast<char>(fileStream.get()), 'f');
642 fileStream.seekg(0, ios_base::end);
643 CPPUNIT_ASSERT_EQUAL(fileStream.tellg(), static_cast<NativeFileStream::pos_type>(47));
644 fileStream.flush();
645 fileStream.close();
646 CPPUNIT_ASSERT(!fileStream.is_open());
647#endif
648 try {
649 fileStream.open(-1, ios_base::in | ios_base::out | ios_base::binary);
650 fileStream.get();
651 CPPUNIT_FAIL("expected exception");
652 } catch (const std::ios_base::failure &failure) {
653#ifndef PLATFORM_WINDOWS
655 "expected error message", "(basic_ios::clear|failed reading: Bad file descriptor): iostream error"s, string(failure.what()));
656#else
657 CPP_UTILITIES_UNUSED(failure)
658#endif
659 }
660 fileStream.clear();
661
662 // append + write file via path
663 NativeFileStream fileStream2;
664 fileStream2.exceptions(ios_base::failbit | ios_base::badbit);
665 fileStream2.open(txtFilePath, ios_base::in | ios_base::out | ios_base::app);
666 CPPUNIT_ASSERT(fileStream2.is_open());
667 fileStream2 << "foo";
668 fileStream2.flush();
669 fileStream2.close();
670 CPPUNIT_ASSERT(!fileStream2.is_open());
671 CPPUNIT_ASSERT_EQUAL("file with non-ASCII character 'ä' in its name\nfoo"s, readFile(txtFilePath, 50));
672
673 // truncate + write file via path
674 fileStream2.open(txtFilePath, ios_base::out | ios_base::trunc);
675 CPPUNIT_ASSERT(fileStream2.is_open());
676 fileStream2 << "bar";
677 fileStream2.close();
678 CPPUNIT_ASSERT(!fileStream2.is_open());
679 CPPUNIT_ASSERT_EQUAL("bar"s, readFile(txtFilePath, 4));
680
681 // append + write via file descriptor from file handle
682#ifdef PLATFORM_WINDOWS
683 const auto wideTxtFilePath = NativeFileStream::makeWidePath(txtFilePath);
684 const auto appendFileHandle = _wfopen(wideTxtFilePath.get(), L"a+");
685#else
686 const auto appendFileHandle = fopen(txtFilePath.data(), "a");
687#endif
688 CPPUNIT_ASSERT(appendFileHandle);
689 fileStream2.open(fileno(appendFileHandle), ios_base::out | ios_base::app);
690 CPPUNIT_ASSERT(fileStream2.is_open());
691 fileStream2 << "foo";
692 fileStream2.close();
693 CPPUNIT_ASSERT(!fileStream2.is_open());
694 CPPUNIT_ASSERT_EQUAL("barfoo"s, readFile(txtFilePath, 7));
695}
696#endif
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Definition: global.h:92
Reads primitive data types from a std::istream.
Definition: binaryreader.h:11
std::int64_t readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:505
float readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:379
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...
Definition: binaryreader.h:514
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
Definition: binaryreader.h:580
std::int64_t readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:457
std::int32_t readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:285
std::int32_t readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:415
std::uint32_t readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:448
std::uint16_t readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:406
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 ...
Definition: binaryreader.h:341
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
Definition: binaryreader.h:570
std::uint32_t readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:275
std::int32_t readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:439
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
Definition: binaryreader.h:148
std::int16_t readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:243
std::uint32_t readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:429
std::int64_t readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:481
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 ...
Definition: binaryreader.h:360
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
Definition: binaryreader.h:160
std::uint64_t readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:471
std::uint64_t readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:495
std::uint16_t readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:252
std::int32_t readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:261
std::int64_t readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:303
double readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:388
std::uint32_t readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:294
std::int64_t readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:327
std::int16_t readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:397
double readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:542
std::int64_t readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:351
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 ...
Definition: binaryreader.h:317
float readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:533
std::uint8_t readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Definition: binaryreader.h:560
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:14
void writeFloat32LE(float value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
Definition: binarywriter.h:517
void writeUInt40BE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:318
void writeUInt64LE(std::uint64_t value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:499
void writeInt40LE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:451
void writeUInt40LE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:461
void writeInt64BE(std::int64_t value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:347
void writeInt32LE(std::int32_t value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:432
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
Definition: binarywriter.h:157
void writeUInt24LE(std::uint32_t value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:422
void writeFloat32BE(float value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
Definition: binarywriter.h:374
void writeInt24BE(std::int32_t value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:269
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...
Definition: binarywriter.h:338
void writeUInt32BE(std::uint32_t value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:298
void writeFloat64BE(double value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
Definition: binarywriter.h:383
void writeFloat64LE(double value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
Definition: binarywriter.h:526
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...
Definition: binarywriter.h:535
void writeUInt56LE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:481
void writeInt56LE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:471
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
Definition: binarywriter.h:145
void writeUInt16BE(std::uint16_t value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:259
void writeUInt64BE(std::uint64_t value)
Writes a 64-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:356
void writeInt56BE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:328
void writeInt16LE(std::int16_t value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:392
void writeUInt32LE(std::uint32_t value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:441
void writeUInt16LE(std::uint16_t value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:401
void writeInt16BE(std::int16_t value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:250
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
Definition: binarywriter.h:555
void writeInt24LE(std::int32_t value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:411
void writeUInt24BE(std::uint32_t value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:279
void writeTerminatedString(const std::string &value)
Writes a terminated string to the current stream and advances the current position of the stream by t...
Definition: binarywriter.h:543
void writeInt64LE(std::int64_t value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:490
void writeInt32BE(std::int32_t value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:289
void writeInt40BE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:308
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
Definition: binarywriter.h:242
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:18
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 ...
Definition: buffersearch.h:15
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:16
void copy(std::istream &input, std::ostream &output, std::uint64_t count)
Copies count bytes from input to output.
Definition: copy.h:43
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:60
void testBufferSearch()
Tests the BufferSearch class.
Definition: iotests.cpp:308
void testBinaryReader()
Tests the most important methods of the BinaryReader.
Definition: iotests.cpp:111
void testAnsiEscapeCodes()
Tests formatting functions of CppUtilities::EscapeCodes namespace.
Definition: iotests.cpp:562
void testIniFile()
Tests IniFile.
Definition: iotests.cpp:371
void testBitReader()
Tests the BitReader class.
Definition: iotests.cpp:281
void testBinaryWriter()
Tests the most important methods of the BinaryWriter.
Definition: iotests.cpp:177
void setUp() override
Definition: iotests.cpp:100
void testCopy()
Tests CopyHelper.
Definition: iotests.cpp:499
void testPathUtilities()
Tests fileName() and removeInvalidChars().
Definition: iotests.cpp:342
void tearDown() override
Definition: iotests.cpp:104
void testWriteFile()
Tests writeFile().
Definition: iotests.cpp:552
void testReadFile()
Tests readFile().
Definition: iotests.cpp:522
void testAdvancedIniFile()
Tests AdvancedIniFile.
Definition: iotests.cpp:412
CPPUNIT_TEST_SUITE_REGISTRATION(IoTests)
std::ostream & operator<<(std::ostream &out, const std::wstring &s)
Allows printing std::wstring using CPPUNIT_ASSERT_EQUAL.
Definition: iotests.cpp:8
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:332
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:150
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
Definition: testutils.h:168
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.
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:247
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
std::fstream NativeFileStream
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:292
#define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString)
Asserts whether the specified string matches the specified regex.
Definition: testutils.h:302
constexpr int i