view mcabber/src/utf8.c @ 374:bd5638c21834

Improve logging system (traces) There are now two trace logging levels: * tracelog_level = 1: Most messages from the log window are written to disk (LPRINT_LOG) * tracelog_level =2: LPRINT_LOG & LPRINT_DEBUG messages are written to disk The trace file name is set with the "tracelog_file" option.
author Mikael Berthe <mikael@lilotux.net>
date Mon, 25 Jul 2005 21:46:35 +0100
parents 33b8e801ffa6
children
line wrap: on
line source

#include <stdlib.h>
#include <string.h>

#include "utf8.h"


/* Desc: convert UTF8 -> ASCII
 * 
 * In  : UTF8 string
 * Out : ASCII string
 *
 * Note: it is up to the caller to free the returned string
 */
char *utf8_decode(const char *src)
{
  unsigned char *ret, *aux;
  
  if (!src) return NULL;

  aux = ret = calloc(1, strlen(src) + 1);

  while (*src) {
    unsigned char lead = *src++;
    if ((lead & 0xe0) == 0xc0) {
      unsigned char ch2 = *src++;
      *aux = ((lead & 0x1f) << 6) | (ch2 & 0x3f);
    } else {
      *aux = lead;
    }
    aux++;
  }

  return (char*)ret;
}


/* Desc: convert ASCII -> UTF8
 * 
 * In  : ASCII string
 * Out : UTF8 string
 *
 * Note: it is up to the caller to free the returned string
 */
char *utf8_encode(const char *src)
{
  unsigned char *ret, *aux;
  
  if (!src) return NULL;

  aux = ret = calloc(1, (strlen(src) * 2) + 1);

  while (*src) {
    unsigned char ch = *src++;
    if (ch < 0x80U) {
      *aux++ = ch;
    } else {  /* if (ch < 0x800U) { */
      *aux++ = 0xc0 | (ch >> 6);
      *aux++ = 0x80 | (ch & 0x3f);
    }
  }

  return (char*)ret;
}