2011-06-28

merge CIDR

Perl提供了個方便的模組CIDR-Lite,可將單一IP或IP Range整合成CIDR格式輸出。
#!/usr/bin/perl
use Net::CIDR::Lite;

my $cidr = Net::CIDR::Lite->new;
$cidr->add("192.168.0.0/23");
$cidr->add("192.168.2.0/23");
$cidr->add("192.168.4.0/24");
$cidr->add("192.168.5.0/24");
$cidr->add_range("192.168.6.0-192.168.7.255");
print "$_\n" for $cidr->list;

輸出結果:
192.168.0.0/21

2011-06-22

PerlRegEx for Delphi

Regular Expressions -- 在一堆雜亂的文字資料中,快速有效幫你列出符合格式資料的不二選擇,不論是system log、XML或是HTML..等等,在效率上有些時候並不遜於用XML Parser或HTML Parser,但是比專用的Parser更具彈性。
在FreeBSD/Linix的環境使用習慣了,但Delphi預設並沒有功能類似的元件,還好有個好用的免費元件Delphi Regular Expressions Classes可供使用,否則真有殘廢一半了的感覺。
於使用TPerlRegEx類別時必須將相關的程式部份撰寫完整再進行程式測試,否則會造成Delphi產生[Fatal Error] Internal error: L3169 或 [Fatal Error] Internal error: L3170的怪怪錯誤,白癡的我還以為是工作環境因為亂玩又出了狀況,就把它給掛了重來..都用1、2年了..唉..真是%$^&*#....囧。

2011-06-02

Delphi Inifiles Buffer Size 修正

Delphi提供了方便讀寫.ini檔的物件TIniFile,但是預設ReadSection的buffer size僅只16k,如果有較大量資料讀取時,會造成資料讀取不完全的問題。
其實經過修正source code再產生新的.dcu檔,並覆蓋原始同名的檔案即可解決這個問題,步驟如下:
  • Delphi 版本 7.0
  • 需修正的檔案\Delphi7\Source\Rtl\Common\IniFiles.pas
  • 需更新的檔案\Delphi7\Lib\IniFiles.dcu
  1. File -> New -> Application
  2. Project -> Add to Project -> 選取 \Delphi7\Source\Rtl\Common\IniFiles.pas
  3. 找到下列下列程式碼並自行修正buffer size
    procedure TIniFile.ReadSection(const Section: string; Strings: TStrings);
    const
       BufSize = 16384;  //<-預設為16k
  4. 儲存檔案,File -> Save
  5. 設定Compiler參數,Project -> Options -> Compiler -> 取消Debugging群組所有的選項
  6. 產生IniFiles.dcu檔,Project -> Compile Project or Ctrl + F9
  7. 將\Delphi7\Source\Rtl\Common\IniFiles.dcu覆蓋原檔\Delphi7\Lib\IniFiles.dcu即更新完成