Kanru’s 探險日誌

當發現美好的事物時,所要做的第一件事,就是把它分享給所有人

Archive for the ‘Programming’ Category

Bibot3 新功能

leave comments »

好吧,拖了很久才寫。

自從 bibot2 從 zion 人間蒸發之後[1],就開始了用 mono 寫新的 bibot3 的計畫,但是對於到底要有什麼功能一直沒有什麼好點子。從 bibot2 的經驗來看,網址自動紀錄大概是最多人在用的功能,我從來沒想到 url.dot 掛掉之後會有那麼多人詢問…

某天跟 chihchun 閒聊,討論到把網址丟到某個 social bookmarks 服務(像是 del.icio.us) 的可能性,既然 del.icio.us 有公開 api,那麼應該不難實做,甚至比自己刻資料庫還簡單。

於是,在某天下課後查了一下 api 研究怎麼使用,然後就誕生了 http://del.icio.us/bibot。結果比想像中的有趣,因為 del.icio.us 有 tag 的功能,所以可以根據 tag 來判斷某網址是誰、在哪個 channel 提到的,也會自動紀錄時間和更新 title。

附帶一提,tinyurl.com 也有 api 可以呼叫,所以短網址的功能也可以輕鬆完成,不用再自己 parse 網頁的 output。

[1]: 某次停電之後,位在 zion 的 lvm 上的某 vserver 整個消失,只剩下 lost+found ,至今找不到原因。

Written by Kanru Chen

April 2nd, 2006 at 1:21 am

Posted in Programming

Tagged with ,

借書

leave comments »

今天去圖書館挖寶,又借了三本書:

本來還要借 Advanced .NET Remoting 的,可是應該是看不完,就先留給別人了…

很喜歡逛圖書館,在裡面搜刮的感覺很棒… 面對永遠都看不完的書,有時後會想:住在圖書館裡面好了 XD

btw, 似乎應該要找一些文學類的來看阿 @@

Written by Kanru Chen

March 2nd, 2006 at 9:34 pm

Posted in Programming

Tagged with

.NET - Plugin

with 2 comments

我之前在 .NET - Load Plugins 寫過如何實做簡單的 plugin interface,最近真正在用的時候才發現問題。

Assembly.Load* 雖然可以把 .dll 檔案動態載入,可是卻找不到任何方法可以 unload.. Assembly 在被載入之後是處於鎖定的狀態,如果這時候把檔案換掉的話,Assembly.Load 便無法再重新載入,會出現 Exception

這樣就沒辦法在後端偷偷換掉 backend 了 -_-

要實做動態的 load, unload 好像是要使用 System.AppDomain 來做(時際上是用 Remoting 的技術),看似簡單,可是我試到現在還沒成功過… 還變成好像 Assembly 被 Cache 起來的情況,怎麼 load, unload 都是用到舊的 Assembly… 天阿

參考:通过应用程序域AppDomain加载和卸载程序集之后,如何再返回原来的主程序域

Written by Kanru Chen

February 18th, 2006 at 10:11 pm

Posted in Programming

Tagged with ,

Short, Int, 加加減減

with one comment

short x = 32767;
x = x + 2;

在 C 裡面,我們可能不小心寫出這樣的 code 而不自覺會造成 overflow,但是在 C# 則會得到

error CS0266: Cannot implicitly convert type ‘int’ to ’short’. An explicit conversion exists (are you missing a cast?)

要改成

x = (short)(x +2);

才行; 不過神奇的是,如果寫成

x += 2;

則不會有任何問題的直接 overflow… 這究竟是什麼樣的設計考量呢?

Written by Kanru Chen

February 16th, 2006 at 12:26 pm

Posted in Programming

Tagged with

ASP.NET 2.0

leave comments »

今天下午心血來潮測試了一下 mono 的 ASP.NET 支援,mono 除了有 apache 用的 mod-mono 模組之外,還提供了一個 standalone 的簡單 webserver — xsp/xsp2 ,主要用來做開發測試用。

用 C# 來寫的程式都很正常,不管是 1.0 或 2.0 profile 都可以跑,可是後來想要測試 Nemerle 時卻不斷發生錯誤…

作 .NET compiler 的人應該要提供 Codedom 的 CodeCompiler,CodeGenerator 等等,供其他程式利用內部 DOM 結構動態產生程式碼,主要是用在 ASP.NET

原來 Nemerle 所實做的 Codedom 是從舊的 C# 版本移植的,後來沒有持續更新到 2.0 所以有些地方會出問題,像是沒有使用 partial class,不能把 aspx 與 codefile 分開等等,為了修正這些錯誤下午都在看 Codedom 與 System.Web.Compilation 的東西,現在已經可以正常的用 Nemerle 寫 ASP.NET 2.0 ;-)

patch 在這裡

Update: This patch has been applied.

Written by Kanru Chen

January 20th, 2006 at 11:07 pm

Posted in Programming

Tagged with ,

Suffix Tree

leave comments »

Suffix Tree,在字串處理上有非常多的應用,尤其是字串的搜尋、定位等等,利用這個資料結構很多都可以在 O(n) 也就是 linear time 做完。

這是在這學期的 字串學(stringology) 學到的,作業就是實做 suffix tree,我用的是 Ukkonen 的演算法,可以用 on-line construction 的方法建構 suffix tree。

我是用 Nemerle 寫的(因為在練習),後來也有用 C# 改了另外的版本,但是還是 Nemerle 的版本比較完整。原本是用 System.Collections.Generic.Dictionary 來存每個 State 裡面的 transitions,可是非常的吃記憶體,用 mono –profile 可以很容易看出來一個 500kb 的檔案,在建構的時候光 Dictionary 就可以吃掉近 150mb 的記憶體;後來改用自己的 linked list 來做,節省了很多資源,2mb 的測試檔可以在 9 秒內建完樹並回報內部節點個數。

整個程式應該還有多可以改進的地方,但是目前的效率作為作業已經可以接受了,先放上來。在這裡

Written by Kanru Chen

January 15th, 2006 at 12:58 am

Posted in Programming

Tagged with ,

ROTE - Our Own Terminal Emulation Library

leave comments »

librote, 是一個 console 底下的 terminal emulator ,這有什麼用呢?為什麼在 console 下還需要 terminal emulator?

因為它可以內嵌在在許多不同的程式裡面,尤其是 ncurses 的程式,可以把 rote 的 output 直接寫到一個 WINDOW 裡面,對於要內嵌 terminal 功能的程式來說非常方便,甚至還有人利用這個 library 寫出網頁用的 terminal emulator — anyterm

我是在找有什麼東西可以幫助我完成 console-chewing 的 library 時發現這個的,真的非常方便,剛剛花了些時間便完成最基礎的中文輸入功能。(以 off-the-screen 的方式)

不過對於多位元的編碼似乎無法處理,一些 utf8 的字會被切割,看來還是要花時間 hack 了。

Written by Kanru Chen

January 13th, 2006 at 2:06 am

Posted in Programming

Tagged with ,

SIC/XE Assembler

with one comment

SIC 是 Simplified Instruction Computer

這是系統程式的作業..,我用 perl 寫的,大致功能都有了,但是 code 還不是很乾淨。

懶得整理了,程式可以從這裡抓到。

ps. 隨便 google 了一下,還真多人寫過這程式… XD

Written by Kanru Chen

January 6th, 2006 at 1:39 am

Posted in Programming

System.Text.UTF8Encoding

with 3 comments

在用 SmartIRC4Net 寫 irc 程式的時候發現,如果把 encoding 設成 System.Text.Encoding.UTF8 會導致無法連上 irc server。

很好奇的看了 SmartIRC4Net 的 code 結果沒什麼收穫,用的是一般的 StreamWriter 的初始化方法;如果改用 System.Text.UTF8Encoding 就可以正確連上了,而且這是 StreamWriter 預設的 encoding。

到底 Encoding.UTF8 和 UTF8Encoding 兩個有什麼不一樣呢?看了一下 mono 的實做方法,使用上這兩個都是 用 UTF8Encoding 的 instance,只是 Encoding.UTF8 是有 BOM 的,UTF8Encoding 則預設沒有。

就是因為那個多出來的 BOM 導致 irc server 無法認出正確的指令(因為不是 vaild ascii code),在一般使用上也要注意這兩者的差別。

Written by Kanru Chen

December 29th, 2005 at 2:25 am

Posted in Programming

Tagged with ,

Nemerle - Redefining symbols

leave comments »

在 nemerle 裡面,可以用

def a = 123;

這樣的格式來定義一個 immutable 的變數

然而,這樣的寫法也是 vaild 的

def a = 123;
def a = "123";

雖然看起來像是給 immutable 的 a assign 了兩次不同的值,實際上兩者有完全不同的意義,所以

def (x, y) = (1, 3);
def (x, y) = (y, x);

也都是被接受的語法。

詳細:Side note: redefining symbols

Written by Kanru Chen

December 28th, 2005 at 10:57 pm

Posted in Programming

Tagged with ,