source: FTPfs/curlftpfs-0.9.1/charset_utils.c @ 10

Last change on this file since 10 was 10, checked in by zsjheng, 16 years ago
File size: 1.1 KB
Line 
1/*
2    FTP file system
3    Copyright (C) 2007 Robson Braga Araujo <robsonbraga@gmail.com>
4
5    This program can be distributed under the terms of the GNU GPL.
6    See the file COPYING.
7*/
8
9#include "config.h"
10
11#include <string.h>
12#include <limits.h>
13#include <iconv.h>
14#include <errno.h>
15#include <stdlib.h>
16
17#include "ftpfs.h"
18
19int convert_charsets(const char* from, const char* to, char** str) {
20  iconv_t cd;
21  char* s = *str;
22
23  if (!s || !*s)
24    return 0;
25
26  if (to && from && (cd = iconv_open(to, from)) != (iconv_t)-1) {
27    ICONV_CONST char* ib;
28    char* buf;
29    char* ob;
30    size_t ibl, obl;
31
32    ibl = strlen(s) + 1;
33    ib = s;
34    obl = MB_LEN_MAX * ibl;
35    buf = (char*)malloc(obl * sizeof(char));
36    ob = buf;
37
38    do {
39      if (iconv(cd, &ib, &ibl, &ob, &obl) == (size_t)-1) {
40        DEBUG(2, "iconv return error %d\n", errno);
41        if (obl) {
42          *ob++ = *ib++;
43          ibl--;
44          obl--;
45        }
46      }
47    } while (ibl && obl);
48    *ob = 0;
49    DEBUG(2, "iconv return %s\n", buf);
50    iconv_close (cd);
51    free(*str);
52    *str = buf;
53  } else {
54    DEBUG(2, "iconv_open return error %d\n", errno);
55  }
56 
57  return 0;
58}
Note: See TracBrowser for help on using the repository browser.