• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            網(wǎng)絡(luò)服務(wù)器軟件開發(fā)/中間件開發(fā),關(guān)注ACE/ICE/boost

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              152 Posts :: 3 Stories :: 172 Comments :: 0 Trackbacks

            #

            MySQL服務(wù)維護(hù)筆記


            內(nèi)容摘要:使用MySQL服務(wù)的一些經(jīng)驗(yàn),主要從以下幾個(gè)方面考慮的MySQL服務(wù)規(guī)劃設(shè)計(jì)。對于高負(fù)載站點(diǎn)來說PHP和MySQL運(yùn)行在一起(或者說任何應(yīng)用和數(shù)據(jù)庫運(yùn)行在一起的規(guī)劃)都是性能最大的瓶頸,這樣的設(shè)計(jì)有如讓人一手畫圓一手畫方,這樣2個(gè)人的工作效率肯定不如讓一個(gè)人專門畫圓一個(gè)人專門畫方效率高,讓應(yīng)用和數(shù)據(jù)庫都跑在一臺高性能服務(wù)器上說不定還不如跑在2臺普通服務(wù)器上快。

            以下就是針對MySQL作為專門的數(shù)據(jù)庫服務(wù)器的優(yōu)化建議:

            1. MySQL服務(wù)的安裝/配置的通用性;
            2. 系統(tǒng)的升級和數(shù)據(jù)遷移方便性;
            3. 備份和系統(tǒng)快速恢復(fù);
            4. 數(shù)據(jù)庫應(yīng)用的設(shè)計(jì)要點(diǎn);
            5. 一次應(yīng)用優(yōu)化實(shí)戰(zhàn);

            MySQL服務(wù)器的規(guī)劃
            =================
            為了以后維護(hù),升級備份的方便和數(shù)據(jù)的安全性,最好將MySQL程序文件和數(shù)據(jù)分別安裝在“不同的硬件”上。

                     /   / 
            | /usr <== 操作系統(tǒng)
            | /home/mysql <== mysql主目錄,為了方便升級,這只是一個(gè)最新版本目錄的鏈接
            硬盤1==>| /home/mysql-3.23.54/ <== 最新版本的mysql /home/mysql鏈接到這里
            \ /home/mysql-old/ <== 以前運(yùn)行的舊版本的mysql

            / /data/app_1/ <== 應(yīng)用數(shù)據(jù)和啟動腳本等
            硬盤2==>| /data/app_2/
            \ /data/app_3/

            MySQL服務(wù)的安裝和服務(wù)的啟動:
            MySQL一般使用當(dāng)前STABLE的版本:
            盡量不使用--with-charset=選項(xiàng),我感覺with-charset只在按字母排序的時(shí)候才有用,這些選項(xiàng)會對數(shù)據(jù)的遷移帶來很多麻煩。
            盡量不使用innodb,innodb主要用于需要外鍵,事務(wù)等企業(yè)級支持,代價(jià)是速度比MYISAM有數(shù)量級的下降。
            ./configure --prefix=/home/mysql --without-innodb
            make
            make install

            服務(wù)的啟動和停止
            ================
            1 復(fù)制缺省的mysql/var/mysql到 /data/app_1/目錄下,
            2 MySQLD的啟動腳本:start_mysql.sh
            #!/bin/sh
            rundir=`dirname "$0"`
            echo "$rundir"
            /home/mysql/bin/safe_mysqld --user=mysql --pid-file="$rundir"/mysql.pid --datadir="$rundir"/var "$@"\
            -O max_connections=500 -O wait_timeout=600 -O key_buffer=32M --port=3402 --socket="$rundir"/mysql.sock &

            注釋:
            --pid-file="$rundir"/mysql.pid --socket="$rundir"/mysql.sock --datadir="$rundir"/var
            目的都是將相應(yīng)數(shù)據(jù)和應(yīng)用臨時(shí)文件放在一起;
            -O 后面一般是服務(wù)器啟動全局變量優(yōu)化參數(shù),有時(shí)候需要根據(jù)具體應(yīng)用調(diào)整;
            --port: 不同的應(yīng)用使用PORT參數(shù)分布到不同的服務(wù)上去,一個(gè)服務(wù)可以提供的連接數(shù)一般是MySQL服務(wù)的主要瓶頸;

            修改不同的服務(wù)到不同的端口后,在rc.local文件中加入:
            /data/app_1/start_mysql.sh
            /data/app_2/start_mysql.sh
            /data/app_3/start_mysql.sh
            注意:必須寫全路徑

            3 MySQLD的停止腳本:stop_mysql.sh
            #!/bin/sh
            rundir=`dirname "$0"`
            echo "$rundir"
            /home/mysql/bin/mysqladmin -u mysql -S"$rundir"/mysql.sock shutdown

            使用這個(gè)腳本的好處在于:
            1 多個(gè)服務(wù)啟動:對于不同服務(wù)只需要修改腳本中的--port[=端口號]參數(shù)。單個(gè)目錄下的數(shù)據(jù)和服務(wù)腳本都是可以獨(dú)立打包的。
            2 所有服務(wù)相應(yīng)文件都位于/data/app_1/目錄下:比如:mysql.pid mysql.sock,當(dāng)一臺服務(wù)器上啟動多個(gè)服務(wù)時(shí),多個(gè)服務(wù)不會互相影響。但都放到缺省的/tmp/下則有可能被其他應(yīng)用誤刪。
            3 當(dāng)硬盤1出問題以后,直接將硬盤2放到一臺裝好MySQL的服務(wù)器上就可以立刻恢復(fù)服務(wù)(如果放到my.cnf里則還需要備份相應(yīng)的配置文件)。

            服務(wù)啟動后/data/app_1/下相應(yīng)的文件和目錄分布如下:
            /data/app_1/
                start_mysql.sh 服務(wù)啟動腳本
                stop_mysql.sh 服務(wù)停止腳本
                mysql.pid 服務(wù)的進(jìn)程ID
                mysql.sock 服務(wù)的SOCK
                var/ 數(shù)據(jù)區(qū)
                   mysql/ 用戶庫
                   app_1_db_1/ 應(yīng)用庫
                   app_1_db_2/
            ...
            /data/app_2/
            ...

            查看所有的應(yīng)用進(jìn)程ID:
            cat /data/*/mysql.pid

            查看所有數(shù)據(jù)庫的錯(cuò)誤日志:
            cat /data/*/var/*.err

            個(gè)人建議:MySQL的主要瓶頸在PORT的連接數(shù)上,因此,將表結(jié)構(gòu)優(yōu)化好以后,相應(yīng)單個(gè)MySQL服務(wù)的CPU占用仍然在10%以上,就要考慮將服務(wù)拆分到多個(gè)PORT上運(yùn)行了。

            服務(wù)的備份
            ==========
            盡量使用MySQL DUMP而不是直接備份數(shù)據(jù)文件,以下是一個(gè)按weekday將數(shù)據(jù)輪循備份的腳本:備份的間隔和周期可以根據(jù)備份的需求確定
            /home/mysql/bin/mysqldump -S/data/app_1/mysql.sock -umysql db_name | gzip -f>/path/to/backup/db_name.`date +%w`.dump.gz
            因此寫在CRONTAB中一般是:
            15 4 * * * /home/mysql/bin/mysqldump -S/data/app_1/mysql.sock -umysql db_name | gzip -f>/path/to/backup/db_name.`date +\%w`.dump.gz
            注意:
            1 在crontab中'%'需要轉(zhuǎn)義成'\%'
            2 根據(jù)日志統(tǒng)計(jì),應(yīng)用負(fù)載最低的時(shí)候一般是在早上4-6點(diǎn)

            先備份在本地然后傳到遠(yuǎn)程的備份服務(wù)器上,或者直接建立一個(gè)數(shù)據(jù)庫備份帳號,直接在遠(yuǎn)程的服務(wù)器上備份,遠(yuǎn)程備份只需要將以上腳本中的-S /path/to/msyql.sock改成-h IP.ADDRESS即可。

            數(shù)據(jù)的恢復(fù)和系統(tǒng)的升級
            ======================
            日常維護(hù)和數(shù)據(jù)遷移:在數(shù)據(jù)盤沒有被破壞的情況下
            硬盤一般是系統(tǒng)中壽命最低的硬件。而系統(tǒng)(包括操作系統(tǒng)和MySQL應(yīng)用)的升級和硬件升級,都會遇到數(shù)據(jù)遷移的問題。
            只要數(shù)據(jù)不變,先裝好服務(wù)器,然后直接將數(shù)據(jù)盤(硬盤2)安裝上,只需要將啟動腳本重新加入到rc.local文件中,系統(tǒng)就算是很好的恢復(fù)了。

            災(zāi)難恢復(fù):數(shù)據(jù)庫數(shù)據(jù)本身被破壞的情況下
            確定破壞的時(shí)間點(diǎn),然后從備份數(shù)據(jù)中恢復(fù)。

            應(yīng)用的設(shè)計(jì)要點(diǎn)
            ==============
            如果MySQL應(yīng)用占用的CPU超過10%就應(yīng)該考慮優(yōu)化了。

            1. 如果這個(gè)服務(wù)可以被其他非數(shù)據(jù)庫應(yīng)用代替(比如很多基于數(shù)據(jù)庫的計(jì)數(shù)器完全可以用WEB日志統(tǒng)計(jì)代替)最好將其禁用:
              非用數(shù)據(jù)庫不可嗎?雖然數(shù)據(jù)庫的確可以簡化很多應(yīng)用的結(jié)構(gòu)設(shè)計(jì),但本身也是一個(gè)系統(tǒng)資源消耗比較大的應(yīng)用。在某些情況下文本,DBM比數(shù)據(jù)庫是更好的選擇,比如:很多應(yīng)用如果沒有很高的實(shí)時(shí)統(tǒng)計(jì)需求的話,完全可以先記錄到文件日志中,定期的導(dǎo)入到數(shù)據(jù)庫中做后續(xù)統(tǒng)計(jì)分析。如果還是需要記錄簡單的2維鍵-值對應(yīng)結(jié)構(gòu)的話可以使用類似于DBM的HEAP類型表。因?yàn)镠EAP表全部在內(nèi)存中存取,效率非常高,但服務(wù)器突然斷電時(shí)有可能出現(xiàn)數(shù)據(jù)丟失,所以非常適合存儲在線用戶信息,日志等臨時(shí)數(shù)據(jù)。即使需要使用數(shù)據(jù)庫的,應(yīng)用如果沒有太復(fù)雜的數(shù)據(jù)完整性需求的化,完全可以不使用那些支持外鍵的商業(yè)數(shù)據(jù)庫,比如MySQL。只有非常需要完整的商業(yè)邏輯和事務(wù)完整性的時(shí)候才需要Oracle這樣的大型數(shù)據(jù)庫。對于高負(fù)載應(yīng)用來說完全可以把日志文件,DBM,MySQL等輕量級方式做前端數(shù)據(jù)采集格式,然后用Oracle MSSQL DB2 Sybase等做數(shù)據(jù)庫倉庫以完成復(fù)雜的數(shù)據(jù)庫挖掘分析工作。
              有朋友和我說用標(biāo)準(zhǔn)的MyISAM表代替了InnoDB表以后,數(shù)據(jù)庫性能提高了20倍。

            2. 數(shù)據(jù)庫服務(wù)的主要瓶頸:單個(gè)服務(wù)的連接數(shù)
              對于一個(gè)應(yīng)用來說,如果數(shù)據(jù)庫表結(jié)構(gòu)的設(shè)計(jì)能夠按照數(shù)據(jù)庫原理的范式來設(shè)計(jì)的話,并且已經(jīng)使用了最新版本的MySQL,并且按照比較優(yōu)化的方式運(yùn)行了,那么最后的主要瓶頸一般在于單個(gè)服務(wù)的連接數(shù),即使一個(gè)數(shù)據(jù)庫可以支持并發(fā)500個(gè)連接,最好也不要把應(yīng)用用到這個(gè)地步,因?yàn)椴l(fā)連接數(shù)過多數(shù)據(jù)庫服務(wù)本身用于調(diào)度的線程的開銷也會非常大了。所以如果應(yīng)用允許的話:讓一臺機(jī)器多跑幾個(gè)MySQL服務(wù)分擔(dān)。將服務(wù)均衡的規(guī)劃到多個(gè)MySQL服務(wù)端口上:比如app_1 ==> 3301 app_2 ==> 3302...app_9 ==> 3309。一個(gè)1G內(nèi)存的機(jī)器跑上10個(gè)MySQL是很正常的。讓10個(gè)MySQLD承擔(dān)1000個(gè)并發(fā)連接效率要比讓2個(gè)MySQLD承擔(dān)1000個(gè)效率高的多。當(dāng)然,這樣也會帶來一些應(yīng)用編程上的復(fù)雜度;

            3. 使用單獨(dú)的數(shù)據(jù)庫服務(wù)器(不要讓數(shù)據(jù)庫和前臺WEB服務(wù)搶內(nèi)存),MySQL擁有更多的內(nèi)存就可能能有效的進(jìn)行結(jié)果集的緩存;在前面的啟動腳本中有一個(gè)-O key_buffer=32M參數(shù)就是用于將缺省的8M索引緩存增加到32M(當(dāng)然對于)

            4. 應(yīng)用盡量使用PCONNECT和polling機(jī)制,用于節(jié)省MySQL服務(wù)建立連接的開銷,但也會造成MySQL并發(fā)鏈接數(shù)過多(每個(gè)HTTPD都會對應(yīng)一個(gè)MySQL線程);

            5. 表的橫向拆分:讓最常被訪問的10%的數(shù)據(jù)放在一個(gè)小表里,90%的歷史數(shù)據(jù)放在一個(gè)歸檔表里(所謂:快慢表),數(shù)據(jù)中間通過定期“搬家”和定期刪除無效數(shù)據(jù)來節(jié)省,畢竟大部分應(yīng)用(比如論壇)訪問2個(gè)月前數(shù)據(jù)的幾率會非常少,而且價(jià)值也不是很高。這樣對于應(yīng)用來說總是在一個(gè)比較小的結(jié)果級中進(jìn)行數(shù)據(jù)選擇,比較有利于數(shù)據(jù)的緩存,不要指望MySQL中對單表記錄條數(shù)在10萬級以上還有比較高的效率。而且有時(shí)候數(shù)據(jù)沒有必要做那么精確,比如一個(gè)快表中查到了某個(gè)人發(fā)表的文章有60條結(jié)果,快表和慢表的比例是1:20,那么就可以簡單的估計(jì)這個(gè)人一共發(fā)表了1200篇。Google的搜索結(jié)果數(shù)也是一樣:對于很多上十萬的結(jié)果數(shù),后面很多的數(shù)字都是通過一定的算法估計(jì)出來的。

            6. 數(shù)據(jù)庫字段設(shè)計(jì):表的縱向拆分(過渡范化):將所有的定長字段(char, int等)放在一個(gè)表里,所有的變長字段(varchar,text,blob等)放在另外一個(gè)表里,2個(gè)表之間通過主鍵關(guān)聯(lián),這樣,定長字段表可以得到很大的優(yōu)化(這樣可以使用HEAP表類型,數(shù)據(jù)完全在內(nèi)存中存?。?,這里也說明另外一個(gè)原則,對于我們來說,盡量使用定長字段可以通過空間的損失換取訪問效率的提高。在MySQL4中也出現(xiàn)了支持外鍵和事務(wù)的InnoDB類型表,標(biāo)準(zhǔn)的MyISAM格式表和基于HASH結(jié)構(gòu)的HEAP內(nèi)存表,MySQL之所以支持多種表類型,實(shí)際上是針對不同應(yīng)用提供了不同的優(yōu)化方式;

            7. 仔細(xì)的檢查應(yīng)用的索引設(shè)計(jì):可以在服務(wù)啟動參數(shù)中加入 --log-slow-queries[=file]用于跟蹤分析應(yīng)用瓶頸,對于跟蹤服務(wù)瓶頸最簡單的方法就是用MySQL的status查看MySQL服務(wù)的運(yùn)行統(tǒng)計(jì)和show processlist來查看當(dāng)前服務(wù)中正在運(yùn)行的SQL,如果某個(gè)SQL經(jīng)常出現(xiàn)在PROCESS LIST中,一。有可能被查詢的此時(shí)非常多,二,里面有影響查詢的字段沒有索引,三,返回的結(jié)果數(shù)過多數(shù)據(jù)庫正在排序(SORTING);所以做一個(gè)腳本:比如每2秒運(yùn)行以下show processlist;把結(jié)果輸出到文件中,看到底是什么查詢在吃CPU。

            8. 全文檢索:如果相應(yīng)字段沒有做全文索引的話,全文檢索將是一個(gè)非常消耗CPU的功能,因?yàn)槿臋z索是用不上一般數(shù)據(jù)庫的索引的,所以要進(jìn)行相應(yīng)字段記錄遍歷。關(guān)于全文索引可以參考一下基于Java的全文索引引擎lucene的介紹。

            9. 前臺應(yīng)用的記錄緩存:比如一個(gè)經(jīng)常使用數(shù)據(jù)庫認(rèn)證,如果需要有更新用戶最后登陸時(shí)間的操作,最好記錄更新后就把用戶放到一個(gè)緩存中(設(shè)置2個(gè)小時(shí)后過期),這樣如果用戶在2個(gè)小時(shí)內(nèi)再次使用到登陸,就直接從緩存里認(rèn)證,避免了過于頻繁的數(shù)據(jù)庫操作。

            10. 查詢優(yōu)先的表應(yīng)該盡可能為where和order by字句中的字段加上索引,數(shù)據(jù)庫更新插入優(yōu)先的應(yīng)用索引越少越好。

            總之:對于任何數(shù)據(jù)庫單表記錄超過100萬條優(yōu)化都是比較困難的,關(guān)鍵是要把應(yīng)用能夠轉(zhuǎn)化成數(shù)據(jù)庫比較擅長的數(shù)據(jù)上限內(nèi)。也就是把復(fù)雜需求簡化成比較成熟的解決方案內(nèi)。

            一次優(yōu)化實(shí)戰(zhàn)
            ============
            以下例子是對一個(gè)論壇應(yīng)用進(jìn)行的優(yōu)化:

            1. 用Webalizer代替了原來的通過數(shù)據(jù)庫的統(tǒng)計(jì)。
            2. 首先通過TOP命令查看MySQL服務(wù)的CPU占用左右80%和內(nèi)存占用:10M,說明數(shù)據(jù)庫的索引緩存已經(jīng)用完了,修改啟動參數(shù),增加了-O key_buffer=32M,過一段時(shí)間等數(shù)據(jù)庫穩(wěn)定后看的內(nèi)存占用是否達(dá)到上限。最后將緩存一直增加到64M,數(shù)據(jù)庫緩存才基本能充分使用。對于一個(gè)數(shù)據(jù)庫應(yīng)用來說,把內(nèi)存給數(shù)據(jù)庫比給WEB服務(wù)實(shí)用的多,因?yàn)镸ySQL查詢速度的提高能加快web應(yīng)用從而節(jié)省并發(fā)的WEB服務(wù)所占用的內(nèi)存資源。
            3. 用show processlist;統(tǒng)計(jì)經(jīng)常出現(xiàn)的SQL:

              每分鐘運(yùn)行一次show processlist并記錄日志:
              * * * * * (/home/mysql/bin/mysql -uuser -ppassword < /home/chedong/show_processlist.sql >>  /home/chedong/mysql_processlist.log)

              show_processlist.sql里就一句:
              show processlist;

              比如可以從日志中將包含where的字句過濾出來:
              grep where mysql_processlist.log
              如果發(fā)現(xiàn)有死鎖,一定要重新審視一下數(shù)據(jù)庫設(shè)計(jì)了,對于一般情況:查詢速度很慢,就將SQL where字句中沒有索引的字段加上索引,如果是排序慢就將order by字句中沒有索引的字段加上。對于有%like%的查詢,考慮以后禁用和使用全文索引加速。

            4. 還是根據(jù)show processlist;看經(jīng)常有那些數(shù)據(jù)庫被頻繁使用,考慮將數(shù)據(jù)庫拆分到其他服務(wù)端口上。

            MSSQL到MySQL的數(shù)據(jù)遷移:ACCESS+MySQL ODBC Driver

            在以前的幾次數(shù)據(jù)遷移實(shí)踐過程中,我發(fā)現(xiàn)最簡便的數(shù)據(jù)遷移過程并不是通過專業(yè)的數(shù)據(jù)庫遷移工具,也不是MSSQL自身的DTS進(jìn)行數(shù)據(jù)遷移(遷移過程中間會有很多表出錯(cuò)誤警告),但通過將MSSQL數(shù)據(jù)庫通過ACCESS獲取外部數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫中,然后用ACCESS的表==>右鍵==>導(dǎo)出,制定ODBC,通過MySQL的DSN將數(shù)據(jù)導(dǎo)出。這樣遷移大部分?jǐn)?shù)據(jù)都會非常順利,如果導(dǎo)出的表有索引問題,還會出添加索引提示(DTS就不行),然后剩余的工作就是在MySQL中設(shè)計(jì)字段對應(yīng)的SQL腳本了。

            參考文檔:

            MySQL的參考:
            http://dev.mysql.com/doc/

            posted @ 2008-01-12 17:45 true 閱讀(300) | 評論 (0)編輯 收藏

             
            守護(hù)進(jìn)程(Daemon)是運(yùn)行在后臺的一種特殊進(jìn)程。它獨(dú)立于控制終端并且周期性地執(zhí)行某種任務(wù)或等待處理某些發(fā)生的事件。守護(hù)進(jìn)程是一種很有用的進(jìn)程。 Linux的大多數(shù)服務(wù)器就是用守護(hù)進(jìn)程實(shí)現(xiàn)的。比如,Internet服務(wù)器inetd,Web服務(wù)器httpd等。同時(shí),守護(hù)進(jìn)程完成許多系統(tǒng)任務(wù)。比如,作業(yè)規(guī)劃進(jìn)程crond,打印進(jìn)程lpd等。
            守護(hù)進(jìn)程的編程本身并不復(fù)雜,復(fù)雜的是各種版本的Unix的實(shí)現(xiàn)機(jī)制不盡相同,造成不同 Unix環(huán)境下守護(hù)進(jìn)程的編程規(guī)則并不一致。需要注意,照搬某些書上的規(guī)則(特別是BSD4.3和低版本的System V)到Linux會出現(xiàn)錯(cuò)誤的。下面將給出Linux下守護(hù)進(jìn)程的編程要點(diǎn)和詳細(xì)實(shí)例。
            一. 守護(hù)進(jìn)程及其特性
            守護(hù)進(jìn)程最重要的特性是后臺運(yùn)行。在這一點(diǎn)上DOS下的常駐內(nèi)存程序TSR與之相似。其次,守護(hù)進(jìn)程必須與其運(yùn)行前的環(huán)境隔離開來。這些環(huán)境包括未關(guān)閉的文件描述符,控制終端,會話和進(jìn)程組,工作目錄以及文件創(chuàng)建掩模等。這些環(huán)境通常是守護(hù)進(jìn)程從執(zhí)行它的父進(jìn)程(特別是shell)中繼承下來的。最后,守護(hù)進(jìn)程的啟動方式有其特殊之處。它可以在Linux系統(tǒng)啟動時(shí)從啟動腳本/etc/rc.d中啟動,可以由作業(yè)規(guī)劃進(jìn)程crond啟動,還可以由用戶終端(通常是 shell)執(zhí)行。
            總之,除開這些特殊性以外,守護(hù)進(jìn)程與普通進(jìn)程基本上沒有什么區(qū)別。因此,編寫守護(hù)進(jìn)程實(shí)際上是把一個(gè)普通進(jìn)程按照上述的守護(hù)進(jìn)程的特性改造成為守護(hù)進(jìn)程。如果對進(jìn)程有比較深入的認(rèn)識就更容易理解和編程了。
            二. 守護(hù)進(jìn)程的編程要點(diǎn)
            前面講過,不同Unix環(huán)境下守護(hù)進(jìn)程的編程規(guī)則并不一致。所幸的是守護(hù)進(jìn)程的編程原則其實(shí)都一樣,區(qū)別在于具體的實(shí)現(xiàn)細(xì)節(jié)不同。這個(gè)原則就是要滿足守護(hù)進(jìn)程的特性。同時(shí),Linux是基于Syetem V的SVR4并遵循Posix標(biāo)準(zhǔn),實(shí)現(xiàn)起來與BSD4相比更方便。編程要點(diǎn)如下;
            1. 在后臺運(yùn)行。
            為避免掛起控制終端將Daemon放入后臺執(zhí)行。方法是在進(jìn)程中調(diào)用fork使父進(jìn)程終止,讓Daemon在子進(jìn)程中后臺執(zhí)行。
            if(pid=fork())
            exit(0);//是父進(jìn)程,結(jié)束父進(jìn)程,子進(jìn)程繼續(xù)
            2. 脫離控制終端,登錄會話和進(jìn)程組
            有必要先介紹一下Linux中的進(jìn)程與控制終端,登錄會話和進(jìn)程組之間的關(guān)系:進(jìn)程屬于一個(gè)進(jìn)程組,進(jìn)程組號(GID)就是進(jìn)程組長的進(jìn)程號(PID)。登錄會話可以包含多個(gè)進(jìn)程組。這些進(jìn)程組共享一個(gè)控制終端。這個(gè)控制終端通常是創(chuàng)建進(jìn)程的登錄終端。
            控制終端,登錄會話和進(jìn)程組通常是從父進(jìn)程繼承下來的。我們的目的就是要擺脫它們,使之不受它們的影響。方法是在第1點(diǎn)的基礎(chǔ)上,調(diào)用setsid()使進(jìn)程成為會話組長:
            setsid();
            說明:當(dāng)進(jìn)程是會話組長時(shí)setsid()調(diào)用失敗。但第一點(diǎn)已經(jīng)保證進(jìn)程不是會話組長。setsid()調(diào)用成功后,進(jìn)程成為新的會話組長和新的進(jìn)程組長,并與原來的登錄會話和進(jìn)程組脫離。由于會話過程對控制終端的獨(dú)占性,進(jìn)程同時(shí)與控制終端脫離。
            3. 禁止進(jìn)程重新打開控制終端
            現(xiàn)在,進(jìn)程已經(jīng)成為無終端的會話組長。但它可以重新申請打開一個(gè)控制終端。可以通過使進(jìn)程不再成為會話組長來禁止進(jìn)程重新打開控制終端:

            if(pid=fork())
            exit(0);//結(jié)束第一子進(jìn)程,第二子進(jìn)程繼續(xù)(第二子進(jìn)程不再是會話組長)
            4. 關(guān)閉打開的文件描述符
            進(jìn)程從創(chuàng)建它的父進(jìn)程那里繼承了打開的文件描述符。如不關(guān)閉,將會浪費(fèi)系統(tǒng)資源,造成進(jìn)程所在的文件系統(tǒng)無法卸下以及引起無法預(yù)料的錯(cuò)誤。按如下方法關(guān)閉它們:
            for(i=0;i 關(guān)閉打開的文件描述符close(i);>
            5. 改變當(dāng)前工作目錄
            進(jìn)程活動時(shí),其工作目錄所在的文件系統(tǒng)不能卸下。一般需要將工作目錄改變到根目錄。對于需要轉(zhuǎn)儲核心,寫運(yùn)行日志的進(jìn)程將工作目錄改變到特定目錄如/tmpchdir("/")
            6. 重設(shè)文件創(chuàng)建掩模
            進(jìn)程從創(chuàng)建它的父進(jìn)程那里繼承了文件創(chuàng)建掩模。它可能修改守護(hù)進(jìn)程所創(chuàng)建的文件的存取位。為防止這一點(diǎn),將文件創(chuàng)建掩模清除:umask(0);
            7. 處理SIGCHLD信號
            處理SIGCHLD信號并不是必須的。但對于某些進(jìn)程,特別是服務(wù)器進(jìn)程往往在請求到來時(shí)生成子進(jìn)程處理請求。如果父進(jìn)程不等待子進(jìn)程結(jié)束,子進(jìn)程將成為僵尸進(jìn)程(zombie)從而占用系統(tǒng)資源。如果父進(jìn)程等待子進(jìn)程結(jié)束,將增加父進(jìn)程的負(fù)擔(dān),影響服務(wù)器進(jìn)程的并發(fā)性能。在Linux下可以簡單地將 SIGCHLD信號的操作設(shè)為SIG_IGN。
            signal(SIGCHLD,SIG_IGN);
            這樣,內(nèi)核在子進(jìn)程結(jié)束時(shí)不會產(chǎn)生僵尸進(jìn)程。這一點(diǎn)與BSD4不同,BSD4下必須顯式等待子進(jìn)程結(jié)束才能釋放僵尸進(jìn)程。
            三. 守護(hù)進(jìn)程實(shí)例
            守護(hù)進(jìn)程實(shí)例包括兩部分:主程序test.c和初始化程序init.c。主程序每隔一分鐘向/tmp目錄中的日志test.log報(bào)告運(yùn)行狀態(tài)。初始化程序中的init_daemon函數(shù)負(fù)責(zé)生成守護(hù)進(jìn)程。讀者可以利用init_daemon函數(shù)生成自己的守護(hù)進(jìn)程。
            1. init.c清單

            #include < unistd.h >
            #include < signal.h >
            #include < sys/param.h >
            #include < sys/types.h >
            #include < sys/stat.h >
            void init_daemon(void)
            {
            int pid;
            int i;
            if(pid=fork())
            exit(0);//是父進(jìn)程,結(jié)束父進(jìn)程
            else if(pid< 0)
            exit(1);//fork失敗,退出
            //是第一子進(jìn)程,后臺繼續(xù)執(zhí)行
            setsid();//第一子進(jìn)程成為新的會話組長和進(jìn)程組長
            //并與控制終端分離
            if(pid=fork())
            exit(0);//是第一子進(jìn)程,結(jié)束第一子進(jìn)程
            else if(pid< 0)
            exit(1);//fork失敗,退出
            //是第二子進(jìn)程,繼續(xù)
            //第二子進(jìn)程不再是會話組長

            for(i=0;i< NOFILE;++i)//關(guān)閉打開的文件描述符
            close(i);
            chdir("/tmp");//改變工作目錄到/tmp
            umask(0);//重設(shè)文件創(chuàng)建掩模
            return;
            }
            2. test.c清單
            #include < stdio.h >
            #include < time.h >

            void init_daemon(void);//守護(hù)進(jìn)程初始化函數(shù)

            main()
            {
            FILE *fp;
            time_t t;
            init_daemon();//初始化為Daemon

            while(1)//每隔一分鐘向test.log報(bào)告運(yùn)行狀態(tài)
            {
            sleep(60);//睡眠一分鐘
            if((fp=fopen("test.log","a")) >=0)
            {
            t=time(0);
            fprintf(fp,"Im here at %sn",asctime(localtime(&t)) );
            fclose(fp);
            }
            }
            }
            以上程序在RedHat Linux6.0下編譯通過。步驟如下:
            編譯:gcc -g -o test init.c test.c
            執(zhí)行:./test
            查看進(jìn)程:ps -ef
            從輸出可以發(fā)現(xiàn)test守護(hù)進(jìn)程的各種特性滿足上面的要求。
            posted @ 2007-11-26 10:22 true 閱讀(502) | 評論 (1)編輯 收藏

             

            一 C++ 中 string與wstring互轉(zhuǎn)

            方法一:

            string WideToMutilByte(const wstring& _src)
            {
            int nBufSize = WideCharToMultiByte(GetACP(), 0, _src.c_str(),-1, NULL, 0, 0, FALSE);

            char *szBuf = new char[nBufSize];

            WideCharToMultiByte(GetACP(), 0, _src.c_str(),-1, szBuf, nBufSize, 0, FALSE);

            string strRet(szBuf);

            delete []szBuf;
            szBuf = NULL;

            return strRet;
            }

            wstring MutilByteToWide(const string& _src)
            {
            //計(jì)算字符串 string 轉(zhuǎn)成 wchar_t 之后占用的內(nèi)存字節(jié)數(shù)
            int nBufSize = MultiByteToWideChar(GetACP(),0,_src.c_str(),-1,NULL,0);

            //為 wsbuf 分配內(nèi)存 BufSize 個(gè)字節(jié)
            wchar_t *wsBuf = new wchar_t[nBufSize];

            //轉(zhuǎn)化為 unicode 的 WideString
            MultiByteToWideChar(GetACP(),0,_src.c_str(),-1,wsBuf,nBufSize);

            wstring wstrRet(wsBuf);

            delete []wsBuf;
            wsBuf = NULL;

            return wstrRet;
            }

             


            轉(zhuǎn)載:csdn

            這篇文章里,我將給出幾種C++ std::string和std::wstring相互轉(zhuǎn)換的轉(zhuǎn)換方法。
             
            第一種方法:調(diào)用WideCharToMultiByte()和MultiByteToWideChar(),代碼如下(關(guān)于詳細(xì)的解釋,可以參考《windows核心編程》):
             

            #include <string>
            #include <windows.h>
            using namespace std;
            //Converting a WChar string to a Ansi string
            std::string WChar2Ansi(LPCWSTR pwszSrc)
            {
                     int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
             
                     if (nLen<= 0) return std::string("");
             
                     char* pszDst = new char[nLen];
                     if (NULL == pszDst) return std::string("");
             
                     WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
                     pszDst[nLen -1] = 0;
             
                     std::string strTemp(pszDst);
                     delete [] pszDst;
             
                     return strTemp;
            }

             
            string ws2s(wstring& inputws)
            {
                    return WChar2Ansi(inputws.c_str());
            }

             

             
            //Converting a Ansi string to WChar string


            std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
             
            {
                int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
                if(nSize <= 0) return NULL;
             
                     WCHAR *pwszDst = new WCHAR[nSize+1];
                if( NULL == pwszDst) return NULL;
             
                MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
                pwszDst[nSize] = 0;
             
                if( pwszDst[0] == 0xFEFF)                    // skip Oxfeff
                    for(int i = 0; i < nSize; i ++)
                                        pwszDst[i] = pwszDst[i+1];
             
                wstring wcharString(pwszDst);
                     delete pwszDst;
             
                return wcharString;
            }

             
            std::wstring s2ws(const string& s)
            {
                 return Ansi2WChar(s.c_str(),s.size());
            }


             
             
            第二種方法:采用ATL封裝_bstr_t的過渡:(注,_bstr_是Microsoft Specific的,所以下面代碼可以在VS2005通過,無移植性);


            #include <string>
            #include <comutil.h>
            using namespace std;
            #pragma comment(lib, "comsuppw.lib")
             
            string ws2s(const wstring& ws);
            wstring s2ws(const string& s);
             
            string ws2s(const wstring& ws)
            {
                     _bstr_t t = ws.c_str();
                     char* pchar = (char*)t;
                     string result = pchar;
                     return result;
            }

             
            wstring s2ws(const string& s)
            {
                     _bstr_t t = s.c_str();
                     wchar_t* pwchar = (wchar_t*)t;
                     wstring result = pwchar;
                     return result;
            }


             
            第三種方法:使用CRT庫的mbstowcs()函數(shù)和wcstombs()函數(shù),平臺無關(guān),需設(shè)定locale。


            #include <string>
            #include <locale.h>
            using namespace std;
            string ws2s(const wstring& ws)
            {
                     string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";
             
                     setlocale(LC_ALL, "chs");
             
                     const wchar_t* _Source = ws.c_str();
                     size_t _Dsize = 2 * ws.size() + 1;
                     char *_Dest = new char[_Dsize];
                     memset(_Dest,0,_Dsize);
                     wcstombs(_Dest,_Source,_Dsize);
                     string result = _Dest;
                     delete []_Dest;
             
                     setlocale(LC_ALL, curLocale.c_str());
             
                     return result;
            }

             
            wstring s2ws(const string& s)
            {
                     setlocale(LC_ALL, "chs");
             
                     const char* _Source = s.c_str();
                     size_t _Dsize = s.size() + 1;
                     wchar_t *_Dest = new wchar_t[_Dsize];
                     wmemset(_Dest, 0, _Dsize);
                     mbstowcs(_Dest,_Source,_Dsize);
                     wstring result = _Dest;
                     delete []_Dest;
             
                     setlocale(LC_ALL, "C");
             
                     return result;
            }


            二 utf8.utf16.utf32的相互轉(zhuǎn)化

            可以參考Unicode.org 上有ConvertUTF.c和ConvertUTF.h (下載地址:http://www.unicode.org/Public/PROGRAMS/CVTUTF/

            實(shí)現(xiàn)文件ConvertUTF.c:(.h?。?br>/**//*
             * Copyright 2001-2004 Unicode, Inc.
             *
             * Disclaimer
             *
             * This source code is provided as is by Unicode, Inc. No claims are
             * made as to fitness for any particular purpose. No warranties of any
             * kind are expressed or implied. The recipient agrees to determine
             * applicability of information provided. If this file has been
             * purchased on magnetic or optical media from Unicode, Inc., the
             * sole remedy for any claim will be exchange of defective media
             * within 90 days of receipt.
             *
             * Limitations on Rights to Redistribute This Code
             *
             * Unicode, Inc. hereby grants the right to freely use the information
             * supplied in this file in the creation of products supporting the
             * Unicode Standard, and to make copies of this file in any form
             * for internal or external distribution as long as this notice
             * remains attached.
             */

            /**//* ---------------------------------------------------------------------

                Conversions between UTF32, UTF-16, and UTF-8. Source code file.
                Author: Mark E. Davis, 1994.
                Rev History: Rick McGowan, fixes & updates May 2001.
                Sept 2001: fixed const & error conditions per
                mods suggested by S. Parent & A. Lillich.
                June 2002: Tim Dodd added detection and handling of incomplete
                source sequences, enhanced error detection, added casts
                to eliminate compiler warnings.
                July 2003: slight mods to back out aggressive FFFE detection.
                Jan 2004: updated switches in from-UTF8 conversions.
                Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.

                See the header file "ConvertUTF.h" for complete documentation.

            ------------------------------------------------------------------------ */


            #include "ConvertUTF.h"
            #ifdef CVTUTF_DEBUG
            #include <stdio.h>
            #endif

            static const int halfShift  = 10; /**//* used for shifting by 10 bits */

            static const UTF32 halfBase = 0x0010000UL;
            static const UTF32 halfMask = 0x3FFUL;

            #define UNI_SUR_HIGH_START  (UTF32)0xD800
            #define UNI_SUR_HIGH_END    (UTF32)0xDBFF
            #define UNI_SUR_LOW_START   (UTF32)0xDC00
            #define UNI_SUR_LOW_END     (UTF32)0xDFFF
            #define false       0
            #define true        1

            /**//* --------------------------------------------------------------------- */

            ConversionResult ConvertUTF32toUTF16 (
                const UTF32** sourceStart, const UTF32* sourceEnd,
                UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
                ConversionResult result = conversionOK;
                const UTF32* source = *sourceStart;
                UTF16* target = *targetStart;
                while (source < sourceEnd) {
                UTF32 ch;
                if (target >= targetEnd) {
                    result = targetExhausted; break;
                }
                ch = *source++;
                if (ch <= UNI_MAX_BMP) { /**//* Target is a character <= 0xFFFF */
                    /**//* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
                    if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
                    if (flags == strictConversion) {
                        --source; /**//* return to the illegal value itself */
                        result = sourceIllegal;
                        break;
                    } else {
                        *target++ = UNI_REPLACEMENT_CHAR;
                    }
                    } else {
                    *target++ = (UTF16)ch; /**//* normal case */
                    }
                } else if (ch > UNI_MAX_LEGAL_UTF32) {
                    if (flags == strictConversion) {
                    result = sourceIllegal;
                    } else {
                    *target++ = UNI_REPLACEMENT_CHAR;
                    }
                } else {
                    /**//* target is a character in range 0xFFFF - 0x10FFFF. */
                    if (target + 1 >= targetEnd) {
                    --source; /**//* Back up source pointer! */
                    result = targetExhausted; break;
                    }
                    ch -= halfBase;
                    *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
                    *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
                }
                }
                *sourceStart = source;
                *targetStart = target;
                return result;
            }

            /**//* --------------------------------------------------------------------- */

            ConversionResult ConvertUTF16toUTF32 (
                const UTF16** sourceStart, const UTF16* sourceEnd,
                UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
                ConversionResult result = conversionOK;
                const UTF16* source = *sourceStart;
                UTF32* target = *targetStart;
                UTF32 ch, ch2;
                while (source < sourceEnd) {
                const UTF16* oldSource = source; /**//*  In case we have to back up because of target overflow. */
                ch = *source++;
                /**//* If we have a surrogate pair, convert to UTF32 first. */
                if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
                    /**//* If the 16 bits following the high surrogate are in the source buffer */
                    if (source < sourceEnd) {
                    ch2 = *source;
                    /**//* If it's a low surrogate, convert to UTF32. */
                    if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
                        ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
                        + (ch2 - UNI_SUR_LOW_START) + halfBase;
                        ++source;
                    } else if (flags == strictConversion) { /**//* it's an unpaired high surrogate */
                        --source; /**//* return to the illegal value itself */
                        result = sourceIllegal;
                        break;
                    }
                    } else { /**//* We don't have the 16 bits following the high surrogate. */
                    --source; /**//* return to the high surrogate */
                    result = sourceExhausted;
                    break;
                    }
                } else if (flags == strictConversion) {
                    /**//* UTF-16 surrogate values are illegal in UTF-32 */
                    if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
                    --source; /**//* return to the illegal value itself */
                    result = sourceIllegal;
                    break;
                    }
                }
                if (target >= targetEnd) {
                    source = oldSource; /**//* Back up source pointer! */
                    result = targetExhausted; break;
                }
                *target++ = ch;
                }
                *sourceStart = source;
                *targetStart = target;
            #ifdef CVTUTF_DEBUG
            if (result == sourceIllegal) {
                fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
                fflush(stderr);
            }
            #endif
                return result;
            }

            /**//* --------------------------------------------------------------------- */

            /**//*
             * Index into the table below with the first byte of a UTF-8 sequence to
             * get the number of trailing bytes that are supposed to follow it.
             * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
             * left as-is for anyone who may want to do such conversion, which was
             * allowed in earlier algorithms.
             */
            static const char trailingBytesForUTF8[256] = {
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
            };

            /**//*
             * Magic values subtracted from a buffer value during UTF8 conversion.
             * This table contains as many values as there might be trailing bytes
             * in a UTF-8 sequence.
             */
            static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
                         0x03C82080UL, 0xFA082080UL, 0x82082080UL };

            /**//*
             * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
             * into the first byte, depending on how many bytes follow.  There are
             * as many entries in this table as there are UTF-8 sequence types.
             * (I.e., one byte sequence, two byte etc.). Remember that sequencs
             * for *legal* UTF-8 will be 4 or fewer bytes total.
             */
            static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };

            /**//* --------------------------------------------------------------------- */

            /**//* The interface converts a whole buffer to avoid function-call overhead.
             * Constants have been gathered. Loops & conditionals have been removed as
             * much as possible for efficiency, in favor of drop-through switches.
             * (See "Note A" at the bottom of the file for equivalent code.)
             * If your compiler supports it, the "isLegalUTF8" call can be turned
             * into an inline function.
             */

            /**//* --------------------------------------------------------------------- */

            ConversionResult ConvertUTF16toUTF8 (
                const UTF16** sourceStart, const UTF16* sourceEnd,
                UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
                ConversionResult result = conversionOK;
                const UTF16* source = *sourceStart;
                UTF8* target = *targetStart;
                while (source < sourceEnd) {
                UTF32 ch;
                unsigned short bytesToWrite = 0;
                const UTF32 byteMask = 0xBF;
                const UTF32 byteMark = 0x80;
                const UTF16* oldSource = source; /**//* In case we have to back up because of target overflow. */
                ch = *source++;
                /**//* If we have a surrogate pair, convert to UTF32 first. */
                if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
                    /**//* If the 16 bits following the high surrogate are in the source buffer */
                    if (source < sourceEnd) {
                    UTF32 ch2 = *source;
                    /**//* If it's a low surrogate, convert to UTF32. */
                    if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
                        ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
                        + (ch2 - UNI_SUR_LOW_START) + halfBase;
                        ++source;
                    } else if (flags == strictConversion) { /**//* it's an unpaired high surrogate */
                        --source; /**//* return to the illegal value itself */
                        result = sourceIllegal;
                        break;
                    }
                    } else { /**//* We don't have the 16 bits following the high surrogate. */
                    --source; /**//* return to the high surrogate */
                    result = sourceExhausted;
                    break;
                    }
                } else if (flags == strictConversion) {
                    /**//* UTF-16 surrogate values are illegal in UTF-32 */
                    if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
                    --source; /**//* return to the illegal value itself */
                    result = sourceIllegal;
                    break;
                    }
                }
                /**//* Figure out how many bytes the result will require */
                if (ch < (UTF32)0x80) {         bytesToWrite = 1;
                } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
                } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
                } else if (ch < (UTF32)0x110000) {  bytesToWrite = 4;
                } else {                bytesToWrite = 3;
                                    ch = UNI_REPLACEMENT_CHAR;
                }

                target += bytesToWrite;
                if (target > targetEnd) {
                    source = oldSource; /**//* Back up source pointer! */
                    target -= bytesToWrite; result = targetExhausted; break;
                }
                switch (bytesToWrite) { /**//* note: everything falls through. */
                    case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
                    case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
                    case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
                    case 1: *--target =  (UTF8)(ch | firstByteMark[bytesToWrite]);
                }
                target += bytesToWrite;
                }
                *sourceStart = source;
                *targetStart = target;
                return result;
            }

            /**//* --------------------------------------------------------------------- */

            /**//*
             * Utility routine to tell whether a sequence of bytes is legal UTF-8.
             * This must be called with the length pre-determined by the first byte.
             * If not calling this from ConvertUTF8to*, then the length can be set by:
             *  length = trailingBytesForUTF8[*source]+1;
             * and the sequence is illegal right away if there aren't that many bytes
             * available.
             * If presented with a length > 4, this returns false.  The Unicode
             * definition of UTF-8 goes up to 4-byte sequences.
             */

            static Boolean isLegalUTF8(const UTF8 *source, int length) {
                UTF8 a;
                const UTF8 *srcptr = source+length;
                switch (length) {
                default: return false;
                /**//* Everything else falls through when "true" */
                case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
                case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
                case 2: if ((a = (*--srcptr)) > 0xBF) return false;

                switch (*source) {
                    /**//* no fall-through in this inner switch */
                    case 0xE0: if (a < 0xA0) return false; break;
                    case 0xED: if (a > 0x9F) return false; break;
                    case 0xF0: if (a < 0x90) return false; break;
                    case 0xF4: if (a > 0x8F) return false; break;
                    default:   if (a < 0x80) return false;
                }

                case 1: if (*source >= 0x80 && *source < 0xC2) return false;
                }
                if (*source > 0xF4) return false;
                return true;
            }

            /**//* --------------------------------------------------------------------- */

            /**//*
             * Exported function to return whether a UTF-8 sequence is legal or not.
             * This is not used here; it's just exported.
             */
            Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
                int length = trailingBytesForUTF8[*source]+1;
                if (source+length > sourceEnd) {
                return false;
                }
                return isLegalUTF8(source, length);
            }

            /**//* --------------------------------------------------------------------- */

            ConversionResult ConvertUTF8toUTF16 (
                const UTF8** sourceStart, const UTF8* sourceEnd,
                UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
                ConversionResult result = conversionOK;
                const UTF8* source = *sourceStart;
                UTF16* target = *targetStart;
                while (source < sourceEnd) {
                UTF32 ch = 0;
                unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
                if (source + extraBytesToRead >= sourceEnd) {
                    result = sourceExhausted; break;
                }
                /**//* Do this check whether lenient or strict */
                if (! isLegalUTF8(source, extraBytesToRead+1)) {
                    result = sourceIllegal;
                    break;
                }
                /**//*
                 * The cases all fall through. See "Note A" below.
                 */
                switch (extraBytesToRead) {
                    case 5: ch += *source++; ch <<= 6; /**//* remember, illegal UTF-8 */
                    case 4: ch += *source++; ch <<= 6; /**//* remember, illegal UTF-8 */
                    case 3: ch += *source++; ch <<= 6;
                    case 2: ch += *source++; ch <<= 6;
                    case 1: ch += *source++; ch <<= 6;
                    case 0: ch += *source++;
                }
                ch -= offsetsFromUTF8[extraBytesToRead];

                if (target >= targetEnd) {
                    source -= (extraBytesToRead+1); /**//* Back up source pointer! */
                    result = targetExhausted; break;
                }
                if (ch <= UNI_MAX_BMP) { /**//* Target is a character <= 0xFFFF */
                    /**//* UTF-16 surrogate values are illegal in UTF-32 */
                    if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
                    if (flags == strictConversion) {
                        source -= (extraBytesToRead+1); /**//* return to the illegal value itself */
                        result = sourceIllegal;
                        break;
                    } else {
                        *target++ = UNI_REPLACEMENT_CHAR;
                    }
                    } else {
                    *target++ = (UTF16)ch; /**//* normal case */
                    }
                } else if (ch > UNI_MAX_UTF16) {
                    if (flags == strictConversion) {
                    result = sourceIllegal;
                    source -= (extraBytesToRead+1); /**//* return to the start */
                    break; /**//* Bail out; shouldn't continue */
                    } else {
                    *target++ = UNI_REPLACEMENT_CHAR;
                    }
                } else {
                    /**//* target is a character in range 0xFFFF - 0x10FFFF. */
                    if (target + 1 >= targetEnd) {
                    source -= (extraBytesToRead+1); /**//* Back up source pointer! */
                    result = targetExhausted; break;
                    }
                    ch -= halfBase;
                    *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
                    *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
                }
                }
                *sourceStart = source;
                *targetStart = target;
                return result;
            }

            /**//* --------------------------------------------------------------------- */

            ConversionResult ConvertUTF32toUTF8 (
                const UTF32** sourceStart, const UTF32* sourceEnd,
                UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
                ConversionResult result = conversionOK;
                const UTF32* source = *sourceStart;
                UTF8* target = *targetStart;
                while (source < sourceEnd) {
                UTF32 ch;
                unsigned short bytesToWrite = 0;
                const UTF32 byteMask = 0xBF;
                const UTF32 byteMark = 0x80;
                ch = *source++;
                if (flags == strictConversion ) {
                    /**//* UTF-16 surrogate values are illegal in UTF-32 */
                    if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
                    --source; /**//* return to the illegal value itself */
                    result = sourceIllegal;
                    break;
                    }
                }
                /**//*
                 * Figure out how many bytes the result will require. Turn any
                 * illegally large UTF32 things (> Plane 17) into replacement chars.
                 */
                if (ch < (UTF32)0x80) {         bytesToWrite = 1;
                } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
                } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
                } else if (ch <= UNI_MAX_LEGAL_UTF32) {  bytesToWrite = 4;
                } else {                bytesToWrite = 3;
                                    ch = UNI_REPLACEMENT_CHAR;
                                    result = sourceIllegal;
                }
               
                target += bytesToWrite;
                if (target > targetEnd) {
                    --source; /**//* Back up source pointer! */
                    target -= bytesToWrite; result = targetExhausted; break;
                }
                switch (bytesToWrite) { /**//* note: everything falls through. */
                    case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
                    case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
                    case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
                    case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
                }
                target += bytesToWrite;
                }
                *sourceStart = source;
                *targetStart = target;
                return result;
            }

            /**//* --------------------------------------------------------------------- */

            ConversionResult ConvertUTF8toUTF32 (
                const UTF8** sourceStart, const UTF8* sourceEnd,
                UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
                ConversionResult result = conversionOK;
                const UTF8* source = *sourceStart;
                UTF32* target = *targetStart;
                while (source < sourceEnd) {
                UTF32 ch = 0;
                unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
                if (source + extraBytesToRead >= sourceEnd) {
                    result = sourceExhausted; break;
                }
                /**//* Do this check whether lenient or strict */
                if (! isLegalUTF8(source, extraBytesToRead+1)) {
                    result = sourceIllegal;
                    break;
                }
                /**//*
                 * The cases all fall through. See "Note A" below.
                 */
                switch (extraBytesToRead) {
                    case 5: ch += *source++; ch <<= 6;
                    case 4: ch += *source++; ch <<= 6;
                    case 3: ch += *source++; ch <<= 6;
                    case 2: ch += *source++; ch <<= 6;
                    case 1: ch += *source++; ch <<= 6;
                    case 0: ch += *source++;
                }
                ch -= offsetsFromUTF8[extraBytesToRead];

                if (target >= targetEnd) {
                    source -= (extraBytesToRead+1); /**//* Back up the source pointer! */
                    result = targetExhausted; break;
                }
                if (ch <= UNI_MAX_LEGAL_UTF32) {
                    /**//*
                     * UTF-16 surrogate values are illegal in UTF-32, and anything
                     * over Plane 17 (> 0x10FFFF) is illegal.
                     */
                    if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
                    if (flags == strictConversion) {
                        source -= (extraBytesToRead+1); /**//* return to the illegal value itself */
                        result = sourceIllegal;
                        break;
                    } else {
                        *target++ = UNI_REPLACEMENT_CHAR;
                    }
                    } else {
                    *target++ = ch;
                    }
                } else { /**//* i.e., ch > UNI_MAX_LEGAL_UTF32 */
                    result = sourceIllegal;
                    *target++ = UNI_REPLACEMENT_CHAR;
                }
                }
                *sourceStart = source;
                *targetStart = target;
                return result;
            }

            /**//* ---------------------------------------------------------------------

                Note A.
                The fall-through switches in UTF-8 reading code save a
                temp variable, some decrements & conditionals.  The switches
                are equivalent to the following loop:
                {
                    int tmpBytesToRead = extraBytesToRead+1;
                    do {
                    ch += *source++;
                    --tmpBytesToRead;
                    if (tmpBytesToRead) ch <<= 6;
                    } while (tmpBytesToRead > 0);
                }
                In UTF-8 writing code, the switches on "bytesToWrite" are
                similarly unrolled loops.

               --------------------------------------------------------------------- */

             

            三 C++ 的字符串與C#的轉(zhuǎn)化

            1)將system::String 轉(zhuǎn)化為C++的string:
            // convert_system_string.cpp
            // compile with: /clr
            #include <string>
            #include <iostream>
            using namespace std;
            using namespace System;

            void MarshalString ( String ^ s, string& os ) {
               using namespace Runtime::InteropServices;
               const char* chars =
                  (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
               os = chars;
               Marshal::FreeHGlobal(IntPtr((void*)chars));
            }

            void MarshalString ( String ^ s, wstring& os ) {
               using namespace Runtime::InteropServices;
               const wchar_t* chars =
                  (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
               os = chars;
               Marshal::FreeHGlobal(IntPtr((void*)chars));
            }

            int main() {
               string a = "test";
               wstring b = L"test2";
               String ^ c = gcnew String("abcd");

               cout << a << endl;
               MarshalString(c, a);
               c = "efgh";
               MarshalString(c, b);
               cout << a << endl;
               wcout << b << endl;
            }


            2)將System::String轉(zhuǎn)化為char*或w_char*
            // convert_string_to_wchar.cpp
            // compile with: /clr
            #include < stdio.h >
            #include < stdlib.h >
            #include < vcclr.h >

            using namespace System;

            int main() {
               String ^str = "Hello";

               // Pin memory so GC can't move it while native function is called
               pin_ptr<const wchar_t> wch = PtrToStringChars(str);
               printf_s("%S\n", wch);

               // Conversion to char* :
               // Can just convert wchar_t* to char* using one of the
               // conversion functions such as:
               // WideCharToMultiByte()
               // wcstombs_s()
               //  etc
               size_t convertedChars = 0;
               size_t  sizeInBytes = ((str->Length + 1) * 2);
               errno_t err = 0;
               char    *ch = (char *)malloc(sizeInBytes);

               err = wcstombs_s(&convertedChars,
                                ch, sizeInBytes,
                                wch, sizeInBytes);
               if (err != 0)
                  printf_s("wcstombs_s  failed!\n");

                printf_s("%s\n", ch);
            }

            posted @ 2007-11-18 19:48 true 閱讀(525) | 評論 (0)編輯 收藏

            問題描述:大部分的vs.net 2005的用戶在新建“win32項(xiàng)目-windows應(yīng)用程序”的時(shí)候,新建的工程都通不過去,出現(xiàn)如下提示:
            Solution to “MSVCR80D.dll not found” by hua.
            “沒有找到MSVCR80D.dll,因此這個(gè)應(yīng)用程序未能啟動。重新安裝應(yīng)用程序可能會修復(fù)此問題。”的完美解決方案^_^感覺偶做的還不錯(cuò)

            問題所在:由于vs.net 2005 采用了一種新的DLL方案,搞成一個(gè)exe還要配有一個(gè)manifest文件(一般在嵌入文件里了,所以看不到,不過也可以不嵌入,這樣會生產(chǎn)一個(gè)<程序名>.exe.manifest的文件,沒它exe自己就轉(zhuǎn)不了了:)這是個(gè)新功能,微軟弄了個(gè)新工具(mt.exe),結(jié)果不好用,好像是fat32下時(shí)間戳有問題(在ntfs下這個(gè)問題就沒有了),搞得manifest有時(shí)嵌入不到exe中(默認(rèn)配置是嵌入的,所以就報(bào)錯(cuò)找不到dll了。

            解決方案(3個(gè)都可以,由以第3個(gè)最帥,我做的:):
            1.    微軟對于這個(gè)問題應(yīng)該也有處理,不過感覺不是很人性化。在“屬性->配置屬性->清單工具->常規(guī)“下有一個(gè)”使用FAT32解決辦法,把它選成是,就可以了。(注意:一定要先配置這個(gè)選項(xiàng),然后再編譯工程,要不然還是不好用:)
            2.    找到你的工程的文件夾,如(myproject),找到其下的myproject\myproject\Debug\ myproject.rec,把它刪掉(刪掉整個(gè)Debug目錄也可以),重新編譯,搞定!
            3.    本解決方案是俺獨(dú)創(chuàng)的,感覺爽多了,可以直接再應(yīng)用向?qū)е信渲茫瑖?yán)重符合高級人機(jī)界面要求:)好,
            1)    首先找到你的vs.net安裝目錄(如我的是E:\Program Files\Microsoft Visual Studio 8),定位到Microsoft Visual Studio 8\VC\VCWizards\AppWiz\Generic\Application文件夾,備份這個(gè)Application文件夾,不然一會你自己改咂了我可不管?。海?。
            2)    打開html\2052,看到兩個(gè)文件了吧,就那個(gè)AppSettings.htm了,這個(gè)管著你的那個(gè)配置向?qū)У慕缑?,用UE(不要告訴我你不知道ue啥東西,baidu it)打開,在266行“                </SPAN>”后回車,然后插入一下內(nèi)容:
            <!-- this (hua)section is added by HUA. -->
                                <br><br><br><br><br>
                                
                            <span class="itemTextTop" id="FILE_SYSTEM_SPAN" title="">選擇你所使用的文件系統(tǒng):
                                
                                   <P CLASS="Spacer"> </P>
                                
                                    <INPUT TYPE="radio" CLASS="Radio" checked onPropertyChange="" NAME="filesystem" ID="FAT32" ACCESSKEY="F" TITLE="FAT32">
                                    <DIV CLASS="itemTextRadioB" ID="FAT32_DIV" TITLE="FAT32">
                                    <LABEL FOR="FAT32" ID="FAT32_LABEL">FAT32(<U>F</U>)</LABEL>
                                    </DIV>

                                  <BR>

                                    <INPUT TYPE="radio" CLASS="Radio" onPropertyChange="" NAME="filesystem" ID="NTFS" ACCESSKEY="N" TITLE="NTFS">
                                    <DIV CLASS="itemTextRadioB" ID="NTFS_DIV" TITLE="NTFS">
                                    <LABEL FOR="NTFS" ID="NTFS_LABEL">NTFS(<U>N</U>)</LABEL>
                                    </DIV>
                            </span>
            <!-- end of (hua)section -->
            好,保存關(guān)閉,這個(gè)改完了,準(zhǔn)備下一個(gè)。

            3)    打開scripts\2052,這里就一個(gè)文件,ue打開它,找到138行“        var bATL = wizard.FindSymbol("SUPPORT_ATL");”其后回車,插入如下內(nèi)容:
            // this (hua)section is added by HUA.
                    var MFTool = config.Tools("VCManifestTool");
                    MFTool.UseFAT32Workaround = true;
            // end of (hua)section    
                    好,繼續(xù)找到210行(源文件的210,你加了上邊的語句就不是210了:)“        config = proj.Object.Configurations.Item("Release");”注意這次要在這行“前邊”加如下內(nèi)容:
            // this (hua)section is added by HUA.
                    if(bFAT32)
                    {
                        var MFTool = config.Tools("VCManifestTool");
                        MFTool.UseFAT32Workaround = true;
                    }
            // end of (hua)section    
            好了,終于都改完了,打開你的vs.net 2005新建一個(gè)win32應(yīng)用程序看看吧,效果還不錯(cuò)吧:)為了這個(gè)問題,耽誤了我一天的考研復(fù)習(xí)時(shí)間,希望大家能用的上。
            另外附個(gè)國外的bbs:http://forums.microsoft.com/MSDN/default.aspx?SiteID=1
            Msdn的,肯定不錯(cuò)了,上邊有vs.net的開發(fā)人員活動,都是很官方的東西,大家可以看看,不過英語要夠好哦:)
            posted @ 2007-11-17 01:37 true 閱讀(589) | 評論 (0)編輯 收藏

            (一) 先講一下XML中的物殊字符,手動填寫時(shí)注意一下。

            字符                  字符實(shí)體
            &                      &amp;或&
            '                      &apos;或'
            >                      &gt;或>
            <                      &lt;或&<
            "                       &quot;或"

            (二) CMarkup類的源代碼。

            這是目前的最新版本;

            這是官網(wǎng)示例文件,取出里面的Markup.cpp和Markup.h,導(dǎo)入你的工程里面,CMarkup類就可以用了;

            下載地址:http://www.firstobject.com/Markup83.zip

            (三) 創(chuàng)建一個(gè)XML文檔。

            CMarkup xml;
            xml.AddElem( "ORDER" );
            xml.AddChildElem( "ITEM" );
            xml.IntoElem();
            xml.AddChildElem( "SN", "132487A-J" );
            xml.AddChildElem( "NAME", "crank casing" );
            xml.AddChildElem( "QTY", "1" );
            xml.Save("c:\\UserInfo.xml");

            效果如下:

            <ORDER>
            <ITEM>
            <SN>132487A-J</SN>
            <NAME>crank casing</NAME>
            <QTY>1</QTY>
            </ITEM>
            </ORDER>
            (四) 瀏覽特定元素
            CMarkup xml;
            xml.Load("UserInfo.xml");
            while ( xml.FindChildElem("ITEM") ) {     xml.IntoElem();     xml.FindChildElem( "SN" );     CString csSN = xml.GetChildData();     xml.FindChildElem( "QTY" );     int nQty = atoi( xml.GetChildData() );     xml.OutOfElem(); }
            (五)增加元素和屬性
            添加在最后面,使用的是AddElem;添加在最前面,使用InsertElem。
            CMarkup xml;
            xml.Load("c:\\UserInfo.xml");
            xml.AddElem( "ORDER" );
            xml.IntoElem(); // 進(jìn)入 ORDER



                xml.AddElem( "ITEM" );     xml.IntoElem(); // 進(jìn)入 ITEM     xml.AddElem( "SN", "4238764-A" ); //添加元素     xml.AddElem( "NAME", "bearing" );//添加元素     xml.AddElem( "QTY", "15" );//添加元素     xml.OutOfElem(); // 退出 ITEM 
            xml.AddElem( "SHIPMENT" );
            xml.IntoElem(); // 進(jìn)入 SHIPMENT
            xml.AddElem( "POC" );//添加元素
            xml.SetAttrib( "type", "non-emergency");//添加屬性
            xml.IntoElem(); // 進(jìn)入 POC
            xml.AddElem( "NAME", "John Smith");//添加元素
            xml.AddElem( "TEL", "555-1234");//添加元素
            xml.Save("c:\\UserInfo.xml");

             

            效果如下:

            <ORDER>
            <ITEM>
            <SN>132487A-J</SN>
            <NAME>crank casing</NAME>
            <QTY>1</QTY>
            </ITEM>
            <ITEM>
            <SN>4238764-A</SN>
            <NAME>bearing</NAME>
            <QTY>15</QTY>
            </ITEM>
            <SHIPMENT>
            <POC type="non-emergency">
            <NAME>John Smith</NAME>
            <TEL>555-1234</TEL>
            </POC>
            </SHIPMENT>
            </ORDER>

            (六) 修改元素和屬性

            如將POC中的屬性type改成:change;

            元素TEL改成:123456789

                   CMarkup xml;
             if (xml.Load("UserInfo.xml"))
             {
              CString strUserID = _T("");
              xml.ResetMainPos();
              if (xml.FindChildElem("SHIPMENT"))
              {
               xml.IntoElem();
               if (xml.FindChildElem("POC"))
               {
                xml.IntoElem();
                CString str_type=xml.GetAttrib("type");
                MessageBox(str_type);
                xml.SetAttrib("type","change");
                strUserID = xml.GetData();
                
                if (xml.FindChildElem("TEL"))
                {
                 xml.IntoElem();
                 xml.SetData("123456789");
                 xml.Save("UserInfo.xml");
                 return;
                }
               }
              }
             }

            (七)刪除元素:

            刪除SN=132487A-J的項(xiàng)目。

            CMarkup xml;
             if (xml.Load("UserInfo.xml"))
             {
              CString strUserID = _T("");
              xml.ResetMainPos();
              if (xml.FindChildElem("ITEM"))
              {
               xml.IntoElem();
               CString str_sn;
               xml.FindChildElem("SN");
               str_sn=xml.GetChildData();
               if(str_sn=="132487A-J")
               {
                xml.RemoveElem();
                xml.Save("UserInfo.xml");
               }
              }
             }

            posted @ 2007-11-15 22:02 true 閱讀(876) | 評論 (0)編輯 收藏

            awk 用法小結(jié)

            awk 用法:awk ' pattern {action} '

            變量名 含義
            ARGC 命令行變元個(gè)數(shù)
            ARGV 命令行變元數(shù)組
            FILENAME 當(dāng)前輸入文件名
            FNR 當(dāng)前文件中的記錄號
            FS 輸入域分隔符,默認(rèn)為一個(gè)空格
            RS 輸入記錄分隔符
            NF 當(dāng)前記錄里域個(gè)數(shù)
            NR 到目前為止記錄數(shù)
            OFS 輸出域分隔符
            ORS 輸出記錄分隔符

            1、awk '/101/' file 顯示文件file中包含101的匹配行。
            awk '/101/,/105/' file
            awk '$1 == 5' file
            awk '$1 == "CT"' file 注意必須帶雙引號
            awk '$1 * $2 >100 ' file
            awk '$2 >5 && $2<=15' file
            2、awk '{print NR,NF,$1,$NF,}' file 顯示文件file的當(dāng)前記錄號、域數(shù)和每一行的第一個(gè)和最后一個(gè)域。
            awk '/101/ {print $1,$2 + 10}' file 顯示文件file的匹配行的第一、二個(gè)域加10。
            awk '/101/ {print $1$2}' file
            awk '/101/ {print $1 $2}' file 顯示文件file的匹配行的第一、二個(gè)域,但顯示時(shí)域中間沒有分隔符。
            3、df | awk '$4>1000000 ' 通過管道符獲得輸入,如:顯示第4個(gè)域滿足條件的行。
            4、awk -F "|" '{print $1}' file 按照新的分隔符“|”進(jìn)行操作。
            awk 'BEGIN { FS="[: \t|]" }
            {print $1,$2,$3}' file 通過設(shè)置輸入分隔符(FS="[: \t|]")修改輸入分隔符。

            Sep="|"
            awk -F $Sep '{print $1}' file 按照環(huán)境變量Sep的值做為分隔符。
            awk -F '[ :\t|]' '{print $1}' file 按照正則表達(dá)式的值做為分隔符,這里代表空格、:、TAB、|同時(shí)做為分隔符。
            awk -F '[][]' '{print $1}' file 按照正則表達(dá)式的值做為分隔符,這里代表[、]
            5、awk -f awkfile file 通過文件awkfile的內(nèi)容依次進(jìn)行控制。
            cat awkfile
            /101/{print "\047 Hello! \047"} --遇到匹配行以后打印 ' Hello! '.\047代表單引號。
            {print $1,$2} --因?yàn)闆]有模式控制,打印每一行的前兩個(gè)域。
            6、awk '$1 ~ /101/ {print $1}' file 顯示文件中第一個(gè)域匹配101的行(記錄)。
            7、awk 'BEGIN { OFS="%"}
            {print $1,$2}' file 通過設(shè)置輸出分隔符(OFS="%")修改輸出格式。
            8、awk 'BEGIN { max=100 ;print "max=" max} BEGIN 表示在處理任意行之前進(jìn)行的操作。
            {max=($1 >max ?$1:max); print $1,"Now max is "max}' file 取得文件第一個(gè)域的最大值。
            (表達(dá)式1?表達(dá)式2:表達(dá)式3 相當(dāng)于:
            if (表達(dá)式1)
            表達(dá)式2
            else
            表達(dá)式3
            awk '{print ($1>4 ? "high "$1: "low "$1)}' file
            9、awk '$1 * $2 >100 {print $1}' file 顯示文件中第一個(gè)域匹配101的行(記錄)。
            10、awk '{$1 == 'Chi' {$3 = 'China'; print}' file 找到匹配行后先將第3個(gè)域替換后再顯示該行(記錄)。
            awk '{$7 %= 3; print $7}' file 將第7域被3除,并將余數(shù)賦給第7域再打印。
            11、awk '/tom/ {wage=$2+$3; printf wage}' file 找到匹配行后為變量wage賦值并打印該變量。
            12、awk '/tom/ {count++;}
            END {print "tom was found "count" times"}' file END表示在所有輸入行處理完后進(jìn)行處理。
            13、awk 'gsub(/\$/,"");gsub(/,/,""); cost+=$4;
            END {print "The total is $" cost>"filename"}' file gsub函數(shù)用空串替換$和,再將結(jié)果輸出到filename中。
            1 2 3 $1,200.00
            1 2 3 $2,300.00
            1 2 3 $4,000.00

            awk '{gsub(/\$/,"");gsub(/,/,"");
            if ($4>1000&&$4<2000) c1+=$4;
            else if ($4>2000&&$4<3000) c2+=$4;
            else if ($4>3000&&$4<4000) c3+=$4;
            else c4+=$4; }
            END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file
            通過if和else if完成條件語句

            awk '{gsub(/\$/,"");gsub(/,/,"");
            if ($4>3000&&$4<4000) exit;
            else c4+=$4; }
            END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file
            通過exit在某條件時(shí)退出,但是仍執(zhí)行END操作。
            awk '{gsub(/\$/,"");gsub(/,/,"");
            if ($4>3000) next;
            else c4+=$4; }
            END {printf "c4=[%d]\n",c4}"' file
            通過next在某條件時(shí)跳過該行,對下一行執(zhí)行操作。


            14、awk '{ print FILENAME,$0 }' file1 file2 file3>fileall 把file1、file2、file3的文件內(nèi)容全部寫到fileall中,格式為
            打印文件并前置文件名。
            15、awk ' $1!=previous { close(previous); previous=$1 }
            {print substr($0,index($0," ") +1)>$1}' fileall 把合并后的文件重新分拆為3個(gè)文件。并與原文件一致。
            16、awk 'BEGIN {"date"|getline d; print d}' 通過管道把date的執(zhí)行結(jié)果送給getline,并賦給變量d,然后打印。
            17、awk 'BEGIN {system("echo \"Input your name:\\c\""); getline d;print "\nYour name is",d,"\b!\n"}'
            通過getline命令交互輸入name,并顯示出來。
            awk 'BEGIN {FS=":"; while(getline< "/etc/passwd" >0) { if($1~"050[0-9]_") print $1}}'
            打印/etc/passwd文件中用戶名包含050x_的用戶名。

            18、awk '{ i=1;while(i<NF) {print NF,$i;i++}}' file 通過while語句實(shí)現(xiàn)循環(huán)。
            awk '{ for(i=1;i<NF;i++) {print NF,$i}}' file 通過for語句實(shí)現(xiàn)循環(huán)。
            type file|awk -F "/" '
            { for(i=1;i<NF;i++)
            { if(i==NF-1) { printf "%s",$i }
            else { printf "%s/",$i } }}' 顯示一個(gè)文件的全路徑。
            用for和if顯示日期
            awk 'BEGIN {
            for(j=1;j<=12;j++)
            { flag=0;
            printf "\n%d月份\n",j;
            for(i=1;i<=31;i++)
            {
            if (j==2&&i>28) flag=1;
            if ((j==4||j==6||j==9||j==11)&&i>30) flag=1;
            if (flag==0) {printf "%02d%02d ",j,i}
            }
            }
            }'
            19、在awk中調(diào)用系統(tǒng)變量必須用單引號,如果是雙引號,則表示字符串
            Flag=abcd
            awk '{print '$Flag'}' 結(jié)果為abcd
            awk '{print "$Flag"}' 結(jié)果為$Flag
            posted @ 2007-11-13 12:02 true 閱讀(402) | 評論 (0)編輯 收藏

            一、 簡單查詢

              簡單的Transact-SQL查詢只包括選擇列表、FROM子句和WHERE子句。它們分別說明所查詢列、查詢的表或視圖、以及搜索條件等。
              例如,下面的語句查詢testtable表中姓名為"張三"的nickname字段和email字段。

               SELECT nickname,email
              FROM testtable
              WHERE name='張三'

              (一) 選擇列表

              選擇列表(select_list)指出所查詢列,它可以是一組列名列表、星號、表達(dá)式、變量(包括局部變量和全局變量)等構(gòu)成。

              1、選擇所有列

              例如,下面語句顯示testtable表中所有列的數(shù)據(jù):

               SELECT *
              FROM testtable

              2、選擇部分列并指定它們的顯示次序

              查詢結(jié)果集合中數(shù)據(jù)的排列順序與選擇列表中所指定的列名排列順序相同。
              例如:

               SELECT nickname,email
              FROM testtable

              3、更改列標(biāo)題

              在選擇列表中,可重新指定列標(biāo)題。定義格式為:
              列標(biāo)題=列名
              列名 列標(biāo)題
              如果指定的列標(biāo)題不是標(biāo)準(zhǔn)的標(biāo)識符格式時(shí),應(yīng)使用引號定界符,例如,下列語句使用漢字顯示列標(biāo)題:

               SELECT 昵稱=nickname,電子郵件=email
              FROM testtable

              4、刪除重復(fù)行

              SELECT語句中使用ALL或DISTINCT選項(xiàng)來顯示表中符合條件的所有行或刪除其中重復(fù)的數(shù)據(jù)行,默認(rèn)為ALL。使用DISTINCT選項(xiàng)時(shí),對于所有重復(fù)的數(shù)據(jù)行在SELECT返回的結(jié)果集合中只保留一行。

              5、限制返回的行數(shù)

              使用TOP n [PERCENT]選項(xiàng)限制返回的數(shù)據(jù)行數(shù),TOP n說明返回n行,而TOP n PERCENT時(shí),說明n是表示一百分?jǐn)?shù),指定返回的行數(shù)等于總行數(shù)的百分之幾。
              例如:

               SELECT TOP 2 *
              FROM testtable
              SELECT TOP 20 PERCENT *
              FROM testtable

              (二)FROM子句

              FROM子句指定SELECT語句查詢及與查詢相關(guān)的表或視圖。在FROM子句中最多可指定256個(gè)表或視圖,它們之間用逗號分隔。
              在FROM子句同時(shí)指定多個(gè)表或視圖時(shí),如果選擇列表中存在同名列,這時(shí)應(yīng)使用對象名限定這些列所屬的表或視圖。例如在usertable和citytable表中同時(shí)存在cityid列,在查詢兩個(gè)表中的cityid時(shí)應(yīng)使用下面語句格式加以限定:

                SELECT username,citytable.cityid
              FROM usertable,citytable
              WHERE usertable.cityid=citytable.cityid

              在FROM子句中可用以下兩種格式為表或視圖指定別名:
              表名 as 別名
              表名 別名

              (二) FROM子句

              FROM子句指定SELECT語句查詢及與查詢相關(guān)的表或視圖。在FROM子句中最多可指定256個(gè)表或視圖,它們之間用逗號分隔。
              在FROM子句同時(shí)指定多個(gè)表或視圖時(shí),如果選擇列表中存在同名列,這時(shí)應(yīng)使用對象名限定這些列所屬的表或視圖。例如在usertable和citytable表中同時(shí)存在cityid列,在查詢兩個(gè)表中的cityid時(shí)應(yīng)使用下面語句格式加以限定:

               SELECT username,citytable.cityid
              FROM usertable,citytable
              WHERE usertable.cityid=citytable.cityid

              在FROM子句中可用以下兩種格式為表或視圖指定別名:
              表名 as 別名
              表名 別名
              例如上面語句可用表的別名格式表示為:

               SELECT username,b.cityid
              FROM usertable a,citytable b
              WHERE a.cityid=b.cityid

              SELECT不僅能從表或視圖中檢索數(shù)據(jù),它還能夠從其它查詢語句所返回的結(jié)果集合中查詢數(shù)據(jù)。

              例如:

                SELECT a.au_fname+a.au_lname
              FROM authors a,titleauthor ta
              (SELECT title_id,title
              FROM titles
              WHERE ytd_sales>10000
              ) AS t
              WHERE a.au_id=ta.au_id
              AND ta.title_id=t.title_id

              此例中,將SELECT返回的結(jié)果集合給予一別名t,然后再從中檢索數(shù)據(jù)。

              (三) 使用WHERE子句設(shè)置查詢條件

              WHERE子句設(shè)置查詢條件,過濾掉不需要的數(shù)據(jù)行。例如下面語句查詢年齡大于20的數(shù)據(jù):

               SELECT *
              FROM usertable
              WHERE age>20

              WHERE子句可包括各種條件運(yùn)算符:
              比較運(yùn)算符(大小比較):>、>=、=、<、<=、<>、!>、!<
              范圍運(yùn)算符(表達(dá)式值是否在指定的范圍):BETWEEN...AND...
              NOT BETWEEN...AND...
              列表運(yùn)算符(判斷表達(dá)式是否為列表中的指定項(xiàng)):IN (項(xiàng)1,項(xiàng)2......)
              NOT IN (項(xiàng)1,項(xiàng)2......)
              模式匹配符(判斷值是否與指定的字符通配格式相符):LIKE、NOT LIKE
              空值判斷符(判斷表達(dá)式是否為空):IS NULL、NOT IS NULL
              邏輯運(yùn)算符(用于多條件的邏輯連接):NOT、AND、OR

              1、范圍運(yùn)算符例:age BETWEEN 10 AND 30相當(dāng)于age>=10 AND age<=30
              2、列表運(yùn)算符例:country IN ('Germany','China')
              3、模式匹配符例:常用于模糊查找,它判斷列值是否與指定的字符串格式相匹配??捎糜赾har、varchar、text、ntext、datetime和smalldatetime等類型查詢。
              可使用以下通配字符:
              百分號%:可匹配任意類型和長度的字符,如果是中文,請使用兩個(gè)百分號即%%。
              下劃線_:匹配單個(gè)任意字符,它常用來限制表達(dá)式的字符長度。
              方括號[]:指定一個(gè)字符、字符串或范圍,要求所匹配對象為它們中的任一個(gè)。[^]:其取值也[] 相同,但它要求所匹配對象為指定字符以外的任一個(gè)字符。
              例如:
              限制以Publishing結(jié)尾,使用LIKE '%Publishing'
              限制以A開頭:LIKE '[A]%'
              限制以A開頭外:LIKE '[^A]%'

              4、空值判斷符例WHERE age IS NULL

              5、邏輯運(yùn)算符:優(yōu)先級為NOT、AND、OR

              (四)查詢結(jié)果排序

              使用ORDER BY子句對查詢返回的結(jié)果按一列或多列排序。ORDER BY子句的語法格式為:
              ORDER BY {column_name [ASC|DESC]} [,...n]
              其中ASC表示升序,為默認(rèn)值,DESC為降序。ORDER BY不能按ntext、text和image數(shù)據(jù)類型進(jìn)行排
              序。
              例如:

                SELECT *
              FROM usertable
              ORDER BY age desc,userid ASC

              另外,可以根據(jù)表達(dá)式進(jìn)行排序。

              二、 聯(lián)合查詢

              UNION運(yùn)算符可以將兩個(gè)或兩個(gè)以上上SELECT語句的查詢結(jié)果集合合并成一個(gè)結(jié)果集合顯示,即執(zhí)行聯(lián)合查詢。UNION的語法格式為:

                select_statement
              UNION [ALL] selectstatement
              [UNION [ALL] selectstatement][...n]

              其中selectstatement為待聯(lián)合的SELECT查詢語句。

              ALL選項(xiàng)表示將所有行合并到結(jié)果集合中。不指定該項(xiàng)時(shí),被聯(lián)合查詢結(jié)果集合中的重復(fù)行將只保留一行。

              聯(lián)合查詢時(shí),查詢結(jié)果的列標(biāo)題為第一個(gè)查詢語句的列標(biāo)題。因此,要定義列標(biāo)題必須在第一個(gè)查詢語句中定義。要對聯(lián)合查詢結(jié)果排序時(shí),也必須使用第一查詢語句中的列名、列標(biāo)題或者列序號。

              在使用UNION 運(yùn)算符時(shí),應(yīng)保證每個(gè)聯(lián)合查詢語句的選擇列表中有相同數(shù)量的表達(dá)式,并且每個(gè)查詢選擇表達(dá)式應(yīng)具有相同的數(shù)據(jù)類型,或是可以自動將它們轉(zhuǎn)換為相同的數(shù)據(jù)類型。在自動轉(zhuǎn)換時(shí),對于數(shù)值類型,系統(tǒng)將低精度的數(shù)據(jù)類型轉(zhuǎn)換為高精度的數(shù)據(jù)類型。

              在包括多個(gè)查詢的UNION語句中,其執(zhí)行順序是自左至右,使用括號可以改變這一執(zhí)行順序。例如:

              查詢1 UNION (查詢2 UNION 查詢3)

              三、連接查詢

              通過連接運(yùn)算符可以實(shí)現(xiàn)多個(gè)表查詢。連接是關(guān)系數(shù)據(jù)庫模型的主要特點(diǎn),也是它區(qū)別于其它類型數(shù)據(jù)庫管理系統(tǒng)的一個(gè)標(biāo)志。

              在關(guān)系數(shù)據(jù)庫管理系統(tǒng)中,表建立時(shí)各數(shù)據(jù)之間的關(guān)系不必確定,常把一個(gè)實(shí)體的所有信息存放在一個(gè)表中。當(dāng)檢索數(shù)據(jù)時(shí),通過連接操作查詢出存放在多個(gè)表中的不同實(shí)體的信息。連接操作給用戶帶來很大的靈活性,他們可以在任何時(shí)候增加新的數(shù)據(jù)類型。為不同實(shí)體創(chuàng)建新的表,爾后通過連接進(jìn)行查詢。

              連接可以在SELECT 語句的FROM子句或WHERE子句中建立,似是而非在FROM子句中指出連接時(shí)有助于將連接操作與WHERE子句中的搜索條件區(qū)分開來。所以,在Transact-SQL中推薦使用這種方法。

              SQL-92標(biāo)準(zhǔn)所定義的FROM子句的連接語法格式為:

               FROM join_table join_type join_table
              [ON (join_condition)]

              其中join_table指出參與連接操作的表名,連接可以對同一個(gè)表操作,也可以對多表操作,對同一個(gè)表操作的連接又稱做自連接。

              join_type 指出連接類型,可分為三種:內(nèi)連接、外連接和交叉連接。內(nèi)連接(INNER JOIN)使用比較運(yùn)算符進(jìn)行表間某(些)列數(shù)據(jù)的比較操作,并列出這些表中與連接條件相匹配的數(shù)據(jù)行。根據(jù)所使用的比較方式不同,內(nèi)連接又分為等值連接、自然連接和不等連接三種。外連接分為左外連接(LEFT OUTER JOIN或LEFT JOIN)、右外連接(RIGHT OUTER JOIN或RIGHT JOIN)和全外連接(FULL OUTER JOIN或FULL JOIN)三種。與內(nèi)連接不同的是,外連接不只列出與連接條件相匹配的行,而是列出左表(左外連接時(shí))、右表(右外連接時(shí))或兩個(gè)表(全外連接時(shí))中所有符合搜索條件的數(shù)據(jù)行。

              交叉連接(CROSS JOIN)沒有WHERE 子句,它返回連接表中所有數(shù)據(jù)行的笛卡爾積,其結(jié)果集合中的數(shù)據(jù)行數(shù)等于第一個(gè)表中符合查詢條件的數(shù)據(jù)行數(shù)乘以第二個(gè)表中符合查詢條件的數(shù)據(jù)行數(shù)。

              連接操作中的ON (join_condition) 子句指出連接條件,它由被連接表中的列和比較運(yùn)算符、邏輯運(yùn)算符等構(gòu)成。

              無論哪種連接都不能對text、ntext和image數(shù)據(jù)類型列進(jìn)行直接連接,但可以對這三種列進(jìn)行間接連接。例如:

               SELECT p1.pub_id,p2.pub_id,p1.pr_info
              FROM pub_info AS p1 INNER JOIN pub_info AS p2
              ON DATALENGTH(p1.pr_info)=DATALENGTH(p2.pr_info)

              (一)內(nèi)連接
              內(nèi)連接查詢操作列出與連接條件匹配的數(shù)據(jù)行,它使用比較運(yùn)算符比較被連接列的列值。內(nèi)連接分三種:
              1、等值連接:在連接條件中使用等于號(=)運(yùn)算符比較被連接列的列值,其查詢結(jié)果中列出被連接表中的所有列,包括其中的重復(fù)列。
              2、不等連接: 在連接條件使用除等于運(yùn)算符以外的其它比較運(yùn)算符比較被連接的列的列值。這些運(yùn)算符包括>、>=、<=、<、!>、!<和<>。
              3、自然連接:在連接條件中使用等于(=)運(yùn)算符比較被連接列的列值,但它使用選擇列表指出查詢結(jié)果集合中所包括的列,并刪除連接表中的重復(fù)列。
              例,下面使用等值連接列出authors和publishers表中位于同一城市的作者和出版社:

               SELECT *
              FROM authors AS a INNER JOIN publishers AS p
              ON a.city=p.city
              又如使用自然連接,在選擇列表中刪除authors 和publishers 表中重復(fù)列(city和state):
              SELECT a.*,p.pub_id,p.pub_name,p.country
              FROM authors AS a INNER JOIN publishers AS p
              ON a.city=p.city

              (二)外連接
              內(nèi)連接時(shí),返回查詢結(jié)果集合中的僅是符合查詢條件( WHERE 搜索條件或 HAVING 條件)和連接條件的行。而采用外連接時(shí),它返回到查詢結(jié)果集合中的不僅包含符合連接條件的行,而且還包括左表(左外連接時(shí))、右表(右外連接時(shí))或兩個(gè)邊接表(全外連接)中的所有數(shù)據(jù)行。如下面使用左外連接將論壇內(nèi)容和作者信息連接起來:

               SELECT a.*,b.* FROM luntan LEFT JOIN usertable as b
              ON a.username=b.username

              下面使用全外連接將city表中的所有作者以及user表中的所有作者,以及他們所在的城市:

                SELECT a.*,b.*
              FROM city as a FULL OUTER JOIN user as b
              ON a.username=b.username

              (三)交叉連接
              交叉連接不帶WHERE 子句,它返回被連接的兩個(gè)表所有數(shù)據(jù)行的笛卡爾積,返回到結(jié)果集合中的數(shù)據(jù)行數(shù)等于第一個(gè)表中符合查詢條件的數(shù)據(jù)行數(shù)乘以第二個(gè)表中符合查詢條件的數(shù)據(jù)行數(shù)。例,titles表中有6類圖書,而publishers表中有8家出版社,則下列交叉連接檢索到的記錄數(shù)將等于6*8=48行。
               SELECT type,pub_name
              FROM titles CROSS JOIN publishers
              ORDER BY type

            修改字段屬性

            alter table tablename modify id int(10) unsigned auto_increment primary key not null

            修改默認(rèn)值

            alter table tablename alter id default 0

            給字段增加primary key

            alter table tablename add primary key(id);

            刪除primary key

            1、alter table tablename drop primary key;

            2、drop primary key on tablename;


            查看table表結(jié)構(gòu)

            show create table tableName;


            修改table表數(shù)據(jù)引擎

            alter table tableName ENGINE = MyISAM (InnoDB);

            增加字段
            ALTER TABLE `table` ADD `field` INT(11) UNSIGNED NOT NULL

            刪除字段

            alert table 'table' drop 'field'

             

            posted @ 2007-09-05 14:49 true 閱讀(588) | 評論 (0)編輯 收藏

            MySQL使用tips

            作者:葉金榮 (Email:imysql@gmail.com) 來源:http://imysql.cn (2006-07-12 17:05:03)


            1、用mysql內(nèi)置函數(shù)轉(zhuǎn)換ip地址和數(shù)字
            利用兩個(gè)內(nèi)置函數(shù)
            inet_aton:將ip地址轉(zhuǎn)換成數(shù)字型
            inet_ntoa:將數(shù)字型轉(zhuǎn)換成ip地址

            2、充分利用mysql內(nèi)置的format函數(shù)
            尤其是在處理字符格式的時(shí)候,例如將12345轉(zhuǎn)換成12,345這樣的,只要用:format(12345,0)即可,如果用format(12345,2)則顯示的是12,345.00了...

            3、利用mysql的內(nèi)置函數(shù)處理時(shí)間戳問題
            eg : select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x');
            結(jié)果: 2004 3rd August 03:35:48 2004

            4、利用mysql_convert_table_format轉(zhuǎn)換表類型
            需要DBI和DBD的mysql相關(guān)模塊支持才能用,例子:
            mysql_convert_table_format --user=root --password='xx' --type=myisam test yejr

            5、修改mysql表中的字段名
            alter table tb_name change old_col new_col definition...

            6、利用臨時(shí)變量
            select @var1:=a1+a2 as a_sum,@var2:=b1+b2 as b_sum,@var1+@var2 as total_sum from test_table xxx;

            7、用int類型存儲ip地址
            原先錯(cuò)誤的認(rèn)為必須用bigint才夠,后來發(fā)現(xiàn)使用int unsigned類型就足夠了。 :)

            8、CREATE TABLE IF NOT EXISTS ... select 語法局限
            盡管只是對目標(biāo)表的insert操作,但是‘居然’不允許源表的insert操作,真是莫名其妙

            9、利用IF函數(shù)快速修改ENUM字段值
            一個(gè)例子:
            update rule set enable = if('0' = enable,'1','0') where xxx;
            enable 類型:enum('0','1') not null default '0'

            10、事務(wù)無法嵌套

            11、避免長時(shí)間的sleep連接造成的連接數(shù)超出問題
            設(shè)定全局變量 wait_timeout 和 interactive_timeout 為比較小的值,例如 10(s),就能使每個(gè)sleep連接在10s之后如果還沒有查詢的話自動斷開。

            (http://www.fanqiang.com)
            posted @ 2007-08-30 10:20 true 閱讀(257) | 評論 (0)編輯 收藏

            http://www.codeproject.com/macro/KingsTools.asp

            Kings Tools

            Kings Tools

            Introduction

            As good as Visual Studio .NET is, I still miss some features in it. But MS knew that they couldn't fulfill every wish so they provided a way to write addins. That's what I've done. Sure, most of the functions in my Tools could also be done with macros, but I wanted them all packed together with an installer.

            Tools

            • Run Doxygen
            • Insert Doxygen comments
            • Build Solution stats
            • Dependency Graph
            • Inheritance Graph
            • Swap .h<->.cpp
            • Colorize
            • } End of
            • #region/#endregion for c++
            • Search the web

            Run Doxygen

            This command first pops up a dialog box in which you can configure the output Doxygen should produce. For those who don't know Doxygen: it's a free tool to generate source documentations. It can produce documentation in different formats like html and even windows help format! See http://www.doxygen.org/ for details. Since the dialog box doesn't offer all possible settings for doxygen, you can always edit the file Doxyfile.cfg manually which is created the first time you run it. All settings in that file override the settings you enter in the dialog box.

            Doxygen configuration dialog

            If you set Doxygen to generate html output, the resulting index.html is opened inside the IDE. A winhelp output (index.chm) will be opened outside the IDE.

            The command available from the Tools menu builds the documentation for the whole solution. If you don't want that for example if you have several third party projects in your solution then you can build the documentation also for single projects. To do that the KingsTools add a command to the right click menu in the solution explorer.

            If you want to update Doxygen to a newer version (as soon as one is released) simply overwrite the doxygen.exe in the installation directory. The same applies to the dot.exe.

            TODO: find a way to integrate the generated windows help file into VS help.

            Insert Doxygen comments

            Doxygen needs comments that follow certain conventions to build documentation from. This part of the tools inserts them for you. Either from the right click menu in the code editor window or from the submenu under Tools->Kings Tools. Just place the caret over a method or class header. The inserted comment for a method or function would look like this:

            				/**
            *
            * \param one
            * \param two
            * \param three
            * \return
            */
            BOOL myfunction(int one, int two, int three);
            

            You now have to simply insert a description in the second comment line and descriptions for each parameter of the function/method. And of course a description of the return value.

            You can customize the function comments by editing the files "functionheadertop.txt", "functionparams.txt" and "functionheaderbottom.txt". Please read the comments inside those files on how to do that. If you don't want to change the function comments for all your projects then you can place any of those files into your project directory (that way it will be used for your project) or inside the folder of your source files (that way it will be used only for the files inside that specific folder).

            The inserted comment for a class looks like this:

            				/**
            * \ingroup projectname
            *
            * \par requirements
            * win98 or later, win2k or later, win95 with IE4 or later, winNT4 with IE4
            * or later
            *
            * \author user
            *
            * \par license
            * This code is absolutely free to use and modify. The code is provided
            * "as is" with no expressed or implied warranty. The author accepts no
            * liability if it causes any damage to your computer, causes your pet to
            * fall ill, increases baldness or makes your car start emitting strange
            * noises when you start it up. This code has no bugs, just undocumented
            * features!
            *
            * \version 1.0
            * \date 06-2002
            * \todo
            * \bug
            * \warning
            *
            */
            class CRegBase
            

            The '\ingroup projectname' means that the class is inside the project 'projectname'. That statement helps Doxygen to group classes together. Insert the description of the class right after that statement. If you want to include pictures to illustrate the class, use '\image html "picture.jpg"'. For more helpful tags you can use please check out the Doxygen website. The '\par requirements' section you have to modify yourself to fit the truth of your class. It's not necessary for Doxygen, but I found it very useful to give that information inside a class documentation. The name after the '\author' tag is the currently logged in user. Maybe you want to change that too to include an email address.

            You can customize the class comments by editing the file "classheader.txt" Please read the comments inside that file on how to do that. If you don't want to change the class comments for all your projects then you can place that files into your project directory (that way it will be used for your project) or inside the folder of your source files (that way it will be used only for the files inside that specific folder).

            The last few tags should be self-explanatory. Under the line '\version' I usually insert short descriptions of what changed between versions.

            Build Solution stats

            This is a simple line counter. It counts all the lines of all files in your solution, grouped by projects. The generated html file with the counted lines (code, comments, empty) is then opened in the IDE. Since I haven't found a way to add a file directly to a solution and not to a project the file is just opened for view in the IDE.

            Dependency and Inheritance graph

            These two commands build graphs of the class relations in your solution. See my previous article about this. The difference to my old tool is that it now generates graphs for all projects in the solution and puts all the graphs in one single html page.

            Swap .h<->.cpp

            This is something a simple macro could also do: it swaps between header and code files. For better accessibility it also is on the right click menu of the code editor. Really nothing special but it can be useful sometimes.

            Colorize

            This tool goes through all files of the current solution and looks for class, function and macronames. It then writes them to a usertype.dat file, makes the IDE to read that file and deletes it again. After you run this tool, all class, function and macronames of your solution appear colored in the code editor. Default color is the same color as normal keywords, but you can change that under Tools->Options, in the Options dialog select Environment->Fonts and Colors.

            If you don't want the colors anymore, just run the command 'disable coloring' and everything will be in normal colors again. I didn't want to overwrite some possible usertype.dat file already created by some user so the tool simply creates a temporary usertype.dat file instead. If you want to have the colors again the next time the IDE starts, you either have to rerun the command (doesn't take very long to execute) or change the code of the tool yourself.

            } End of

            Have you ever wrote a bunch of code which looked like this:

            Braces without comments

            Ok, I admit this isn't a very good style of programming, but sometimes it can't be avoided. And in those cases the code is horrible to read because you don't know which closing brace belongs to which opening statement without scrolling or using the macro 'Edit.GotoBrace' several times. This tool provides a function which inserts comments after the closing brace automatically. The code snippet above would look like this:

            Braces with comments

            Comments are only inserted for closing braces of if, while, for and switch statements.

            If you don't want to insert comments automatically while editing, you can turn off this function. If you just don't want those comments at specific places you have to move the caret either upwards (instead of downwards which happens if you press enter) or click with the mouse so that the caret doesn't go to the line below the closing brace. Comments are also not inserted when the opening brace is less than two lines above.

            #region/#endregion for C++

            VS.NET introduced to possibility to outline portions of text in the code editor. That's a very useful feature wthat helps navigating through big bunches of code. But the outlined sections are not saved between sessions. VB and C# provide keywords to outline sections. In VB its '#Region' and '#End Region', in C# its '#region' and '#endregion'. Only for C++ MS didn't provide such keywords (at least I haven't found them yet). With this tool you can now enable that feature for C++ too. To prevent compiler errors for those who have not installed this tool I used '//#region' and '//#endregion' as the keywords. With the comment lines before the compiler won't complain. Use those keywords like this:

            outlined sections

            Whenever you open a document with such keywords the tool will automatically create outlining sections. The section are also created when you type the '//#endregion' keyword and a matching '//#region' is found. As you can see, you can easily nest the sections. The code above would then look like this:

            outlined sections

            outlined sections

            This function can't be deactivated. If you don't want it, simply don't use those keywords :)

            Search the web

            These two small addons perform a simple web site search either in the google groups or on CP. Select a piece of text in the code editor, right click to pop up the menu and then select where to search for the selected text. That's all. The search results will be opened inside VS.NET.

            right click menu

            Install

            To install the tools, just double-click the *.msi file and follow the instructions. If the tools are not automatically activated the next time you start the IDE, then please activate them under Tools->Add-In Manager. Make sure you select both the addin and the box 'startup'.

            All additional files needed for the tools are also packed in the installer, including Doxygen and the dot files. So you don't have to grab them separately from the web.

            Source

            Full source code is provided with these tools. The addin is written in VB.NET cause first there was just one simple tool that I wanted immediately - and VB is good enough for that. Then the tool grew and I added more functions. So the code is surely not the best example for good programming (no plan, no structure -> chaos). But maybe it might still be of interest for those who want to write their own addins. It shows a way to create submenus and how to add a toolbar.

            Revision History

            24.06.03
            • fixed bug in Doxygen part: the path to the binaries weren't enclosed in ""
            • made necessary changes to make the addin work with VS.NET2003 (projectitems are now recursive!)
            • updated the Doxygen binaries to the newest version
            • the dialogs are now centered to the IDE
            18.04.03
            • fixed some bugs in the }EndOf function
            • added template files for doxygen comments
            • fixed bug in the graph functions if the solution contained "misc" files
            • Doxygen 1.3 is now included
            • removed the toolbar - it slowed the editor down
            • for most commands disabled the check for project type (C++, C#, VB, ...) - if you use a function for a project type for what it isn't designed it just won't work...
            04.10.02
            • enabled }EndOf and the solution statistics also for C# projects
            21.9.02
            • fixed a bug in the }EndOf tool
            • fixed bug where Doxygen couldn't be started when a file was in the Solution->Misc folder
            • added possibility to run Doxygen for single projects (right click menu in solution explorer)
            • included newest Doxygen and Dot version
            • added a proper uninstaller. The uninstaller now deletes all added commands.
            7.9.02
            • fixed a bug reported by Darren Schroeder
            8.8.02
            • removed forgotten test code which caused annoying behaviour
            • made sure that for WinHelp output (Doxygen) also html output is selected
            10.8.02
            • fixed a bug reported by Jeff Combs: now the addin is only loaded when the IDE is started (the IDE is NOT started when devenv is called with /build, /clean or /deploy command line switches!)
            12.8.02
            • Run Doxygen now includes not only project directories but all directories of the project files.
            • The Toolbar can now be altered and the altered state is saved by the IDE
            • Uninstalling now works better: the toolbar is gone after the second start of the IDE after uninstalling without modifying the source.
            posted @ 2007-08-27 01:19 true 閱讀(655) | 評論 (1)編輯 收藏

            開源數(shù)據(jù)庫概覽

            開源世界真是太奇妙了,雖然不排除卑鄙無恥的直接盜用并貫為自己的產(chǎn)品,但開源可以無私到隨便你怎樣用。

            接觸開源有很長的一段時(shí)間了,先是學(xué)習(xí)別人的,然后還參與了開源,在sf.net上,我主持和參與了數(shù)個(gè)開源項(xiàng)目,當(dāng)然,都不是大型的項(xiàng)目,只是嘗試一下。

            我所關(guān)注的開源項(xiàng)目方面很多,每方面都有很多優(yōu)秀的作品,我將會在接下來的系列隨筆中介紹,這次介紹數(shù)據(jù)庫。

            這個(gè)星球上的數(shù)據(jù)庫實(shí)在不勝枚舉,這里只列一些我接觸過的常見的。

            可以稍微夸張點(diǎn)說,有交互的應(yīng)用,起碼得用一下數(shù)據(jù)保存,即便是自定義結(jié)構(gòu)的數(shù)據(jù)保存,還是最常見的INI、XML等,都可以算是“數(shù)據(jù)庫”,真正點(diǎn)的,如DBase系列、FoxBase、FoxPro、MSAccess、InterBase、MS SQL Server、Oracle、DB2等,這些是商業(yè)化的數(shù)據(jù)庫,前面幾個(gè)只能算是數(shù)據(jù)庫,后面幾個(gè)是RMDBS(關(guān)系型數(shù)據(jù)庫管理系統(tǒng))。

            對應(yīng)商業(yè)化的,有開源的:SQLite、SimpleSQL、Berkely DBMinosseFirebird( 前身是是Borland公司的InterBase)、PostgreSQLMySQL等。

            SQLite:大家可以看我的SQLite系列隨筆,C編寫的,可以跨操作平臺,支持大部分ANSI SQL 92,它是嵌入式的輕量級關(guān)系形數(shù)據(jù)庫引擎,只需要一個(gè)DLL,體積為250k,數(shù)據(jù)庫也只是一個(gè)文件,零配置,便可工作。既然開源,你甚至可以把它嵌入你的程序中。核心開發(fā)人員只有一個(gè),最近加入了另外一個(gè),也就是2個(gè)人而已,實(shí)在佩服,目前發(fā)展到3.1.0,相當(dāng)高效穩(wěn)定,有開源驅(qū)動在sourceforge.net上有其ADO.NET Data Provider for SQLite :https://sourceforge.net/projects/adodotnetsqlite/

            SimpleSQL:相對SQLite要大幾倍,但也是輕量級的,功能稍微強(qiáng)大一點(diǎn),C++編寫,有OLE、Java等版本。

            Berkely DB:C++編寫的大型關(guān)系型數(shù)據(jù)庫系統(tǒng),還額外地支持XML(把XML當(dāng)成數(shù)據(jù)庫),號稱2百萬的安裝量,MySQL也只不過號稱5百萬安裝量而已,跨平臺。

            Minosse:純C#編寫的大型關(guān)系型數(shù)據(jù)庫系統(tǒng),理想是超越MS SQL Server!最新版本:0.2.0,真難得,純Java寫的看得多了,純C#的,不是移植別人的,還是第一個(gè),佩服作者:包含C/S和嵌入式版本,并可跨越大部分平臺,因?yàn)樗挥肳indows的東西,可以在Mono下編譯。

            Firebird:這個(gè)東西太牛了,目前有1.5穩(wěn)定版本已經(jīng)擁有大量特性,完全支持ANSI SQL92、98等,一些超酷的特性讓人瘋狂(1.0特性、1.5特性從這里開始研究),主要開發(fā)人員是一個(gè)俄羅斯人,目前開發(fā)隊(duì)伍已經(jīng)擴(kuò)大到近100人,有3種模式,單機(jī)獨(dú)立,典型C/S,超級服務(wù)器。2.0版本和3.0版本將在近期推出,看完其路線圖(2.0、3.0)你就會瘋掉。有.NET驅(qū)動,目前是1.7beta版。主要特性: 
                ◆A.C.I.D; 
                ◆MGA(任何版本的引擎都可以處理同一數(shù)據(jù)庫記錄); 
                ◆PSQL(存儲過程)超級強(qiáng)大,ms sql相對的太次,它啥都能在服務(wù)器端實(shí)現(xiàn)并推送到客戶端成為強(qiáng)大的報(bào)表,存儲過程; 
                ◆觸發(fā)器都可以在客戶端獲取監(jiān)控追蹤; 
                ◆自動只讀模式; 
                ◆創(chuàng)新的事務(wù)保證絕對不會出錯(cuò); 
                ◆24*7運(yùn)行中仍然可以隨時(shí)備份數(shù)據(jù)庫; 
                ◆統(tǒng)一觸發(fā)器:任何操作都可以讓某表唯一的觸發(fā)器來總控; 
                ◆大部分語言都可以寫plug-in,并直接在存儲過程中調(diào)用函數(shù); 
                ◆c->c++,更加少的代碼但更加快的速度; 
                ◆3種運(yùn)行模式,甚至可以嵌入式; 
                ◆主流語言都可以調(diào)用它; 
                ◆動態(tài)sql執(zhí)行; 
                ◆事務(wù)保存點(diǎn);

            PostgreSQL:POSTGRES數(shù)據(jù)庫的后開源版本,號稱擁有任何其他數(shù)據(jù)庫沒有的大量新特性,似乎目標(biāo)是要做超大型的OO關(guān)系型數(shù)據(jù)庫系統(tǒng),目前已經(jīng)發(fā)展到8.0,有.NET驅(qū)動,中文官方網(wǎng)站有詳細(xì)介紹。

            MySQL:這個(gè),不用說了吧?號稱全球最受歡迎的開源數(shù)據(jù)庫,但讓我奇怪的是,PostgreSQL都有簡體中文的支持:包括內(nèi)核、管理工具、QA等等,在最新版本MySQL中,我卻沒有發(fā)現(xiàn)... ,有.NET驅(qū)動,其中MySQL Connector/Net就是原來在sf.net上的ByteFX.Data項(xiàng)目,作者已經(jīng)加入了MySQL團(tuán)隊(duì),參看《感慨 20 之開源的前途/錢圖?(1數(shù)據(jù)庫)》。
                
                網(wǎng)友評論
            RunEverywhere:   純Java寫的數(shù)據(jù)庫- -
              
              
              
              純Java數(shù)據(jù)庫包括:
              Informix, Cloudscape(也就是Apache Derby數(shù)據(jù)庫),JDataStore(Borland公司),HSQLDB, db4o, PointBase(Oracle創(chuàng)始人開發(fā)),
              
              Berkeley DB Java Edition 2.0 開源數(shù)據(jù)庫等等。誰有證據(jù)證明Oracle和DB2中Java使用的比例請告知。只知Oracle和DB2中有大量的.class文件,但不知是否有C/C++開發(fā)的部分,畢竟java也能編譯成.exe和.dll文件。
              
              Oracle數(shù)據(jù)庫(使用了Java開發(fā),但不知是否是純Java)
              www.oracle.com
              
              
              DB2數(shù)據(jù)庫(使用了Java開發(fā),但不知是否是純Java):
              www-306.ibm.com/software/data/db2/
              
              Informix數(shù)據(jù)庫
              
              IBM 在 2001 年七月初購併 Informix,將Informix 轉(zhuǎn)換為以Java 語言開發(fā)的環(huán)境之外,並採納 Informix
              的資料複製功能,提升 DB2 災(zāi)難復(fù)原與資料複製的能力
              IBM 每年投資十億美元於資料庫管理軟體的研發(fā)工作,致力於強(qiáng)化資訊管理軟體解決方案的技術(shù)優(yōu)勢與產(chǎn)品效能,去 ( 2003 ) 年並取得超過
              
              230 項(xiàng)相關(guān)專利權(quán);又於日前捐出價(jià)值超過八千五百萬美元的 Java 資料庫軟體 Cloudscape 給 Apache
              
              
              http://www.ibm.com/news/tw/2004/11/tw_zh_20041119_linux.html
              Apache Derby 是一種用 100% 純 Java 編寫的關(guān)系數(shù)據(jù)庫。該項(xiàng)目最初被稱作 Cloudscape™,IBM 于 2004 年 8 月將它捐獻(xiàn)給了 Apache 基金組織
              http://www-128.ibm.com/developerworks/cn/db2/library/techarticles/dm-0505gibson/?ca=dwcn-newsletter-db2
              
              
              Cloudscape 開源數(shù)據(jù)庫
              
              於日前捐出價(jià)值超過八千五百萬美元的 Java 資料庫軟體 Cloudscape 給 Apache
              
              http://www.ibm.com/news/tw/2004/11/tw_zh_20041119_linux.html
              
              
              
              JDataStore數(shù)據(jù)庫
              
              Borland公司出品:
              www.borland.com/us/products/jdatastore/
              
              
              HSQLDB開源數(shù)據(jù)庫
              
              http://hsqldb.sf.net
              
              
              
              Berkeley DB Java Edition 2.0 開源數(shù)據(jù)庫
              
              http://www.sleepycat.com/
              
              
              db4o開源數(shù)據(jù)庫
              www.db4o.com/
              
              
              
              
              
              
              還有一些Java數(shù)據(jù)庫:
              
              在全球最大的java開發(fā)者雜志上的一份對最受歡迎的Java數(shù)據(jù)庫的調(diào)查:
              
              Best Enterprise Database:
              
               No Nominee
               Berkeley DB Java Edition Sleepycat Software
               Birdstep RDM Embedded 7.1 Birdstep Technology
               Daffodil DB Daffodil Software Ltd.
               db4o db4objects
               EAC MySQL Cluster Emic Networks
               HSQLDB HSQLDB Development Team
               IBM DB2 Universal Database IBM
               IBM Informix IDS v10 IBM
               JDataStore 7 High Availability Edition Borland Software
               ObjectDB for Java/JDO ObjectDB
               Oracle Database 10g Oracle Corporation
               Oracle Database Lite 10g Oracle Corporation
               PointBase Embedded PointBase / DataMirror Corp.
               Sybase Adaptive Server Enterprise (ASE) Sybase, Inc.
              
              
              http://jdj.sys-con.com/general/readerschoice.htm
              
              http://nuclearjava.blogchina.com/2006316.html (2005.06.26)

            posted @ 2007-08-20 12:13 true 閱讀(1094) | 評論 (0)編輯 收藏

            僅列出標(biāo)題
            共15頁: First 7 8 9 10 11 12 13 14 15 
            国产精品嫩草影院久久| 久久精品这里热有精品| 久久久久国产精品麻豆AR影院| 久久久久久久综合日本亚洲| 热99RE久久精品这里都是精品免费 | 99久久国产综合精品女同图片| 久久久亚洲裙底偷窥综合| 久久天天躁狠狠躁夜夜avapp | 午夜人妻久久久久久久久| 久久精品一区二区国产| 国产亚洲成人久久| 亚洲综合日韩久久成人AV| 精品一二三区久久aaa片| 久久久久高潮综合影院| 久久精品国产2020| 天天爽天天狠久久久综合麻豆| 久久亚洲AV成人出白浆无码国产| 久久亚洲精品成人av无码网站| 精品少妇人妻av无码久久| 久久亚洲精品无码观看不卡| 久久天堂AV综合合色蜜桃网| 国产Av激情久久无码天堂| 久久99精品久久久久久| 精品久久久久国产免费| 国产精品久久久久久久久软件| 久久无码专区国产精品发布| 久久国产色AV免费看| 久久亚洲精品成人无码网站 | 久久丫精品国产亚洲av不卡 | 久久婷婷五月综合色奶水99啪| 亚洲αv久久久噜噜噜噜噜| 99久久久国产精品免费无卡顿| 99久久精品费精品国产 | 久久青青草原国产精品免费 | 亚洲国产精品无码久久一线 | 久久精品视频91| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 久久久久亚洲AV片无码下载蜜桃| 九九久久自然熟的香蕉图片| 久久国产精品免费一区二区三区| 久久伊人精品一区二区三区|