diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index 93f92f0795..b33650b124 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -71,6 +71,38 @@ namespace File auto fs = FileStream(path, FILE_MODE_WRITE); fs.Write(buffer, length); } + + std::vector ReadAllLines(const std::string &path) + { + std::vector lines; + size_t length; + char * data = (char *)ReadAllBytes(path, &length); + char * lineStart = data; + char * ch = data; + char lastC = 0; + for (size_t i = 0; i < length; i++) + { + char c = *ch; + if (c == '\n' && lastC == '\r') + { + // Ignore \r\n + lineStart = ch + 1; + } + else if (c == '\n' || c == '\r') + { + lines.emplace_back(lineStart, ch - lineStart); + lineStart = ch + 1; + } + lastC = c; + ch++; + } + + // Last line + lines.emplace_back(lineStart, ch - lineStart); + + Memory::Free(data); + return lines; + } } extern "C" diff --git a/src/openrct2/core/File.h b/src/openrct2/core/File.h index 3029838021..d15481b3b3 100644 --- a/src/openrct2/core/File.h +++ b/src/openrct2/core/File.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include "../common.h" namespace File @@ -27,4 +28,5 @@ namespace File bool Move(const std::string &srcPath, const std::string &dstPath); void * ReadAllBytes(const std::string &path, size_t * length); void WriteAllBytes(const std::string &path, const void * buffer, size_t length); + std::vector ReadAllLines(const std::string &path); }