libtconv-0.1 manual
tconv::convert
unsigned int tconv::convert(char * const outP, const char * const inP, const unsigned int max=0)
declared in tconv.h, include tconv.h

outP output buffer handle,
inP source text buffer handle,
max count of characters to convert, optional, defaults to 0.

This is a main tool to be used. It quicly converts a block of text from one character set to another. Function returns tconv::error when it fails or number of bytes (not characters) converted, in case of success.

When max is not provided (eq. 0), function assumes the input source is NULL-terminated and converts every character, until the final NULL-byte. When max is provided as positive integer, function attempts to convert exactly that number of characters but also stops when it finds the NULL-byte along its way.

The output location needs to be wide enough to store entire set of converted characters.

NOTE: It is valid to pass the same location as input and output, as long as it is big enough to store the output string.

Example
// C++ source

#include <iostream>
#include <fstream>
#include <cstring>
#include "tconv.h"

using namespace std;

int main(){
  const char fName[]={"test_utf8.txt"};
  ifstream fs;
  char cBuff[256];
  tconv tc("pl_PL.utf8", "pl_PL.iso_8859-2");

  fs.open(fName);
  if(! fs.good()){
    cout << "Can not open " << fName << endl;
    return 1; // whoops
  }

  memset(cBuff, 0, 256);
  fs.read(cBuff, 256); // we read 1st 256 bytes
  fs.close();

  if(tc.convert(cBuff,cBuff) == tconv::error)
    cout << "Conversion failed..." << endl;
  else {
    // Note that cBuff will show up as garbage,
    // if converted into encoding not supported
    // by stdout.
    cout << "Conversion successful..." << endl
      << cBuff << endl;
  }

  return 0;
}
Copyright 2015 TinyIT All rights reserved
Last modified 2015-06-04