銀波貝特美布丁

最簡單的 linked list 範例!

LL 這個概念可以讓程式在處理資料的時候即時的要求記憶體,與之相對的就是 array ,array 在程式執行時會一口氣要求宣告的記憶體數量,沒用到的就浪費了,雖然這個缺點可以利用 realloc 來彌補。BUT!realloc 也必須想辦法知道下一批讀進來的資料量多大才能使用。

?Download ll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct list{
    int no;
    char cont[50];
    struct list* next;
};
typedef struct list node;
 
int main(){
    node* head = NULL;
    node* current = NULL;
    node* prev = NULL;
    int no;
    char cont[50];
 
    while( 1 ){
        printf("no :");
        scanf("%d", &no);
        if( no == 0 )break;
        current = (node*)malloc( sizeof(node) );
        if( current == NULL){
            printf("Error\n");
            return 1;
        }
        current->no = no;
        printf("cont :");
        scanf("%s", cont);
        strncpy(current->cont,cont,49);
 
        current->next = NULL;
        if(head == NULL){
            head = current;
            prev = current;
        }
        else{
            prev->next = current;
        }
        prev = current;
    }
    //For Display
    current = head;
    while( 1 ){
        printf("%d,%s\n", current->no, current->cont);
        if( current->next == NULL)break;
        current = current->next;
    }
    return 0;
}

編譯方法:

gcc -Wall ll.c

linked list 的概念就是把 struct 的東西在每一次需要的時候 malloc 出來,然後利用 struct 裡面的指標指向下一個(當然這邊需要一點判斷,判斷是否為 LL 的頭等等)

如此一來,在使用資料的時候就可以利用指向下一個 struct 的指標來移動目前正在使用的 struct!

本程式執行範例如下:

no :1
cont :abc
no :2
cont :def
no :3
cont :hij
no :4
cont :klm
no :0
1,abc
2,def
3,hij
4,klm

參考資料:
http://blog.xuite.net/coke750101/coketech/20739300

Tags: ,

超陽春,FTP相簿系統,免SQL(2)

之前有發過這麼一篇文章 超陽春,FTP相簿系統,免SQL

今天要加上點擊下載的功能,為了要有這個功能,我加了一個叫做 dl.php 的檔案負責下載的工作,我們會在 index.php 把要下載的檔案路徑以及要儲存的檔名當作參數,利用 GET 方法傳遞給 dl.php

?Download index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>photo</title>
</head>
<body>
<?php
$path = $_GET['path'];
if( $path == NULL){
	$path = ".";
}
if( ereg("\.\.", $path) ){
	$path= ".";
}
echo "path is $path (點擊檔名下載,或者使用<a href=\"https://addons.mozilla.org/zh-TW/firefox/addon/3006#\" target=_blank>下載軟體</a>下載所有媒體)<br>\n";
if( @!chdir("$path") ){
	echo "error when chdir to $path<br>\n";
	$path= ".";
}
exec("ls", $file);
$end = strrpos($path, "/");
$upper_path = substr($path, 0, $end);
echo "<a href=\"index.php?path=".urlencode("$upper_path")."\">上一層</a><br>\n";
echo "<center>\n";
echo "<table border=0>\n";
$i = 1;
$j = 0;
foreach( $file as $per_file ){
	$string = filetype("$per_file");
	if( $string == "file" && exif_imagetype("$per_file") ){
		if( !ereg("thumb", $per_file) ){
			if( !file_exists("$per_file"."thumb") ){
				$size = GetImageSize("$per_file");
				if($size[0] > $size[1]){
					$height = $size[1]/($size[0]/200);
					$width = 200;
				}
				else{
					$width = $size[0]/($size[1]/200);
					$height = 200;
				}
				$im0 = imageCreateFromJPEG("$per_file");
				$im1 = imagecreatetruecolor($width, $height);
				imagecopyresized($im1, $im0 ,0 ,0 ,0, 0, $width, $height, $size[0], $size[1]);
				imagejpeg($im1, "$per_file"."thumb");
			}
			echo "<td align=center valign=middle height=180 width=180>";
			$j = $i-1;
			echo "<a href=\"fullscreen.php?path=".urlencode("$path")."&i=$j\"><img src=\"$path/{$per_file}thumb\"></a>";
			echo "<br />";
			echo "<a href=\"dl.php?dlph=$path/$per_file&saveasname=$per_file\">$per_file</a>";
			echo "</td>\n";
			if( ($i%5) == 0 ){
				echo "<tr>\n";
			}
			$i++;
		}
	}
	elseif($string == "dir"){
		echo "<a href=\"index.php?path=".urlencode("$path/$per_file")."\">$per_file</a> \n";
	}
}
echo "</table>\n";
echo "</center>\n";
?>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<title>slide</title>
</head>
<body>
<?php
$path = $_GET['path'];
$i = $_GET['i'];
$total = 0;
if( @!chdir("$path") ){
	echo "error when chdir to $path<br>\n";
	$path= ".";
}
exec("ls", $file);
foreach( $file as $per_file ){
	if( !ereg("thumb", $per_file) ){
		$list_file[] = $per_file;
		$total++;
	}
}
$j = $i+1;
if($j == $total){
	echo "<a href=\"index.php?path=".urlencode("$path")."\">上一層</a><br>\n";
	echo "<center>\n";
    echo "<a href=\"dl.php?dlph=".urlencode("$path/$list_file[$i]")."&saveasname=$list_file[$i]\">下載</a><br />";
	echo "<a href=\"index.php?path=".urlencode("$path")."\"><img src=\"$path/$list_file[$i]\"></a>\n";
	echo "</center>\n";
}
else{
	echo "<a href=\"index.php?path=".urlencode("$path")."\">上一層</a><br>\n";
	echo "<center>\n";
    echo "<a href=\"dl.php?dlph=".urlencode("$path/$list_file[$i]")."&saveasname=$list_file[$i]\">下載</a><br />";
	echo "<a href=\"fullscreen.php?path=".urlencode("$path")."&i=$j\"><img src=\"$path/$list_file[$i]\"></a>\n";
	echo "</center>\n";
}
?>
</body>
</html>
?Download dl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$dlph = $_GET['dlph'];
$saveasname = $_GET['saveasname']; //要被儲存成的檔名
$len = filesize($dlph);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; Filename="'.$saveasname.'"');
header("Content-Length: ".$len);
@readfile($dlph);
exit;
?>

參考資料:header

Tags:

Vim 加上 actionscript 的語法顏色標示

先到 這邊 下載 actionscript.vim ,這是 actionscript 的語法設定檔

將下載下來的檔案複製到 /usr/share/vim/syntax 底下

修改 filetype.vim 檔案 ,我的路徑是在/usr/share/vim/vim71/filetype.vim,這個路徑可能因為系統而不一樣
原文:

1
2
" Atlas
au BufNewFile,BufRead *.atl,*.as         setf atlas

修改後:

1
2
3
4
5
" ActionScript
au BufNewFile,BufRead *.as          setf actionscript
 
" Atlas
au BufNewFile,BufRead *.atl         setf atlas

如果你沒有 root 權限來修改 filetype.vim 的話

也可以在你家目錄的 .vimrc 加入下面這一行

1
autocmd BufRead *.as set filetype=actionscript

這樣 vim 便會在啟動的時候讀入設定,正確顯示出 as 的語法顏色

Tags: , , ,