2012年11月7日 星期三

草稿,動態連結編譯法

前陣子嘗試使用 gcc來編譯c語言撰寫的shared  library,下面是摸索過程中有所幫助的網路先進們的資源.

等周末有空的時候,我再來把之前的經驗整理成快快樂樂懶人包.

http://www.jollen.org/blog/2007/02/dynamic_loader_1_dlopen.html
http://linux.die.net/man/3/dlopen
http://web.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html
http://stackoverflow.com/questions/756174/how-to-export-symbols-from-a-shared-library
http://www.linuxjournal.com/article/3687
http://docstore.mik.ua/orelly/linux/run/ch13_01.htm

然後也找到了,下一階段想要玩得 perl + c library的教學
http://gdwang.blogspot.tw/

至於 python的遊戲,也找到了電子書裡的教學,只是人生苦短啊,擠得出時間來玩這個嗎?

2012年6月6日 星期三

003 sort ip [perl]



#!/usr/bin/perl -w

use strict;

my @argv = @ARGV;
my @ips = ();

if (0==scalar((@argv))) { &print_Usage(); exit(0); }
&get_Parameter(\@argv,\@ips);

print("Before:\n");
foreach(@ips) { print("\t$_\n"); }

my @ips_sorted = sort sort_ip @ips;

print("After:\n");
foreach(@ips_sorted) { print("\t$_\n"); }


sub sort_ip()
{
    my ($a1,$a2,$a3,$a4) = split(/\./,$a);
    my ($b1,$b2,$b3,$b4) = split(/\./,$b);
    $a1 <=> $b1 or $a2 <=> $b2 or $a3 <=> $b3 or $a4 <=> $b4;
}


sub get_Parameter($$)
{
    my ($pargv,$pips) = @_;
    foreach(@$pargv)
    {
if (my(@ip)= $_ =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
{
   my $bError = 0;
   if (0==$ip[0] or 0==$ip[3]) { $bError = 1; }
   else { foreach(@ip) { if ($_ > 255) { $bError = 1; last; } } }
   if (not $bError) { push(@$pips,join(".",@ip)); }
}
    }
}

sub print_Usage()
{
    my $strSelf = $0; $strSelf =~ s/.+[\\\/]//;
    print("usage: $strSelf ip1 ip2 ip3\n");
}

002 get parameter [perl]



#!/usr/bin/perl -w

use strict;

my @argv = @ARGV;

if (0==scalar((@argv))) { &print_Usage(); exit(0); }
&get_Parameter();

sub get_Parameter()
{
    while(@argv)
    {
        my $strWord = shift(@argv);
        my ($name,$value) = (undef,undef);
        if ( ($name,$value) = $strWord =~ /^\-(\w)\=(\w+)$/) { }
        elsif ( ($name,$value) = $strWord =~ /^\-\-(\w{2,})\=(\w+)$/) { }
        elsif ($strWord =~ /^\-\-(\w{2,})$/)
        {
            if (scalar(@argv)==0) { print("X: insufficient parameter for $strWord\n"); last; }
            ($name,$value) = ($1,shift(@argv));
        }
        elsif ($strWord =~ /^\-(\w)$/)
        {
            if (scalar(@argv)==0) { print("X: insufficient parameter for $strWord\n"); last; }
            ($name,$value) = ($1,shift(@argv));
        }
        else
        {
            print("X: unrecognized parameter: $strWord\n"); next;
        }
        &take_Action($name,$value);
    }
}

sub take_Action($$)
{
    my ($operator,$operant) = @_;
    if    ($operator eq "h" or $operator eq "hi")  { print("Hello $operant\n"); }
    elsif ($operator eq "b" or $operator eq "bye") { print("Goodbye $operant\n"); }
    else  { print("X: undefined parameter: $operator\n"); }
}

sub print_Usage()
{
    my $strSelf = $0; $strSelf =~ s/.+[\\\/]//;
    print("usage: $strSelf\n".
          "       --hi Name\n".
          "         -h Name\n".
          "      --bye Name\n".
          "         -b Name\n".
          "       --hi=Name\n".
          "         -b=Name\n");
}

2012年5月28日 星期一

001 Hello world


















=====
C: main.c
=====

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

int main(int argc, char** argv)
{
    printf("Hello world!\n");

    printf("Press Any Key to Exit: ");
    char cTmp = 0;
    cTmp = getchar();
    /*
    int getchar ( void ); */
    //The character read is returned as an int value.

    return 0;
}

=====
PERL: hello_world.pl
=====

#!/usr/bin/perl -w

use strict;

main();

sub main()
{
    printf("Hello world!\n");
 
    print 'Press Enter to Exit: ';  
    my $strTmp = <stdin>;
    #<stdin> stands for standard input. It can be abbreviated by using simple &lt;&gt;.
 
    return 0;
}

=====
PYTHON: hello_world.py
=====

#!/usr/bin/python

def main():
    print "Hello world!"
    print 'Press Enter to Exit: ',
    # "" equals to '' in python
    # use "," in the end to refuse new-line
    strTmp = raw_input()

main()

=====
JAVA: hello_world.java
=====

import java.io.*;

public class hello_world
{
    public static void main(String[] args) throws IOException
    {
        System.out.println("Hello world!");
     
        System.out.printf("Press Enter to Exit: ");
        // use BufferedReader to get input from STDIN
        BufferedReader bufTmp = new BufferedReader(new InputStreamReader(System.in));
        String strTmp;
        strTmp = bufTmp.readLine();
    }
}