* added file handling functions to DefScript
** if you are using vc80, dont forget to add DefScriptFileFunctions.cpp to the project file until updated here!
This commit is contained in:
parent
02df5d5261
commit
134cd66130
@ -159,6 +159,23 @@ void DefScriptPackage::_InitFunctions(void)
|
|||||||
AddFunc("bbsetwpos",&DefScriptPackage::func_bbsetwpos);
|
AddFunc("bbsetwpos",&DefScriptPackage::func_bbsetwpos);
|
||||||
AddFunc("bbsize",&DefScriptPackage::func_bbsize);
|
AddFunc("bbsize",&DefScriptPackage::func_bbsize);
|
||||||
|
|
||||||
|
// file functions
|
||||||
|
AddFunc("fopen",&DefScriptPackage::func_fopen);
|
||||||
|
AddFunc("fclose",&DefScriptPackage::func_fclose);
|
||||||
|
AddFunc("fisopen",&DefScriptPackage::func_fisopen);
|
||||||
|
AddFunc("feof",&DefScriptPackage::func_feof);
|
||||||
|
AddFunc("frpos",&DefScriptPackage::func_frpos);
|
||||||
|
AddFunc("fwpos",&DefScriptPackage::func_fwpos);
|
||||||
|
AddFunc("fdel",&DefScriptPackage::func_fdel);
|
||||||
|
AddFunc("fflush",&DefScriptPackage::func_fflush);
|
||||||
|
AddFunc("fwrite",&DefScriptPackage::func_fwrite);
|
||||||
|
AddFunc("fread",&DefScriptPackage::func_fread);
|
||||||
|
AddFunc("freadb",&DefScriptPackage::func_freadb);
|
||||||
|
AddFunc("fwriteb",&DefScriptPackage::func_fwriteb);
|
||||||
|
AddFunc("freadbb",&DefScriptPackage::func_freadbb);
|
||||||
|
AddFunc("fwritebb",&DefScriptPackage::func_fwritebb);
|
||||||
|
AddFunc("fsize",&DefScriptPackage::func_fsize);
|
||||||
|
AddFunc("fexists",&DefScriptPackage::func_fileexists); // name alternative for "fileexists"
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set), bool esc)
|
void DefScriptPackage::AddFunc(std::string n,DefReturnResult (DefScriptPackage::*f)(CmdSet& Set), bool esc)
|
||||||
|
|||||||
@ -137,6 +137,7 @@ public:
|
|||||||
void DelFunc(std::string);
|
void DelFunc(std::string);
|
||||||
TypeStorage<DefList> lists;
|
TypeStorage<DefList> lists;
|
||||||
TypeStorage<ByteBuffer> bytebuffers;
|
TypeStorage<ByteBuffer> bytebuffers;
|
||||||
|
TypeStorage<std::fstream> files;
|
||||||
std::string SecureString(std::string);
|
std::string SecureString(std::string);
|
||||||
std::string EscapeString(std::string);
|
std::string EscapeString(std::string);
|
||||||
std::string UnescapeString(std::string);
|
std::string UnescapeString(std::string);
|
||||||
@ -253,6 +254,23 @@ private:
|
|||||||
DefReturnResult func_bbtextlike(CmdSet&);
|
DefReturnResult func_bbtextlike(CmdSet&);
|
||||||
DefReturnResult func_bbsize(CmdSet&);
|
DefReturnResult func_bbsize(CmdSet&);
|
||||||
|
|
||||||
|
// file functions
|
||||||
|
DefReturnResult DefScriptPackage::func_fopen(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fclose(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fisopen(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_feof(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_frpos(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fwpos(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fdel(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fflush(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fwrite(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fread(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_freadb(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fwriteb(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_freadbb(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fwritebb(CmdSet&);
|
||||||
|
DefReturnResult DefScriptPackage::func_fsize(CmdSet&);
|
||||||
|
|
||||||
// setup own function declarations here
|
// setup own function declarations here
|
||||||
# include "DefScriptInterfaceInclude.h"
|
# include "DefScriptInterfaceInclude.h"
|
||||||
|
|
||||||
|
|||||||
@ -38,7 +38,7 @@ DefReturnResult DefScriptPackage::func_bbdelete(CmdSet& Set)
|
|||||||
// Appends data to ByteBuffer
|
// Appends data to ByteBuffer
|
||||||
// @def - data
|
// @def - data
|
||||||
// @0 - bytebuffer identifier
|
// @0 - bytebuffer identifier
|
||||||
// @1 - datatype of added data (uint8,uint16,uint32,uint64,float,double,string)
|
// @1 - datatype of added data (uint8,uint16,uint32,uint64,float,double,string,strnz)
|
||||||
DefReturnResult DefScriptPackage::func_bbappend(CmdSet& Set)
|
DefReturnResult DefScriptPackage::func_bbappend(CmdSet& Set)
|
||||||
{
|
{
|
||||||
ByteBuffer *bb = bytebuffers.Get(_NormalizeVarName(Set.arg[0],Set.myname));
|
ByteBuffer *bb = bytebuffers.Get(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
@ -173,4 +173,3 @@ DefReturnResult DefScriptPackage::func_bbsize(CmdSet& Set)
|
|||||||
|
|
||||||
return DefScriptTools::toString((uint64)bb->size());
|
return DefScriptTools::toString((uint64)bb->size());
|
||||||
}
|
}
|
||||||
|
|
||||||
290
src/Client/DefScript/DefScriptFileFunctions.cpp
Normal file
290
src/Client/DefScript/DefScriptFileFunctions.cpp
Normal file
@ -0,0 +1,290 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
#include "DefScript.h"
|
||||||
|
#include "DefScriptTools.h"
|
||||||
|
#include "SysDefs.h"
|
||||||
|
|
||||||
|
using namespace DefScriptTools;
|
||||||
|
|
||||||
|
#define FH_MACRO_READ_I(_f,_sty,_ty) if(_sty==(#_ty)) { _ty _in; (_f)->read((char*)&_in,sizeof(_ty)); return toString((uint64)_in); }
|
||||||
|
#define FH_MACRO_READ_F(_f,_sty,_ty) if(_sty==(#_ty)) { _ty _in; (_f)->read((char*)&_in,sizeof(_ty)); return toString((ldbl)_in); }
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fopen(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.Get(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
|
if(fh->is_open())
|
||||||
|
return "";
|
||||||
|
std::string mode = stringToLower(Set.arg[1]);
|
||||||
|
std::ios::openmode m = 0;
|
||||||
|
if(mode.find('b') != std::string::npos)
|
||||||
|
m |= std::ios_base::binary;
|
||||||
|
if(mode.find('a') != std::string::npos)
|
||||||
|
m |= std::ios_base::app;
|
||||||
|
if(mode.find('r') != std::string::npos)
|
||||||
|
m |= std::ios_base::in;
|
||||||
|
if(mode.find('w') != std::string::npos)
|
||||||
|
m |= std::ios_base::out;
|
||||||
|
|
||||||
|
// sort out some possible errors with bad open modes
|
||||||
|
if(m & std::ios_base::app)
|
||||||
|
m |= std::ios_base::out; // always open for writing when appending
|
||||||
|
if(m == 0 || m == std::ios_base::binary)
|
||||||
|
m |= std::ios_base::out | std::ios_base::in; // if no openmode or binary only specified, use both r+w additionally
|
||||||
|
|
||||||
|
fh->open(Set.defaultarg.c_str(), m);
|
||||||
|
|
||||||
|
// if the file didnt open properly, like if a not existing file opened in read+write mode,
|
||||||
|
// try fallback mode with write mode only
|
||||||
|
if(!fh->is_open() && (m & std::ios_base::in))
|
||||||
|
{
|
||||||
|
m &= (~std::ios_base::in);
|
||||||
|
fh->open(Set.defaultarg.c_str(), m);
|
||||||
|
}
|
||||||
|
_DEFSC_DEBUG( if(!fh->is_open()) printf("DefScript: Can't open file '%s'\n",Set.defaultarg.c_str()));
|
||||||
|
return fh->is_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fclose(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
if(fh)
|
||||||
|
{
|
||||||
|
fh->flush();
|
||||||
|
fh->close();
|
||||||
|
files.DeleteByPtr(fh);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fisopen(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
return fh && fh->good() && fh->is_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_feof(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
return (!fh) || fh->eof();
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_frpos(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
if(!fh)
|
||||||
|
return "";
|
||||||
|
return toString((uint64)fh->tellg());
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fwpos(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
if(!fh)
|
||||||
|
return "";
|
||||||
|
return toString((uint64)fh->tellp());
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fdel(CmdSet& Set)
|
||||||
|
{
|
||||||
|
return (remove(Set.defaultarg.c_str()) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fflush(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
fh->flush();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fwrite(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
{
|
||||||
|
*fh << Set.defaultarg;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fread(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[1],Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
{
|
||||||
|
uint64 bytes;
|
||||||
|
uint64 read = 0;
|
||||||
|
if(stringToLower(Set.defaultarg) == "all")
|
||||||
|
bytes = -1LL;
|
||||||
|
else
|
||||||
|
bytes = toUint64(Set.defaultarg);
|
||||||
|
std::string ret;
|
||||||
|
ret.reserve(bytes);
|
||||||
|
for(uint64 i = 0; i < bytes && !fh->eof(); i++)
|
||||||
|
{
|
||||||
|
ret += fh->get();
|
||||||
|
read++;
|
||||||
|
}
|
||||||
|
return toString(read);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_freadb(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
{
|
||||||
|
std::string t = stringToLower(Set.defaultarg);
|
||||||
|
if(t == "string")
|
||||||
|
{
|
||||||
|
std::string ret;
|
||||||
|
while(char c = fh->get() && !fh->eof())
|
||||||
|
ret += c;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
else if(t == "strnz")
|
||||||
|
{
|
||||||
|
uint32 bytes = (uint32)toUint64(Set.arg[1]);
|
||||||
|
if(bytes)
|
||||||
|
{
|
||||||
|
std::string g;
|
||||||
|
for(uint32 i = 0; i < bytes; i++)
|
||||||
|
{
|
||||||
|
g += fh->get();
|
||||||
|
}
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
FH_MACRO_READ_I(fh,t,uint8);
|
||||||
|
FH_MACRO_READ_I(fh,t,uint16);
|
||||||
|
FH_MACRO_READ_I(fh,t,uint32);
|
||||||
|
FH_MACRO_READ_I(fh,t,uint64);
|
||||||
|
FH_MACRO_READ_F(fh,t,float);
|
||||||
|
FH_MACRO_READ_F(fh,t,double);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fwriteb(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
{
|
||||||
|
std::string t = stringToLower(Set.arg[1]);
|
||||||
|
if(t == "string")
|
||||||
|
{
|
||||||
|
fh->write(Set.defaultarg.c_str(), Set.defaultarg.size() + 1); // also put \0
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "strnz")
|
||||||
|
{
|
||||||
|
fh->write(Set.defaultarg.c_str(), Set.defaultarg.size()); // skip \0
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "uint8")
|
||||||
|
{
|
||||||
|
uint8 o = (uint8)toUint64(Set.defaultarg);
|
||||||
|
fh->write((char*)&o,sizeof(uint8));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "uint16")
|
||||||
|
{
|
||||||
|
uint16 o = (uint16)toUint64(Set.defaultarg);
|
||||||
|
fh->write((char*)&o,sizeof(uint16));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "uint32")
|
||||||
|
{
|
||||||
|
uint32 o = (uint32)toUint64(Set.defaultarg);
|
||||||
|
fh->write((char*)&o,sizeof(uint32));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "uint64")
|
||||||
|
{
|
||||||
|
uint64 o = toUint64(Set.defaultarg);
|
||||||
|
fh->write((char*)&o,sizeof(uint64));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "float")
|
||||||
|
{
|
||||||
|
float o = (float)toNumber(Set.defaultarg);
|
||||||
|
fh->write((char*)&o,sizeof(float));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if(t == "double")
|
||||||
|
{
|
||||||
|
double o = (double)toNumber(Set.defaultarg);
|
||||||
|
fh->write((char*)&o,sizeof(double));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fwritebb(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
{
|
||||||
|
ByteBuffer *bb = bytebuffers.GetNoCreate(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
if(bb)
|
||||||
|
{
|
||||||
|
uint32 bytes = (uint32)toUint64(Set.arg[1]);
|
||||||
|
if(!bytes)
|
||||||
|
{
|
||||||
|
bytes = bb->size();
|
||||||
|
}
|
||||||
|
if(bytes)
|
||||||
|
{
|
||||||
|
fh->write((char*)bb->contents(), bytes);
|
||||||
|
}
|
||||||
|
return toString((uint64)bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_freadbb(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname));
|
||||||
|
if(fh && fh->is_open())
|
||||||
|
{
|
||||||
|
ByteBuffer *bb = bytebuffers.Get(_NormalizeVarName(Set.defaultarg,Set.myname));
|
||||||
|
uint32 bytes = (uint32)toUint64(Set.arg[1]);
|
||||||
|
if(!bytes)
|
||||||
|
{
|
||||||
|
// get amount of remaining bytes
|
||||||
|
std::ios::pos_type oldpos = fh->tellg();
|
||||||
|
fh->seekg(0,std::ios_base::end);
|
||||||
|
bytes = uint32(fh->tellg() - oldpos);
|
||||||
|
fh->seekg(oldpos,std::ios_base::beg);
|
||||||
|
}
|
||||||
|
if(bytes)
|
||||||
|
{
|
||||||
|
bb->resize(bb->size() + bytes);
|
||||||
|
fh->read((char*)bb->contents(), bytes);
|
||||||
|
}
|
||||||
|
return toString((uint64)bytes);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
DefReturnResult DefScriptPackage::func_fsize(CmdSet& Set)
|
||||||
|
{
|
||||||
|
std::ifstream f;
|
||||||
|
f.open(Set.defaultarg.c_str(), std::ios_base::binary | std::ios_base::in);
|
||||||
|
if (!f.good() || f.eof() || !f.is_open()) { return ""; }
|
||||||
|
f.seekg(0, std::ios_base::beg);
|
||||||
|
std::ifstream::pos_type begin_pos = f.tellg(), end_pos;
|
||||||
|
f.seekg(0, std::ios_base::end);
|
||||||
|
end_pos = f.tellg();
|
||||||
|
f.close();
|
||||||
|
return toString((uint64)(end_pos - begin_pos));
|
||||||
|
}
|
||||||
@ -43,6 +43,7 @@ std::string DefScriptTools::toString(uint64 num)
|
|||||||
ss << num;
|
ss << num;
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert a string into ldbl
|
// convert a string into ldbl
|
||||||
// valid input formats:
|
// valid input formats:
|
||||||
// normal numbers: 5439
|
// normal numbers: 5439
|
||||||
|
|||||||
@ -10,6 +10,7 @@ public:
|
|||||||
~TypeStorage();
|
~TypeStorage();
|
||||||
bool Exists(std::string);
|
bool Exists(std::string);
|
||||||
void Delete(std::string);
|
void Delete(std::string);
|
||||||
|
void DeleteByPtr(T*);
|
||||||
T *Get(std::string);
|
T *Get(std::string);
|
||||||
T *GetNoCreate(std::string);
|
T *GetNoCreate(std::string);
|
||||||
void Assign(std::string,T*);
|
void Assign(std::string,T*);
|
||||||
@ -55,6 +56,20 @@ template<class T> void TypeStorage<T>::Delete(std::string s)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delete object with that ptr, if present
|
||||||
|
template<class T> void TypeStorage<T>::DeleteByPtr(T *ptr)
|
||||||
|
{
|
||||||
|
for(std::map<std::string,T*>::iterator it = _storage.begin(); it != _storage.end(); it++)
|
||||||
|
{
|
||||||
|
if(it->second == ptr)
|
||||||
|
{
|
||||||
|
delete it->second;
|
||||||
|
_storage.erase(it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return the the object with that name. return NULL if not found
|
// return the the object with that name. return NULL if not found
|
||||||
template <class T> T *TypeStorage<T>::GetNoCreate(std::string s)
|
template <class T> T *TypeStorage<T>::GetNoCreate(std::string s)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -224,6 +224,9 @@
|
|||||||
<File
|
<File
|
||||||
RelativePath=".\Client\DefScript\DefScriptDefines.h">
|
RelativePath=".\Client\DefScript\DefScriptDefines.h">
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\Client\DefScript\DefScriptFileFunctions.cpp">
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\Client\DefScript\DefScriptFunctions.cpp">
|
RelativePath=".\Client\DefScript\DefScriptFunctions.cpp">
|
||||||
</File>
|
</File>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user