GithubHelp home page GithubHelp logo

exiconv's Introduction

ExIconv

中文文档

It's an encoding auto-detection library in C

For cross-platform and C++ version, please using the estring which support Conan package manager:

https://github.com/sunxfancy/estring

It combines libiconv and libcharsetdetect to auto-detect encoding and directly transform to UTF-32.

Example:

#include "stdio.h"
#include "exiconv.h"
#define BUFFER_SIZE 4096

int main(int argc, const char * argv[]) {
    FILE* f = fopen(argv[1], "r");
    size_t outsize;
    echar_t* str = autoreadfile(f, &outsize);
    printf("echar_len = %d\n", estrlen(str));
    char* utf8_str = conv2utf8(str, &outsize);
    printf("utf8:%s\n", utf8_str);
    fclose(f);
    return 0;
}

Functions:

typedef uint32_t echar_t;
struct FILE;


/**
 * @brief translate UTF-32 string to utf8 for output
 * 
 * @param data UTF-32 array
 * @param outsize length of the string after transform
 * 
 * @return It will auto malloc space for the new string, please free it after use
 */
extern char* 
conv2utf8 (const echar_t* data, size_t* outsize);


/**
 * @brief translate utf8 to UTF-32 array
 * 
 * @param data utf8 string array 
 * @param outsize length of the string after transform
 * 
 * @return It will auto malloc space for the new string, please free it after use
 */
extern echar_t*
utf8conv2echar (const char* data, size_t* outsize);




/**
 * @brief translate UTF-32 string to a special encode
 * 
 * @param data UTF-32 array
 * @param outsize length of the string after transform
 * 
 * @return It will auto malloc space for the new string, please free it after use
 */
extern char* 
echar2code (const echar_t* data, size_t* outsize, const char* encode);


/**
 * @brief translate a special encode to UTF-32 string 
 * 
 * @param data string array
 * @param outsize length of the string after transform
 * 
 * @return It will auto malloc space for the new string, please free it after use
 */
extern echar_t*
code2echar (const char* data, size_t* outsize, const char* encode);




/**
 * @brief Read the file and auto-detect encoding, then translate into UTF-32 array
 * 
 * @param f FILE pointer (binary read mode)
 * @param outsize length of the string after transform
 * 
 * @return It will auto malloc space for the new string, please free it after use
 */
extern echar_t*
autoreadfile (FILE* f, size_t* outsize);


/**
 * @brief auto-detect encoding, then translate into UTF-32 array
 * 
 * @param data string data
 * @param outsize length of the string after transform
 * 
 * @return It will auto malloc space for the new string, please free it after use
 */
extern echar_t*
autoreadchar (const char* data, size_t* outsize);


/**
 * @brief detect encoding
 * 
 * @param data string data end with '\0'
 * @return name of the encoding, NULL if it's failure
 */
extern const char*
encodedetect (const char* data);

/**
 * @brief detect encoding for the file
 * 
 * @param data FILE pointer (binary read mode)
 * @return name of the encoding, NULL if it's failure
 */
extern const char*
fileencodedetect (FILE* f);


/**
 * @brief get the length of string
 * 
 * @param str string data end with '\0'
 * @return length
 */
extern size_t
estrlen (const echar_t* str);


/**
 * Free the data
 */
#define FreeStr(p) free_str((void**)&p)

extern void
free_str(void** p);

exiconv's People

Contributors

sunxfancy avatar xssss1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.