📄 nnadkstubplugin.cpp
字号:
/* /////////////////////////////////////////////////////////////////////
// BEGIN_COPYRIGHT
// Copyright (C) 1995-2001 New Era Of Networks, Inc.
// END_COPYRIGHT
//
// BEGIN_DISCLAIMER
// New Era Of Networks, Inc. shall have no liability with respect to
// the infringement of copyrights, trade secrets or any patents by
// New Era Of Networks, Inc. or any part thereof except where
// specifically provided by License Agreement.
//
// In no event will New Era Of Networks, Inc. be liable for any lost
// revenue or profits or other special, indirect and consequential
// damages, even if New Era Of Networks, Inc. has been advised of the
// possibility of such damages.
//
// New Era Of Networks, Inc.
// 6550 Greenwood Plaza Blvd.
// Englewood, Colorado 80111
// USA
// END_DISCLAIMER
//
////////////////////////////////////////////////////////////////////// */
//******************************************************************
//
//******************************************************************
#include <INFR/Streams.h>
#include "NNADKStubPlugIn.h"
#include <ADK/NNADKLogging.h>
#include <ADK/NNADKMacro.h>
USING_NAMESPACE(NNSY::INFR)
USING_NAMESPACE(NNSY::ADK)
USING_NAMESPACE(NNSY::CONFIG)
USING_NAMESPACE(NNSY::NDO)
static bool VerboseOn = true;
typedef struct FileObjects
{
IFStream* pInfile;
OFStream* pOutfile;
int instance;
} FileObjectsStruct;
#ifdef _WIN32_WINNT
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif
NNADKSTUBPLUGIN_API bool initAdapter(NNSY_CONFIG_NAMESPACE NNConfig& config,
NNSY_ADK_NAMESPACE e_NNAdapterMode mode,
void** userdata)
{
// TO DO ... Add any setup code. Add all user defined data to userdata
// it is recommended that you assign a struct (that contains anydata you
// need) to the user data. this is better than making all you data global.
// RETURN VALUES
// return true if you have no problem with setup
// return false if you have a problem;
// returning false will cause the could to report an msgor and exit;
bool iret = false;
*userdata = new FileObjectsStruct;
FileObjectsStruct* pFileObjects = (FileObjectsStruct*) (*userdata);
pFileObjects->pInfile = NULL;
pFileObjects->pOutfile = NULL;
pFileObjects->instance = 0;
// userdata = (void**) (&pFileObjects);
if ( ( mode == AM_Schema ) ||
( mode == AM_Acquire ) ||
( mode == AM_Process ) ||
( mode == AM_AcquireBuffer ) ||
( mode == AM_ProcessBuffer) )
{
STL_STRING InFileName = config.getStringValue("Adapter.in_file_name");
IFStream* pInfile = new IFStream;
pFileObjects->pInfile = pInfile;
if ( VerboseOn )
{
StandardErrorStream << "Opening input file named "
<< InFileName << StreamEndLine;
}
pInfile->open(InFileName.c_str());
if ( pInfile->good() )
{
if (VerboseOn)
{
StandardErrorStream << "File opened successfully"
<< StreamEndLine;
}
if (pInfile->eof())
{
StandardErrorStream << "File" << InFileName
<< " contains no data to evaluate.";
StandardErrorStream << StreamEndLine;
iret = false;
}
else
{
iret = true;
}
}
else
{
StandardErrorStream << "File Could Not Be Opened" << StreamEndLine;
NNADK_THROW_CRITICAL ("File Could Not Be Opened");
iret = false;
}
}
if ( ( mode == AM_Deliver ) ||
( mode == AM_Process ) ||
( mode == AM_DeliverBuffer ) ||
( mode == AM_ProcessBuffer) )
{
STL_STRING OutFileName = config.getStringValue("Adapter.out_file_name");
OFStream* pOutfile = new OFStream;
pFileObjects->pOutfile = pOutfile;
if ( VerboseOn )
{
StandardErrorStream << "Opening output file named "
<< OutFileName << StreamEndLine;
}
pOutfile->open(OutFileName.c_str());
if ( pOutfile->good() )
{
if (VerboseOn)
{
StandardErrorStream << "File opened successfully"
<< StreamEndLine;
pFileObjects->instance = 0;
iret = true;
}
}
else
{
StandardErrorStream << "File Could Not Be Opened" << StreamEndLine;
NNADK_THROW_CRITICAL ("File Could Not Be Opened");
iret = false;
}
}
return iret;
}
NNADKSTUBPLUGIN_API void shutdownAdapter(void** userdata)
{
// TO DO .... do any clean up you need.
if (!userdata)
{
StandardErrorStream << "User Data not set" << StreamEndLine;
NNADK_THROW_CRITICAL ("User Data not set");
}
else
{
FileObjectsStruct* pFileObjects = (FileObjectsStruct*) (*userdata);
if (pFileObjects)
{
if (pFileObjects->pInfile)
{
pFileObjects->pInfile->close();
}
if (pFileObjects->pOutfile)
{
pFileObjects->pOutfile->close();
}
delete pFileObjects;
}
}
}
bool createTree (bool IsSchema,
NNSY_NDO_NAMESPACE NNDOObject& ndo,
void* userdata)
{
bool iret = false;
char ch = 0;
size_t pos = 0;
STL_STRING buf;
bool nameFound = false;
IFStream* pInfile = NULL;
STL_STRING* pName;
STL_STRING* pValue;
char TreeName[33];
NNDOSchemaTree* pSchemaTree;
NNDOSchemaNode* pSchemaNode;
NNDODataTree* pDataTree;
NNDODataNode* pDataNode;
if (!( (FileObjectsStruct*) userdata) )
{
StandardErrorStream << "User Data not set" << StreamEndLine;
NNADK_THROW_CRITICAL ("User Data not set");
}
else
{
FileObjectsStruct* pFileObjects = (FileObjectsStruct*) (userdata);
if (pFileObjects)
{
if (pFileObjects->pInfile)
{
pInfile = pFileObjects->pInfile;
sprintf(TreeName, "N%d", ++(pFileObjects->instance) );
iret = true;
}
else
{
NNADK_THROW_CRITICAL ("No file to read");
iret = false;
}
}
else
{
NNADK_THROW_CRITICAL ("No file to read");
iret = false;
}
}
if (iret)
{
ndo.setName (TreeName);
if (IsSchema)
{
pSchemaTree = ndo.createSchemaTree();
}
else
{
pDataTree = ndo.createDataTree();
}
if (pSchemaTree || pDataTree)
{
iret = false; // set so will not come back unless get data
while ((ch != EOF) && (pInfile->get(ch)))
{
if (ch == ',' ) {
if ((pos > 0) && (buf[pos- 1] == '\\'))
{
buf[pos-1] = ',';
}
else
{
pName = new STL_STRING (buf);
buf = "";
pos = 0;
}
}
else if ((ch == EOF) || (ch == '\n'))
{
if (pos == 0)
{
if (ch == EOF)
{
StandardErrorStream
<< "data all done"
<< StreamEndLine;
iret = false;
}
else
{
iret = true;
}
break;
}
pValue = new STL_STRING (buf);
pos = 0;
buf = "";
if (pName && pValue)
{
if (IsSchema)
{
pSchemaNode = pSchemaTree->createNode (*pName);
pSchemaNode->setDataType (DT_String);
pSchemaTree->appendChild (pSchemaNode);
}
else
{
pDataNode = pDataTree->createNode (*pName);
pDataNode->setDataType (DT_String);
pDataNode->setData (*pValue);
pDataTree->appendChild (pDataNode);
}
if (VerboseOn)
{
StandardErrorStream <<pName->c_str();
StandardErrorStream << "\t\t"
<< pValue->c_str()
<< StreamEndLine;
}
delete pName;
delete pValue;
pName = 0;
pValue = 0;
iret = true;
} // Name and Value set
else
{
StandardErrorStream
<< "Error - missing name or value"
<< StreamEndLine;
NNADK_THROW_CRITICAL ("Error - missing name or value");
iret = false;
break;
} // No Name or Value
} // newline or EOF
else
{
buf += ch;
pos++;
}
} // end while
} // Tree created
else
{
StandardErrorStream
<< "Error - cannot create tree"
<< StreamEndLine;
NNADK_THROW_CRITICAL ("Error - cannot create tree");
iret = false;
}
} // file object found
return iret;
}
bool createBuffer (NNSY_ADK_NAMESPACE NNDataBuffer &dataBuffer,
void *userdata)
{
bool iret = false;
char ch = 0;
size_t pos = 0;
STL_STRING buf;
IFStream* pInfile = NULL;
dataBuffer.clear();
if (!userdata)
{
StandardErrorStream << "User Data not set" << StreamEndLine;
NNADK_THROW_CRITICAL ("User Data not set");
}
else
{
FileObjectsStruct* pFileObjects = (FileObjectsStruct*) (userdata);
if (pFileObjects)
{
if (pFileObjects->pInfile)
{
pInfile = pFileObjects->pInfile;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -