📄 filestream.h
字号:
#ifndef _filestraam_h_
#define _filestraam_h_
#include "BrewString.h"
#include "AEEStdlib.h"
#include "AEEAppGen.h"
#include "AEEFile.h"
#include "CbkDispatcher.h"
//A few utility macros; We shall eventually move this into the AEE Services.
#define ISDIGIT(c) ( (unsigned) ((c) - '0') < 10)
#define ISALPHA(c) ( (unsigned) ( ((c)|32) - 'a') < 26 )
#define ISALNUM(c) ( ISDIGIT(c) || ISALPHA(c) )
struct FDS: public IRelease
{
static void createFileDataStruct(FDS*& ds)
{
if (ds==0)
ds = new FDS();
}
virtual void releaseResource( )
{
delete this;
}
virtual int getUID( )
{
return 500;
}
static int getID()
{
return 500;
}
template <class R>
static FDS* initDataStruct(R* rr, char* address, const String& params )
{
FDS* ds=static_cast<FDS*>(rr->getRegistered(FDS::getID()));
FDS::createFileDataStruct(ds);
ds->address = address;
ds->parameters = params;
if (!ds->data.isEmpty())
ds->data="";
rr->registerResource(ds);
return ds;
}
char* address;
String parameters;
String data;
int error;
UINT bufSize;
private:
FDS() : bufSize(2)
{}
FDS( const FDS &Value );
const FDS &operator = ( const FDS &Rhs );
};
template <class Y, class F>
struct pkCBK;
class FileStream
{
public:
FileStream(IShell* shell, IExecute* pp, FDS* ds) :
shell_(shell),
processPolicy_(pp),
f_(0),
fMgr_(0),
buf_(new char[ds->bufSize+1]),
ds_(ds)
{
}
~FileStream();
int init();
int initConnection();
void reset();
private:
void write();
void read();
void onError( int errorCode) ;
void onData( ) ;
void releaseResources();
void releaseResource();
private:
IShell* shell_;
IFileMgr *fMgr_;
IFile *f_;
IExecute* processPolicy_;
char* buf_;
typedef pkCBK0<FileStream, void (FileStream::*)()> P;
P p_;
typedef pkAEECBK<FileStream> A;
A a_;
FDS* ds_;
private:
FileStream( const FileStream &Value );
const FileStream &operator = ( const FileStream &Rhs );
};
FileStream::~FileStream()
{
releaseResources();
}
void FileStream::releaseResource()
{
if (f_)
{
IFILE_Cancel(f_, 0, 0);
IFILE_Release(f_);
f_ = 0;
}
}
void FileStream::reset()
{
releaseResource();
}
void FileStream::releaseResources()
{
releaseResource();
if (fMgr_)
{
IFILEMGR_Release(fMgr_);
fMgr_ = 0;
}
delete[] buf_;
buf_ = 0;
}
int FileStream::init()
{
int err;
if ((err = ISHELL_CreateInstance(shell_, AEECLSID_FILEMGR, (void**)(&fMgr_))) != SUCCESS)
{
return err;
}
if (!fMgr_)
{
return EFAILED;
}
return SUCCESS;
}
int FileStream::initConnection()
{
if (f_)
{
return SUCCESS; //safe approach if a connection is tried
//before ending the previous one - isReusable==true
}
/*AEE*/OpenFileMode mode;
if (ds_->parameters.isEmpty())
{
mode = _OFM_READ;
}
else
{
mode = IFILEMGR_Test(fMgr_, ds_->address) != SUCCESS? _OFM_CREATE : _OFM_READWRITE;
}
f_ = IFILEMGR_OpenFile(fMgr_, ds_->address, mode);
if (!f_)
{
return IFILEMGR_GetLastError(fMgr_);
}
if (mode == _OFM_READ)
{
p_.setCallback(this, &FileStream::read);
a_.setCallback(this, &FileStream::read, shell_);
}
else
{
a_.setCallback(this, &FileStream::write, shell_);
}
a_();
return SUCCESS;
}
void FileStream::write ()
{
const char *psz = ds_->parameters.toCharArray();
int rv = IFILE_Write(f_,
(byte *)psz, (uint16)ds_->bufSize);
if (rv == 0)
{
onError(IFILEMGR_GetLastError(fMgr_)) ;
return;
}
int len = ds_->parameters.length();
if (rv < len)
{
ds_->parameters.partialString(rv, len);
a_();
return;
}
onData();
return;
}
void FileStream::read ()
{
if (!f_)
{
DBGPRINTF("file == 0 in read");
onError(EFAILED);
return;
}
int32 rv = IFILE_Read(f_, buf_, ds_->bufSize);
if (rv > 0)
{
buf_[rv] = 0;
ds_->data += buf_;
IFILE_Readable(f_, (PFNNOTIFY)P::callbackHandler, &p_);
return;
}
else if (rv == 0)
{
onData();
return;
}
}
void FileStream::onError( int errorCode) {
ds_->error = errorCode;
ds_->data = "";
processPolicy_->onCbk() ;
}
void FileStream::onData( ) {
ds_->error = SUCCESS;
processPolicy_->onCbk() ;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -