2011-03-28 13:09

[C語言] 讀取簡易的設定檔

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CONFIG_PATH "test.conf"
#define SIZE         256

/*config vars*/
char path[SIZE];
char extension[SIZE];
int limit=0;

bool readConfig(){
    char name[SIZE];
    char value[SIZE];

    FILE *fp = fopen(CONFIG_PATH, "r");
    if (fp == NULL) { return false; }
    while(!feof(fp)){
        memset(name,0,SIZE); memset(value,0,SIZE);

        /*Read Data*/
        fscanf(fp,"%s = %s\n", name, value);

        if (!strcmp(name, "path")){
            strcpy(path, value);

        }else if (!strcmp(name, "extension")){
            strcpy(extension, value);

        }else if (!strcmp(name, "limit")){
            limit = atoi(value);
        }
    }
    fclose(fp);

    return true;
}


/*= main function=*/
int main(int argc, const char ** argv){
    memset(path,0,SIZE);
    memset(extension,0,SIZE);

    /*read config*/
    if(!readConfig()){
        fprintf(stderr,"read config fail!");
        return 1;
    }

    printf("path = %s\n", path);
    printf("extension = %s\n", extension);
    printf("limit = %d\n", limit);
    return 0;
}

test.conf
path = /tmp
extension = jpg,jpeg,png
limit = 10000
2011-03-28 12:53

[C語言] 用 fnmatch 依副檔名過濾檔案類型

#include <stdio.h>
#include <string.h>
#include <fnmatch.h>

#define SIZE 256

int main(int argc, const char ** argv){
    char extension[] = "jpg,png,gif";

    char extensionTemp[SIZE];
    char pattern[SIZE];
    char *token;

    for (int i = 1; i < argc; i++){
        strcpy(extensionTemp,extension);
        token = strtok(extensionTemp,",");
        while (token != NULL && strlen(token)>0){
            strcpy (pattern,"*."); // *.
            strcat (pattern,token); // *.jpg

            if( fnmatch(pattern,argv[i],0) == 0 ) {
                printf("%s\n",argv[i]);
            }

            token = strtok(NULL,",");
        }
    }

    return 0;
}
2011-03-28 12:53

[C語言] 取得檔案副檔名

#include <string.h>

char *getExtension(char *fileName){
    int len = strlen(fileName);
    int i = len;
    while( fileName[i]!='.' && i>0 ){ i--; }

    if(fileName[i]=='.'){
        return &fileName[i+1];
    }else{
        return &fileName[len];
    }
}
2011-03-28 02:06

在 Blogger 加上各種推文按鈕(修正Plurk 的Bug)

網友提出了 Plurk 的 bug
說實在的找了半天的文件
Blogger 並沒有可以直接使用的參數
看來又只能依靠 JavaScript 來處理這種鳥問題了

最終版程式碼
在 Blogger 加上各種推文按鈕



CSS 程式碼

/*Social Icon*/
.social{
background:transparent url(http://www.xxxx.com/social_icons.png) 0 0 no-repeat;
/*16*16按鈕的圖片網址*/

display: -moz-inline-box;
display: inline-block;
height:16px;
margin:0 4px;
text-indent:-999999px;
vertical-align:middle;
width:16px;
opacity:0.75;
overflow:hidden;
}
.social:hover{
opacity:1;
}

a.toFacebook{ background-position: 0 0; }
a.toPlurk{ background-position: 0 -16px; }
a.toTwitter{ background-position: 0 -32px; }
a.toTechnorati{ background-position: 0 -48px; }
a.toDelicious{ background-position: 0 -64px; }
a.toBuzz{ background-position: 0 -80px; }
a.toDigg{ background-position: 0 -96px; }
a.toStumbleUpon{ background-position: 0 -112px; }
a.toDesignFloat{ background-position: 0 -128px; }
a.getAtomRSS{ background-position: 0 -144px; }
a.toReader{ background-position: 0 -160px; }

首先打開『版面配置』→『修改HTML』
將『展開小裝置範本』打勾
找到 ]]></b:skin> 將 CSS 的程式貼在上一行
小技巧:所有的瀏覽器都有搜尋功能 (Ctrl + F),只要搜尋 "skin" 就可以找到這行了



HTML 程式碼

找尋 <div class='post-footer'> 這一行
然後向下找尋相對應的 </div>
這裡沒有比較清楚的標示
找起來會比較困難一點
將下面的程式貼在 </div> 之前
<div class='post-footer-line post-footer-line-99'>
 <span class='post-social-icons' name='post-social-icons' expr:t='data:post.title' expr:u='data:post.url' expr:h='data:blog.homepageUrl'></span>
</div>


JavaScript 程式碼

找尋 </body> 這一行,將 JavaScript 的程式貼在上一行
<script type="text/javascript">
var data = [
    {&quot;c&quot;:&quot;toFacebook&quot;, &quot;u&quot;:&quot;http://www.facebook.com/sharer.php?u=$url$&amp;t=$title$&quot;,&quot;t&quot;:&quot;Facebook&quot;},
    {&quot;c&quot;:&quot;toPlurk&quot;, &quot;u&quot;:&quot;http://www.plurk.com/?qualifier=shares&amp;status=$url$ ($title$)&quot;,&quot;t&quot;:&quot;Plurk&quot;},
    {&quot;c&quot;:&quot;toTwitter&quot;, &quot;u&quot;:&quot;http://twitter.com/home?status=$title$ $url$&quot;,&quot;t&quot;:&quot;Twitter&quot;},
    {&quot;c&quot;:&quot;toTechnorati&quot;, &quot;u&quot;:&quot;http://technorati.com/faves?add=$url$ $title$&quot;,&quot;t&quot;:&quot;Technorati&quot;},
    {&quot;c&quot;:&quot;toDelicious&quot;, &quot;u&quot;:&quot;http://del.icio.us/post?url=$url$ $title$&quot;,&quot;t&quot;:&quot;Delicious&quot;},
    {&quot;c&quot;:&quot;toDigg&quot;, &quot;u&quot;:&quot;http://digg.com/submit?phase=2&amp;url=$url$&amp;title=$title$&quot;,&quot;t&quot;:&quot;Digg&quot;},  
    {&quot;c&quot;:&quot;toStumbleUpon&quot;, &quot;u&quot;:&quot;http://www.stumbleupon.com/submit?url=$url$&amp;title=$title$&quot;,&quot;t&quot;:&quot;Stumble Upon&quot;},
    {&quot;c&quot;:&quot;toDesignFloat&quot;, &quot;u&quot;:&quot;http://www.designfloat.com/submit.php?url=$url$&amp;title=$title$&quot;,&quot;t&quot;:&quot;Design Float&quot;},
    {&quot;c&quot;:&quot;toReader&quot;, &quot;u&quot;:&quot;http://www.google.com/reader/link?url=$url$&amp;title=$title$&amp;srcURL=$home$&quot;,&quot;t&quot;:&quot;Google Reader&quot;},
    {&quot;c&quot;:&quot;toBuzz&quot;, &quot;u&quot;:&quot;http://www.google.com/buzz/post?url=$url$&quot;,&quot;t&quot;:&quot;Buzz&quot;},
    {&quot;c&quot;:&quot;getAtomRSS&quot;, &quot;u&quot;:&quot;/feeds/posts/default&quot;, &quot;t&quot;:&quot;Atom / RSS&quot;}   
];


var list=document.getElementsByName(&#039;post-social-icons&#039;);

for (var li=0,ll=list.length; li&lt;ll; li++){
    var title=encodeURIComponent(list[li].getAttribute(&#039;t&#039;));    
    var url=encodeURIComponent(list[li].getAttribute(&#039;u&#039;));    
    var home=encodeURIComponent(list[li].getAttribute(&#039;h&#039;));
    var temp=&quot;分享至 &amp;#65306;&quot;;
    for (var di=0,dl=data.length; di&lt;dl; di++){
        var href=data[di].u.replace(&quot;$title$&quot;,title).replace(&quot;$url$&quot;,url).replace(&quot;$home$&quot;,home);
        temp+=&quot;&lt;a class=&#039;social &quot;+data[di].c+&quot;&#039; href=&#039;&quot;+href+&quot;&#039; title=&#039;分享至 &quot;+data[di].t+&quot;&#039;&gt;&quot;+data[di].t+&quot;&lt;/a&gt;&quot;
    };
    list[li].innerHTML=temp;
};
</script>


JavaScript 原始碼

var data = [
    {"c":"toFacebook", "u":"http://www.facebook.com/sharer.php?u=$url$&t=$title$","t":"Facebook"},
    {"c":"toPlurk", "u":"http://www.plurk.com/?qualifier=shares&status=$url$ ($title$)","t":"Plurk"},
    {"c":"toTwitter", "u":"http://twitter.com/home?status=$title$ $url$","t":"Twitter"},
    {"c":"toTechnorati", "u":"http://technorati.com/faves?add=$url$ $title$","t":"Technorati"},
    {"c":"toDelicious", "u":"http://del.icio.us/post?url=$url$ $title$","t":"Delicious"},
    {"c":"toDigg", "u":"http://digg.com/submit?phase=2&url=$url$&title=$title$","t":"Digg"},  
    {"c":"toStumbleUpon", "u":"http://www.stumbleupon.com/submit?url=$url$&title=$title$","t":"Stumble Upon"},
    {"c":"toDesignFloat", "u":"http://www.designfloat.com/submit.php?url=$url$&title=$title$","t":"Design Float"},
    {"c":"toReader", "u":"http://www.google.com/reader/link?url=$url$&title=$title$&srcURL=$home$","t":"Google Reader"},
    {"c":"toBuzz", "u":"http://www.google.com/buzz/post?url=$url$","t":"Buzz"},
    {"c":"getAtomRSS", "u":"/feeds/posts/default", "t":"Atom / RSS"}   
];


var list=document.getElementsByName('post-social-icons');

for (var li=0,ll=list.length; li<ll; li++){
    var title=encodeURIComponent(list[li].getAttribute('t'));    
    var url=encodeURIComponent(list[li].getAttribute('u'));    
    var home=encodeURIComponent(list[li].getAttribute('h'));
    var temp="分享至 :";
    for (var di=0,dl=data.length; di<dl; di++){
        var href=data[di].u.replace("$title$",title).replace("$url$",url).replace("$home$",home);
        temp+="<a class='social "+data[di].c+"' href='"+href+"' title='分享至 "+data[di].t+"'>"+data[di].t+"</a>"
    };
    list[li].innerHTML=temp;
};


圖片來源:

Social Media Network Icons | Komodo Media
2011-03-21 01:16

[轉載] PHP中冒号、endif、endwhile、endfor这些都是什么

轉載自:PHP中冒号、endif、endwhile、endfor这些都是什么

我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如:
<?php if( empty($_GET['a']) ): ?>
<font color="red">空的</font>
<?php endif; ?>


对于相当一部分PHP爱好者来说根本没见过啊,这些是什么东西呢?难道是那些博客的开发者自己搞的仿PHP的模板语言?
非也,其实这些都是PHP的语法,只不过不常用而已,这些都是PHP流程控制的替代语法。
这里就就给大家详细说一下PHP流程控制的替代语法。什么是替代语法?
简单的说就是一些语法的另类写法。

PHP中那些语法有替代语法?
流程控制(包括if,while,forforeach,switch)这几个语句有替代语法。

替代语法的基本形式:
左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,endwhile;,endfor;,endforeach; 以及 endswitch;

举个例子吧:
<?php if ($a<0): ?>
是负数拉
<?php endif; ?>

上面的语句等同于
<?php if ($a<0){ ?>
是负数拉
<?php } ?>



为什么纯PHP代码里面几乎看不到?
这些语法有点不符合C家族的传统有点另类,大家不大习惯于这种语法,而且不是很方便

大家都不习惯,而且这么另类要他有什么用?蛋疼吗?
存在就是合理,它自有它的用处,这些语法能发挥的地方是在PHP和HTML混合页面的代码里面。好处如下:
  1. 使HTML和PHP混合页面代码更加干净整齐。
    有代码洁癖的朋友最惧怕的就是乱七八糟的混合代码,有了这些没有花括号的替代语法,各位爱干净的朋友开心到尿震。
  2. 流程控制逻辑更清晰,代码更容易阅读
    要改别人的PHP和HTML混合代码,打开发现,我擦!太TMD垃圾了!如果用替代语法,我想再垃圾的程序开发人员也不至于写的太乱吧。
  3. 一些从ASP等其他类basic语言家族转来的朋友,会更容易使用PHP。


讲半天没用的,捞点干的行不?这东西怎么用?

根据之前描述的使用方法,if语句的替代语法使用如下:
<?php if ($a == 5): ?>
  <div>等于5</div>
<?php elseif ($a == 6): ?>
  <div>等于5</div>
<?php else: ?>
  <div>不是5就是6</div>
<?php endif; ?>

while替代语法:
<?php while (expr): ?>
  <li>循环点什么</li>
<?php endwhile; ?>

for替代语法:
<?php for (expr1; expr2; expr3): ?>
  <li>循环点什么</li>
<?php endfor; ?>

foreach替代语法:
<?php foreach (expr1): ?>
  <li>循环点什么</li>
<?php endforeach; ?>

switch替代语法:
<?php
switch ($i):
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
        echo "i is not equal to 0, 1 or 2";
endswitch;
?>
2011-03-02 16:19

[PHP] 取得生日對應的星座名稱

在 Google 上找了一下能用 function,花了十分鐘決定自己寫了,說實在的在 social 的網站上這算是基本需求,而以前寫的也不知道丟哪去了,這次好好記錄下來免得以後又要再寫一份。

function getStarSignsName($month, $day) {
    $list=array(
        array('name'=>"摩羯座",'min'=>'12-22','max'=>'01-19'),  
        array('name'=>"水瓶座",'min'=>'01-20','max'=>'02-18'),
        array('name'=>"雙魚座",'min'=>'02-19','max'=>'03-20'),
        array('name'=>"牡羊座",'min'=>'03-21','max'=>'04-19'),
        array('name'=>"金牛座",'min'=>'04-20','max'=>'05-20'),
        array('name'=>"雙子座",'min'=>'05-21','max'=>'06-21'),
        array('name'=>"巨蟹座",'min'=>'06-22','max'=>'07-22'),
        array('name'=>"獅子座",'min'=>'07-23','max'=>'08-22'),
        array('name'=>"處女座",'min'=>'08-23','max'=>'09-22'),
        array('name'=>"天秤座",'min'=>'09-23','max'=>'10-23'),
        array('name'=>"天蠍座",'min'=>'10-24','max'=>'11-22'),
        array('name'=>"射手座",'min'=>'11-23','max'=>'12-21'),
    ); 
    
    $time=strtotime("1970-$month-$day");
    foreach ($list as $row){
        $min=strtotime("1970-".$row['min']);
        $max=strtotime("1970-".$row['max']);
        if($min<=$time && $time<=$max){
            return $row['name'];
        }
    }
    /*other to 摩羯座*/
    return $list[0]['name'];
}


測試程式:
$test=array(
    array('n'=>"水瓶座",'m'=>'01','d'=>'20'),
    array('n'=>"水瓶座",'m'=>'02','d'=>'08'),
    array('n'=>"水瓶座",'m'=>'02','d'=>'18'),
    
    array('n'=>"雙魚座",'m'=>'02','d'=>'19'),
    array('n'=>"雙魚座",'m'=>'03','d'=>'10'),
    array('n'=>"雙魚座",'m'=>'03','d'=>'20'),
    
    array('n'=>"牡羊座",'m'=>'03','d'=>'21'),
    array('n'=>"牡羊座",'m'=>'04','d'=>'09'),
    array('n'=>"牡羊座",'m'=>'04','d'=>'19'),
    
    array('n'=>"金牛座",'m'=>'04','d'=>'20'),
    array('n'=>"金牛座",'m'=>'05','d'=>'10'),
    array('n'=>"金牛座",'m'=>'05','d'=>'20'),
    
    array('n'=>"雙子座",'m'=>'05','d'=>'21'),
    array('n'=>"雙子座",'m'=>'06','d'=>'1'),
    array('n'=>"雙子座",'m'=>'06','d'=>'21'),
    
    array('n'=>"巨蟹座",'m'=>'06','d'=>'22'),
    array('n'=>"巨蟹座",'m'=>'07','d'=>'02'),
    array('n'=>"巨蟹座",'m'=>'07','d'=>'22'),
    
    array('n'=>"獅子座",'m'=>'07','d'=>'23'),
    array('n'=>"獅子座",'m'=>'08','d'=>'02'),
    array('n'=>"獅子座",'m'=>'08','d'=>'22'),
    
    array('n'=>"處女座",'m'=>'08','d'=>'23'),
    array('n'=>"處女座",'m'=>'09','d'=>'02'),
    array('n'=>"處女座",'m'=>'09','d'=>'22'),
    
    array('n'=>"天秤座",'m'=>'09','d'=>'23'),
    array('n'=>"天秤座",'m'=>'10','d'=>'03'),
    array('n'=>"天秤座",'m'=>'10','d'=>'23'),
    
    array('n'=>"天蠍座",'m'=>'10','d'=>'24'),
    array('n'=>"天蠍座",'m'=>'11','d'=>'02'),
    array('n'=>"天蠍座",'m'=>'11','d'=>'22'),
    
    array('n'=>"射手座",'m'=>'11','d'=>'23'),
    array('n'=>"射手座",'m'=>'12','d'=>'02'),
    array('n'=>"射手座",'m'=>'12','d'=>'21'),
    
    array('n'=>"摩羯座",'m'=>'12','d'=>'22'),
    array('n'=>"摩羯座",'m'=>'01','d'=>'09'),
    array('n'=>"摩羯座",'m'=>'01','d'=>'19'),
);

foreach ($test as $v){
    if(getStarSignsName($v['m'],$v['d'])!==$v['n']){
        echo $v['n'],"error<br/>";
    }else{
        echo $v['n'],"OK<br/>";
    }
}