[C CODE] BIG5 (ascii) to UTF-8 轉碼 sample code – use iconv , libiconv

♥ ♥ ♥ 如果這頁訊息對您有幫助 請幫我點上方廣告↑ ↑ ↑ 感謝您的幫助 ♥ ♥ ♥

#include "iconv.h"
#define MAX_STRING_LEN 10*1024

char tmp[MAX_STRING_LEN];
char p2[MAX_STRING_LEN];
iconv_t cd;
int v1 , v2;
char *sin , *sout;

cd = iconv_open("utf-8","big5");
if (cd==0) return -1;

// ----> LOOP
sin=p2;
sout=tmp;
v1=strlen(p2)+1;
v2=MAX_STRING_LEN;
if ( !iconv(cd,&sin,&v1,&sout,&v2) ) strcpy(dat[26],tmp);
else strcpy(dat[26],"Error");

// ----> LOOP END

iconv_close(cd);

這邊看到一句話:

首先 iconv_open() 函式就是做 “開啟” 動作,也就是當我們要將編碼系統 A 轉換到編碼系統 B 時,必須先呼叫此函式,將 FROMENC 設成編碼系統 A 的名字,同時將 TOENC 設成編碼系統 B 的名字,這時此函式就會做類似檔案開啟的動作,傳回一個代表此轉換管道的資料結構 iconv_t 供後續使用。事實上,在系統的實作中真的是將 iconv_open() 當作 “開啟檔案” 來處理,故它會受到目前系統或同一行程中可開啟檔案數所限,如果系統或程式的其他部分已開啟了太多的檔案以至於逼近系統上限,則有可能這邊的 iconv_open() 會失敗。

我想到一個問題 , 不曉得 PHP 裡面的 iconv 函式是否有同樣問題, 好像用 mbstring 安全一些!!

看來寫這種 convert encoding 的程式 真的要很小心 , 避免把 iconv_open 寫進 loop 裡面 , 免得他造成吃記憶體怪物!!

Sample code , 用 getopt 取得 command line 的參數

5c30343361478502ed9711dd38115baf

#include "stdlib.h"
#include "stdio.h"
#include "getopt.h"
#include "string.h"

void print_help(int exval);
char program[100] = "sample";

int main(int argc, char *argv[]) {
  int opt;
  strcpy(program,argv[0]);
  if (argc == 1) print_help(1);
  while((opt = getopt(argc, argv, "hVvf:o:")) != -1) {
    switch(opt) {
      case 'h':
        print_help(0);
        break;
      case 'V':
        printf("%s", program);
        exit(0);
        break;
      case 'v':
        printf("%s: Verbose option is set '%c'\n", program, optopt);
        break;
      case 'f':
        printf("%s: Filename %s\n", program, optarg);
        break;
      case 'o':
        printf("Output: %s\n", optarg);
        break;
      case ':':
        fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", program, optopt);
        print_help(1);
        break;
      case '?':
        fprintf(stderr, "%s: Error - No such option: '%c'\n\n", program, optopt);
        print_help(1);
    }
  }
  
  for(; optind < argc; optind++) printf("argument: %s\n", argv[optind]);
  return 0;
}

// -----
void print_help(int exval) {
  printf("Usage: %s [-h] [-V] [-f FILE] [-o FILE]\n\n", program);
  printf(" -h print this help and exit\n");
  printf(" -V print version and exit\n\n");
  printf(" -v set verbose flag\n");
  printf(" -f FILE set intput file\n");
  printf(" -o FILE set output file\n\n");
  exit(exval);
}

我的 subversion / svn server 設定

OS : Gentoo Linux 2.6.22-gentoo-r5 #11

gcc –version
gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.1)

svnserve –version
svnserve, version 1.5.4 (r33841)
compiled Dec 17 2008, 08:11:10

/usr/bin/svnserve –foreground –daemon –root=/var/svn

 

b3427f82eb24f02a7a8c162d19f344c6

74fe80d6a738a7f3beb1c6da3e33b80f

 

一些 subversion command 下的操作:

用 svn co 一個 source 到目錄下 , 把要加到 svn  的檔案/目錄 copy 過來 , 下 svn status 會看到一堆 ? 表示那些是新的檔案

e7cd532c5adf186cfae84be0973cfcf6

下 svn add * 或 svn add dirname

84befb5056c4efb725a40ba8ef42577f

下 svn status 可以看到 剛剛的 ? 變成 A 了 (add)

eaf23dbfa9a596768ee8e351bb792062

下 svn ci 把 source code commit 進 svn

5ffc7c116629e31e25b3b274d4f9ee4a

cd 到工作目錄

svn –username monster export svn://xxx.com/my_repos/search/src

把 source code export 出來

如果程式語言是艘船, 那麼 …. 是….

http://compsci.ca/blog/if-a-programming-language-was-a-boat/

…..

If a programming language was a boat…

This one is inspired by a recent forum post, that still leaves me in amazement.

Hi, Im wondering how i can create a boat in turing and if someone can post a example.

This makes no sense, since one doesn’t normally make water vehicles in Turing, the programming language. Though this got me thinking — if a programming language was a boat, what would it be?

Turing

Turing would definitely be a kayak (thank you for comments). It’s small. It’s human powered. It’s often used as a beginner “boat”. And it’s also very Canadian.

Turing programming language as a canoe
Original photo by naokomc

Java

Java is a cargo ship. It’s very bulky. It’s very enterprise~y. Though it can also carry a lot of weight. Will carry a project, but not very fun to drive.

Java programming language as a cargo ship
Original photo by cfarivar

Perl

Perl is a tugboat. Powerful enough to tug Java around, in 80 characters or less.

Perl programming language as a tugboat
Original photo by xeeliz

Ruby

Ruby is difficult to describe. It’s sleek, sexy, and very fun to drive. Here’s a picture. Very trendy.

Ruby programming language as a speed boat
Original photo by Tony Falcon

PHP

PHP is a bamboo raft. A series of hacks held together by string. Still keeps afloat though.

PHP programming language as a raft
Original photo by permanently scatterbrained

C

C is a nuclear submarine. The instructions are probably in a foreign language, but all of the hardware itself is optimized for performance.

C programming language as a nuclear submarine
Original photo by Ryan C. McGinley

HTML

HTML isn’t really a programming language boat.

HTML is not a boat
Original photo by ascendeddaniel

There’s a lot more to this, and it’s all up for discussion. How would your favourite programming language fare in open waters?

Virtual appliance

看到 http://virtualappliances.net/ 這裡面立即提供了 5 總 ready to go 的 appliance  , 只要 “抓下來 / 啟動 就立即可用” —> 這就是 virtual appliance 的精神 , 看了這頁頓時覺得 這些 appliance 真的對 developer 非常 friendy , 看看這頁
http://virtualappliances.net/documentation/readme/README.lamp.html

裝好立即有

  • Apache HTTP Server
  • PHP
  • Perl
  • Python
  • ZendOptimizer (www.zend.com)
  • MySQL
  • phpMyAdmin

然後要開發就只要到它開放的 samba  目錄下寫程式即可 , 這點跟我包裝的 Monster LAMP Pack 精神是相同的.

opensolaris / Optimized Open Source Software Stack (Cool Stack)

看到一個有用的套件

for the Sun Solaris Operating System(TM)

http://cooltools.sunsource.net/coolstack/

Cool Stack includes several packages in the SVR4 package format, so you can install just the ones you need. Some of the applications in Cool Stack already ship with Solaris, but these are either older versions and/or not built with full optimization. Further, Cool Stack has been pre-configured to have the most popular applications (Apache, PHP, MySQL) to work seamlessly out of the box.
Deploying PHP From Cool Stack in Sun Java System Web Server

Configuring Cool Stack PHP With Web Server

Next, do the following:

1. Go to the Cool Stack PHP 5 installation location. Type:

cd /opt/coolstack/php5

In that directory is a script called setup-ws7-php.sh.

2. Run setup-ws7-php.sh. Type:

./setup-ws7-php.sh

This message is displayed:

Usage:
This script will configure Coolstack PHP with Sun Java System Web Server
7. Here, you will need to provide the top level location of your Web Server
7 installation and your Web Server 7 instance_name name to which this
script should configure to run PHP scripts.

Enter your Web Server installation location(/sun/webserver7):

3. Type the full path for your Web Server installation.

The script then prompts you to type a Web Server instance name. That name is the path to a directory in your installation location—one that contains all the configuration files for running your Web site. If you are using Web Server in Sun Java Enterprise System 5, your instances are under /var/opt/SUNWwbsvr7.

For the example in this article, cite the instance https-coolstack that you created previously. To enable a different instance for PHP, type that instance name. Your instance is then ready for PHP, which you can deploy with Cool Stack 1.2 PHP on Web Server.

Finally, do the following:

1. Start Web Server. Type:

/sun/webserver7/https-coolstack/bin/startserv

2. Create sample PHP files under /sun/webserver7/https-coolstack/docs.