JeffHung.Blog

(My smile insists of having nose. :-)

My gindent configuration

基本上,我是用 FreeBSD 的,而 FreeBSD 本就已經有內建一個 indent(1)。不過可惜的是,正如其 man-page 的 BUGS 這節所述:"The indent utility has even more switches than ls(1)." 其複雜簡潔的設定,使其設定檔 ~/.indent.pro 如天書一般難解。因此,衝著 gindent 的 ~/.indent.pro 可以使用 C/C++/Java 註解的特性,我選擇改用 gindent。
Indent 的設定,正代表著設定者對於 C/C++ 程式的排版風格的喜好。為了調整 gindent 的設定檔至我所喜好的模樣,當年亦花了不少時間作實驗。看了 s88 兄的《gnu’s indent》後,我亦有感而發,拿出我的 ~/.indent.pro 來陷醜:
// ---------------------------------------------------------------------// gindent options// ---------------------------------------------------------------------

// ---------------------------------------------------------------------// -bad, --blank-lines-after-declarations// Force blank lines after the declarations.// See BLANK LINES.-bad

// [...]

Parameterized Unit Tests

《Parameterized Unit Tests》:

Parameterized unit tests extend the current industry practice of using closed unit tests defined as parameterless methods. Parameterized unit tests separate two concerns: 1) They specify the external behavior of the involved methods for all possible test arguments. 2) Test cases can be re-obtained as traditional closed unit tests by instantiating [...]

Trac Aggregtor

Trac 是我目前正在用的 issue tracking system,由於整合了 wiki 與 subversion,故幾乎可以拿來當作 project management system 來用了。不過,目前的 Trac 即使是進展到了 1.0 版,也仍然將是 single project 的系統。然而,實際工作上,多個 project 一併進行,設置互有牽扯的情況非常普遍。因此,我寫了一個簡單的 PHP 網頁,剛好可以簡單地當作 multi-project trac portal 來用。
使用前,請先依照 TracMultiProjects 的說明,設定好 multi-project 的環境。然後,修改這個 PHP 網頁最前面的 define(),TRAC_DIR 是指你放那堆 trac environment 的目錄,而 TRAC_USR 則是你登入 trac 用的帳號。最後,把這個網頁放在你覺得最適合的地方即可。

<?php

define('TRAC_DIR', '/trac');
define('TRAC_USR', 'jeffhung');

$tracagg_errors = array();
function tracagg_raise_error($errmsg)
{
array_push($tracagg_errors, $errmsg);
}

function wrap_html_text($text)
{
[...]

在 unicode 文字檔裡檢查 big5 字元

之前碰到一個問題就是,我們有一堆 UTF-8 的 XML 檔案,但實際上這些檔案應該都只包含中文字,也就是可以用 BIG5 呈現的字。然而,因為是 UTF-8 是 Unicode 的關係,所以實際上可能存在有無法轉換成 BIG5 的字,因而違反程式的期待,造成程式的問題。為了避免這樣子的問題,我們必須要檢查這堆 UTF-8 的 XML 檔案,確認每個檔案裡用到的每個 UTF-8 字元,都可以被轉換成 BIG5。這個問題,在考慮到其實 BIG5 也有很多變種的情況下,越是複雜。
不過還好,想通了話其實就很簡單:用 iconv 試轉不就得了:

SHELL> find . -type f -name '*.xml' \
| xargs -n 1 -t -x iconv -f UTF-8 -t CP950 \
[...]

big/little endian 的典故

在《C++ Primer 答客問 28~40》看到的 big/little endian 典故:

"The term big endian is derived from Jonathan Swift's Gulliver's Travels, in which the Big-Endians were a group of people who opposed the emperor's decree that eggs should be broken at the small end before they were eaten."
-- Microsoft Press Computer Dictionary 3/e p.49

User defined asertion in PHP

PHP 內建的 assertion 功能已經很強了,但有時候,就是差了這麼一點點,因此,也許我們需要自己做自己的 assertion。
自己做 assertion 怎樣做都可以,反正是自己做。只不過,有些東西還是不一定容易取得,好比說,發生 assertion failure 的 file name、line number。這些東西內建的 assert() 會印,但如果我們要自己做 assertion,那我們就得自己來。
在自己的 assertion function 裡面用 __FILE__、__LINE__ 這類 magic constants 也只會印出實做自己的 assertion function 那一處的檔案及行號,無效。幸好,PHP 裡有個和 Perl 內建的 caller() 功能更強一些的 debug_backtrace()。利用這個函式,我們就可以做自己的 assertion function,用法與內建的 assert() 幾乎一致:

function my_assert($expr)
{
eval('$eval_result__=('.$expr.');');
if (!$eval_result__) {
[...]

Declare struct in C/C++ enabled style

我都是這樣子 declare structure 的,當然,這樣子的 structure 必須要是單純的struct (ie. POD),不能有 member function 之類 members 存在:

#ifndef __cplusplus
typedef struct LinkList LinkList;
#endif /* __cplusplus */
struct LinkList
{
/* ... */
LinkList* next;
};

這樣一來,我們就可以像 C++ 一樣直接使用 type name 來 declare instances。幾個重點:

其實 C++ 也可以 struct Foo 或是 Foo 混著用來 declare instances,不過這樣總是難看了些,所以用上面那招,統一使用 C++ 的方法,包括在 structure 裡面的 member 在內。
在 C/C++ [...]

輸入網址,輸出以文字模式排版的網頁內容

既然有上一篇個技巧,當然就可以包成網頁,平常當成一個 utility 來用,這樣若是要轉文章到 bbs 上的話,就可以叫這個網頁出來排版,畫面就可以排得漂漂亮亮啦。:-)
呈現出來的效果如:http://www.jeffhung.idv.tw/html2text.php (目前無法使用)。
<?php
// Author: Chien-Chou Hung
// License: BSDL
?><html>
<head>
<title>html2text</title>
</head>
<body>

<form name="f" method="POST">
<b>Convert:</b> <input type="text" name="url" value="<?php

echo (empty($url) ? "http://" : $url);

?>" size="50" maxlength="255">
<input type="submit"><br>
Text browser:
<input type="radio" name="browser" value="links" checked>
[...]

抓網頁並以文字模式輸出

簡單的小 script,能夠抓任意網頁,然後以文字模式的方式排版輸出,用到 lynx 跟 awk。用的時候把 $url 代換成要抓的網址就可以了。
lynx -dump $url | awk ' /process$/ { print; exit } { print } '

PHP equivelent include guards

在 C/C++ 裡,可以用 #ifdef/#define/#endif 來做 include guard 避免重複引入,而在 PHP 裡則需要用 include_once() 及 require_once() 等 _once 系列的函式,來避免重複引入。比較麻煩的是,對於 PHP 而言,相對路徑的起點是程式碼執行起點所在的那個檔案的目錄,而不是呼叫 include_once()、require_once() 的程式檔案所在的目錄。
舉個例子來說:被執行的 a.php 裡引入了 lib/b.php,然後 lib/b.php 又引入了 c.php,此時,PHP 會到 a.php 所在的目錄尋找 c.php,而非 b.php 所在的目錄尋找。
這個問題,對於 library writer 會產生相當大的困擾,除非使用者將 library 安裝至 PHP 的 library 目錄 (例如在 FreeBSD 下用 ports 安裝 PHP,其 library 目錄即為 /usr/local/lib/php),直接以該目錄為相對路徑起點,才不會有此困擾。
因此,我寫了下面這些 functions 來解決這樣的問題:

////////////////////////////////////////////////////////////////////////
// Hacks for include [...]

 Prev 1 2 3 ...5 6 7 8 9 10 11 12 13 Next