RTF文件解析器

类别:编程语言 点击:0 评论:0 推荐:
(1)说明 这个转换器是为了将RTF文件流转换成为内存中的段、句逻辑结构。 (2)RTF格式说明 参考的是微软的RTF格式说明 (3)代码说明  i.首先是定义的逻辑结构,以类接口的形式提供以下是定义 class IParser; class IParagraphs; class IParagraph; class ISentences; class ISentence; class IParaStyle; class IParser { /* the method of rtf parser */ public: /* parse the rtf stream and create format text char* stream - (in)the rtf stream buffer return error code if need. */ virtual int parse( char* stream ) = 0; /* release object */ virtual void release() = 0; /* the properties of parser */ public: /* get paragraphs of rtf */ virtual IParagraphs* get_paragraphs() = 0; /* get fonts count */ virtual int get_fntcount() = 0; /* get font */ virtual FontStyle* get_fnt( int nIndex ) = 0; /* get color count */ virtual int get_clrcount() = 0; /* get color */ virtual ColorStyle get_color( int nIndex ) = 0; }; class IParagraphs { /* the methods of paragraphs */ public: /* add color into tables */ virtual void add( IParagraph* paragraph ) = 0; /* release object */ virtual void release() = 0; /* copy object */ virtual IParagraphs* copy() = 0; /* the properties of paragraphs */ public: /* the paragraph count */ virtual int count() = 0; /* the paragraph item */ virtual IParagraph* item( int nIndex ) = 0; /* the top item */ virtual IParagraph* top() = 0; /* the header item */ virtual IParagraph* header() = 0; }; class IParagraph { /* the methods of paragraph */ public: /* release object */ virtual void release() = 0; /* the properties of paragraph */ public: /* get paragraph sentences */ virtual ISentences* get_sentences() = 0; /* get paragraph style */ virtual IParaStyle* get_style() = 0; }; class ISentences { /* the methods of sentences */ public: /* add sentence into tables */ virtual void add( ISentence* sentence ) = 0; /* release object */ virtual void release() = 0; /* the properties of paragraphs */ public: /* the sentence count */ virtual int count() = 0; /* the sentence item */ virtual ISentence* item( int nIndex ) = 0; }; class ISentence { /* the methods of sentences */ public: /* release object */ virtual void release() = 0; /* the properties of paragraphs */ public: /* content property */ virtual char* get_content() = 0; virtual void set_content(char* content) = 0; /* sentence style property */ virtual ISentStyle* get_style() = 0; virtual void set_style( ISentStyle* style ) = 0; /* sentence size */ virtual SIZE& get_size() = 0; };  ii.实现说明,对应IParser接口的实现对象是XParser,实现主要用到几个数据结构: stack mstk 这个栈主要是为了处理{}配对用的,当栈空的时候表示流分析完成 stack mstatus 这个栈表示当前{}的属性是什么举个例子{\fonttbl{\f0\fnil\fprq2\fcharset134 Times New Roman;}},当看到第一个{时压入属性fonttbl,当碰到第二个{时压入属性f,在栈中直到碰到}时再弹出,通过判断栈顶来确定当前的要处理的属性。  iii.主要代码说明,最主要的是代码解析部份的代码,主要对这部份代码做一个说明 int XParser::parse( char* stream ) { char* psz = stream; /* parse the rtf stream */ while( psz ) { switch( *psz ) { case '{': { /* push the '{' into stack and pop it find '}' */ mstk.push(*psz); psz++; /* get main key word and push it into stack */ ErrorCode nErrNum; long nOff = 0; if( ( nErrNum = (ErrorCode)parsestatus(psz,nOff) ) != ec_OK ) return nErrNum; psz += nOff; /* move stream pointer*/ break; } case '}': { /* if stack overflow donothing */ if( !mstk.size() ) return ec_StackOverflow; /* pair '{' and pop it */ mstk.pop(); /* here trace debug info*/ tracedebug(); /* pop status */ mstatus.pop(); /* parse is finished */ if( !mstk.size() ) return ec_OK; /* move stream pointer*/ psz++; break; } case '\\': { if( isbreak( *psz , *(psz+1) ) ) { ErrorCode nErrNum; long nOff = 0; if( ( nErrNum = (ErrorCode)parseproperty(psz,nOff) ) != ec_OK ) return nErrNum; psz += nOff; } else { ErrorCode nErrNum; long nOff = 0; if( ( nErrNum = (ErrorCode)parsedefault(psz,nOff) ) != ec_OK ) return nErrNum; psz += nOff; } break; } default: { ErrorCode nErrNum; long nOff = 0; if( ( nErrNum = (ErrorCode)parsedefault(psz,nOff) ) != ec_OK ) return nErrNum; psz += nOff; break; } } } return ec_OK; } 这里要说明的因为文字内容中存在\{}这些特殊的字符,这些字符在RTF有特殊的意义,所以当碰到\字符时要向前再看一个字符,以确定是不后面跟着的是一个属性字符串,这就是isbreak( *psz , *(psz+1) )这句要做的事情,如果是就当属性流做处理,如果不是就当成转义字符串做处理。 (4)其它  i.关于字FontStyle到LOGFONT的转换,因为RTF中的字符的大小用的是halt-points 做单位要想生成相关LOGFONT中的lfHeight要做转换代码如下: HDC hdc = GetDC(NULL); LONG yPerInch = ::GetDeviceCaps( hdc , LOGPIXELSY ); ::ReleaseDC(NULL,hdc); mlogfnt.lfHeight = style->mfntsize*yPerInch/144; 这是参考CFontDialog中一个函数实现的FillInLogFont  ii.代码是用两天时间改写的,写完觉得如果用Lex & Yacc的话可能会写得更安全些。 iii.对于Word保存的RTF文档,这儿没有做处理,它的文本内容保存在不同的属性中,如保存title,bookmark中 所有代码可以从 http://www.xppt.com/code.rar 下载,如果代码有错误请回信:[email protected]

本文地址:http://com.8s8s.com/it/it26704.htm