* fixed typo in fread func, thx bLuma!
* added new funcs: freadline, fseekr, fseekw
This commit is contained in:
parent
134cd66130
commit
40cef66995
@ -176,6 +176,9 @@ void DefScriptPackage::_InitFunctions(void)
|
||||
AddFunc("fwritebb",&DefScriptPackage::func_fwritebb);
|
||||
AddFunc("fsize",&DefScriptPackage::func_fsize);
|
||||
AddFunc("fexists",&DefScriptPackage::func_fileexists); // name alternative for "fileexists"
|
||||
AddFunc("freadline",&DefScriptPackage::func_freadline);
|
||||
AddFunc("fseekw",&DefScriptPackage::func_fseekw);
|
||||
AddFunc("fseekr",&DefScriptPackage::func_fseekr);
|
||||
}
|
||||
|
||||
void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set), bool esc)
|
||||
|
||||
@ -270,6 +270,9 @@ private:
|
||||
DefReturnResult DefScriptPackage::func_freadbb(CmdSet&);
|
||||
DefReturnResult DefScriptPackage::func_fwritebb(CmdSet&);
|
||||
DefReturnResult DefScriptPackage::func_fsize(CmdSet&);
|
||||
DefReturnResult DefScriptPackage::func_freadline(CmdSet&);
|
||||
DefReturnResult DefScriptPackage::func_fseekw(CmdSet&);
|
||||
DefReturnResult DefScriptPackage::func_fseekr(CmdSet&);
|
||||
|
||||
// setup own function declarations here
|
||||
# include "DefScriptInterfaceInclude.h"
|
||||
|
||||
@ -113,7 +113,7 @@ DefReturnResult DefScriptPackage::func_fwrite(CmdSet& Set)
|
||||
|
||||
DefReturnResult DefScriptPackage::func_fread(CmdSet& Set)
|
||||
{
|
||||
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[1],Set.myname));
|
||||
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
if(fh && fh->is_open())
|
||||
{
|
||||
uint64 bytes;
|
||||
@ -288,3 +288,65 @@ DefReturnResult DefScriptPackage::func_fsize(CmdSet& Set)
|
||||
f.close();
|
||||
return toString((uint64)(end_pos - begin_pos));
|
||||
}
|
||||
|
||||
DefReturnResult DefScriptPackage::func_freadline(CmdSet& Set)
|
||||
{
|
||||
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||
if(fh && fh->is_open())
|
||||
{
|
||||
char c;
|
||||
std::string line;
|
||||
line.reserve(30); // rough guess to speedup appending
|
||||
while(!fh->eof())
|
||||
{
|
||||
c = fh->get();
|
||||
if(c == '\n' || fh->eof())
|
||||
return line;
|
||||
else
|
||||
line += c;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
DefReturnResult DefScriptPackage::func_fseekw(CmdSet& Set)
|
||||
{
|
||||
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
if(fh && fh->is_open())
|
||||
{
|
||||
std::string w = stringToLower(Set.arg[1]);
|
||||
uint32 pos = (uint32)toUint64(Set.defaultarg);
|
||||
if(w.empty() || w == "begin")
|
||||
{
|
||||
fh->seekp(pos, std::ios_base::beg);
|
||||
return true;
|
||||
}
|
||||
else if(w == "end")
|
||||
{
|
||||
fh->seekp(pos, std::ios_base::end);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
DefReturnResult DefScriptPackage::func_fseekr(CmdSet& Set)
|
||||
{
|
||||
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||
if(fh && fh->is_open())
|
||||
{
|
||||
std::string w = stringToLower(Set.arg[1]);
|
||||
uint32 pos = (uint32)toUint64(Set.defaultarg);
|
||||
if(w.empty() || w == "begin")
|
||||
{
|
||||
fh->seekg(pos, std::ios_base::beg);
|
||||
return true;
|
||||
}
|
||||
else if(w == "end")
|
||||
{
|
||||
fh->seekg(pos, std::ios_base::end);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user