diff options
| author | Bent Bisballe Nyeng <deva@aasimon.org> | 2025-03-31 20:47:53 +0200 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2025-03-31 20:47:53 +0200 | 
| commit | 494401c0e0d29018f0ac53e91a3481d12e427ea6 (patch) | |
| tree | c1118fe814ce6c91b55dd72fdef5fbaa6e751c0e /src | |
| parent | f1b763881a5a36d63d9826fc4201e38bfc604629 (diff) | |
Improved file read utility function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/util.cc | 12 | 
1 files changed, 8 insertions, 4 deletions
| diff --git a/src/util.cc b/src/util.cc index 9052118..3517e0b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -24,13 +24,17 @@ std::string readFile(const std::string& fileName)  	std::ifstream ifs(fileName.c_str(),  	                  std::ios::in | std::ios::binary | std::ios::ate); -	std::ifstream::pos_type fileSize = ifs.tellg(); +	auto size = ifs.tellg(); +	if(size < 0) +	{ +		return {}; +	}  	ifs.seekg(0, std::ios::beg); -	std::vector<char> bytes(static_cast<std::size_t>(fileSize)); -	ifs.read(bytes.data(), fileSize); +	std::string bytes(static_cast<std::size_t>(size), '\0'); +	ifs.read(bytes.data(), static_cast<std::streamsize>(bytes.size())); -	return {bytes.data(), static_cast<std::size_t>(fileSize)}; +	return bytes;  }  ctor::language languageFromExtension(const std::filesystem::path& file) | 
