<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhoneGeek 爱疯极客 &#187; 跨平台</title>
	<atom:link href="http://www.iphone-geek.cn/tag/%e8%b7%a8%e5%b9%b3%e5%8f%b0/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Thu, 08 Dec 2011 01:18:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>跨平台代码分享之七 &#8211; 一个简单的音效引擎</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%83-%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e9%9f%b3%e6%95%88%e5%bc%95%e6%93%8e</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%83-%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e9%9f%b3%e6%95%88%e5%bc%95%e6%93%8e#comments</comments>
		<pubDate>Wed, 06 Jan 2010 06:18:19 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[基础]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[音频]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[OpenAL]]></category>
		<category><![CDATA[开源项目]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=727</guid>
		<description><![CDATA[游戏都需要一个声音引擎，一般来说，有三种类型：

播放音效。音效一般是比较简短的声音文件。
播放背景音乐。
乐器，音效模拟。

&#160;
本文介绍一个简单跨平台的音效引擎，主要针对第一种类型。实际上，也可以适用于第二种类型，但由于背景音乐文件较大，占用内存较大，所以本文介绍的方法并不是最为有效率的（特别是对于像iPhone之类内存较为紧张的平台，但仍然可用）。可以在本引擎的基础上加以改进，比如采用“流”（streaming）技术。而第三种类型，可能涉及到DSP（数字处理技术），比较复杂，另外在不同平台有一些不同的技术，如Windows下的VST/VSTi，Mac OSX下的AU等，不在本文的讨论范围。
&#160;
本文介绍的音效引擎是基于OpenAL的，并且是我在许多项目中使用的引擎的简化部分（去除错误处理及乐器模拟部分）。
&#160;
有关OpenAL的使用，网上有许多教程，比如：OpenAL编程教程等。iPhone上的OpenAL使用，本网站也有几篇文章。所以这里不再赘述。
&#160;
本引擎的目的，是简化声音播放接口。最简单的情况下可以仅仅使用两条语句，就播放一段音效。而繁复的OpenAL初始化功能等都由引擎自动完成。下例展示了怎样播放一段音效的最为典型的调用：
&#160;
123audio::CSound sound&#40;&#34;sound.wav&#34;&#41;;

sound.play&#40;&#41;;
&#160;
本引擎由三个类和一个公共函数构成。audio::CEngine是引擎的核心部分，它是一个单例类（Singleton)，负责直接与OpenAL接口，用户无需与其打交道。audio::CBuffer类对应于OpenAL中的缓存，通过audio::openAudioData函数打开音效文件，创建内部缓存，用户也无需直接使用。audio::CSound是用户直接使用的类，它提供了诸如play（），stop（），setVolume（）和setPosition（）几个接口函数。如需扩展功能，添加一些其他功能，可以直接加在此类中，并在audio::CEngine中实现。
&#160;
以上三个类可以直接使用在不同平台中。与平台相关的代码，都在audio::openAudioData()中。它的作用是打开不同的声音文件。对于Windows，我使用了一个开源项目库libsndfile，用来打开诸如wav，caf，aiff，flac，ogg等常用音效文件（注意：mp3并未包括其中。由于使用mp3的软件需要支付一定许可证费用，本人也不喜欢使用）。而对于Mac OSX（iPhone），则使用了core audio。这里大家可能会有一个疑问，既然libsndfile也是跨平台的，为什么在iPhone上不使用它呢？确实，libsndfile支持Windows，Linux，Mac OSX，Sun Solaris等平台，而iPhone并不支持ogg等格式，使用libsndfile不正好填补这个空白？是的，我也是这样想的，但是，有两点原因初始我暂时没有使用它：

没有现成的iPhone库。由于libsndfile需要使用makefile由GCC编译（我曾试过使用Xcode编译，没有成功），我也不是这方面的专家。但是，libsndfile在Mac OSX下编译没有问题，所以只需稍作修改，在为iPhone进行编译也不是没有可能的事情。
由于其许可证的限制。libsndfile是基于LGPL的，它要求使用者采用动态连接库，而众所周知，iPhone（官方）是不支持动态链接的。如果使用静态链接，则需要随软件发布源代码或目标代码。其实，这也不是无法克服的，LGPL只要求发布与libsnd相关的目标代码，所以对你的软件并无影响。对吗？

&#160;
本文给出了Windows下和iPhone下的应用。在Windows下使用需要下载OpenAL SDK和libsndfile。为方便编译好的libsndfile随示例程序给出给出。（你需要修改一下VS 项目文件中的有关OpenAL的头文件和库文件路径，另外，在OpenAL include目录中创建一个OpenAL目录，并将.h复制到其下，这样我就不需要修改#include语句了。）
&#160;
示例程序audiolib下载。（项目在test目录下）
]]></description>
			<content:encoded><![CDATA[<p>游戏都需要一个声音引擎，一般来说，有三种类型：</p>
<ol>
<li>播放音效。音效一般是比较简短的声音文件。</li>
<li>播放背景音乐。</li>
<li>乐器，音效模拟。</li>
</ol>
<p>&nbsp;</p>
<p>本文介绍一个简单跨平台的音效引擎，主要针对第一种类型。实际上，也可以适用于第二种类型，但由于背景音乐文件较大，占用内存较大，所以本文介绍的方法并不是最为有效率的（特别是对于像iPhone之类内存较为紧张的平台，但仍然可用）。可以在本引擎的基础上加以改进，比如采用“流”（streaming）技术。而第三种类型，可能涉及到DSP（数字处理技术），比较复杂，另外在不同平台有一些不同的技术，如Windows下的VST/VSTi，Mac OSX下的AU等，不在本文的讨论范围。</p>
<p>&nbsp;</p>
<p>本文介绍的音效引擎是基于OpenAL的，并且是我在许多项目中使用的引擎的简化部分（去除错误处理及乐器模拟部分）。</p>
<p>&nbsp;</p>
<p>有关OpenAL的使用，网上有许多教程，比如：<a href="http://www.devmaster.net/articles/openal-tutorials/lesson1.php">OpenAL编程教程</a>等。iPhone上的OpenAL使用，本网站也有几篇文章。所以这里不再赘述。</p>
<p>&nbsp;</p>
<p>本引擎的目的，是简化声音播放接口。最简单的情况下可以仅仅使用两条语句，就播放一段音效。而繁复的OpenAL初始化功能等都由引擎自动完成。下例展示了怎样播放一段音效的最为典型的调用：</p>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">audio<span style="color: #008080;">::</span><span style="color: #007788;">CSound</span> sound<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;sound.wav&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
sound.<span style="color: #007788;">play</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>本引擎由三个类和一个公共函数构成。audio::CEngine是引擎的核心部分，它是一个单例类（Singleton)，负责直接与OpenAL接口，用户无需与其打交道。audio::CBuffer类对应于OpenAL中的缓存，通过audio::openAudioData函数打开音效文件，创建内部缓存，用户也无需直接使用。audio::CSound是用户直接使用的类，它提供了诸如play（），stop（），setVolume（）和setPosition（）几个接口函数。如需扩展功能，添加一些其他功能，可以直接加在此类中，并在audio::CEngine中实现。</p>
<p>&nbsp;</p>
<p>以上三个类可以直接使用在不同平台中。与平台相关的代码，都在audio::openAudioData()中。它的作用是打开不同的声音文件。对于Windows，我使用了一个开源项目库<a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a>，用来打开诸如wav，caf，aiff，flac，ogg等常用音效文件（<strong>注意：mp3并未包括其中。由于使用mp3的软件需要支付一定许可证费用，本人也不喜欢使用</strong>）。而对于Mac OSX（iPhone），则使用了core audio。这里大家可能会有一个疑问，既然libsndfile也是跨平台的，为什么在iPhone上不使用它呢？确实，libsndfile支持Windows，Linux，Mac OSX，Sun Solaris等平台，而iPhone并不支持ogg等格式，使用libsndfile不正好填补这个空白？是的，我也是这样想的，但是，有两点原因初始我暂时没有使用它：</p>
<ol>
<li>没有现成的iPhone库。由于libsndfile需要使用makefile由GCC编译（我曾试过使用Xcode编译，没有成功），我也不是这方面的专家。但是，libsndfile在Mac OSX下编译没有问题，所以只需稍作修改，在为iPhone进行编译也不是没有可能的事情。</li>
<li>由于其许可证的限制。libsndfile是基于LGPL的，它要求使用者采用动态连接库，而众所周知，iPhone（官方）是不支持动态链接的。<strong>如果使用静态链接，则需要随软件发布源代码或目标代码。</strong>其实，这也不是无法克服的，LGPL只要求发布与libsnd相关的目标代码，所以对你的软件并无影响。对吗？</li>
</ol>
<p>&nbsp;</p>
<p>本文给出了Windows下和iPhone下的应用。在Windows下使用需要下载<a href="http://connect.creativelabs.com/openal/Downloads/oalinst.zip">OpenAL SDK</a>和<a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a>。为方便编译好的libsndfile随示例程序给出给出。（你需要修改一下VS 项目文件中的有关OpenAL的头文件和库文件路径，另外，在OpenAL include目录中创建一个OpenAL目录，并将.h复制到其下，这样我就不需要修改#include语句了。）</p>
<p>&nbsp;</p>
<p>示例程序<a href="../wp-content/uploads/2010/01/audiolib.zip">audiolib</a>下载。（项目在test目录下）<a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/audiolib.zip"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%83-%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e9%9f%b3%e6%95%88%e5%bc%95%e6%93%8e/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Poco C++ for iPhone之一 &#8211; 编译</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/poco-c-for-iphone%e4%b9%8b%e4%b8%80-%e7%bc%96%e8%af%91</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/poco-c-for-iphone%e4%b9%8b%e4%b8%80-%e7%bc%96%e8%af%91#comments</comments>
		<pubDate>Tue, 29 Dec 2009 02:39:58 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[基础]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[开源项目]]></category>
		<category><![CDATA[源代码]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=554</guid>
		<description><![CDATA[Poco C++是一个强大的跨平台C++库，你可以在iPhone程序中使用它，如果：

你打算使你的程序跨平台。
你喜欢使用C++或希望使用Poco C++中的功能如：XML，Zip等。

&#160;
目前，最新Poco C++版本为1.3.6P1，根据文档，它可以编译为iPhone静态库。但是我试用了一下，无法使用在iPhone Xcode项目中链接，不知何故。所以我自己创建了下列Xcode项目：

Poco Foundation
Poco Util
Poco Net
Poco XML
Poco Zip

&#160;
另外，由于Crypto，Data，NetSSL_OpenSSL库需要外部库而且目前我不需要使用，所以没有包括。另外我增加了一个目录iPhoneSamples，它目前包括了一个示例程序HelloPoco，演示了怎样在iPhone项目中使用Poco C++库（此演示程序十分简单，它调用Poco中的UUID功能，产生一个UUID，显示与屏幕上）。下面是包括了此展示程序以及各Xcode文件的Poco包的下载：
&#160;
poco-1.3.6p1
]]></description>
			<content:encoded><![CDATA[<p>Poco C++是一个强大的跨平台C++库，你可以在iPhone程序中使用它，如果：</p>
<ol>
<li>你打算使你的程序跨平台。</li>
<li>你喜欢使用C++或希望使用Poco C++中的功能如：XML，Zip等。</li>
</ol>
<p>&nbsp;</p>
<p>目前，最新Poco C++版本为1.3.6P1，根据文档，它可以编译为iPhone静态库。但是我试用了一下，无法使用在iPhone Xcode项目中链接，不知何故。所以我自己创建了下列Xcode项目：</p>
<ul>
<li>Poco Foundation</li>
<li>Poco Util</li>
<li>Poco Net</li>
<li>Poco XML</li>
<li>Poco Zip</li>
</ul>
<p>&nbsp;</p>
<p>另外，由于Crypto，Data，NetSSL_OpenSSL库需要外部库而且目前我不需要使用，所以没有包括。另外我增加了一个目录iPhoneSamples，它目前包括了一个示例程序HelloPoco，演示了怎样在iPhone项目中使用Poco C++库（此演示程序十分简单，它调用Poco中的UUID功能，产生一个UUID，显示与屏幕上）。下面是包括了此展示程序以及各Xcode文件的Poco包的下载：</p>
<p>&nbsp;</p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2009/12/poco-1.3.6.zip">poco-1.3.6p1</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/poco-c-for-iphone%e4%b9%8b%e4%b8%80-%e7%bc%96%e8%af%91/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之六 &#8211; 使用minizip进行解压</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e5%85%ad-%e4%bd%bf%e7%94%a8minizip%e8%bf%9b%e8%a1%8c%e8%a7%a3%e5%8e%8b</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e5%85%ad-%e4%bd%bf%e7%94%a8minizip%e8%bf%9b%e8%a1%8c%e8%a7%a3%e5%8e%8b#comments</comments>
		<pubDate>Wed, 23 Dec 2009 10:56:03 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[基础]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[压缩解压]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=451</guid>
		<description><![CDATA[压缩和解压技术应用很广泛，比如游戏程序中的资源管理器可以使用压缩技术将许多文件打包，便于管理，也节约了空间。本文介绍使用minizip进行解压。由于可以使用zip工具比如winzip，betterzip等进行压缩，所以本文并未涉及压缩。
&#160;

minizip++
&#160;
我编写了一个简单的类，与minizip和zlib一起（实际上，Mac OSX中包括了zlib库，但其他平台不一定有zlib库，所以我将其包括在源码中），构成了minizip++。此类仅包含一个类zip::CReader，此类只包括一个函数openFile用于从zip文件中读取一个文件。
&#160;
范例概述
&#160;
本示例程序很简单，它从data.zip文件中提取data/hello.txt显示于屏幕上，有兴趣的话，你可以扩展此程序，从zip中提取图像然后显示出来。
&#160;
废话少说，下面介绍程序：

1. 先建立一个View based Xcode项目ziptest，再创建一个目录data，在此目录下建立一个文本文件hello.txt，编辑此文本文件，加入你想在屏幕上显示的内容，比如，我在hello.txt中加入“Hello World！”。压缩data目录，然后将压缩文件data.zip加入Xcode的resource。（注意：此时加入的zip文件将出现在Targets下的“Link Binary With Libraries”下，编译时会出现一个警告，zip文件将不会被加入到.app中。所以你应该将data.zip移动到&#8221;Copy Bundle Resources&#8221;下）

2. ziptestViewController.h部分如下：
12345@interface ziptestViewController : UIViewController &#123;
&#160; &#160; UILabel* _mytext;
&#125;

@property &#40;nonatomic,retain&#41; IBOutlet UILabel* _mytext;
3. 将ziptestViewController.m重命名为ziptestViewController.mm，部分代码如下：

4. 双击ziptestViewController.xib打开Interface Builder。添加一个Label。Control+点击File Owner拖动到新建的Label，选取_mytext。

5.然后加入header和library搜索路径。比如，我的Header Search Paths为：../../external/minizip++。然后按iphone项目使用静态库的最佳方法一文中介绍的方法加入ziptest项目。
6.在viewDidLoad下进入下列代码：
12345&#160; &#160; std::stringstream in;
&#160; &#160; zip::CReader reader;
&#160; &#160; NSString* zippath=&#91;&#91;&#91;NSBundle mainBundle&#93;resourcePath&#93;stringByAppendingPathComponent:@&#34;data.zip&#34;&#93;;
&#160; &#160; reader.openFile&#40;&#91;ziptestViewController getString:zippath&#93;,&#34;data/hello.txt&#34;,in&#41;;
&#160; &#160; _mytext.text = &#160;&#91;NSString stringWithCString:in.str&#40;&#41;.c_str&#40;&#41;&#93;;
6. 按Build and Go运行程序。
&#160;
minizip++中CReader类中用于解压的代码如下：
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970namespace zip
&#123;
&#160; &#160; 
&#160; &#160; void [...]]]></description>
			<content:encoded><![CDATA[<p>压缩和解压技术应用很广泛，比如游戏程序中的资源管理器可以使用压缩技术将许多文件打包，便于管理，也节约了空间。本文介绍使用minizip进行解压。由于可以使用zip工具比如winzip，betterzip等进行压缩，所以本文并未涉及压缩。</p>
<p>&nbsp;</p>
<p><span id="more-451"></span></p>
<h2>minizip++</h2>
<p>&nbsp;</p>
<p>我编写了一个简单的类，与minizip和zlib一起（实际上，Mac OSX中包括了zlib库，但其他平台不一定有zlib库，所以我将其包括在源码中），构成了minizip++。此类仅包含一个类zip::CReader，此类只包括一个函数openFile用于从zip文件中读取一个文件。</p>
<p>&nbsp;</p>
<h2>范例概述</h2>
<p>&nbsp;</p>
<p>本示例程序很简单，它从data.zip文件中提取data/hello.txt显示于屏幕上，有兴趣的话，你可以扩展此程序，从zip中提取图像然后显示出来。</p>
<p>&nbsp;</p>
<p>废话少说，下面介绍程序：</p>
<ul>
<li>1. 先建立一个View based Xcode项目ziptest，再创建一个目录data，在此目录下建立一个文本文件hello.txt，编辑此文本文件，加入你想在屏幕上显示的内容，比如，我在hello.txt中加入“Hello World！”。压缩data目录，然后将压缩文件data.zip加入Xcode的resource。（注意：此时加入的zip文件将出现在Targets下的“Link Binary With Libraries”下，编译时会出现一个警告，zip文件将不会被加入到.app中。所以你应该将data.zip移动到&#8221;Copy Bundle Resources&#8221;下）
</li>
<li>2. ziptestViewController.h部分如下：
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> ziptestViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UILabel<span style="color: #002200;">*</span> _mytext;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> IBOutlet UILabel<span style="color: #002200;">*</span> _mytext;</div></td></tr></tbody></table></div>
<li>3. 将ziptestViewController.m重命名为ziptestViewController.mm，部分代码如下：
</li>
<li>4. 双击ziptestViewController.xib打开Interface Builder。添加一个Label。Control+点击File Owner拖动到新建的Label，选取_mytext。</li>
</li>
<li>5.然后加入header和library搜索路径。比如，我的Header Search Paths为：../../external/minizip++。然后按<a href="编程/iphone项目使用静态库的最佳方法">iphone项目使用静态库的最佳方法</a>一文中介绍的方法加入ziptest项目。</li>
<li>6.在viewDidLoad下进入下列代码：</li>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; std<span style="color: #002200;">::</span>stringstream <span style="color: #a61390;">in</span>;<br />
&nbsp; &nbsp; zip<span style="color: #002200;">::</span>CReader reader;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> zippath<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/"><span style="color: #400080;">NSBundle</span></a> mainBundle<span style="color: #002200;">&#93;</span>resourcePath<span style="color: #002200;">&#93;</span>stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data.zip&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; reader.openFile<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>ziptestViewController getString<span style="color: #002200;">:</span>zippath<span style="color: #002200;">&#93;</span>,<span style="color: #bf1d1a;">&quot;data/hello.txt&quot;</span>,<span style="color: #a61390;">in</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; _mytext.text <span style="color: #002200;">=</span> &nbsp;<span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithCString<span style="color: #002200;">:</span><span style="color: #a61390;">in</span>.str<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>.c_str<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<li>6. 按Build and Go运行程序。</li>
<p>&nbsp;</p>
<p>minizip++中CReader类中用于解压的代码如下：</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">namespace</span> zip<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CReader<span style="color: #008080;">::</span><span style="color: #007788;">openFile</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> zip,<span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> file,std<span style="color: #008080;">::</span><span style="color: #007788;">iostream</span><span style="color: #000040;">&amp;</span> in<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; unzFile uf<span style="color: #000080;">=</span><span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// open zip file</span><br />
<span style="color: #339900;">#ifdef USEWIN32IOAPI</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;zlib_filefunc_def ffunc<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fill_win32_filefunc<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>ffunc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uf <span style="color: #000080;">=</span> unzOpen2<span style="color: #008000;">&#40;</span>zip.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #000040;">&amp;</span>ffunc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; uf <span style="color: #000080;">=</span> unzOpen<span style="color: #008000;">&#40;</span>zip.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#endif</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>uf<span style="color: #000080;">==</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;No zip file found!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// find the file inside zip &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> unzLocateFile<span style="color: #008000;">&#40;</span>uf,file.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,0<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> UNZ_OK <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%s not found in zip<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,file.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// file founded,extract it into the stream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// open it </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> unzOpenCurrentFile<span style="color: #008000;">&#40;</span>uf<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> UNZ_OK <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: error handling</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;unzOpenCurrentFile error<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// get the file info</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">char</span> filename_inzip<span style="color: #008000;">&#91;</span>256<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> err<span style="color: #000080;">=</span>UNZ_OK<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; unz_file_info file_info<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; err <span style="color: #000080;">=</span> unzGetCurrentFileInfo<span style="color: #008000;">&#40;</span>uf,<span style="color: #000040;">&amp;</span>file_info,filename_inzip,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>filename_inzip<span style="color: #008000;">&#41;</span>,<span style="color: #0000ff;">NULL</span>,0,<span style="color: #0000ff;">NULL</span>,0<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>err<span style="color: #000040;">!</span><span style="color: #000080;">=</span>UNZ_OK<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: error handling</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;error %d with zipfile in unzGetCurrentFileInfo<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,err<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// read the file into stream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #008000;">&#40;</span>file_info.<span style="color: #007788;">uncompressed_size</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> unzReadCurrentFile<span style="color: #008000;">&#40;</span>uf,buf,file_info.<span style="color: #007788;">uncompressed_size</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: error handling</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;error in unzReadCurrentFile!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">free</span><span style="color: #008000;">&#40;</span>buf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// write to istream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; in.<span style="color: #007788;">rdbuf</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sputn<span style="color: #008000;">&#40;</span>buf,file_info.<span style="color: #007788;">uncompressed_size</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">free</span><span style="color: #008000;">&#40;</span>buf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; unzCloseCurrentFile<span style="color: #008000;">&#40;</span>uf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; unzClose<span style="color: #008000;">&#40;</span>uf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h2><strong>源码下载</strong></h2>
<p>&nbsp;</p>
<p><a href='http://www.iphone-geek.cn/wp-content/uploads/2009/12/ziptest.zip'>ziptest</a></p>
<p>&nbsp;</p>
<p>注意：编译时，你需要修改头文件路径或者按照我的路径解压源代码。我的路径如下：</p>
<p>&nbsp;</p>
<p><quoteblock><br />
root<br />
  |&#8212;&#8212; external<br />
                 |&#8212;&#8212;&#8212;&#8211; minizip++</p>
<p>  |&#8212;&#8212; test<br />
                 |&#8212;&#8212;&#8212;&#8211; ziptest<br />
</quoteblock></p>
<p>&nbsp;</p>
<p><a href='http://www.iphone-geek.cn/wp-content/uploads/2009/12/minizip++.zip'>minizip++</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e5%85%ad-%e4%bd%bf%e7%94%a8minizip%e8%bf%9b%e8%a1%8c%e8%a7%a3%e5%8e%8b/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之五 &#8211; 字符串转换</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%94-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%94-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2#comments</comments>
		<pubDate>Tue, 22 Dec 2009 02:20:33 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[基础]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=494</guid>
		<description><![CDATA[严格地说，本文主题与跨平台代码无关。但是由于在iPhone上，通常界面程序还是由Objective-C编写的，经常需要用到STL string和NSString之间的转换。下面是转换的代码：
&#160;
NSString -&#62; std::string
12345678910+&#40;std::string&#41;getString:&#40;NSString*&#41;theString
&#123;
&#160; &#160; int size = &#91;theString length&#93;*sizeof&#40;char&#41;;
&#160; &#160; char *buffer = &#40;char*&#41; malloc&#40;size+1&#41;;
&#160; &#160; memset&#40;buffer, 0, size+1&#41;;
&#160; &#160; &#91;theString getCString:buffer maxLength:size+1 encoding:NSASCIIStringEncoding&#93;;
&#160; &#160; std::string str&#40;buffer,size&#41;;
&#160; &#160; free&#40;buffer&#41;;
&#160; &#160; return str;
&#125;
&#160;
std::string -> NSString
1234+&#40;NSString*&#41;getNSString:&#40;const std::string&#38;&#41;theString
&#123;
&#160; &#160; return &#91;NSString stringWithCString:theString.c_str&#40;&#41;&#93;;
&#125;
]]></description>
			<content:encoded><![CDATA[<p>严格地说，本文主题与跨平台代码无关。但是由于在iPhone上，通常界面程序还是由Objective-C编写的，经常需要用到STL string和NSString之间的转换。下面是转换的代码：</p>
<p>&nbsp;</p>
<p><H3><strong>NSString -&gt; std::string</strong></H3></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>std<span style="color: #002200;">::</span><span style="color: #a61390;">string</span><span style="color: #002200;">&#41;</span>getString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> size <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>theString length<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/sizeof.html"><span style="color: #a61390;">sizeof</span></a><span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>buffer <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html"><span style="color: #a61390;">malloc</span></a><span style="color: #002200;">&#40;</span>size<span style="color: #002200;">+</span>1<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/memset.html"><span style="color: #a61390;">memset</span></a><span style="color: #002200;">&#40;</span>buffer, 0, size<span style="color: #002200;">+</span>1<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theString getCString<span style="color: #002200;">:</span>buffer maxLength<span style="color: #002200;">:</span>size<span style="color: #002200;">+</span>1 encoding<span style="color: #002200;">:</span>NSASCIIStringEncoding<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; std<span style="color: #002200;">::</span><span style="color: #a61390;">string</span> str<span style="color: #002200;">&#40;</span>buffer,size<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/free.html"><span style="color: #a61390;">free</span></a><span style="color: #002200;">&#40;</span>buffer<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> str;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p><H3><strong>std::string -> NSString</strong></H3></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>getNSString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> std<span style="color: #002200;">::</span>string<span style="color: #002200;">&amp;</span><span style="color: #002200;">&#41;</span>theString<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithCString<span style="color: #002200;">:</span>theString.c_str<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%94-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之四 – 开源跨平台C++库介绍</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e5%9b%9b-%e2%80%93-%e5%bc%80%e6%ba%90%e8%b7%a8%e5%b9%b3%e5%8f%b0c%e5%ba%93%e4%bb%8b%e7%bb%8d</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e5%9b%9b-%e2%80%93-%e5%bc%80%e6%ba%90%e8%b7%a8%e5%b9%b3%e5%8f%b0c%e5%ba%93%e4%bb%8b%e7%bb%8d#comments</comments>
		<pubDate>Wed, 16 Dec 2009 08:09:59 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[基础]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[开源项目]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=439</guid>
		<description><![CDATA[做项目时发现一个开源C++库，称为POCO（POrtable COmponents &#8211; 可移植元件），非常方便好用。
&#160;
特性：
&#160;
    * 线程，程序同步及多线程编程高级抽象
    * 流及文件系统访问
    * 共享库将类加载
    * 功能强大的日志和错误报告
    * 安全及加密
    * 网络编程 (TCP/IP 套接字, HTTP客户端和HTTP服务器, FTP, SMTP, POP3, 等)
    * XML解析 (SAX2 和 DOM) 及生成
    * 配置文件及选项处理
 [...]]]></description>
			<content:encoded><![CDATA[<p>做项目时发现一个开源C++库，称为<a href="http://pocoproject.org/">POCO</a>（POrtable COmponents &#8211; 可移植元件），非常方便好用。</p>
<p>&nbsp;</p>
<h2>特性：</h2>
<p>&nbsp;</p>
<p>    * 线程，程序同步及多线程编程高级抽象<br />
    * 流及文件系统访问<br />
    * 共享库将类加载<br />
    * 功能强大的日志和错误报告<br />
    * 安全及加密<br />
    * 网络编程 (TCP/IP 套接字, HTTP客户端和HTTP服务器, FTP, SMTP, POP3, 等)<br />
    * XML解析 (SAX2 和 DOM) 及生成<br />
    * 配置文件及选项处理<br />
    * SQL数据库访问(ODBC, MySQL, SQLite)</p>
<p>&nbsp;</p>
<h2>可以运行的平台包括：</h2>
<p>&nbsp;</p>
<p>    * Windows<br />
    * Mac OS X<br />
    * iPhone OS<br />
    * (embedded) Linux<br />
    * HP-UX<br />
    * Tru64<br />
    * Solaris<br />
    * QNX</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e5%9b%9b-%e2%80%93-%e5%bc%80%e6%ba%90%e8%b7%a8%e5%b9%b3%e5%8f%b0c%e5%ba%93%e4%bb%8b%e7%bb%8d/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之三 – XML解析器</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%89-%e2%80%93-xml%e8%a7%a3%e6%9e%90%e5%99%a8</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%89-%e2%80%93-xml%e8%a7%a3%e6%9e%90%e5%99%a8#comments</comments>
		<pubDate>Wed, 25 Nov 2009 03:21:57 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[XML解析器]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=318</guid>
		<description><![CDATA[XML格式使用越来越广泛，不论是游戏还是普通应用软件都有使用。而我喜欢用xml作为配置文件。XML解析的方法实在很多，最根本的两种是SAX和DOM。SAX需要一边读取文件一边解析，速度较快；而DOM采用树状结构保存解析文件，使用方便。iPone自带的XML解析器一则无法跨平台，一则使用不便，还有一些bug（我是指SDK 2.0，新版本SDK 3.0的XML解析器没用过，所以没有发言权），所以我决定使用其他开源XML解析器。开源的XML解析器也有很多种，比如libxml2，TouchXML（iPhone版），tinyxml等。其中tinyxml简单易用，如果你不要求诸如DTD等功能，那么tinyxml绝对应该是你的首选。我采用的是ticpp（tinyxml的cpp版本，采用DOM方法）。
&#160;

项目下载
&#160;
下面是ticpp的Xcode项目下载。
&#160;
（注：虽然我讨论的是跨平台代码，但我这里只是提供了Xcode项目，要在Windows下使用也是十分简单的事情，你可以自创Visual Studio项目文件或使用GNU make）
&#160;
使用方法
&#160;
这里我给出一个xml文件示例：
123456&#60;options version=&#34;1.0&#34;&#62;
&#60;levels&#62;
&#160; &#160; &#60;level name=&#34;InTheBeginning&#34; enemy=&#34;10&#34; /&#62;
&#160; &#160; &#60;level name=&#34;Fighting&#34; enemy=&#34;100&#34; /&#62; &#160; &#160;
&#60;/levels&#62;
&#60;/options&#62;
&#160;
首先，我定义了一个XML解析器类，它定义了XML解析的接口，我使用ticpp，但是如果你更改XML解析器为比如libxml2，你只需更改类的实现部分，你运用XML解析器的程序代码仍然可以保持不变。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174//////////////////////// 头文件 ///////////////////////////////////
/*
&#160;* &#160;xmlparser.h
&#160;* &#160;utils
&#160;*
&#160;* &#160;Created by Bagusflyer on 09-6-27.
&#160;* &#160;Copyright 2009 www.iphone-geek.cn. All rights reserved.
&#160;*
&#160;*/

#ifndef _UTILS_XMLPARSER_H_
#define _UTILS_XMLPARSER_H_

#include &#60;fstream&#62;
#include &#34;ticpp/ticpp.h&#34;
&#160; &#160; 
typedef ticpp::Element XmlNode;
typedef ticpp::Document XmlDocument; 

using namspace std；

namespace utils
&#123;

&#160; &#160; class CXmlParser
&#160; &#160; &#123;
&#160; &#160; public:
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>XML格式使用越来越广泛，不论是游戏还是普通应用软件都有使用。而我喜欢用xml作为配置文件。XML解析的方法实在很多，最根本的两种是SAX和DOM。SAX需要一边读取文件一边解析，速度较快；而DOM采用树状结构保存解析文件，使用方便。iPone自带的XML解析器一则无法跨平台，一则使用不便，还有一些bug（我是指SDK 2.0，新版本SDK 3.0的XML解析器没用过，所以没有发言权），所以我决定使用其他开源XML解析器。开源的XML解析器也有很多种，比如libxml2，TouchXML（iPhone版），tinyxml等。其中tinyxml简单易用，如果你不要求诸如DTD等功能，那么tinyxml绝对应该是你的首选。我采用的是ticpp（tinyxml的cpp版本，采用DOM方法）。</p>
<p>&nbsp;</p>
<p><span id="more-318"></span></p>
<h2>项目下载</h2>
<p>&nbsp;</p>
<p>下面是ticpp的Xcode项目<a href='http://www.iphone-geek.cn/wp-content/uploads/2009/11/ticpp.zip'>下载</a>。</p>
<p>&nbsp;</p>
<p>（注：虽然我讨论的是跨平台代码，但我这里只是提供了Xcode项目，要在Windows下使用也是十分简单的事情，你可以自创Visual Studio项目文件或使用GNU make）</p>
<p>&nbsp;</p>
<h2>使用方法</h2>
<p>&nbsp;</p>
<p>这里我给出一个xml文件示例：</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;options</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;levels<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;InTheBeginning&quot;</span> <span style="color: #000066;">enemy</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Fighting&quot;</span> <span style="color: #000066;">enemy</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> &nbsp; &nbsp;<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/levels<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/options<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>首先，我定义了一个XML解析器类，它定义了XML解析的接口，我使用ticpp，但是如果你更改XML解析器为比如libxml2，你只需更改类的实现部分，你运用XML解析器的程序代码仍然可以保持不变。</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">//////////////////////// 头文件 ///////////////////////////////////</span><br />
<span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;xmlparser.h<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-27.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#ifndef _UTILS_XMLPARSER_H_</span><br />
<span style="color: #339900;">#define _UTILS_XMLPARSER_H_</span><br />
<br />
<span style="color: #339900;">#include &lt;fstream&gt;</span><br />
<span style="color: #339900;">#include &quot;ticpp/ticpp.h&quot;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #0000ff;">typedef</span> ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Element</span> XmlNode<span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">typedef</span> ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Document</span> XmlDocument<span style="color: #008080;">;</span> <br />
<br />
<span style="color: #0000ff;">using</span> namspace std；<br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CXmlParser<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CXmlParser<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>m_pRootNode<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> parse<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> file,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> parse<span style="color: #008000;">&#40;</span>ifstream<span style="color: #000040;">&amp;</span> stream,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> parse<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf, <span style="color: #0000ff;">size_t</span> len, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootNode<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> getAttribute<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib, <span style="color: #0000ff;">int</span> defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">float</span> getAttributeFloat<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib, <span style="color: #0000ff;">float</span> defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; string getAttribute<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; string getText<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; string getName<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> getFirstChild<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> nodeName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> getFirstChild<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> getNextChild<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,string nodeName<span style="color: #000080;">=</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> rootNode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> m_pRootNode<span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlDocument m_doc<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span>&nbsp; &nbsp; m_pRootNode<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif // _UTILS_XMLPARSER_H_</span><br />
<br />
<span style="color: #666666;">//////////////////////////// 实现文件 ///////////////////////////////</span><br />
<span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;xmlparser.cpp<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-27.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#include &quot;utils/xmlparser.h&quot;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib,<span style="color: #0000ff;">int</span> defval<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">long</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; elem.<span style="color: #007788;">GetAttributeOrDefault</span><span style="color: #008000;">&#40;</span>attrib,<span style="color: #000040;">&amp;</span>value,defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">float</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getAttributeFloat</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib, <span style="color: #0000ff;">float</span> defval<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">float</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; elem.<span style="color: #007788;">GetAttributeOrDefault</span><span style="color: #008000;">&#40;</span>attrib,<span style="color: #000040;">&amp;</span>value,defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; string CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; string value<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; elem.<span style="color: #007788;">GetAttributeOrDefault</span><span style="color: #008000;">&#40;</span>attrib,<span style="color: #000040;">&amp;</span>value,_T<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> file,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">ifstream</span> in<span style="color: #008000;">&#40;</span>file.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>in.<span style="color: #007788;">good</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 注意：错误检查，略去</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">//throw XmlParserError(_T(&quot;Can't open &quot;+file));</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parse<span style="color: #008000;">&#40;</span>in,rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span>ifstream<span style="color: #000040;">&amp;</span> stream,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span style="color: #007788;">seekg</span><span style="color: #008000;">&#40;</span>0,std<span style="color: #008080;">::</span><span style="color: #007788;">ios</span><span style="color: #008080;">::</span><span style="color: #007788;">end</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">size_t</span> len <span style="color: #000080;">=</span> stream.<span style="color: #007788;">tellg</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span style="color: #007788;">seekg</span><span style="color: #008000;">&#40;</span>0,std<span style="color: #008080;">::</span><span style="color: #007788;">ios</span><span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#91;</span>len<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span>buf,len<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>buf,len,rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buf<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Exception</span><span style="color: #000040;">&amp;</span> ex<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buf<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> ex<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf, <span style="color: #0000ff;">size_t</span> len, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootNode<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// Load a document</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> data<span style="color: #008000;">&#40;</span>buf,len<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_doc.<span style="color: #007788;">Parse</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pRootNode <span style="color: #000080;">=</span> m_doc.<span style="color: #007788;">FirstChildElement</span><span style="color: #008000;">&#40;</span>rootNode<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">catch</span><span style="color: #008000;">&#40;</span> ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Exception</span><span style="color: #000040;">&amp;</span> ex <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// If any function has an error, execution will enter here.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// Report the error</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 注意：请使用自己的XmlParser exception类</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// throw XmlParserError(&quot;Failed to parse xml&quot;); </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; string CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getText</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">GetTextOrDefault</span><span style="color: #008000;">&#40;</span>_T<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; string CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getName</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">Value</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> nodeName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">FirstChildElement</span><span style="color: #008000;">&#40;</span>nodeName,<span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">FirstChildElement</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getNextChild</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,string nodeName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> nodeName.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">NextSiblingElement</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">NextSiblingElement</span><span style="color: #008000;">&#40;</span>nodeName,<span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// namespace utils</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>然后，在你的代码（比如我有一个COptionParser，它包含了一个utils::CXmlParser对象m_xmlPaser）中使用XmlParser进行解析：</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">/////////////////// 以下是进行解析的代码 //////////////////////////////</span><br />
<span style="color: #0000ff;">void</span> parser<span style="color: #008000;">&#40;</span>string<span style="color: #000040;">&amp;</span> xmlFile<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_xmlParser.<span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span>xmlFile,<span style="color: #FF0000;">&quot;options&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 解析version属性</span><br />
&nbsp; &nbsp; string version <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>m_xmlParser.<span style="color: #007788;">rootNode</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,<span style="color: #FF0000;">&quot;version&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> pLevelsNode <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>m_xmlParser.<span style="color: #007788;">rootNode</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #FF0000;">&quot;levels&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>pLevelsNode <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 定义自己的exception</span><br />
&nbsp; &nbsp; <span style="color: #666666;">//throw utils::XmlParserError(&quot;No levels found&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pLevelsNode <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 解析每个Level</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> chd <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>pLevelsNode ,<span style="color: #FF0000;">&quot;level&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>chd<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 解析level名称</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string name <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>chd,<span style="color: #FF0000;">&quot;name&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 解析敌人数目，缺省值为15</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> enemies <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span>elem,<span style="color: #FF0000;">&quot;enemy&quot;</span>,15<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 下一个level</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chd <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getNextChild</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>chd,<span style="color: #FF0000;">&quot;level&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>代码虽然不少，可是思路还是很简单的。下一次，我会介绍跨平台压缩库。有什么问题，欢迎留言探讨！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%89-%e2%80%93-xml%e8%a7%a3%e6%9e%90%e5%99%a8/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之二 &#8211; 定时器</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%8c-%e5%ae%9a%e6%97%b6%e5%99%a8</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%8c-%e5%ae%9a%e6%97%b6%e5%99%a8#comments</comments>
		<pubDate>Fri, 20 Nov 2009 03:58:03 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[定时器]]></category>
		<category><![CDATA[源代码]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=278</guid>
		<description><![CDATA[定时器的应用实在是太广了，从普通应用程序到游戏，大部分程序都会用到定时器。如果你打算让你的应用程序跨越平台的限制，一个跨平台的定时器是必不可少的。上一次，我给大家分享了一段有关线程的代码。实际上，定时器的是以线程为基础的，在这里我仍然是使用pthread，当然大家可以直接继承我的utils::CThread类。
&#160;

timer.h：
&#160;
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667/*
&#160;* &#160;timer.h
&#160;* &#160;utils
&#160;*
&#160;* &#160;Created by Bagusflyer on 09-6-2.
&#160;* &#160;Copyright 2009 www.iphone-geek.cn. All rights reserved.
&#160;*
&#160;*/

#ifndef _IPHONEGEEK_UTILS_TIMER_H_
#define _IPHONEGEEK_UTILS_TIMER_H_

#include &#60;pthread.h&#62;

namespace utils
&#123;
&#160; &#160; 
&#160; &#160; class CTimer;
&#160; &#160; 
&#160; &#160; class CTimerCallbackInterface
&#160; &#160; &#123;
&#160; &#160; public:
&#160; &#160; &#160; &#160; virtual void onTimer&#40;const CTimer* timer&#41; = 0;
&#160; &#160; &#125;;

&#160; &#160; class CTimer 
&#160; &#160; &#123;
&#160; &#160; public: &#160; &#160; 
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>定时器的应用实在是太广了，从普通应用程序到游戏，大部分程序都会用到定时器。如果你打算让你的应用程序跨越平台的限制，一个跨平台的定时器是必不可少的。上一次，我给大家分享了一段有关线程的代码。实际上，定时器的是以线程为基础的，在这里我仍然是使用pthread，当然大家可以直接继承我的utils::CThread类。</p>
<p>&nbsp;</p>
<p><span id="more-278"></span></p>
<h2>timer.h：</h2>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;timer.h<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-2.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#ifndef _IPHONEGEEK_UTILS_TIMER_H_</span><br />
<span style="color: #339900;">#define _IPHONEGEEK_UTILS_TIMER_H_</span><br />
<br />
<span style="color: #339900;">#include &lt;pthread.h&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CTimer<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CTimerCallbackInterface<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> onTimer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> CTimer<span style="color: #000040;">*</span> timer<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CTimer <br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">enum</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIMER_STOP&nbsp; <span style="color: #000080;">=</span> 0,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIMER_RUN,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIMER_PAUSE <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span> TimerState<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimer<span style="color: #008000;">&#40;</span>CTimerCallbackInterface<span style="color: #000040;">*</span> callback,<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> interval,<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData,<span style="color: #0000ff;">bool</span> repeat <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> resume<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> pause<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>&nbsp; &nbsp; <span style="color: #666666;">// calling this will destroy CTimer object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> interval<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_ulInterval<span style="color: #008080;">;</span>&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pUserData<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">bool</span> isRepeat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_bRepeat<span style="color: #008080;">;</span> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimerCallbackInterface<span style="color: #000040;">*</span> callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pCallback<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TimerState state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_state<span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimerCallbackInterface<span style="color: #000040;">*</span>&nbsp; &nbsp; m_pCallback<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pUserData<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">bool</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_bRepeat<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_ulInterval<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pthread_t &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_thread<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TimerState&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_state<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> onRun<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; ~CTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif // _IPHONEGEEK_UTILS_TIMER_H_</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h2>timer.cpp:</h2>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;Timer.cpp<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-2.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#include &quot;utils/timer.h&quot;</span><br />
<span style="color: #339900;">#include &lt;assert.h&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// main loop for the timer thread</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> onRun<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">bool</span> run <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimer<span style="color: #000040;">*</span> theTimer <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>CTimer<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> run <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span> theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">TIMER_PAUSE</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msleep<span style="color: #008000;">&#40;</span>50<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">TIMER_RUN</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CTimerCallbackInterface<span style="color: #000040;">*</span> callback <span style="color: #000080;">=</span> theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>onTimer<span style="color: #008000;">&#40;</span>theTimer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>isRepeat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msleep<span style="color: #008000;">&#40;</span>theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>interval<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">TIMER_STOP</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>&nbsp; &nbsp; <span style="color: #666666;">// exit the main loop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> theTimer<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; CTimer<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #008000;">&#40;</span>CTimerCallbackInterface<span style="color: #000040;">*</span> callback,<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> interval,<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData,<span style="color: #0000ff;">bool</span> repeat<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">:</span> m_pCallback<span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pUserData<span style="color: #008000;">&#40;</span>userData<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_bRepeat<span style="color: #008000;">&#40;</span>repeat<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_ulInterval<span style="color: #008000;">&#40;</span>interval<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_state<span style="color: #008000;">&#40;</span>TIMER_PAUSE<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// create a thread</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pthread_create<span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>m_thread,<span style="color: #0000ff;">NULL</span>,onRun, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">this</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: error check</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; CTimer<span style="color: #008080;">::</span>~CTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pthread_join<span style="color: #008000;">&#40;</span>m_thread, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_RUN<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">resume</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: to be done</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_RUN<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_STOP<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">pause</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: to be implemented</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_PAUSE<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>注意：msleep在<a href="%E7%BC%96%E7%A8%8B/%E8%B7%A8%E5%B9%B3%E5%8F%B0%E4%BB%A3%E7%A0%81%E5%88%86%E4%BA%AB-%E7%BA%BF%E7%A8%8B">跨平台代码分享系列之一</a>中有说明。</p>
<p>&nbsp;</p>
<h2>使用方法:</h2>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// .h文件中</span><br />
<span style="color: #0000ff;">class</span> MyApp<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimerCallbackInterface</span> &nbsp;<span style="color: #666666;">// 继承CTimerCallbackInterface</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> onTimer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #000040;">*</span> timer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 实现onTimer</span><br />
<br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// .cpp 文件中某处</span><br />
<span style="color: #666666;">// 参数1 - 指向CTimerCallbackInterface</span><br />
<span style="color: #666666;">// 参数2 - 定时器分辨率，以ms为单位，此例为10ms</span><br />
<span style="color: #666666;">// 参数3 - 数据指针，用来传递数据到定时器回调中</span><br />
<span style="color: #666666;">// 参数4 - 是否循环触发</span><br />
utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #000040;">*</span> timer <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span>,10,data,<span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
<br />
<span style="color: #666666;">// 回调</span><br />
<span style="color: #0000ff;">void</span> MyApp<span style="color: #008080;">::</span><span style="color: #007788;">onTimer</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #000040;">*</span> theTimer<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 指向你想要的功能</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 你可以使用下面方法获取来自于定时器的数据</span><br />
&nbsp; &nbsp; UserData<span style="color: #000040;">*</span> data <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>UserData<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// 释放</span><br />
<span style="color: #666666;">// 记住一定要调用stop，来删除创建的定时器</span><br />
timer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>下次的题目是跨平台xml解析。有什么问题，建议，希望大家给点回复！你们的支持是我坚持下去的动力，谢谢！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%8c-%e5%ae%9a%e6%97%b6%e5%99%a8/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>跨平台代码分享之一 &#8211; 线程</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab-%e7%ba%bf%e7%a8%8b</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab-%e7%ba%bf%e7%a8%8b#comments</comments>
		<pubDate>Sun, 01 Nov 2009 07:40:52 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[线程]]></category>
		<category><![CDATA[跨平台]]></category>

		<guid isPermaLink="false">http://iphonegeekbeta.zuesitech.com/?p=17</guid>
		<description><![CDATA[有些iPhone程序是直接从其他平台移植过来，或者说你喜欢使用cpp，再或者是你打算让你的程序跨越平台，那么使用跨平台的cpp库无疑是一个很好的选择。这就是本系列的主题。
第一篇为大家分享一段本人自用的线程类。它使用pthread，因此可以跨平台使用（windows下需要下载pthread库）。由于程序很简单，就不做什么说明了。

头文件：
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#ifndef _UTILS_THREAD_H_
#define _UTILS_THREAD_H_

#include &#60;pthread.h&#62;

namespace utils
&#123;

class CThread
&#123;

public:
&#160; &#160; typedef void&#40; ThreadCallback&#40; void * &#41; &#41;;

typedef enum
&#123;
&#160; &#160; THREAD_STOP = 0,
&#160; &#160; THREAD_RUN,
&#160; &#160; THREAD_PAUSE
&#125; ThreadState;

CThread&#40;ThreadCallback* callback, void* pUserData&#41;;

~CThread&#40;&#41;;

ThreadState state&#40;&#41; const &#160; &#160; &#160; &#160; &#160; &#123; return m_state; &#160; &#160; &#160; &#125;

ThreadCallback* callback&#40;&#41; const&#160; &#160; &#123; return m_pCallback; &#160; &#125;

void* userData&#40;&#41; const&#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>有些iPhone程序是直接从其他平台移植过来，或者说你喜欢使用cpp，再或者是你打算让你的程序跨越平台，那么使用跨平台的cpp库无疑是一个很好的选择。这就是本系列的主题。</p>
<p>第一篇为大家分享一段本人自用的线程类。它使用pthread，因此可以跨平台使用（windows下需要下载pthread库）。由于程序很简单，就不做什么说明了。</p>
<p><span id="more-17"></span></p>
<h2><span style="color: #000080;"><strong>头文件</strong>：</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#ifndef _UTILS_THREAD_H_</span><br />
<span style="color: #339900;">#define _UTILS_THREAD_H_</span><br />
<br />
<span style="color: #339900;">#include &lt;pthread.h&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #0000ff;">class</span> CThread<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">void</span><span style="color: #008000;">&#40;</span> ThreadCallback<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">enum</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; THREAD_STOP <span style="color: #000080;">=</span> 0,<br />
&nbsp; &nbsp; THREAD_RUN,<br />
&nbsp; &nbsp; THREAD_PAUSE<br />
<span style="color: #008000;">&#125;</span> ThreadState<span style="color: #008080;">;</span><br />
<br />
CThread<span style="color: #008000;">&#40;</span>ThreadCallback<span style="color: #000040;">*</span> callback, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> pUserData<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
~CThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
ThreadState state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_state<span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
ThreadCallback<span style="color: #000040;">*</span> callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pCallback<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pUserdata<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
pthread_t thread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_thread<span style="color: #008080;">;</span>&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">void</span> pause<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
<br />
&nbsp; &nbsp; ThreadCallback<span style="color: #000040;">*</span> &nbsp; &nbsp; m_pCallback<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; pthread_t &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_thread<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; ThreadState &nbsp; &nbsp; &nbsp; &nbsp; m_state<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span>&nbsp; &nbsp; &nbsp; &nbsp; m_uiThreadId<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pUserdata<span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif // _UTILS_THREAD_H_</span></div></td></tr></tbody></table></div>
<h2><span style="color: #000080;">cpp文件:</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &quot;utils/Thread.h&quot;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> threadRun<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>_ptr <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> run <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CThread<span style="color: #000040;">*</span> theThread <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> CThread<span style="color: #000040;">*</span> <span style="color: #008000;">&#41;</span>_ptr<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> run <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span> theThread<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">THREAD_RUN</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CThread<span style="color: #008080;">::</span><span style="color: #007788;">ThreadCallback</span><span style="color: #000040;">*</span> callback <span style="color: #000080;">=</span> theThread<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> callback <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback<span style="color: #008000;">&#40;</span> theThread<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">THREAD_STOP</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">case</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">THREAD_PAUSE</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msleep<span style="color: #008000;">&#40;</span>50<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
CThread<span style="color: #008080;">::</span><span style="color: #007788;">CThread</span><span style="color: #008000;">&#40;</span>ThreadCallback<span style="color: #000040;">*</span> callback, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> pUserData<span style="color: #008000;">&#41;</span><br />
<span style="color: #008080;">:</span> m_pCallback<span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span>,<br />
&nbsp; m_pUserdata<span style="color: #008000;">&#40;</span>pUserData<span style="color: #008000;">&#41;</span>,<br />
&nbsp; m_state<span style="color: #008000;">&#40;</span>THREAD_PAUSE<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_uiThreadId <span style="color: #000080;">=</span> pthread_create<span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>m_thread,<span style="color: #0000ff;">NULL</span>,threadRun, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">this</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
CThread<span style="color: #008080;">::</span>~CThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; pthread_join<span style="color: #008000;">&#40;</span> m_thread, <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_state <span style="color: #000080;">=</span> THREAD_RUN<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_state <span style="color: #000080;">=</span> THREAD_STOP<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> CThread<span style="color: #008080;">::</span><span style="color: #007788;">pause</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_state <span style="color: #000080;">=</span> THREAD_PAUSE<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<h2><span style="color: #000080;">msleep函数：</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &amp;lt;sys/time.h&amp;gt;</span><br />
<br />
<span style="color: #0000ff;">void</span> msleep<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ms <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> microsecs<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">struct</span> timeval tv<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; microsecs <span style="color: #000080;">=</span> ms <span style="color: #000040;">*</span> <span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; tv.<span style="color: #007788;">tv_sec</span> &nbsp;<span style="color: #000080;">=</span> microsecs <span style="color: #000040;">/</span> <span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; tv.<span style="color: #007788;">tv_usec</span> <span style="color: #000080;">=</span> microsecs <span style="color: #000040;">%</span> <span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; select<span style="color: #008000;">&#40;</span> 0, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>tv <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<h2><span style="color: #000080;">使用方法：</span></h2>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> onRun<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<br />
......<br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
utils<span style="color: #008080;">::</span><span style="color: #007788;">CThread</span><span style="color: #000040;">*</span> m_pThread <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CThread</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>ThreadCallback<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>onRun,<span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
m_pThread<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
......<br />
<br />
<span style="color: #0000dd;">delete</span> m_pThread<span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>作者:bagusflyer</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab-%e7%ba%bf%e7%a8%8b/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

