/*
* FML_Adapter.hpp
* 2011-11-30
* [email protected]
*
* TUXEDO FML32 操作方法封装
*/
#ifndef __FML_ADAPTER_H__
#define __FML_ADAPTER_H__
#include <iostream>
#include <string>
#include <sstream>
#include <atmi.h>
#include <stdarg.h>
#include "fml32.h"
#include "fml1632.h"
//////////////////////////////////////////////////////////////////////////
template<class FromType, class ToType>
ToType convert(FromType f)
{
ToType d;
std::stringstream ss;
ss << f;
ss >> d;
return d;
};
class FML_Exception
{
public:
FML_Exception(const std::string& message, long code=-1)
{ __message = message;
__code = code;
}
FML_Exception(const char* format, ...)
{ __code = -1;
char buf[1024];
::memset(buf, 0, sizeof(buf));
va_list ap;
va_start(ap, format);
::vsprintf(buf, format, ap);
va_end(ap);
__message = buf;
}
friend inline std::ostream& operator << (std::ostream& o, const FML_Exception& e)
{ o << "FML_Adapter异常: "
<< " 信息 [" << e.__message << "]"
<< " 代码 [" << e.__code << "]" << std::endl;
return o;
}
const std::string str()
{
std::stringstream ss;
ss << "FML_Adapter异常: "
<< " 信息 [" << __message << "]"
<< " 代码 [" << __code << "]" << std::endl;
return ss.str();
}
public:
std::string __message;
long __code;
};
class FML_Adapter
{
public:
FML_Adapter(FBFR* buffer)
{ __ptr = buffer; }
virtual ~FML_Adapter() {}
public:
std::string operator()(const std::string& fieldName)
{
co.setFieldName(fieldName);
return get();
}
// 运算符重载
FML_Adapter& operator[](const std::string& fieldName)
{ ci.setFieldName(fieldName);
return *this;
}
// 置值
// 注意该处与拷贝构造函数冲突,请勿使用拷贝构造函数
template<class ValueType>
FML_Adapter& operator=(const ValueType& fieldValue)
{ if (ci.fn.empty())
return *this;
add(fieldValue);
return *this;
}
// 删除指定的FML字段(全部)
int remove(const std::string& fieldName)
{ FLDID fieldID = Fldid((char*)fieldName.c_str());
return Fdelall32(__ptr, fieldID);
}
// 判断某字段在FML出现的次数
long has(const std::string& fieldName)
{ FLDID fieldID = Fldid((char*)fieldName.c_str());
return Foccur(__ptr, fieldID);
}
// 取出FML内存中的数据
const std::string str()
{
std::stringstream ss; ss.str("");
FLDID FieldID = FIRSTFLDID;
FLDOCC occ = 0;
char head[1024*10]; ::memset(head, 0, sizeof(head));
const char* formated_1 = " %-21s %8s %3s %-s";
const char* formated_2 = " %-21s %8s %3d %-s";
::sprintf(head, formated_1, "Name", "Type", "OCC", "Value");
ss << " _____________________ ________ ___ _________________________________";
ss << std::endl << head << std::endl;
ss << " ===================== ======== === =================================";
while (Fnext(__ptr, &FieldID, &occ, NULL, NULL) > 0)
{
std::string s = getA(FieldID, occ);
char record[128]; ::memset(record, 0, sizeof(record));
char* name = Fname(FieldID);
int type = Fldtype(FieldID);
std::string tn = Ftype(type);
::sprintf(record, formated_2, name, tn.c_str(), occ, s.c_str());
ss << "\n" << record;
}
return ss.str();
}
// 设置缺省是否抛出异常
void throwException(bool flag=false)
{ __throwException = flag; }
private:
std::string getA(FLDID fieldID, long pos)
{
std::stringstream ss; ss.str("");
int fieldType = Fldtype(fieldID);
int rc =-1;
// 取值
switch (fieldType)
{
case FLD_SHORT:
{ short value;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_LONG:
{ long value;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_CHAR:
{ char value;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_FLOAT:
{ float value = 0;;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_DOUBLE:
{ double value = 0;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_STRING:
{ char value[1024*10];
::memset(value, 0, sizeof(value));
rc = Fget(__ptr, fieldID, pos, (char*)value, 0);
ss << value;
break;
}
default:
{ throw FML_Exception("暂未支持的数据类型");
}
break;
}
return ss.str();
}
protected:
// 取值
std::string get()
{ FLDID fieldID = Fldid((char*)co.fn.c_str());
int fieldType = Fldtype(fieldID);
std::stringstream ss;
long pos = co.oc;
// 判断是否为未定义字段
if (BADFLDID == fieldID)
{ if (__throwException)
{ throw FML_Exception(std::string("FML FieldName: [") + co.fn + std::string("]未定义"));
}
}
// 如果不存在则抛出异常
if (__throwException && 0 == has(co.fn))
{ throw FML_Exception(std::string("FML FieldName: [") + co.fn + std::string("]未设置"));
}
int rc =-1;
// 取值
switch (fieldType)
{
case FLD_SHORT:
{ short value;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_LONG:
{ long value;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_CHAR:
{ char value;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_FLOAT:
{ float value = 0;;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_DOUBLE:
{ double value = 0;
rc = Fget(__ptr, fieldID, pos, (char*)&value, 0);
ss << value;
break;
}
case FLD_STRING:
{ char value[1024*10];
::memset(value, 0, sizeof(value));
rc = Fget(__ptr, fieldID, pos, (char*)value, 0);
ss << value;
break;
}
default:
{ throw FML_Exception("暂未支持的数据类型");
}
break;
}
return ss.str();
}
// 添加值
template<class ValueType>
int add(const ValueType& fieldValue)
{ FLDID fieldID = Fldid((char*)ci.fn.c_str());
int fieldType = Fldtype(fieldID);
long pos = ci.oc;
// 判断是否为未定义字段
if (BADFLDID == fieldID)
{ if (__throwException)
{ throw FML_Exception(std::string("FML FieldName: [") + ci.fn + std::string("]未定义"));
}
}
std::stringstream ss; ss << fieldValue;
int rc = -1;
switch (fieldType)
{
case FLD_SHORT:
{ short value = 0;
ss >> value;
rc = Fadd(__ptr, fieldID, (char*)&value, pos);
break;
}
case FLD_LONG:
{ long value = 0;
ss >> value;
rc = Fadd(__ptr, fieldID, (char*)&value, pos);
break;
}
case FLD_CHAR:
{ char value = 0;
ss >> value;
rc = Fadd(__ptr, fieldID, (char*)&value, pos);
break;
}
case FLD_FLOAT:
{ float value = 0;
ss >> value;
rc = Fadd(__ptr, fieldID, (char*)&value, pos);
break;
}
case FLD_DOUBLE:
{ double value=0;
ss >> value;
rc = Fadd(__ptr, fieldID, (char*)&value, pos);
break;
}
case FLD_STRING:
{ std::string value = ss.str();
rc = Fadd(__ptr, fieldID, (char*)value.c_str(), pos);
break;
}
default:
throw FML_Exception("暂未支持的数据类型");
break;
}
return rc;
}
// 求取类型名称
const std::string Ftype(const int& typeID)
{ std::string type = "";
switch (typeID)
{
case FLD_SHORT:
type = "SHORT";
break;
case FLD_LONG:
type = "LONG";
break;
case FLD_CHAR:
type = "CHAR";
break;
case FLD_FLOAT:
type = "FLOAT";
break;
case FLD_DOUBLE:
type = "DOUBLE";
break;
case FLD_STRING:
type = "STRING";
break;
default:
type = "Unsuported!";
break;
}
return type;
}
protected:
FBFR* __ptr;
bool __throwException;
class cursor
{
public:
long oc; // 取第几个
std::string fn;
public:
cursor() { oc = 0; fn = ""; }
void setFieldName(const std::string& fieldName)
{ (0 == fn.compare(fieldName)) ? oc++ : 0;
fn = fieldName;
}
};
cursor ci;
cursor co;
private:
FML_Adapter& operator=(const FML_Adapter&) { return *this; }
};
#endif
/*
int main(int argc, char** argv)
{
FBFR* buf = (FBFR32*)tpalloc("FML32", NULL, 102400);
FML_Adapter a(buf);
try
{
a["ACCT_ID"] = "123";
a["ACCT_ID"] = "456";
long has = a.has("ACCT_ID");
std::cout << "has = " << has << std::endl;
std::string ACCT_ID = a("ACCT_ID");
std::cout << "ACCT_ID = " << ACCT_ID << std::endl;
}
catch (FML_Exception& e)
{
std::cout << e << std::endl;
}
return 0;
}
*/