Core: LoadFile fixed to work with /proc pseudofiles

This commit is contained in:
Mirek Fidler 2022-05-18 09:52:23 +02:00
parent a5563f9322
commit 34cdcee068
4 changed files with 41 additions and 1 deletions

View file

@ -0,0 +1,11 @@
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
String s = LoadFile("/proc/meminfo");
Cout() << s;
ASSERT(s.GetCount() > 0);
ASSERT(LoadFile("/proc/this_file_does_not_exist").IsVoid());
}

View file

@ -0,0 +1,9 @@
uses
Core;
file
LoadProcFile.cpp;
mainconfig
"" = "";

View file

@ -1216,6 +1216,26 @@ String LoadStream(Stream& in) {
String LoadFile(const char *filename) {
FindFile ff(filename);
if(ff && ff.IsFile()) {
#ifdef PLATFORM_POSIX
if(ff.GetLength() == 0) { // handle speciale cases like /proc/...
int fd = open(filename,O_RDONLY);
if(fd >= 0) {
const int CHUNK = 16;
StringBuffer s;
for(;;) {
int n = s.GetCount();
s.SetCount(n + CHUNK);
int len = read(fd, ~s + n, CHUNK);
if(len != CHUNK) {
if(len > 0)
s.SetCount(n + len);
return s;
}
}
}
return String::GetVoid();
}
#endif
FileIn in(filename);
return LoadStream(in);
}

View file

@ -451,7 +451,7 @@ void IconDes::SaveUndo()
return;
Slot& c = Current();
Vector<ImageIml> undo = UnpackImlData(c.undo);
int maxn = minmax((single_mode ? 4000000 : 400000) / max((int)c.image.GetLength(), 1), 4, 128);
int maxn = minmax((single_mode ? 4000000 : 400000) / max((int)tc.image.GetLength(), 1), 4, 128);
while(undo.GetCount() > maxn)
undo.Remove(0);
if(undo.GetCount() && undo.Top().image == c.image)