1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-24 00:03:11 +01:00

Add File::ReadAllLines helper

This commit is contained in:
Ted John
2017-05-17 19:11:42 +01:00
parent 7ca1173bcb
commit f5a23d77bd
2 changed files with 34 additions and 0 deletions

View File

@@ -71,6 +71,38 @@ namespace File
auto fs = FileStream(path, FILE_MODE_WRITE);
fs.Write(buffer, length);
}
std::vector<std::string> ReadAllLines(const std::string &path)
{
std::vector<std::string> 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"

View File

@@ -17,6 +17,7 @@
#pragma once
#include <string>
#include <vector>
#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<std::string> ReadAllLines(const std::string &path);
}