當 Ubuntu 啟動所有網絡介面時 (/etc/init.d/network 呼叫 ifup -a), 如果網絡介面成功啟動,那麼 /etc/network/if-up.d/ 目錄下的 shell script 會被執行. 例如有個 shell script 是這樣的:
#!/bin/sh
if [ $IFACE != "lo" ]; then
/sbin/route add default gw 192.168.1.254 $IFACE
fi
那麼只要網路介面不是 lo, route 就會執行, 即將 192.168.1.254 設為該介面的 gateway.
更多資料可以在 interfaces 的 man page 找到: man interfaces.
2009年9月12日星期六
2008年10月11日星期六
找出執行程式的檔案路徑 (Linux 和 Windows 版本)
#include "string.h"
#include "stdio.h"
#ifndef __WIN32__
#include "unistd.h"
#include "sys/types.h"
#endif
char getApplicationPath(char *pPath, int nMaxPathSize)
{
char proc_path[20], buf[256], *lastchar;
int len;
#ifdef __WIN32__
len = GetModuleFileName(NULL, buf, 256);
if(len == 0) {
// GetModuleFileName error
return -1;
}
if((len >= 256) || (len >= nMaxPathSize)) {
//insufficient size
return -2;
}
// terminate the returned string first
buf[len] = 0;
// then remove the application name
lastchar = strrchr(buf, '\\');
#else
sprintf(proc_path, "/proc/%d/exe", getpid());
len = readlink(proc_path, buf, 256);
if(ret < 0) {
// readlink error
perror("readlink");
return -1;
}
if((len >= 256) || (len >= nMaxPathSize)) {
// insufficient size
return -2;
}
// terminate the returned string first
buf[len] = 0;
// then remove the application name
lastchar = strrchr(buf, '/');
#endif
if(lastchar != NULL) {
*lastchar = 0;
} else {
// application name not found
return -3;
}
strcpy(pPath, buf);
// success
return 0;
}
#include "stdio.h"
#ifndef __WIN32__
#include "unistd.h"
#include "sys/types.h"
#endif
char getApplicationPath(char *pPath, int nMaxPathSize)
{
char proc_path[20], buf[256], *lastchar;
int len;
#ifdef __WIN32__
len = GetModuleFileName(NULL, buf, 256);
if(len == 0) {
// GetModuleFileName error
return -1;
}
if((len >= 256) || (len >= nMaxPathSize)) {
//insufficient size
return -2;
}
// terminate the returned string first
buf[len] = 0;
// then remove the application name
lastchar = strrchr(buf, '\\');
#else
sprintf(proc_path, "/proc/%d/exe", getpid());
len = readlink(proc_path, buf, 256);
if(ret < 0) {
// readlink error
perror("readlink");
return -1;
}
if((len >= 256) || (len >= nMaxPathSize)) {
// insufficient size
return -2;
}
// terminate the returned string first
buf[len] = 0;
// then remove the application name
lastchar = strrchr(buf, '/');
#endif
if(lastchar != NULL) {
*lastchar = 0;
} else {
// application name not found
return -3;
}
strcpy(pPath, buf);
// success
return 0;
}
2008年10月10日星期五
wxWidgets 的 StretchBlt 方案
如果用過 Win32 SDK 的人都知道有一個 StretchBlt() function 可以將原有的 bitmap 放大或縮小再顯示出來. 可是在 wxWidgets 卻沒有此功能. 如果要在 wxWidgets 做出類似功能, 我們可以:
1. 將原來的 wxBitmap object 用 wxBitmap::ConvertToImage() 轉為 wxImage object
2. 用 wxImage::Rescale() 改變大小
3. 再用新的 wxImage object 產生新的 wxBitmap object
4. 用 wxDC::DrawBitmap() 顯示新的 wxBitmap object
1. 將原來的 wxBitmap object 用 wxBitmap::ConvertToImage() 轉為 wxImage object
2. 用 wxImage::Rescale() 改變大小
3. 再用新的 wxImage object 產生新的 wxBitmap object
4. 用 wxDC::DrawBitmap() 顯示新的 wxBitmap object
2008年9月15日星期一
在 Linux 轉換目錄的方便方法
最新學會兩種用來轉換目錄的新方法:
1.
一般來說我們轉換目錄都會用 cd 指令, 例如我現在 /bin 目錄, 如果要轉到 /etc 目錄, 我會:
cd /etc [enter]
當我在 /etc 目錄要返回 /bin 目錄時, 我可以:
cd - [enter]
而不需要輸入:
cd /etc [enter]
因為 cd - 代表返回之前的目錄
2.
基於以上的例子, 如果由 home 轉到 /bin 再轉到 /etc, 當我輸入:
pushd /bin [enter]
pushd /etc [enter]
我會看見:
/etc /bin ~
如果我輸入:
pushd +2 [enter]
我就會返回 home 目錄.
因為 pushd 指令會 將輸入的目錄儲存在 stack 內, 因此我用 pushd /bin 和 pushd /etc 後,
+0 = /etc
+1 = /bin
+2 = ~
所以 pushd +2 就會返回 home 見錄, 而且 stack 變成:
+0 = ~
+1 = /etc
+2 = /bin
當再次輸入 pushd +2 就會返回 /bin 見錄了.
詳情可以查看 pushd 指令 (man pushd)
1.
一般來說我們轉換目錄都會用 cd 指令, 例如我現在 /bin 目錄, 如果要轉到 /etc 目錄, 我會:
cd /etc [enter]
當我在 /etc 目錄要返回 /bin 目錄時, 我可以:
cd - [enter]
而不需要輸入:
cd /etc [enter]
因為 cd - 代表返回之前的目錄
2.
基於以上的例子, 如果由 home 轉到 /bin 再轉到 /etc, 當我輸入:
pushd /bin [enter]
pushd /etc [enter]
我會看見:
/etc /bin ~
如果我輸入:
pushd +2 [enter]
我就會返回 home 目錄.
因為 pushd 指令會 將輸入的目錄儲存在 stack 內, 因此我用 pushd /bin 和 pushd /etc 後,
+0 = /etc
+1 = /bin
+2 = ~
所以 pushd +2 就會返回 home 見錄, 而且 stack 變成:
+0 = ~
+1 = /etc
+2 = /bin
當再次輸入 pushd +2 就會返回 /bin 見錄了.
詳情可以查看 pushd 指令 (man pushd)
2008年9月14日星期日
Ubuntu 7.10 本地檔案庫
我有一台很舊的電腦正在用 Ubuntu 7.10. 最近我想為該電腦內 Ubuntu 系統升級, 於是做了一隻 AptOnCD. 誰知道 Package Manager 讀不到該 DVD. 最後只有將所有 deb 檔案複製到 Harddisk 內, 然後再做一個檔案庫:
1. 因為沒有安裝 Apache WWW server, 我便需要先安裝它
2. 在 /var/www 資料夾內建立一個 ubuntu 資料夾 (其實可以使用其他名稱)
3. 在 ubuntu 資料夾內建立一個 local 資料夾 (其實一樣可以使用其他名稱)
4. 將所有 deb 檔案複製到 local 資料夾內
5. 在 /var/www/ubuntu 資料夾內輸入: apt-ftparchive packages local > local/Packages [enter]
6. 完成再輸入 gzip local/Packages [enter]
7. 然後將 deb http://localhost/ubuntu local/ 加到 /etc/apt/sources.lst 內 (也可以用 Software sources 加入)
8. 在 Package Manager 內按 Reload 來更新檔案資料
但是在過程中我發現當檔案名稱有 %3a 字串時便會出現問題 (404 File not found). 後來在網上找到解決辦法: 先用 dpkg-name 為所有檔案名稱有 %3a 字串的檔案改名:
dpkg-name *%3a*[enter]
然後才執行 apt-ftparchive.
P.S.
1. 其實 %3a 就是 ":", 不過我們都知道檔案名稱不可以有 ":", 所以才用 %3a 代替.
2. 在 /var/www 資料夾內, 我們需要用 sudo 指令
1. 因為沒有安裝 Apache WWW server, 我便需要先安裝它
2. 在 /var/www 資料夾內建立一個 ubuntu 資料夾 (其實可以使用其他名稱)
3. 在 ubuntu 資料夾內建立一個 local 資料夾 (其實一樣可以使用其他名稱)
4. 將所有 deb 檔案複製到 local 資料夾內
5. 在 /var/www/ubuntu 資料夾內輸入: apt-ftparchive packages local > local/Packages [enter]
6. 完成再輸入 gzip local/Packages [enter]
7. 然後將 deb http://localhost/ubuntu local/ 加到 /etc/apt/sources.lst 內 (也可以用 Software sources 加入)
8. 在 Package Manager 內按 Reload 來更新檔案資料
但是在過程中我發現當檔案名稱有 %3a 字串時便會出現問題 (404 File not found). 後來在網上找到解決辦法: 先用 dpkg-name 為所有檔案名稱有 %3a 字串的檔案改名:
dpkg-name *%3a*
然後才執行 apt-ftparchive.
P.S.
1. 其實 %3a 就是 ":", 不過我們都知道檔案名稱不可以有 ":", 所以才用 %3a 代替.
2. 在 /var/www 資料夾內, 我們需要用 sudo 指令
2008年9月13日星期六
2008年7月6日星期日
wxWidgets: 用來找尋 tree Index (在同一個 level 下)
/**
* Find the tree item index in the same depth
*
* @param pTree tree pinter
* @param rootid parent tree item id
* @param itemid tree item id
*
* @return return item index if success otherwise return -1
*/
int find_treeItem_index(wxTreeCtrl *pTree, wxTreeItemId rootid, wxTreeItemId itemid)
{
wxTreeItemIdValue cookie;
wxTreeItemId id;
int cnt=-1;
// for the first child
id = pTree->GetFirstChild(rootid, cookie);
if(!id.IsOk()) {
return -1;
} else {
cnt++;
}
while(id != itemid) {
id = pTree->GetNextChild(rootid, cookie);
if(!id.IsOk()) {
break;
}
cnt++;
}
if(!id.IsOk()) {
cnt = -1;
}
return cnt;
}
* Find the tree item index in the same depth
*
* @param pTree tree pinter
* @param rootid parent tree item id
* @param itemid tree item id
*
* @return return item index if success otherwise return -1
*/
int find_treeItem_index(wxTreeCtrl *pTree, wxTreeItemId rootid, wxTreeItemId itemid)
{
wxTreeItemIdValue cookie;
wxTreeItemId id;
int cnt=-1;
// for the first child
id = pTree->GetFirstChild(rootid, cookie);
if(!id.IsOk()) {
return -1;
} else {
cnt++;
}
while(id != itemid) {
id = pTree->GetNextChild(rootid, cookie);
if(!id.IsOk()) {
break;
}
cnt++;
}
if(!id.IsOk()) {
cnt = -1;
}
return cnt;
}
訂閱:
文章 (Atom)