cht電腦資訊LanguageC/C++
adm Find login register

測試 charset-detector:自動偵測文件編碼的小程式

qrtt1
1 測試 charset-detector:自動偵測文件編碼的小程式
Promote 0 Bookmark 12008-02-27quote  

      

 

 http://blog.linux.org.tw/~jserv/archives/001672.html

 

 

#include<stdio.h>
#include<stdlib.h>
#include<dlfcn.h>
#include<string.h>
#include"chardetect.h"

void checkFunc(){
   char* error;
   if((error = dlerror())!= NULL){
      fputs(error, stderr);
      exit(1);
   }
}

int main(void) {
    void *handle;
   
    int (*chardet_create)(chardet_t* pdet);
    void (*chardet_destroy)(chardet_t det);
    int (*chardet_reset)(chardet_t det);
    int (*chardet_get_charset)(chardet_t det, char* namebuf, unsigned int buflen);
    int (*chardet_handle_data)(chardet_t det, const char* data, unsigned int len);
    int (*chardet_data_end)(chardet_t det);
   
    handle = dlopen("/home/qrtt1/test/char/chardetect.so", RTLD_LAZY);
   
    chardet_reset = (int(*)(chardet_t)) dlsym(handle, "chardet_reset");
    checkFunc();
   
    chardet_create = (int(*)(chardet_t*)) dlsym(handle, "chardet_create");
    checkFunc();

    chardet_destroy = (int(*)(chardet_t)) dlsym(handle, "chardet_destroy");
    checkFunc();

    chardet_get_charset = (int(*)(chardet_t, char* namebuf, unsigned int buflen)) dlsym(handle, "chardet_get_charset");
    checkFunc();
   
    chardet_handle_data = (int(*)(chardet_t, const char* data, unsigned int len)) dlsym(handle, "chardet_handle_data");
    checkFunc();   

    chardet_data_end = (int(*)(chardet_t)) dlsym(handle, "chardet_data_end");
    checkFunc();
   
    /*
     *
     * */
   
    chardet_t chardetor;
    int ret = (*chardet_create)(&chardetor);
    (*chardet_reset)(chardetor);
   
    const char* message = "傳說中的中文";
    (*chardet_handle_data)(chardetor, message, strlen(message));
    (*chardet_data_end)(chardetor);
   
    char buffer[128];
    (*chardet_get_charset)(chardetor, buffer, 128);
   
    (*chardet_destroy)(chardetor);
    printf("%s\n", buffer);

    dlclose(handle);
}

cht電腦資訊LanguageC/C++
adm Find login register
views:5045