一、SWIG環(huán)境搭建
1、 下載Swig for Windows:http://www.swig.org/download.html
2、 解壓 .zip 文件到目錄,比如:D:\backupsoftware
3、 添加環(huán)境變量到path, 比如: D:\backupsoftware\swigwin-3.0.10
4、 簡單測試安裝是否成功:
打開Dos,在命令行執(zhí)行: swig --help, 顯示 Target Language Options即表明安裝成功。二、以c++為例1、編寫c++源文件//example.h
#include <iostream>
using namespace std;
class Example{
public:
void say_hello();
};
//example.cpp
#include "example.h"
void Example::say_hello()
{
printf("hello");
}
2、再寫一個swig模塊定義文件如下
%module example
%{
#include "example.h"
%}
%include "example.h"
3、通過命令行運行:$ swig -python -c++ example.i 如果是使用C源碼,則選項:$ swig -Python example.i
這樣會創(chuàng)建兩個不同的文件:example_wrap.cxx(如果用c源碼是example_wrap.c),和python文件example.py。
4、使用python.distutils生成模塊動態(tài)庫python自帶一個distutils工具,可以用它來創(chuàng)建python的擴展模塊。使用它也很簡單,只需要先定義一個配置文件,通常是命名為setup.py,如下:#!/usr/bin/env python
"""
setup.py file for SWIG C\+\+/Python example
"""
from distutils.core import setup, Extension
example_module = Extension('_example',
sources=['example.cpp', 'example_wrap.cxx',],
)
setup (name = 'example',
version = '0.1',
author = "www",
description = """Simple swig C\+\+/Python example""",
ext_modules = [example_module],
py_modules = ["example"],
)
注:swig生成的擴展模塊對象名必須使用python模塊名并在前面加上下劃線_,剛才我們通過swig生成的python文件是example.py,所以這里的模塊對象名必須是'_example',否則無法順利編譯。5、編譯
命令行中將當前工作目錄切換到文件example.cpp,example_wrap.cxx,example.py,setup.py所在的目錄,然后輸入以下命令:python setup.py build_ext --inplace會在本目錄下生成_example.pyd模塊。
6、測試import examlpeexample.Example().say_hello()注:如果導(dǎo)入模塊失敗,需要將模塊所在路徑添加到sys.path中,在次導(dǎo)入就會成功
posted @
2016-08-18 11:20 王海光 閱讀(4698) |
評論 (0) |
編輯 收藏
摘要: UISearchBar控件就是要為你完成搜索功能的一個專用控件。它集成了很多你意想不到的功能和特點!首先,還是來普及一下UISearchBar控件API相關(guān)的屬性和方法吧!UISearchBar屬性相關(guān)_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解釋 &nb...
閱讀全文
posted @
2015-03-04 19:23 王海光 閱讀(873) |
評論 (0) |
編輯 收藏
摘要: 標準庫C++標準庫,包括了STL容器,算法和函數(shù)等。C++ Standard Library:是一系列類和函數(shù)的集合,使用核心語言編寫,也是C++ISO自身標準的一部分。Standard Template Library:標準模板庫C POSIX library : POSIX系統(tǒng)的C標準庫規(guī)范ISO C++ Standards Committee :C++標準委員會 ...
閱讀全文
posted @
2014-11-14 08:58 王海光 閱讀(2431) |
評論 (0) |
編輯 收藏
不知道大家有沒有我這種體驗.大家先看看下面這段代碼:int fp=_wopen(L"Hello.dat",O_BINARY | O_CREAT | O_TRUNC | O_RDWR);
if(fp==-1) return;
write(fp,L"123中國人",wcslen(L"123中國人"));
close(fp);
上面這段代碼不知道大家看出什么BUG來了.如果大家看不出毛病也不足為怪,因為這是我們的習(xí)慣導(dǎo)致了我們的錯誤產(chǎn)生.先讓我來分析一下write吧.下面是write的原型:int write( int handle, const void *buffer, unsigned int count );參數(shù):handle 已打開或已創(chuàng)建的文件句柄buffer 待寫入的數(shù)據(jù)count 待寫入的數(shù)據(jù)大小現(xiàn)在分析為什么上面的那代碼有bug,其實主要問題就在一個buffer,和count.如果我們寫入一個Ansi字符串,上面的代碼改成相應(yīng)的形式確實沒有錯.但如果是寫入一個寬字符串,那么上面的代碼就不嚴格.原因就在于count.我們首先看一下strlen和wcslen,如果使用strlen,一般情況下,我們直接作為字符串的長度,而使用wcslen,你會發(fā)現(xiàn),得出的不是字符串的長度而是字符的個數(shù).這就是問題的所在.一般情況下.char的長度是1,這是用sizeof(char)運算出來的結(jié)果.len=strlen(str)*sizeof(char);而我們一般情況下,都只用strlen(str)來等價,這就是平時的習(xí)慣.正是由于這個習(xí)慣所引來的問題,這個習(xí)慣并不適用于寬字符串.因為wcslen(str)*sizeof(wchar_t)并不等于wcslen(strl).這就是習(xí)慣所引起的錯誤.說到這里我想大家都明白了.我在這里把這種習(xí)慣稱之為不良習(xí)慣.所以大家以后在計算字符串長度的時候,千萬不能簡而簡之,一定要len=strlen(str)*sizeof(char),len=wcslen(str)*sizeof(wchar_t).不要再犯這種習(xí)慣性的低級錯誤.
本文轉(zhuǎn)自:
http://blog.csdn.net/aylixuan/article/details/6130820
posted @
2014-10-10 11:53 王海光 閱讀(3817) |
評論 (0) |
編輯 收藏
使用版本:1.1.10今天弄了一下Gloox中自帶的收發(fā)文件例子,收發(fā)文件的例子都是好使的,只不過,在調(diào)試過程中需要注意一些問題,下面將我遇到的問題做個記錄(例子中以In-Band Bytestreams方式收發(fā))。1、發(fā)送文件過程中遇到404錯誤<iq type='error' id='uid-8509a748-00000005' to='wanghaiguang@wanghaiguang-wk/glooxsendfile'from='www@192.168.60.67/Spark 2.6.3'><sixmlns='http://jabber.org/protocol/si' id='uid-8509a748-00000006' profile='http://jabber.org/protocol/si/profile/file-transfer'><file xmlns='http://jabber.org/protocol/si/profile/file-transfer' name='d:\offline.bmp' size='6998'/><feature xmlns='http://jabber.org/protocol/feature-neg'><x xmlns='jabber:x:data' type='form'><field type='list-single'var='stream-method'><option label='ibb'><value>http://jabber.org/protocol/ibb</value></option><option label='oob'><value>jabber:iq:oob</value></option><option label='s5b'><value>http://jabber.org/protocol/bytestreams</value></option><value/></field></x></feature></si><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>
可以將ip地址換成服務(wù)器名稱試試JID j( "www@192.168.60.67/gloox" );——> JID j( "www@wanghaiguang-wk/gloox" );
2、發(fā)送文件過程中遇到503錯誤<iq type='error' id='uid-8663a748-00000005' from='www@wanghaiguang-wk/glooxsend'to='wanghaiguang@wanghaiguang-wk/glooxsendfile'><si xmlns='http://jabber.org/protocol/si' id='uid-8663a748-00000006' profile='http://jabber.org/protocol/si/profile/file-transfer'><file xmlns='http://jabber.org/protocol/si/profile/file-transfer' name='d:\offline.bmp' size='6998'/><feature xmlns='http://jabber.org/protocol/feature-neg'><x xmlns='jabber:x:data' type='form'><field type='list-single' var='stream-method'><option label='ibb'><value>http://jabber.org/protocol/ibb</value></option><option label='oob'><value>jabber:iq:oob</value></option><option label='s5b'><value>http://jabber.org/protocol/bytestreams</value></option><value/></field></x></feature></si><error code='503' type='cancel'><service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>
這個問題可能是由于你接收端的資源名稱與發(fā)送端不匹配//接收端登錄的jid
JID jid( "www@192.168.60.67/glooxsendfile" );
j = new Client( jid, "111" );
//發(fā)送端的jid名稱(發(fā)送給對方的jid名稱)
JID j( "www@wanghaiguang-wk/gloox" );
if( j.resource().empty() )
{
printf( "error: need full jid\n" );
return 1;
}
posted @
2014-09-02 17:18 王海光 閱讀(2472) |
評論 (0) |
編輯 收藏
引文:
調(diào)試GLOOX 1.0.10的注冊功能頗費了一些功夫。總體邏輯如GLOOX自帶的例子一樣是毫無疑問的,但是照搬例子又是不能完成注冊的,返回錯誤碼為4------RegistrationBadRequest。筆者一開始在網(wǎng)上狂搜解決方案,資料少之又少,有建議重寫Client::handleNormalNode函數(shù)(目的是禁止SASL認證)的,有直接繼承Client重寫Client::handleNormalNode函數(shù)的,但都沒說到點子上。經(jīng)過一段時間的研究,在GLOOX的maillist上得到啟發(fā),順利完成注冊。現(xiàn)將解決方案記錄下來:
環(huán)境
客戶端:GLOOX1.0.1.0 VS2008
服務(wù)器:OPENFIRE 默認安裝
對于GLOOX自帶的注冊例子不能正常注冊的問題有人在郵件列表里提出來。一個哥們這樣回答:
Ok, I've found what the problem was
In openFire server parameters, Anonymous Login => Disabled !!!
意思是要禁用openFire服務(wù)器里的選項”注冊和登錄“的”匿名登錄“項。筆者按此說明禁用該選項,果然注冊成功。
這說明開始的注冊失敗是和匿名登錄有關(guān)系的。我們來看一下引用registration_expmple例子登錄失敗時的XML流:
S->C:服務(wù)器返回給客戶端支持的認證機制:
<stream:features xmlns:stream='http://etherx.jabber.org/streams'><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><auth xmlns='http://jabber.org/features/iq-auth'/><register xmlns='http://jabber.org/features/iq-register'/></stream:features>
從上面XML流中我們可以看到,默認openFire支持四種認證機制,分別是:DIGEST-MD5、PLAIN、ANONYMOUS、CRAM-MD5。然后我們看GLOOX客戶端的響應(yīng)流:
C->S:客戶端返回選擇的認證方式:
<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>
可以看出,客戶端”無恥“的選擇了”匿名“--'ANONYMOUS'方式接下來的流程就是客戶端”無恥“的選擇了以匿名的方式登錄了服務(wù)器,然后再發(fā)送注冊請求,請求如下:
<iq id='uid:4e69eccd:00006784' type='set' from='447e0585@zxl/447e0585' xmlns='jabber:client'><query xmlns='jabber:iq:register'><username>bbaxiao</username><password>123456</password><name>test2</name><email>163@gmail.com</email></query></iq>
我們看到,IQ節(jié)里包含“form”屬性,即客戶端匿名身份標識。
注意,一個客戶端已經(jīng)以一個身份(由服務(wù)器臨時分配的一個JID)登錄,建立了會話,在服務(wù)器上我們會看到這個會話,并且服務(wù)器發(fā)送心跳一直維護這個會話。這種情況下,這個客戶端再發(fā)送注冊請求(另一個身份)建立與服務(wù)器的連接是不被允許的。具體請參考XEP-0077(In-Band Registration):我們關(guān)注這兩段:
If the entity cancels its registration with its "home" server (i.e., the server at which it has maintained its XMPP account), then the entity SHOULD NOT include a 'from' or 'to' address in the remove request the server SHOULD then return a <not-authorized/> stream error and terminate all active sessions for the entity. The server SHOULD perform the remove based on the bare JID <localpart@domain.tld> associated with the current session or connection over which it received the remove request. If the server is an instant messaging and presence server that conforms to XMPP IM [8], the server SHOULD also cancel all existing presence subscriptions related to that entity (as stored in the entity's roster).
If the entity cancels its registration with a service other than its home server, its home server MUST stamp a 'from' address on the remove request, which in accordance with XMPP Core will be the entity's full JID <localpart@domain.tld/resource>. The service MUST perform the remove based on the bare JID <localpart@domain.tld> portion of the 'from' address.
If the entity cancels its registration with its "home" server (i.e., the server at which it has maintained its XMPP account), then the entity SHOULD NOT include a 'from' or 'to' address in the remove request the server SHOULD then return a <not-authorized/> stream error and terminate all active sessions for the entity. The server SHOULD perform the remove based on the bare JID <localpart@domain.tld> associated with the current session or connection over which it received the remove request. If the server is an instant messaging and presence server that conforms to XMPP IM [8], the server SHOULD also cancel all existing presence subscriptions related to that entity (as stored in the entity's roster).
If the entity cancels its registration with a service other than its home server, its home server MUST stamp a 'from' address on the remove request, which in accordance with XMPP Core will be the entity's full JID <localpart@domain.tld/resource>. The service MUST perform the remove based on the bare JID <localpart@domain.tld> portion of the 'from' address.
意思是說注冊請求不能包含“from”屬性。正常的注冊流如下:
<iq id='uid:4e69eccd:00003d6c' type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'><username>bbaxiao</username><password>123456</password><name>test2</name><email>163@gmail.com</email></query></iq>
---------------------------
綜上所述,解決方案如下:
一、關(guān)閉openFire的匿名登錄功能。^_^……
二、禁止GLOOX匿名認證功能。
file:client.cpp
fun: int Client::getSaslMechs( Tag* tag )
line:423
//將423行注釋掉即可。
422:if( tag->hasChildWithCData( mech, "ANONYMOUS" ) )
423 //mechs |= SaslMechAnonymous;
重新編譯生成DLL即可。三、手動設(shè)置GLOOX客戶端SASL認證機制
在調(diào)用j->connect()之前設(shè)置SASL認證機制,比如設(shè)置為“DIGEST-MD5”
j->setSASLMechanisms(SaslMechDigestMd5);
這種方式的缺點是需要先確定服務(wù)器支持的認證機制。四、根據(jù)XEP-0077所述,即使其名登錄,注冊流只要不帶“from”屬性應(yīng)該也可以。所以我們要處理發(fā)出的注冊流,去除“from”屬性重新發(fā)送注冊流即可。
本文轉(zhuǎn)自:http://blog.csdn.net/abcpanpeng/article/details/7370974
posted @
2014-08-28 17:59 王海光 閱讀(1537) |
評論 (0) |
編輯 收藏
NSString中如果包括中文字符,在轉(zhuǎn)換為NSURL時得到的值為nil
解決辦法:
NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?q=%@&units=imperial",cityName];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"%@", url);
posted @
2014-08-18 09:45 王海光 閱讀(1690) |
評論 (0) |
編輯 收藏
第一步:
創(chuàng)建2個NSNotificationCenter監(jiān)聽
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil]; //監(jiān)聽是否觸發(fā)home鍵掛起程序.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil]; //監(jiān)聽是否重新進入程序程序.
第二步:
實現(xiàn)2個NSNotificationCenter所觸發(fā)的事件方法
- (void)applicationWillResignActive:(NSNotification *)notification
{
printf("按理說是觸發(fā)home按下\n");
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
printf("按理說是重新進來后響應(yīng)\n");
}
注: 在home鍵觸發(fā)后,AppDelegate響應(yīng)的方法為:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers,
and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
本文轉(zhuǎn)自:http://blog.csdn.net/sqc3375177/article/details/9466687
其他相關(guān)信息:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // Override point for customization after application launch.
- NSLog(@"\n ===> 程序開始 !");
-
- return YES;
- }
-
- - (void)applicationWillResignActive:(UIApplication *)application
- {
-
- NSLog(@"\n ===> 程序暫行 !");
- }
-
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
-
- NSLog(@"\n ===> 程序進入后臺 !");
- }
-
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
-
- NSLog(@"\n ===> 程序進入前臺 !");
- }
-
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- NSLog(@"\n ===> 程序重新激活 !");
-
- }
-
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- NSLog(@"\n ===> 程序意外暫行 !");
-
- UIDevice *device = [UIDevice currentDevice];
-
- }
首次運行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)applicationDidBecomeActive:(UIApplication *)application
首次關(guān)閉(home):
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
再次運行:
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
再次關(guān)閉:
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
本文轉(zhuǎn)自:http://friendlysong.blog.163.com/blog/static/3225243920128144251666/
posted @
2014-08-01 13:31 王海光 閱讀(2358) |
評論 (0) |
編輯 收藏
摘要: UIGestureRecognizer 是一個具體手勢的基類,提供了較為簡單的手勢實現(xiàn)方式 The concrete subclasses of UIGestureRecognizer are the following:UITapGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecog...
閱讀全文
posted @
2014-07-29 13:35 王海光 閱讀(2014) |
評論 (0) |
編輯 收藏
在“Header Search Paths” 中添加“/usr/include/libxml2″
在“Other Linker Flags”添加“-lxml2″
運行后出現(xiàn)錯誤找不到<libxml/tree.h>
解決辦法:在“Header Search Paths” 中添加 ${SDKROOT}/usr/include/libxml2
posted @
2014-07-15 13:50 王海光 閱讀(1126) |
評論 (0) |
編輯 收藏