2007年9月28日星期五

实战TinyXML

实战TinyXML

作者:裕作

(本文为原创,转贴请注明出处:http://blog.csdn.net/KyosukeNo1


这几天在埋头写自己的3D文件浏览器(稍后发布),突发奇想的要把自己的内部格式转化成XML,于是,把以前在研究所时用过的ExPat翻了出来。 ExPat是基于事件的XML解释器,速度挺快的,但结构方面有点不敢恭维--当年写配置文件的导出导入部分花了我足足1个星期!而且由于它是基于事件发生的次序(SAX),似乎有时会发生一些无法控制的情况--例如进入某Level后忘了记录,结果……后面的程序全部死掉!这时想起同事之前推荐的 TinyXML,结果……用了不到3小时就把我的文件导出来了~~呵呵。在阅读本文之前,请先看看我Blog里转贴的《TinyXML学习笔记》,相信它能给各位一个关于TinyXML的初步概念。


言归正传,本文目的在于补全之前《TinyXML学习》的不足,尽量把常用的示例代码列出让大家参考。此外,在本篇最后会给出一个完整的文件读写例子,供读者参考。


1. 编程环境的设置。新建一个项目,起名叫TestTXML。到http://sourceforge.net/projects/tinyxml/ 下载TinyXML的官方例子,并编译第一个Project tinyxml(注意,最好编译Release的版本,代码比较小。然后把生成的tinyxml.lib(如果是Debug版本,叫tinyxmld.lib)连同tinystr.htinyxml.h一起CopyTestTXML项目的目录中。在TestTXML项目里的头文件加入对TinyXML的引用:

#pragma comment(lib,"tinyxml.lib") // 链接Library

#include "tinyxml.h" // TinyXML的头文件


2. 建立一个XML文件:

char* sFilePath = "ikk_doc.xml"; // 文件名称

TiXmlDocument xmlDoc( sFilePath ); // 建立一个XML文件

TiXmlDeclaration Declaration( "1.0","gb2312", "yes" ); // 声明XML的属性

xmlDoc.InsertEndChild( Declaration ); // 写入基本的XML头结构

xmlDoc.SaveFile(); // XML文件写入硬盘

这时,在硬盘上的TestXML项目目录里,ikk_doc.xml文件已经被创建出来了。


3. XML文件里插入Element

所谓的Element,就是在XML里面的Tag,例如在裕作”>简历内容中,“Resume”就是Element的名字,上述的整个字符串就是一个Element。在TinyXML里,插入Element的步骤如下:

TiXmlElement* pElm = NULL;

pElm = new TiXmlElement( "resumes" ); //定义当前的子节点 pElmParent.InsertEndChild( *pElm ); // 把子节点插入父节点中

4. element里插入属性。在刚才例子中,name=”裕作”就是Resume的属性,其中name是属性的名字,”裕作”是属性的值。在当前子节点内插入属性的方法如下:

pElm->SetAttribute( "name", resume.sName );


5. XML里插入文本。在裕作”>简历内容中,“简历内容”就是一段文本,事实上,在TinyXML里,它是被当作一个Text类型的子节点来插入的。还而言之,就是在Resume的子节点中,插入这个Text子节点。插入例子如下:

TiXmlText* pText = NULL;
pText = new TiXmlText( "
简历内容" ); // 定义文本的内容

pElmChild->InsertEndChild( *pText ); //text子节点插入父节点中


在具备了以上背景知识之后,我们已经可以用TinyXML读写一个XML文件了。本文最后的程序将写入,然后重新读取一个XML文件到我们的结构里。这个XML文件的内容如下:


裕作">

26

编程

吹牛

裕作 The Great">

0

编程




以下程序将建立ikk_doc.xml文件,然后重新把内容读取进内存:


#pragma comment(lib,"tinyxml.lib")


#include "string.h"

#include "stdio.h"

#include "tinyxml.h"


#define XML_FILE "ikk_doc.xml"

#define NAME_LENGTH 256 // 名字类字符的分配长度

#define SAFE_DELETE(x) {if(x) delete x; x=NULL;} // 安全删除new分配出来的变量空间

#define SAFE_DELETE_ARRAY(x) {if(x) delete[] x; x=NULL;} // 安全删除new分配出来的数组空间

#define XML_HEADER "" // XML文件头的定义


typedef unsigned int uint32;


// 技能的结构

typedef struct skill_s {

uint32 nLevel; // 技能的程度

char sName[ NAME_LENGTH ]; // 技能的名称


skill_s() {

nLevel = 0;

sName[0] = 0;

}

} skill_t;


// 简历的结构

typedef struct resume_s {

char sName[ NAME_LENGTH ]; // 名字

bool isMan; // 是否男性

uint32 nAge; // 年龄

uint32 nNumSkill; // 技能的数目

skill_t* pSkill; // 技能的结构


resume_s() {

sName[0] = 0;

isMan = false;

nAge = 0;

nNumSkill = 0;

pSkill = NULL;

}

} resume_t;


void exportSkill( TiXmlElement* pElmParent, skill_t skill )

{

int i;

char sBuf[NAME_LENGTH]; // 一个临时存放的字符串

TiXmlElement* pElm = NULL; // 一个指向Element的指针

TiXmlText* pText = NULL; // 一个指向Text的指针

pElm = new TiXmlElement( "skill" );


// 插入等级(以属性形式)

sprintf( sBuf, "%d", skill.nLevel ); // Skill的登记变成字符串临时存进sBuf

pElm->SetAttribute( "level", sBuf ); // 把等级插入Skill


// 插入技能名称(以子Element形式)

pText = new TiXmlText( skill.sName ); // 建立一个Skill的子Element(一个Text形式的子元素)

pElm->InsertEndChild( *pText ); // 把这个Skill的子Element插入Skill

SAFE_DELETE( pText ); // 删除这个Text


// 最后把整个Resume的子节点插入到父节点中

pElmParent->InsertEndChild( *pElm );

}


void importSkill( TiXmlElement* pElm, skill_t* pSkill )

{

int i;

char sBuf[NAME_LENGTH]; // 一个临时存放的字符串

TiXmlElement* pElmChild = NULL; // 一个指向Element的指针

TiXmlText* pText = NULL; // 一个指向Text的指针

// 读取level

pSkill->nLevel = atoi( pElm->Attribute( "level" ) );

// 读取技能名称

strcpy( pSkill->sName, pElm->FirstChild()->Value() );

}


void exportResume( TiXmlElement* pElmParent, resume_t resume )

{

int i;

char sBuf[NAME_LENGTH]; // 一个临时存放的字符串

TiXmlElement* pElm = NULL; // 一个指向Element的指针

TiXmlElement* pElmChild = NULL; // 一个指向Element的指针

TiXmlText* pText = NULL; // 一个指向Text的指针

pElm = new TiXmlElement( "resume" );


// 插入名字(以属性形式)

pElm->SetAttribute( "name", resume.sName );


// 插入性别(以子Element形式)

pElmChild = new TiXmlElement( "gender" ); // 建立一个子ElementGender

if( resume.isMan )

pText = new TiXmlText( "" ); // 建立一个Gender的子Element(一个Text形式的子元素)

else

pText = new TiXmlText( "" ); // 建立一个Gender的子Element(一个Text形式的子元素)

pElmChild->InsertEndChild( *pText ); // 把这个Gender的子Element插入Gender

pElm->InsertEndChild( *pElmChild ); // Gender插入到主Element

SAFE_DELETE( pElmChild ); // 删除已经用完的Gender

SAFE_DELETE( pText ); // 删除这个Text


// 插入年龄(以子Element形式)

pElmChild = new TiXmlElement( "age" ); // 建立一个子ElementAge

sprintf( sBuf, "%d", resume.nAge ); // Age变成字符串临时存进sBuf

pText = new TiXmlText( sBuf ); // 建立一个Age的子Element(一个Text形式的子元素)

pElmChild->InsertEndChild( *pText ); // 把这个Age的子Element插入Age

pElm->InsertEndChild( *pElmChild ); // Age插入到主Element

SAFE_DELETE( pElmChild ); // 删除已经用完的Age

SAFE_DELETE( pText ); // 删除这个Text


// 插入技能子节点

pElmChild = new TiXmlElement( "skills" ); // 建立一个子ElementSkills

sprintf( sBuf, "%d", resume.nNumSkill ); // Skill的数目变成字符串临时存进sBuf

pElmChild->SetAttribute( "num", sBuf ); // 把这个Skills的属性插入Skills

for( i=0; i

{

exportSkill( pElmChild, resume.pSkill[i] ); // 插入一项技能

}

pElm->InsertEndChild( *pElmChild ); // Skills插入到主Element

SAFE_DELETE( pElmChild ); // 删除已经用完的Skills

SAFE_DELETE( pText ); // 删除这个Text


// 最后把整个Resume的子节点插入到父节点中

pElmParent->InsertEndChild( *pElm );


SAFE_DELETE( pElm ); // 删除子节点

}


void importResume( TiXmlElement* pElm, resume_t* pResume )

{

int i;

char sBuf[NAME_LENGTH]; // 一个临时存放的字符串

TiXmlElement* pElmChild = NULL; // 一个指向Element的指针

TiXmlElement* pElmGrandChild = NULL; // 一个指向Element的指针

TiXmlText* pText = NULL; // 一个指向Text的指针

// 读入"resume"子节点

strcpy( pResume->sName, pElm->Attribute( "name" ) );


// 读入"gender"子节点

pElmChild = pElm->FirstChildElement( "gender" );

if( strcmp( "", pElmChild->FirstChild()->Value() ) == 0 )

pResume->isMan = true;

else

pResume->isMan = false;


// 读入"age"子节点

pElmChild = pElm->FirstChildElement( "age" );

pResume->nAge = atoi( pElmChild->FirstChild()->Value() );


// 读入"skills"子节点

pElmChild = pElm->FirstChildElement( "skills" );

pResume->nNumSkill = atoi( pElmChild->Attribute( "num" ) );

pResume->pSkill = new skill_t[pResume->nNumSkill];


pElmGrandChild = pElmChild->FirstChildElement( "skill" ); // 指向第一个Skill

for( i=0; inNumSkill; i++ ) {

importSkill( pElmGrandChild, &(pResume->pSkill[i]) ); // 读取一个Skill

pElmGrandChild = pElmGrandChild->NextSiblingElement(); // 指向下一个Skill

}

}


bool readXML( char* sFilePath, int* nNumResume, resume_t** ppResume ) {

int i; // 用做循环的变量

TiXmlElement* pElmChild = NULL; // 一个指向Element的指针


TiXmlDocument xmlDoc( sFilePath ); // 输入XML路径

if( !xmlDoc.LoadFile() ) // 读取XML并检查是否读入正确

return false;


TiXmlElement* pElmRoot = NULL; // 根节点


pElmRoot = xmlDoc.FirstChildElement( "resumes" ); // 得到根节点


if( !pElmRoot ) {

return false;

}


*nNumResume = atoi( pElmRoot->Attribute( "num" ) ); // 读取Resume的数目

*ppResume = new resume_t[*nNumResume]; // 分配Resume的空间


pElmChild = pElmRoot->FirstChildElement( "resume" ); // 找出第一个Resume

for( i=0; i<*nNumResume; i++ ) {

importResume( pElmChild, &((*ppResume)[i]) ); // 读取Resume的内容

pElmChild = pElmChild->NextSiblingElement(); // 找出下一个Resume

}


return true;

}


bool writeXML( char* sFilePath, int nNumResume, resume_t* pResume )
{

if( !sFilePath || !pResume )

return false; // 确定指针存在


int i; // 用做循环的变量

char sBuf[NAME_LENGTH]; // 一个临时存放的字符串


TiXmlElement* pElm = NULL; // 一个指向Element的指针

TiXmlDeclaration Declaration( "1.0","gb2312", "yes" ); // 建立XML头结构


TiXmlDocument xmlDoc( sFilePath ); // 用存档的文件名字来建立一个XML文件

xmlDoc.InsertEndChild( Declaration ); // XML头结构插入当前文档

// 插入根节点“Resumes”
pElm = new TiXmlElement( "resumes" ); // 建立根节点“Resumes”
sprintf( sBuf, "%d", nNumResume ); // nNumResume变成字符串临时存进sBuf

pElm->SetAttribute( "num", sBuf ); // 建立一个Resumes的子Element


for( i=0; i<2; i++ )
{
exportResume( pElm, pResume[i] ); // 在根节点上插入以上定义的2个简历
}
xmlDoc.InsertEndChild( *pElm );

xmlDoc.SaveFile();


SAFE_DELETE( pElm ); // 删除Element


return true;

}



void main()

{

int i, j;

// + == 设置两份简历 ==========================================================

int nNumResume = 2;

resume_t* pResume = new resume_t[ nNumResume ];


// 1. 初始化第一份简历

strcpy( pResume[0].sName, "裕作" );

pResume[0].isMan = true;

pResume[0].nAge = 26;

pResume[0].nNumSkill = 2;

pResume[0].pSkill = new skill_t[2];


{ // 设置技能列表结构

strcpy( pResume[0].pSkill[0].sName, "编程" );

strcpy( pResume[0].pSkill[1].sName, "吹牛" );

pResume[0].pSkill[0].nLevel = 99;

pResume[0].pSkill[1].nLevel = 1;

}


// 2. 初始化第二份简历

strcpy( pResume[1].sName, "裕作 The Great" );

pResume[1].isMan = true;

pResume[1].nAge = 0;

pResume[1].nNumSkill = 1;

pResume[1].pSkill = new skill_t[1];


{ // 设置技能列表结构

strcpy( pResume[1].pSkill[0].sName, "编程" );

pResume[1].pSkill[0].nLevel = 100;

}

// - == 设置两份简历 ==========================================================


// 把简历以XML形式写入磁盘

if( !writeXML( XML_FILE, nNumResume, pResume ) )

{

printf( "ERROR: can't write the file." );

return;

}


// 删除Resume

nNumResume = 0;

SAFE_DELETE_ARRAY( pResume );

// 重新读入XML文件里的Resume数据

if( !readXML( XML_FILE, &nNumResume, &pResume ) )

{

printf( "ERROR: can't read the file." );

return;

}


// 把所有简历输出到屏幕

if( pResume ) // 确定有Resume

{

for( i=0; i

printf( "简历:======================\n" );

printf( "\t名字:%s\n", pResume[i].sName );

if( pResume[i].isMan )

printf( "\t性别:男\n" );

else

printf( "\t性别:女\n" );

printf( "\t年龄:%d\n", pResume[i].nAge );

printf( "\t职业技能:\n" );

for( j=0; j

printf( "\t\t技能名称:%s\n", pResume[i].pSkill[j].sName );

printf( "\t\t技能等级:%d\n", pResume[i].pSkill[j].nLevel );

}

}

}

}



[trac]有用的插件

[trac]有用的插件 CSDN Blog推出文章指数概念,文章指数是对Blog文章综合评分后推算出的,综合评分项分别是该文章的点击量,回复次数,被网摘收录数量,文章长度和文章类型;满分100,每月更新一次。

trac终于在公司内部使用起来. 现在列一下我认为比较好用的插件.

1. WebAdmin: 这个肯定要装, 用这个 进行管理权限很方便

2. AccountManagerPlugin : 提供以web界面的方式登录. trac默认的登录方式切换用户比较麻烦.

3. TracBlogPlugin : 写blog, 这个我主要用来写开发日志. 依赖TracTags 插件.

4. TracDown : 列出项目释放出来的所有版本, 提供下载.

5. AddCommentMacro : 允许其他人对某个wiki添加评论. 依赖MacroPostPlugin插件.

6. Trac-CC : 显示CruiseControl 自动构建报告 .

7. ThemeEnginePlugin: 如果你想为trac换皮肤, 就要装这个插件

8. OpenOfficeToTracWikiScript : 这个不是trac的插件. 但这个脚本对不会写wiki的人来说很有用. 它是把openoffice文档转换成trac wiki语法格式.

附:

http://trac.edgewall.org/wiki/PluginList 列出常用的插件

http://trac-hacks.org/ trac所有的插件

C/C++跨平台ini文件读写API (已更新)

C/C++跨平台ini文件读写API (已更新)

Posted on 2006-01-24 16:37 天下无双 阅读(1933) 评论(15) 编辑 收藏 引用 所属分类: C/C++

昨 天写了个一组ini文件读写函数。提供大家使用,已经在XP+VC7.1和FC6+GCC4.1,ARM9+arm-linux-gcc3.3中测试过 了,可以跨平台使用。使用标准C写得,支持C++。源程序可以到:http: //www.cppblog.com/Files/dyj057/inifile.zip下载。

/* *
*@file inifile.h
*@cpright (C)2007 GEC
*@auther dengyangjun
*@email dyj057@gmail.com
*@version 0.1
*@create 2007-1-14
*@modify 2007-1-14
*@brief declare ini file operation
*@note
*@history
*/

#ifndef INI_FILE_H_
#define INI_FILE_H_

#ifdef __cplusplus
extern " C "
{
#endif

int read_profile_string( const char * section, const char * key, char * value, int size, const char * file);
int read_profile_int( const char * section, const char * key, int default_value, const char * file);

int write_profile_string( const char * section, const char * key, const char * value, const char * file);

#ifdef __cplusplus
};
// end of extern "C" {
#endif

#endif // end of INI_FILE_H_

/* *
*@file inifile.c
*@cpright (C)2007 GEC
*@auther dengyangjun
*@email dyj057@gmail.com
*@version 0.1
*@create 2007-1-14
*@modify 2007-1-14
*@brief implement ini file operation
*@note
*@history
*/
#include
< stdio.h >
#include
< stdlib.h >
#include
< assert.h >
#include
< string .h >
#include
< ctype.h >

#include
" inifile.h "

#ifdef __cplusplus
extern " C "
{
#endif

#define MAX_FILE_SIZE 8096

#define LEFT_BRACE '['
#define RIGHT_BRACE ']'

static int load_ini_file( const char * file, char * buf, int * file_size)
{
FILE
* in = NULL;
int i = 0 ;
* file_size = 0 ;

assert(file
!= NULL);
assert(buf
!= NULL);

in = fopen(file, " r " );
if ( NULL == in ) {
return 0 ;
}

// load initialization file
while ((buf[i] = fgetc( in )) != EOF) {
i
++ ;
assert( i
< MAX_FILE_SIZE); // file too big
}

buf[i]
= ' \0 ' ;
* file_size = i;

fclose(
in );
return 1 ;
}

/*
*
* Result of the addtion(int)
*
*
*/

static int isnewline( char c)
{
return ( ' \n ' == c || ' \r ' == c ) ? 1 : 0 ;
}
static int isend( char c)
{
return ' \0 ' == c ? 1 : 0 ;
}
static int isleftbarce( char c)
{
return LEFT_BRACE == c ? 1 : 0 ;
}
static int isrightbrace( char c )
{
return RIGHT_BRACE == c ? 1 : 0 ;
}
static int parse_file( const char * section, const char * key, const char * buf, int * sec_s, int * sec_e,
int * key_s, int * key_e, int * value_s, int * value_e)
{
const char * p = buf;
int i = 0 ;

assert(buf
!= NULL);
assert(section
!= NULL && strlen(section));
assert(key
!= NULL && strlen(key));

* sec_e = * sec_s = * key_e = * key_s = * value_s = * value_e = - 1 ;

while ( ! isend(p[i]) )
{
// find the section
if ( ( 0 == i || isnewline(p[i - 1 ]) ) && isleftbarce(p[i]) )
{
int section_start = i + 1 ;

// find the ']'
do
{
i
++ ;
}
while ( ! isrightbrace(p[i]) && ! isend(p[i]));

if ( 0 == strncmp(p + section_start,section, i - section_start))
{
int newline_start = 0 ;

i
++ ;

// Skip over space char after ']'
while (isspace(p[i]))
{
i
++ ;
}

// find the section
* sec_s = section_start;
* sec_e = i;

while ( ! (isnewline(p[i - 1 ]) && isleftbarce(p[i])) && ! isend(p[i]) )
{
int j = 0 ;
// get a new line
newline_start = i;

while ( ! isnewline(p[i]) && ! isend(p[i]) )
{
i
++ ;
}
// now i is equal to end of the line

j
= newline_start;

if ( ' ; ' != p[j]) // skip over comment
{
while (j < i && p[j] != ' = ' )
{
j
++ ;
if ( ' = ' == p[j])
{
if (strncmp(key,p + newline_start,j - newline_start) == 0 )
{
// find the key ok
* key_s = newline_start;
* key_e = j - 1 ;

* value_s = j + 1 ;
* value_e = i;

return 1 ;
}
}
}
}

i
++ ;
}
}
}
else
{
i
++ ;
}
}
return 0 ;
}

/* *
* @brief read_profile_string retrieves a string from the specified section in an initialization file.
* @param section const char * name of the section containing the key name.
* @param key const char * the name of the key whose associated string is to be retrieved.
* @param value char * pointer to the buffer that receives the retrieved string.
* @return int 1 : read success; 0 : read fail.
*/
int read_profile_string( const char * section, const char * key, char * value, int size, const char * file)
{
char buf[MAX_FILE_SIZE] = { 0 };
int file_size;
int sec_s,sec_e,key_s,key_e, value_s, value_e;

// check parameters
assert(section != NULL && strlen(section));
assert(key
!= NULL && strlen(key));
assert(value
!= NULL);
assert(size
> 0 );
assert(file
!= NULL && strlen(key));

if ( ! load_ini_file(file,buf, & file_size))
return 0 ;

if ( ! parse_file(section,key,buf, & sec_s, & sec_e, & key_s, & key_e, & value_s, & value_e))
{
return 0 ; // not find the key
}
else
{
int cpcount = value_e - value_s;

if ( size - 1 < cpcount)
{
cpcount
= size - 1 ;
}

memset(value,
0 , size);
memcpy(value,buf
+ value_s, cpcount );
value[cpcount]
= ' \0 ' ;

return 1 ;
}
}


int read_profile_int( const char * section, const char * key, int default_value, const char * file)
{
char value[ 32 ] = { 0 };
if ( ! read_profile_string(section,key,value, sizeof (value),file))
{
return default_value;
}
else
{
return atoi(value);
}
}

int write_profile_string( const char * section, const char * key, const char * value, const char * file)
{
char buf[MAX_FILE_SIZE] = { 0 };
char w_buf[MAX_FILE_SIZE] = { 0 };
int sec_s,sec_e,key_s,key_e, value_s, value_e;
int value_len = ( int )strlen(value);
int file_size;
FILE
* out ;

// check parameters
assert(section != NULL && strlen(section));
assert(key
!= NULL && strlen(key));
assert(value
!= NULL);
assert(file
!= NULL && strlen(key));

if (!load_ini_file(file,buf, & file_size))
{
sec_s
= - 1 ;
}
else
{
parse_file(section,key,buf,
& sec_s, & sec_e, & key_s, & key_e, & value_s, & value_e);
}

if ( - 1 == sec_s)
{

if ( 0 == file_size)
{
sprintf(w_buf
+ file_size, " [%s]\n%s=%s\n " ,section,key,value);
}
else
{
// not find the section, then add the new section at end of the file
memcpy(w_buf,buf,file_size);
sprintf(w_buf
+ file_size, " \n[%s]\n%s=%s\n " ,section,key,value);
}


}
else if ( - 1 == key_s)
{
// not find the key, then add the new key & value at end of the section
memcpy(w_buf,buf,sec_e);
sprintf(w_buf
+ sec_e, " %s=%s\n " ,key,value);
sprintf(w_buf
+ sec_e + strlen(key) + strlen(value) + 2 ,buf + sec_e, file_size - sec_e);
}
else
{
// update value with new value
memcpy(w_buf,buf,value_s);
memcpy(w_buf
+ value_s,value, value_len);
memcpy(w_buf
+ value_s + value_len, buf + value_e, file_size - value_e);
}

out = fopen(file, " w " );
if (NULL == out )
{
return 0 ;
}

if ( - 1 == fputs(w_buf, out ) )
{
fclose(
out );
return 0 ;
}

fclose(
out );

return 1 ;
}


#ifdef __cplusplus
};
// end of extern "C" {
#endif

#include < stdio.h >
#include
" inifile.h "

// main.c
#define BUF_SIZE 256
int main()
{
const char * file = " myconfig.ini " ;
const char * section = " Db " ;
const char * key = " XX2 " ;

char value[BUF_SIZE] = { 0 };

printf(
" test get profile string\n " );

if ( ! read_profile_string(section, key, value, BUF_SIZE, file))
{

printf(
" read ini file fail\n " );
}
else
{
int x = read_profile_int(section,key, 0 ,file);
printf(
" XX2=%d\n " , x);
printf(
" read: [%s] = [%s]\n " ,key,value);
}

if ( ! write_profile_string(section, " XX2 " , " 2writeOK " ,file))
{
printf(
" write ini file fail\n " );
}
else
{
printf(
" write ini file ok\n " );
}

return 0 ;
}