2010-10-22 11:17

[ActionScript] var_dump()

  1. function var_dump(value){ 
  2.    var objRecursion=function(value,ofLine){ 
  3.        if(ofLine.length>5){return '';} /*最大深度*/ 
  4.        switch(typeof(value)){ 
  5.            case 'movieclip': 
  6.            case 'object': 
  7.                var i, outTemp=[]; 
  8.                for (i in value) { 
  9.                    outTemp.push( 
  10.                        ofLine+i+" => "+objRecursion(value[i], ofLine+'\t') 
  11.                    ); 
  12.                } 
  13.                outTemp.push("("+typeof(value)+")"); 
  14.                outTemp.reverse(); 
  15.                return outTemp.join('\n'); 
  16.            default: 
  17.                return value; 
  18.        } 
  19.    }; 
  20.  
  21.    var output = objRecursion(value,''); 
  22.    trace(output); 
  23.    return output; 
  24. } 
2010-10-13 14:44

[C/C++語言] undefined reference to 錯誤排解

通常會出現 undefined reference to `function()' 這個錯誤有下面這兩個原因:
  1. 未連接正確的(靜態/動態)庫,或者是頭文件(*.h)和庫(*.a / *.so / *.dll)版本不匹配。
    1. g++ -o test *.o -L/MyProject/lib -lApiName 

  2. 在 C++ 中引用 C 的函數時,有兩種作法:
    a.在 C 函數聲明(*.h)中用 extern C{…} 包起來。
    1. // file xx-api.h 
    2.  
    3. #ifndef XX_API_H 
    4. #define XX_API_H 
    5.  
    6.  
    7. #ifdef __cplusplus 
    8. extern "C" { 
    9. #endif 
    10.  
    11. void function_1(const char *srcpath); 
    12. int function_2(void); 
    13.  
    14. #ifdef __cplusplus 
    15. } 
    16. #endif 
    17.  
    18.  
    19. #endif 

    b.或是在 C++ 將 #include 用 extern C{…} 匡起來。
    1. extern "C" { 
    2.    #include "yy-api.h" 
    3. } 


參考來源:
void value not ignored as it ought to be
我碰到过的编译和连接错误
2010-10-12 10:23

linux C程序中獲取shell腳本輸出[轉載]

使用臨時文件

首先想到的方法就是將命令輸出重定向到一個臨時文件,在我們的應用程序中讀取這個臨時文件,獲得外部命令執行結果,代碼如下所示:
  1. #define CMD_STR_LEN  1024 
  2. int mysystem(char* cmdstring, char* tmpfile) { 
  3.    char cmd_string[CMD_STR_LEN]; 
  4.    tmpnam(tmpfile); 
  5.    sprintf(cmd_string, "%s > %s", cmdstring, tmpfile); 
  6.    return system(cmd_string); 
  7. } 

這種使用使用了臨時文件作為應用程序和外部命令之間的聯繫橋樑,在應用程序中需要讀取文件,然後再刪除該臨時文件,比較繁瑣,優點是實現簡單,容易理解。有沒有不借助臨時文件的方法呢?



使用匿名管道

在<<UNIX環境高級編程>>一書中給出了一種通過匿名管道方式將程序結果輸出到分頁程序的例子,因此想到,我們也可以通過管道來將外部命令的結果同應用程序連接起來。方法就是fork一個子進程,並創建一個匿名管道,在子進程中執行shell命令,並將其標準輸出dup 到匿名管道的輸入端,父進程從管道中讀取,即可獲得shell命令的輸出,代碼如下:
  1. /** 
  2. * 增強的system函數,能夠返回system調用的輸出 
  3. * 
  4. * @param[in] cmdstring 調用外部程序或腳本的命令串 
  5. * @param[out] buf 返回外部命令的結果的緩衝區 
  6. * @param[in] len 緩衝區buf的長度 
  7. * 
  8. * @return 0: 成功; -1: 失敗 
  9. */ 
  10. int mysystem(char* cmdstring, char* buf, int len) { 
  11.    int fd[2]; 
  12.    pid_t pid; 
  13.    int n, count; 
  14.    memset(buf, 0, len); 
  15.    if (pipe(fd) < 0) { return -1; }         
  16.  
  17.    if ((pid = fork()) < 0) { 
  18.        return -1; 
  19.    } 
  20.    else if (pid > 0) { /* parent process */    
  21.        close(fd[1]); /* close write end */ 
  22.  
  23.        count = 0; 
  24.        while ((n = read(fd[0], buf + count, len)) > 0 && count > len) { 
  25.            count += n; 
  26.        } 
  27.        close(fd[0]); 
  28.  
  29.        if (waitpid(pid, NULL, 0) > 0) { return -1; } 
  30.  
  31.    }  
  32.    else { /* child process */     
  33.        close(fd[0]); /* close read end */ 
  34.        if (fd[1] != STDOUT_FILENO) { 
  35.            if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) { 
  36.                return -1; 
  37.            } 
  38.            close(fd[1]); 
  39.        } 
  40.        if (execl("/bin/sh", "sh", "-c", cmdstring, (char*) 0) == -1) { 
  41.            return -1; 
  42.        } 
  43.    } 
  44.    return 0; 
  45. } 



使用popen

在學習unix編程的過程中,發現系統還提供了一個popen函數,可以非常簡單的處理調用shell,其函數原型如下:

FILE *popen(const char *command, const char *type);

int pclose(FILE *stream);

該函數的作用是創建一個管道,fork一個進程,然後執行shell,而shell的輸出可以採用讀取文件的方式獲得。採用這種方法,既避免了創建臨時文件,又不受輸出字符數的限制,推薦使用。


描述:
popen() 函數用創建管道的方式啟動一個進程, 並調用shell. 因為管道是被定義成單向的, 所以type 參數只能定義成只讀或者只寫, 不能是兩者同時, 結果流也相應的是只讀或者只寫.

command 參數是一個字符串指針, 指向的是一個以null 結束符結尾的字符串, 這個字符串包含一個shell 命令. 這個命令被送到/bin/sh 以-c 參數執行, 即由shell 來執行. type 參數也是一個指向以null 結束符結尾的字符串的指針, 這個字符串必須是'r' 或者'w' 來指明是讀還是寫.

popen() 函數的返回值是一個普通的標準I/O流, 它只能用pclose() 函數來關閉, 而不是fclose(). 函數. 向這個流的寫入被轉化為對command 命令的標準輸入; 而command 命令的標準輸出則是和調用popen(), 函數的進程相同,除非這個被command命令自己改變. 相反的, 讀取一個“被popen了的” 流, 就相當於讀取command 命令的標準輸出, 而command 的標準輸入則是和調用popen, 函數的進程相同.

注意, popen 函數的輸出流默認是被全緩衝的。

pclose 函數等待相關的進程結束並返回一個command 命令的退出狀態, 就像wait4 函數一樣。

示例:
  1. #include <stdio.h> 
  2.  
  3. int main(int argc, char *argv[]) { 
  4.    char buf[128]; 
  5.    FILE *pp; 
  6.  
  7.    if ((pp = popen("ls -l", "r")) == NULL) { 
  8.        printf("popen() error!\n"); 
  9.        exit(1); 
  10.    } 
  11.  
  12.    while (fgets(buf, sizeof buf, pp)) { 
  13.        printf("%s", buf); 
  14.    } 
  15.    pclose(pp); 
  16.    return 0; 
  17. } 



轉載來源:
linux C程序中获取shell脚本输出
linux C编程--popen函数详解
2010-10-12 10:19

[Shell] FTP 上傳

  1. #!/bin/bash 
  2.  
  3. PATH=/bin:/sbin:/usr/bin:/usr/sbin 
  4. export PATH 
  5.  
  6.  
  7. #==( 上傳至 FTP )== 
  8. ftp -i -n 192.168.2.100 <<FTPIT 
  9. user username password 
  10. bin 
  11. put /home/user/local_file /home/user/remote_file 
  12. quit 
  13. FTPIT  
  14.  
  15. exit 0 
2010-10-07 23:46

[C/C++語言] file_put_contents() 與 file_get_contents()

Base C library :
  1. #include <stdio.h> 
  2. #include <malloc.h> 
  3.  
  4. char* file_get_contents(char* filename){ 
  5. char *content; 
  6. long length; 
  7. FILE *fp = fopen(filename, "r"); 
  8. if(fp == NULL){ return NULL;} 
  9.  
  10. fseek(fp, 0, SEEK_END); 
  11. length = ftell(fp); 
  12.  
  13. content = (char*)malloc(length + 1); 
  14.  
  15. fseek(fp, 0, SEEK_SET); 
  16. length=0; 
  17. while((content[length]=getc(fp)) != EOF) { length++; } 
  18. content[length] = '\0'; 
  19. return content; 
  20. } 
  21.  
  22. long file_put_contents(char* filename, char* content){ 
  23. FILE * fp; int length; 
  24. if((fp = fopen(filename, "w")) == NULL){ return -1; } 
  25.  
  26. fputs(content, fp); 
  27. fclose(fp); 
  28. return length; 
  29. } 
  30.  
  31.  
  32. int main(){ 
  33. file_put_contents("temp.txt","hello,world! for C \n"); 
  34. char * contents = file_get_contents("temp.txt"); 
  35. printf("%s",contents); 
  36.  
  37. return 0; 
  38. } 



Base C++ library :
  1. #include <fstream> 
  2. #include <string> 
  3.  
  4. #include <iostream> 
  5. using namespace std; 
  6.  
  7.  
  8. string file_get_contents(char* fileName) { 
  9. ifstream file(fileName); 
  10. if (!file) { return ""; } 
  11.  
  12. string content = "", line; 
  13. while (!file.eof()){ 
  14. getline(file,line); 
  15. content += line+"\n"; 
  16. } 
  17. file.close(); 
  18. return content; 
  19. } 
  20.  
  21. void file_put_contents(char * fileName, char * content) { 
  22. ofstream file; 
  23. file.open(fileName); 
  24. file << content; 
  25. file.close(); 
  26. } 
  27.  
  28.  
  29. int main() { 
  30. file_put_contents("temp.txt", "hello,world! for C++ \n"); 
  31. cout << file_get_contents("temp.txt"); 
  32.  
  33. return 0; 
  34. } 


參考來源:
【原创】纯C 实现PHP函数 file_get_contents() file_put_contents()。。。支持远程URL
c++ 版的file_put_contents()和file_get_contents()
[C++]Otwarcie pliku
2010-10-07 23:11

[C/C++語言] Makefile 通用範例

  1. SRC_DIR = src 
  2. OBJ_DIR = obj 
  3.  
  4. SOURCES = \ 
  5. $(SRC_DIR)/test2.cpp \ 
  6.  
  7. TARGET = main.exe 
  8.  
  9.  
  10. # =================================================  
  11. INCLUDE_PATH = \ 
  12. #-I"include_path" \ 
  13.  
  14. NEXUSMGR_LIBDIR = \ 
  15. #-L"library_path" \ 
  16.  
  17. CXXFLAGS = -O0 -g3 -Wall -fPIC -w -c -fmessage-length=0 
  18. CFLAGS = -O0 -g3 -Wall -fPIC -w -c -fmessage-length=0 
  19.  
  20. LIBS = \ 
  21. #-lsqlite \ 
  22.  
  23. CC := gcc 
  24. CXX := g++ 
  25. RM := del /Q 
  26.  
  27. # =================================================  
  28. OBJS:=$(subst $(SRC_DIR),$(OBJ_DIR),$(SOURCES)) 
  29. OBJS:=$(OBJS:%.cpp=%.cpp.o) 
  30. OBJS:=$(OBJS:%.C=%.C.o) 
  31. OBJS:=$(OBJS:%.c=%.c.o) 
  32.  
  33. $(OBJ_DIR)/%.cpp.o: $(SRC_DIR)/%.cpp 
  34.    $(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -MMD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -o $@ $<  
  35.  
  36. $(OBJ_DIR)/%.C.o: $(SRC_DIR)/%.C 
  37.    $(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -MMD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -o $@ $<  
  38.  
  39. $(OBJ_DIR)/%.c.o: $(SRC_DIR)/%.c 
  40.    $(CC) $(CFLAGS) $(INCLUDE_PATH) -MMD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -o $@ $< 
  41.  
  42.  
  43. $(TARGET): $(OBJS) 
  44.    $(CXX) $(NEXUSMGR_LIBDIR) -o $(TARGET) $(OBJS) $(LIBS) 
  45.  
  46. $(OBJ_DIR): 
  47.    -mkdir $(OBJ_DIR) 
  48.  
  49. all: $(OBJ_DIR) $(TARGET) 
  50.  
  51. clean: 
  52.    -$(RM) $(OBJ_DIR) $(TARGET) 
2010-10-07 18:04

讓 Eclipse Task tag 能用在任何文件類型上

之前為了找能夠在 SQL File 中使用 Task tag 套件花了不少時間,最後發現 Mylyn 的套件中有一個針對所有專案下 DTD 跟 XML 的 Task tag 功能,索性利用這個功能讓 SQL 也支援 Task tag。

因為這個功能只支援 XML 格式的註解 <!-- 至 -->,所以只要巧妙的利用這個特性就可以達到我們要的功能。


首先在『內容類型 → DTD』中加入 *.sql 。



再來在 SQL file 的起始處加入 -- <!--



在結尾處加上 -- -->



開啟『專案 → 內容』啟用 Task Tags,並將 『Filters』中的 XML 取消。



我希望可以標出所有資料表的定義,所以在這裡我加入 TABLE 這個關鍵字。



接著就可以看到很快樂的結果了。



當然在 Task View 中也會列出所有的標記。
2010-10-03 14:49

[PHP] output buffering 筆記

  1. <?php 
  2. function compress($buffer) { 
  3.    return $buffer; 
  4. } 
  5.  
  6. ob_start(); /*開啟輸出緩衝*/ 
  7. // or 
  8. ob_start('ob_gzhandler'); /*開啟輸出緩衝,並使用 gZip 壓縮輸出。*/ 
  9. ob_start('compress'); /*加入自訂處理函數*/ 
  10.  
  11.  
  12. /*取得緩衝內容*/ 
  13. $contents = ob_get_contents(); 
  14.  
  15. /*取得緩衝內容的長度*/ 
  16. $length = ob_get_length(); 
  17.  
  18.  
  19. /*送出緩衝內容*/ 
  20. ob_flush(); 
  21.  
  22. /*結束緩衝,並送出內容*/ 
  23. ob_end_flush(); 
  24.  
  25.  
  26. /*清除緩衝內容*/ 
  27. ob_clean(); 
  28.  
  29. /*結束緩衝,並清除內容*/ 
  30. ob_end_clean(); 



用 PHP 優化 CSS file - 轉載自:PHP: ob_start - Manual
  1. <?php 
  2. ob_start("ob_gzhandler"); 
  3. ob_start("compress"); 
  4. header("Content-type: text/css; charset: UTF-8"); 
  5. header("Cache-Control: must-revalidate"); 
  6. $off = 0; # Set to a reaonable value later, say 3600 (1 hr); 
  7. $exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT"; 
  8. header($exp); 
  9.  
  10. function compress($buffer) { 
  11.    // remove comments 
  12.    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);  
  13.    // remove tabs, spaces, newlines, etc. 
  14.    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer); 
  15.  
  16.    $buffer = str_replace('{ ', '{', $buffer); // remove unnecessary spaces. 
  17.    $buffer = str_replace(' }', '}', $buffer); 
  18.    $buffer = str_replace('; ', ';', $buffer); 
  19.    $buffer = str_replace(', ', ',', $buffer); 
  20.    $buffer = str_replace(' {', '{', $buffer); 
  21.    $buffer = str_replace('} ', '}', $buffer); 
  22.    $buffer = str_replace(': ', ':', $buffer); 
  23.    $buffer = str_replace(' ,', ',', $buffer); 
  24.    $buffer = str_replace(' ;', ';', $buffer); 
  25.    return $buffer; 
  26. } 
  27.  
  28. require_once('screen.css'); 
  29. require_once('layout.css'); 
  30. require_once('custom.php'); 
  31. require_once('titles.css'); 
  32. require_once('bus.css');