• 2012-11-10

    Compiling levelDB on Windows Cygwin

    Views: 19601 | 1 Comment

    The official levelDB does not support Cygwin platform, trying to compile it on Cygwin may get the error message:”Unknow platform”. Anyway, levelDB works on Windows Cygwin environment, all you have to do is to manually modify some files.

    First, make sure your Cygwin has gcc-4 installed, not gcc-3:

    $ gcc -v
    gcc version 4.5.3 (GCC)
    

    Then, edit build_detect_platform under levelDB source code folder, make it looks like this:

    case "$TARGET_OS" in
        CYGWIN_*)
            PLATFORM=OS_LINUX
            COMMON_FLAGS="$MEMCMP_FLAG -lpthread -DOS_LINUX -DCYGWIN"
            PLATFORM_LDFLAGS="-lpthread"
            PORT_FILE=port/port_posix.cc
            ;;
    

    The “CYGWIN_*” codes is added. Then edit port/port_posix.h, make it looks like this:

    #if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
        defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
        defined(OS_ANDROID) || defined(OS_HPUX) || defined(CYGWIN)
    // Use fread/fwrite/fflush on platforms without _unlocked variants
    #define fread_unlocked fread
    #define fwrite_unlocked fwrite
    #define fflush_unlocked fflush
    #endif
    

    The ” || defined(CYGWIN)” is added, or you may got this error if you try to run make:

    util/env_posix.cc:50: error: `fread_unlocked' undeclared (first use this function)
    

    Now, you can compile levelDB on Windows Cygwin:

    make
    

    If you can find the file libleveldb.a in levelDB source code folder, it’s sucessfull. levelDB is just a library, not a standalone server, so we write a simple CPP code example to test it:

    #include <iostream>
    #include "leveldb/db.h"
    
    int main(){
        leveldb::DB* db;
        leveldb::Options options;
        leveldb::Status status;
    
        options.create_if_missing = true;
    
        status = leveldb::DB::Open(options, "./testdb", &db);
    
        std::string key = "abc";
        std::string value = "123";
        std::string ret;
        std::cout << value;
        status = db->Put(leveldb::WriteOptions(), key, value);
        status = db->Get(leveldb::ReadOptions(), key, &ret);
        std::cout << ret;
    
        return 0;
    }
    

    Compile this code(file named a.cpp) and run with command:

    g++ a.cpp -Iinclude -L. -lleveldb; ./a
    
    Posted by ideawu at 2012-11-10 12:09:50 Tags: ,
  • 2012-09-19

    Always converting PHP’s floating numbers to string with sprintf

    Views: 10998 | No Comments

    I think PHP’s way to deal with float is too BAD!

    PHP code:

    $a = 12345678.123456;
    echo (string) $a; echo "\n";
    echo $a.''; echo "\n";
    echo sprintf('%f', $a); echo "\n";
    

    Outputs:

    12345678.1235
    12345678.1235
    12345678.123456
    
    Posted by ideawu at 2012-09-19 11:42:37
  • 2012-07-29

    Make dynamic programming languages more dynamic

    Views: 8828 | 2 Comments

    The definition of “Dynamic Programming Language” must be redefined!

    Strong typing if not dynamic

    According to the current definition of “Dynamic Programming Language“, a language is called dynamic regardless if it is weak typing or not. But I think strong typing languages such as Python are not dynamic languages.

    Consider what is the result of

    1 + "2" = ?
    

    Let’s see how mainstream programming languages treat this expression:

    Language Result
    Java compile error
    Python runtime exception
    JavaScript “12”(string concat)
    PHP 3(integer addition)

    Most Python learners counldn’t understand why Python throw exception on such expression, why a “Dynamic Language” do so? PHP uses the dot mark “.” to concat strings, perfectly avoid the ambiguity of “+” operator in other language, and automicly convert string literal integer into int as people expected.

    Be tolerant to null

    Most programming languages terminate the execution when encounter statements as follows:

    Object a = null;
    Object v = a.b.c; // ?
    
    Language Result
    Java compile error
    Python runtime error
    JavaScript runtime error
    PHP v = null

    PHP works fine as human expected(with ~E_NOTICE). Why should it terminates at getting null object’s properties? To make the code after keep running, programmers have to do lots of null checks, while all these checks can be eliminated.

    This is how JavaScript does:

    a = null;
    if(a != undefined && a.b != undefined && a.b.c != undefined){
    	v = a.b.c;
    }
    

    Programmers should write codes to take action only if it is in the expected condition, and ignore all unexpected conditions. A dynamic language runtime should not do force programmer to do uneccessary works.

    Codes like

    if(a.b.c == 1){
    	// take action
    }
    

    is always prefered than

    if(a != undefined && a.b != undefined && a.b.c != undefined){
    	if(a.b.c == 1){
    		// take action
    	}
    }
    
    Posted by ideawu at 2012-07-29 15:05:27
  • 2012-07-28

    ideawu’s Paged Thread Comments Plugin

    Views: 10723 | 3 Comments

    ideawu’s Paged Thread Comments, this plugin will not load all comments into memory, it will use MySQL’s LIMIT.

    Download

    Install

    Edit you theme files,

    1. Replace <?php comments_template(); ?> with

    <?php
    if(function_exists('wptc_comments_template')){wptc_comments_template();}
    else{comments_template();}
    ?>
    

    2. Add pagination numbers

    add this code to the comments.php file where you want the pagination numbers to be shown:

    <?php if(function_exists('wptc_comments_pages')): ?>
    	<p class="wptc_comments_pages"><?php wptc_comments_pages(); ?></p>
    <?php endif; ?>
    
    Posted by ideawu at 2012-07-28 00:36:01
  • 2012-07-27

    Simple and clean WordPress theme- ideablog

    Views: 13626 | No Comments

    Features:

    • simple and clean
    • top link – at the top of the whole page
    • bottom link – at the footer
    • paged thread comments – needs wptc plugin

    Help:

    • http://www.ideawu.com/blog/

    License:

    • You may use the theme for free.
    • You may NOT resell this theme
    • The text “Theme by ideawu” and a link back to http://www.ideawu.com/blog/ must be retained in the footer, you may style it look smaller.

    Download

    Posted by ideawu at 2012-07-27 22:41:38 Tags:
|<<<111213141516171819>>>| 15/19 Pages, 91 Results.