解析一个名位noname.xml的文件:
- "1.0" standalone="no" ?>
- <Attributes>
- <Attribute name="AcquireTime" type="EPICS_PV" source="$(P)$(R)cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/>
- <Attribute name="FilePath" type="EPICS_PV" source="$(P)$(R)$(T)FilePath" dbrtype="DBR_STRING" description="File Save Path"/>
- Attributes>
解析以上文件的源文件:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include "NDAttribute.h"
- #include "NDAttributeList.h"
- #include "PVAttribute.h"
- #include
-
-
- int main()
- {
- MAC_HANDLE * macHandle;
- char ** macPairs;
- int status = 0;
- std::ostringstream buff;
- std::string buffer;
- std::ifstream infile;
- std::string fileName;
- std::string macrodefs;
- int bufferSize;
-
- fileName = "noname.xml";
-
- infile.open(fileName.c_str());
-
- buff << infile.rdbuf();
- buffer = buff.str();
-
- printf("The name of a xml file to parse:\n ");
- printf("fileName: %s\n", fileName.c_str());
- printf("The file's content:\n");
- printf("%s\n", buffer.c_str());
- printf("--------------------------------------------------------------\n");
-
- macCreateHandle(&macHandle, 0);
-
- macrodefs = "P=BS:,R=SIM1:,T=TIFF1:";
- printf("The macro defination: %s\n", macrodefs.c_str());
-
- status = macParseDefns(macHandle, macrodefs.c_str() ,&macPairs);
-
- if (status < 0){
- printf("macParseDefns call failed\n");
- return -1;
- }
-
- status = macInstallMacros(macHandle, macPairs);
- if (status < 0){
- printf("macInstallMacros call failed\n");
- return -1;
- }
-
- bufferSize = (int)(buffer.length() * 10);
- char * tmpBuffer = (char *)malloc(sizeof(char ) * bufferSize);
-
- status = macExpandString(macHandle, buffer.c_str(), tmpBuffer, bufferSize);
-
- if (status < 0){
- printf("macExpandString call failed\n");
- free(tmpBuffer);
- free(macPairs);
- return -1;
- }
-
- macrodefs = tmpBuffer;
- free(tmpBuffer);
- macDeleteHandle(macHandle);
-
- printf("Orignal file content: \n");
- printf("'%s'\n\n", buffer.c_str());
- printf("The expended file contenent\n");
- printf("'%s'\n\n", macrodefs.c_str());
-
- const char *pName, *pSource, *pAttrType, *pDescription, *pDBRType;
- xmlDocPtr doc;
- xmlNode *Attr, *Attrs;
-
- doc = xmlReadMemory(macrodefs.c_str(), (int)macrodefs.length(), NULL , NULL, 0);
-
- if (doc == NULL) {
- printf("xmlReadMemory call failed\n");
- return -1;
- }
- Attrs = xmlDocGetRootElement(doc);
-
- if ((!xmlStrEqual(Attrs->name, (const xmlChar *)"Attributes")))
- {
- printf("Cannot find the root 'Attriubtes\n'");
- return -1;
- }
-
- printf("-----------------------------------------------------------------------\n");
- printf("Parse the xml file: %s\n", fileName.c_str());
- PVAttribute * pAttribute;
- NDAttributeList * list = new NDAttributeList();
-
- for (Attr = xmlFirstElementChild(Attrs); Attr; Attr = xmlNextElementSibling(Attr)){
- pName = (const char *)xmlGetProp(Attr, (const xmlChar *)"name");
- if (!pName){
- printf("xmlGetProp '%s' call failed\n", "name");
- return -1;
- }
-
- pDescription = (const char *)xmlGetProp(Attr, (const xmlChar *)"description");
- if (!pDescription) pDescription ="";
-
- pSource = (const char *)xmlGetProp(Attr, (const xmlChar *)"source");
- if (!pSource){
- printf("xmlGetProp '%s' call failed\n", "source");
- return -1;
- }
-
- pAttrType = (const char *)xmlGetProp(Attr, (const xmlChar *)"type");
- if (!pAttrType){
- printf("xmlGetProp '%s' call failed\n", "type");
- return -1;
- }
-
- pDBRType = (const char *)xmlGetProp(Attr, (const xmlChar *)"dbrtype");
- if (!pDBRType){
- printf("xmlGetProp '%s' call failed\n", "dbrtype");
- return -1;
- }
-
-
- int dbrType = DBR_NATIVE;
- if (pDBRType) {
- if (!strcmp(pDBRType, "DBR_CHAR"))
- dbrType = DBR_CHAR;
- else if (!strcmp(pDBRType, "DBR_SHORT"))
- dbrType = DBR_SHORT;
- else if (!strcmp(pDBRType, "DBR_ENUM"))
- dbrType = DBR_ENUM;
- else if (!strcmp(pDBRType, "DBR_INT"))
- dbrType = DBR_INT;
- else if (!strcmp(pDBRType, "DBR_LONG"))
- dbrType = DBR_LONG;
- else if (!strcmp(pDBRType, "DBR_FLOAT"))
- dbrType = DBR_FLOAT;
- else if (!strcmp(pDBRType, "DBR_DOUBLE"))
- dbrType = DBR_DOUBLE;
- else if (!strcmp(pDBRType, "DBR_STRING"))
- dbrType = DBR_STRING;
- else
- dbrType = DBR_NATIVE;
- }
-
- printf("name: %s\tsource: %s\ttype: %s\tdbrtype: %s\tdescription: %s\n", pName, pSource, pAttrType, pDBRType, pDescription);
-
- pAttribute = new PVAttribute(pName, pDescription, pSource, dbrType );
- list->add(pAttribute);
-
- }
-
- epicsThreadSleep(1.0);
- printf("-----------------------------------------------------------------------\n");
- list->updateValues();
- epicsThreadSleep(1.0);
- printf("NDAttributeList and NDAttriubte's information: \n");
- list->report(stdout, 20);
-
- return 0;
- }
先用通道访问测试以上此处用到的EPICS过程变量:
- orangepi@orangepi5:~/C_program/host_program/hostApp$ caget BS:SIM1:cam1:AcquireTime
- BS:SIM1:cam1:AcquireTime 5
- orangepi@orangepi5:~/C_program/host_program/hostApp$ caget -S BS:SIM1:TIFF1:FilePath
- BS:SIM1:TIFF1:FilePath /home/test_gap
编译以上程序,执行结果如下:
- orangepi@orangepi5:~/C_program/host_program/hostApp$ O.linux-aarch64/testXml
- The name of a xml file to parse:
- fileName: noname.xml
- The file's content:
-
-
- --------------------------------------------------------------
- The macro defination: P=BS:,R=SIM1:,T=TIFF1:
- Orignal file content:
- '"1.0" standalone="no" ?>
-
"AcquireTime" type="EPICS_PV" source="$(P)$(R)cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/> -
"FilePath" type="EPICS_PV" source="$(P)$(R)$(T)FilePath" dbrtype="DBR_STRING" description="File Save Path"/> - '
- The expended file contenent
- '"1.0" standalone="no" ?>
-
"AcquireTime" type="EPICS_PV" source="BS:SIM1:cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/> -
"FilePath" type="EPICS_PV" source="BS:SIM1:TIFF1:FilePath" dbrtype="DBR_STRING" description="File Save Path"/> - '
- -----------------------------------------------------------------------
- Parse the xml file: noname.xml
- name: AcquireTime source: BS:SIM1:cam1:AcquireTime type: EPICS_PV dbrtype: DBR_NATIVE description: Camera acquire time
- name: FilePath source: BS:SIM1:TIFF1:FilePath type: EPICS_PV dbrtype: DBR_STRING description: File Save Path
- -----------------------------------------------------------------------
- NDAttributeList and NDAttriubte's information:
-
- NDAttributeList: address=0x556d1b5970:
- number of attributes=2
-
- NDAttribute, address=0x556d1b7f90:
- name=AcquireTime
- description=Camera acquire time
- source type=2
- source type string=NDAttrSourceEPICSPV
- source=BS:SIM1:cam1:AcquireTime
- dataType=NDAttrFloat64
- value=5.000000
- PVAttribute
- dbrType=DBR_invalid
- chanId=0x556d1bf3c0
- eventId=0x7f80000b70
-
- NDAttribute, address=0x556d1dff40:
- name=FilePath
- description=File Save Path
- source type=2
- source type string=NDAttrSourceEPICSPV
- source=BS:SIM1:TIFF1:FilePath
- dataType=NDAttrString
- value=/home/test_gap
- PVAttribute
- dbrType=DBR_STRING
- chanId=0x556d1bf3f8
- eventId=0x7f80000b98