XDR-变长类型数组-空间管理-log
2014年4月8日23:39:02
.x文件:
enum colors{
RED = 2,
YELLOW = 3,
BLUE = 5,
GREEN = 0x10
};
struct lww_int
{
int m_nAge;
bool m_bMale;
string m_sName<255>;
colors m_ayColor[2];
int m_naNum<4>;
};
union lww_union switch(colors clr)
{
case RED:
int m_nAge;
case YELLOW:
bool m_bMale;
case BLUE:
string m_sName<255>;
case GREEN:
colors m_ayColor[2];
};
C程序:
#include <stdio.h>
#include <rpc/rpc.h>
#include "lww.h"
//int main()
//{
// printf("begin\n");
//
// FILE * pFile = fopen("lww.txt","wb");
// XDR XDR;
// xdrstdio_create(&XDR,pFile,XDR_ENCODE);
//
// colors aRED = RED;
// colors aYELLOW = YELLOW;
// colors aBLUE = BLUE;
// colors aGREEN = GREEN;
// lww_int aLwwInt;
// aLwwInt.m_nAge = 28;
// aLwwInt.m_bMale = TRUE;
// aLwwInt.m_sName = (char *)"1234";
// aLwwInt.m_ayColor[0] = GREEN;
// aLwwInt.m_ayColor[1] = BLUE;
//
// int naInt[2];
// naInt[0] = 1;
// naInt[1] = 2;
// aLwwInt.m_naNum.m_naNum_len = 2;
// aLwwInt.m_naNum.m_naNum_val = naInt;
//
// lww_union aLwwUnion;
// aLwwUnion.clr = RED;
// aLwwUnion.lww_union_u.m_nAge = 28 * 2;
//
// xdr_colors(&XDR,&aRED);
// xdr_colors(&XDR,&aYELLOW);
// xdr_colors(&XDR,&aBLUE);
// xdr_colors(&XDR,&aGREEN);
// xdr_lww_int(&XDR,&aLwwInt);
// xdr_lww_union(&XDR,&aLwwUnion);
//
// xdr_destroy(&XDR);
// fclose(pFile);
//
// printf("end\n");
//
// return 0;
//}
void printColor(colors aColor)
{
switch(aColor)
{
case RED:
printf("红色\n");
break;
case YELLOW:
printf("黄色\n");
break;
case BLUE:
printf("蓝色\n");
break;
case GREEN:
printf("绿色\n");
break;
default:
printf("错误色\n");
break;
}
return;
}
void printBool(bool_t bPrint)
{
switch(bPrint)
{
case TRUE:
printf("真\n");
break;
case FALSE:
printf("假\n");
break;
default:
printf("错误\n");
break;
}
return;
}
lww_int aLwwInt;
void writeTLogFile(char * str)
{
FILE * pFileW = fopen("log.txt","a+");
if(NULL == pFileW)
{
return;
}
fprintf(pFileW,"%s\n",str);
fclose(pFileW);
return;
}
void writeBLogFile(char * str,int nCount)
{
FILE * pFileW = fopen("log.txt","ab+");
if(NULL == pFileW)
{
return;
}
fwrite(str,1,nCount,pFileW);
fclose(pFileW);
return;
}
void writeB2TLogFile(char * str,int nCount)
{
char sBuffer[400] = {0};
strcpy(sBuffer,"0X");
char * pBuffer = sBuffer + 2;
for(int i = 0;i<nCount;i++)
{
sprintf(pBuffer,"%02X",*str);
str++;
pBuffer += 2;
}
writeTLogFile(sBuffer);
return;
}
int main()
{
printf("begin\n");
writeTLogFile("加载前:");
writeB2TLogFile((char *)&aLwwInt,sizeof(aLwwInt));
FILE * pFile = fopen("lww.txt","rb");
XDR XDR;
xdrstdio_create(&XDR,pFile,XDR_DECODE);
colors aColor;
char sName[255] = {0};
//lww_int aLwwInt;
//aLwwInt.m_sName = sName;
xdr_colors(&XDR,&aColor);
printColor(aColor);
xdr_colors(&XDR,&aColor);
printColor(aColor);
xdr_colors(&XDR,&aColor);
printColor(aColor);
xdr_colors(&XDR,&aColor);
printColor(aColor);
xdr_lww_int(&XDR,&aLwwInt);
printf("%d\n",aLwwInt.m_nAge);
printBool(aLwwInt.m_bMale);
printf("%s\n",aLwwInt.m_sName);
printColor(aLwwInt.m_ayColor[0]);
printColor(aLwwInt.m_ayColor[1]);
printf("长%d\n",aLwwInt.m_naNum.m_naNum_len);
printf("元素0:%d\n",aLwwInt.m_naNum.m_naNum_val[0]);
printf("元素1:%d\n",aLwwInt.m_naNum.m_naNum_val[1]);
writeTLogFile("加载后:");
writeB2TLogFile((char *)&aLwwInt,sizeof(aLwwInt));
lww_union aLwwUnion;
xdr_lww_union(&XDR,&aLwwUnion);
if(RED == aLwwUnion.clr)
{
printf("%d\n",aLwwUnion.lww_union_u.m_nAge);
}
xdr_free((xdrproc_t)xdr_lww_int,(char *)&aLwwInt);
writeTLogFile("释放后:");
writeB2TLogFile((char *)&aLwwInt,sizeof(aLwwInt));
xdr_destroy(&XDR);
fclose(pFile);
printf("end\n");
return 0;
}