顯示具有 Programming 標籤的文章。 顯示所有文章
顯示具有 Programming 標籤的文章。 顯示所有文章

星期二, 5月 18, 2010

Linux中的Type with Constant Length

許久以來,programming時常看到一堆類似的type define.

今天剛好用到,就整理一下嘍













---------------------





Linux中用户态和内核态定义了若干定长类型, 以32位无符号整数为例:





u_int32_t (BSD类型,目前netfilter中就使用这种类型)





用户态定义位于sys/types.h





在内核态见头文件include/linux/types.h





typedef __u32 u_int32_t;





uint32_t (C99类型, 内核态和用户态都能使用的类型,可移植性更好)





在用户态中见头文件/usr/include/stdint.h





在内核态见头文件include/linux/types.h





typedef __u32 uint32_t;











u32





typedef unsigned int u32;





include/asm-i386/types.h





只能在内核态使用











__u32 (Linux严格类型,内核中应该使用这种类型)





typedef unsigned int __u32;





include/asm-i386/types.h





也可以在用户态使用











unsigned long





unsigned int





标准C语言整数类型, 架构相关













----轉自周知远的博客













總結來說,推薦使用 uintxx_t 系列的C99 type.





只要include 即可。




















星期一, 12月 07, 2009

work around with vimdiff + cvs diff

We'd like to make cvs's diff output colorful.

If you're vim people, you could do it in 2 ways.



  • Redirect cvs diff to vim by "| vim -".

    Then change highlight settings in .vimrc as follows:




    • hi diffAdded ctermfg=Yellow ctermbg=DarkGray guifg=Yellow


    • hi diffRemoved ctermfg=Red ctermbg=DarkGray guifg=Red




  • Or, you could use vimdiff by running the script(cvsvimdiff.sh) in the end.

    This script let you enjoy vimdiff's block diff with cvs.

    Usage: "cvsvimdiff -r version1 -r version2 filename"

    Besides, you should change color scheme to make a comfortable displays of vimdiff.




    • hi DiffAdd term=reverse cterm=bold ctermbg=green ctermfg=white


    • hi DiffChange term=reverse cterm=bold ctermbg=cyan ctermfg=black


    • hi DiffText term=reverse cterm=bold ctermbg=gray ctermfg=black


    • hi DiffDelete term=reverse cterm=bold ctermbg=red ctermfg=black





The following is cvsvimdiff.sh. You can create it and put it in your PATH.



#!/bin/bash

#

# Copyright (C) 2005-2006,

# Stefano Zacchiroli

# Enrico Tassi

#

# This is free software, you can redistribute it and/or modify it under the

# terms of the GNU General Public License version 2 as published by the Free

# Software Foundation.

#



vimdiff="vimdiff"

suffix="vimcvsdiff"

rev1="HEAD"



if [ "$1" == "-g" ] ; then

vimdiff="gvimdiff -f"

shift 1

fi

if [ "$1" == "-r" ]; then

rev1="$2"

shift 2

fi

if [ "$1" == "-r" ]; then

rev2="$2"

shift 2

fi



files="$1"

if [ -z "$files" ]; then

files=$(cvs -n update -r HEAD 2> /dev/null | grep -e "^[MU]" | cut -c 3-)

fi

for f in $files; do

if ! [ -f $f ]; then break; fi

patch=`mktemp $prefix.XXXXXX`

orig=`mktemp $prefix.XXXXXX`

trap "rm -f $patch $orig" EXIT



if ! [ -z "$rev2" ]; then

tmp1=$f.$rev1

tmp2=$f.$rev2

cvs up -p -r $rev1 $f > $tmp1

cvs up -p -r $rev2 $f > $tmp2

$vimdiff $tmp1 $tmp2

rm -f $tmp1 $tmp2

else

cp "$f" $orig

cut -d '/' -f 2 < CVS/Entries | grep "^$f\$" > /dev/null || break

cvs diff -r $rev1 -u "$f" > $patch

if ! [ $? -eq 1 ]; then break; fi

cp "$f" $orig

patch -R -p0 $orig $patch

$vimdiff $orig $f

fi

done


星期六, 10月 13, 2007

Textmate的543

Well, seems textmate is the best code editor about keyword/code completion.
Although there are so many hotkey you need to remember before you can perform a MAGIC completion of all kinds of programming language, it still is worth a try.
##CONTINUE##
IF you are a vim user or your mother-tone is a CJK-language, you MIGHT need the following plugins of textmate.
There are so many "programmers" using VIM or something else. And some key bindings are very important, especially vim's hjkl moving mode. But native textmate doesn't support this.

Hence, the "vimate" was born and it is still under developing. The most recent update is on Sep. 07'.

Another important thing is the width of the CJK words are 2-byte and obviously textmate treat them as 1-byte font. 
A good news is textmate 2.0 will support utf8. But just like the slogan of apple's new OS - ONLY IN LEOPARD ! Yes, textmate 2.0 is only in leopard. Damn...

Before apple ships leopard, we have another solution: make a new font with 1-byte-width CJK word. Thanks to Japanese-hetima to provide this idea and shinbang to provide his own new font.

星期五, 8月 03, 2007

漫畫下載器....

最近學了tcl,而又想寫個大陸的漫畫網站的gg人,所以就決定用tcl+tk啦~
在今天大功告成,簡記幾個mistakes
分別是writing binary data, use ::http to get binary data with -channel specified, get http in background
##CONTINUE##
  • Writing Binary Data: 平常都是使用 puts $channel "string" 寫入資料,但是當想寫入binary資料時,就必需要設定 fconfigure $channel -translation binary 來更正channel的設定

  • http package: 其實想把http的data content寫到檔案中,也可以用set $channel [open ...]打開channel,然後使用 ::http::geturl $URL -channel $channel,將 content直接放到channel中。這個方法就不需要先改channel的translation了。

  • fetch http in background: 基本上,一本漫畫都是近百頁的。如果一次抓一張,實在是.....有些慢。所以使用 ::http::geturl $URL -command Callbackfunction ,做到non-blocked http request with callback function。雖然每個檔案不是使用多分割下載,但是已經比之前好很多了。唯一的困難點在於參數的傳遞,因為callback function的參數是定死的。還好能從URL中parse圖片的名字。

附上目前的 source code

package require http
frame .n1
label .n1.filetosave -text "Save to:"
entry .n1.folder -textvariable folder
button .n1.openfolder -text "openfolder" -command setfolder
button .n1.rename -text "rename" -command rename
pack .n1.filetosave .n1.folder .n1.openfolder .n1.rename -side left
frame .n2
label .n2.folderurl -text "folderurl:"
entry .n2.url -textvariable url
button .n2.download -text "download" -command download
button .n2.stop -text "stop" -command stop
pack .n2.folderurl .n2.url .n2.download .n2.stop -side left
label .debug -text ""
label .state -text "Idle"
pack .state .n1 .n2 .debug -side top -fill both
set folder "comic_temp"
set serverno 2
set url "http://dm.99770.com/Comic/618/25601/?v=1*s=2"
set pagenumber 5
# =================================================================
# get http://dm.99770.com/Comic/618/25601/?v=1*s=2
# find pattern:
# 1. picurl=""
# 2. http://.....love.js
# get *love.js
# find pattern: ServerList[]
proc setfolder {} {
global folder
set tfolder [tk_chooseDirectory ]
if {$tfolder!= ""} {
set folder $tfolder
}
}
proc httpcallback { file token } {
# upvar #0 $token state
set fn [open $file w]
fconfigure $fn -translation binary
puts $fn [::http::data $token]
close $fn
}
proc wget { url { file "NULL"} } {
if {$file=="NULL"} {
return [::http::data [::http::geturl $url]]
} else {
::http::geturl $url -command [list httpcallback $file]
}
}
proc stop {} {
if {[.state cget -text]=="Downloading"} {
.state configure -text "Stop"
}
}
proc download {} {
global serverno ,pagenumber
set url [.n2.url get ]
set server ""

set fd [.n1.folder get]
if {[file exists $fd]==0} {
file mkdir $fd
}
cd $fd

.debug configure -text ""
.state configure -text "Downloading"
regexp {s=(\d+)} $url tmp serverno
set data [wget $url]
regexp {http[^>]*love.js} $data matched
debug [format "%s%s" "love js:" $matched]
set lovejs [wget $matched]
set i 0
while {\
[regexp -indices -line {^ServerList\[\d\]=[^;]*;} $lovejs index] } {
incr i
set tt [string range $lovejs [lindex $index 0] [lindex $index 1]]
if {$i==$serverno} {
regexp {http.*/} $tt server
debug [format "server: %s" $server]
}
set lovejs [string range $lovejs [lindex $index 1] end]
}

set index [expr [string last "/" $url]+1]
set baseurl [string replace $url $index end]
set i 1
while {1} {
if {[.state cget -text]=="Stop"} {
return
}
set url [format "%s%d.htm" $baseurl $i]
set data [wget $url]
regexp {picurl=\"([^;]*)\"} $data pic pic2
regexp {datas=([^;]*)} $data tmp pagenumber
set url [format "%s%s" $server $pic2]
set pic [lindex [split $pic2 "/"] end]
wget $url [format "%.3d%s" $i [string range $pic end-3 end]]
if {$i>=$pagenumber} {
break
}
incr i
}
.state configure -text "Complete"
cd ..
}

proc rename {} {
cd [.n1.folder get]
set i 1
foreach {f} [glob -nocomplain *] {
file rename $f [format "%.3d%s" $i [string range $f end-3 end]]
incr i
}
cd ..
}

proc debug { err } {
.debug configure -text [format "%s\n%s" [.debug cget -text] $err]
}

星期六, 6月 16, 2007

Error: unresolved external symbol _WinMain@16

  1. 如果你是用VS開新專案,然後手動加入source code
  2. 如果以前是用VS2003或是更早的版本,然後轉到VS2005上
  3. 想把一個Win32 Console Application增加 UI,但是背後的Console Window不知如何拿掉
  4. 想把專案的Console Window叫出來。
以上的狀況,常會遇到一個Error:[ unresolved external symbol _WinMain@16 referenced in
function ___tmainCRTStartup ]。其中,粗體字部分也許會不同。
這種問題是因為,Source Code中的main,也就是我們預計的程式enrty point 與VS預期的名字相異,造成VS抓不到entry point,當然就出現了unresolved symbol。
一般來說,是由於SubSystem沒設定好所造成的。 更改Linker->System->SubSystem即可
但如果你的程式中,有新舊兩種WinAPI,比如include 又加入新版的Window Form( 有design view的 ),此時VS可能會搞混,所以上面的方法就失效了。

今天花了4個小時左右,終於把這個問題搞懂.....Orz
重點記錄幾個地方:( 以VS2005為例 ) Project Configuration Property中
  1. General -> CRL support:如果用VS2005的Windows Form,是需要用到CRL( /crl )的,如果想把project compile成lib、dll,似乎要用/crl safe。
  2. C/C++ -> Preprocessor:Definitions 中,有無_CONSOLE、_WINDOWS、WINDOWS等。這個地方其實就等同設定#define _CONSOLE...等。
  3. C/C++ -> Precompiled Headers:一般是設定成stdafx.h。不設定....似乎也沒差。
  4. Linker -> System -> SubSystem:設定成 WINDOWS、CONSOLE兩類最常見。如果沒設定,VS會看你的main function來選擇
    1. CONSOLE:Win32 character-mode application. Console applications are given a console by the operating system. If main or wmain is defined, CONSOLE is the default.
    2. Extensible Firmware Interface:The EFI_* subsystems. See the EFI specification for more information. For example, see the Intel web site. The minimum version and default version is 1.0.
    3. NATIVE:Device drivers for Windows NT. If /DRIVER:WDM is specified, NATIVE is the default.
    4. POSIX:Application that runs with the POSIX subsystem in Windows NT.
    5. WINDOWS:Application does not require a console, probably because it creates its own windows for interaction with the user. If WinMain or wWinMain is defined, WINDOWS is the default.
  5. Linker -> Advanced -> Entry Point:其實就是指定你的main function的起始位置。這是用在當你選了SubSystem成Console或是Windows,VS卻認錯你的entry point。此時,用這邊手動指定即可。