File: charset_utils.c

package info (click to toggle)
curlftpfs 0.9.2-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,028 kB
  • sloc: sh: 8,885; ansic: 2,846; makefile: 42
file content (58 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
    FTP file system
    Copyright (C) 2007 Robson Braga Araujo <robsonbraga@gmail.com>

    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.
*/

#include "config.h"

#include <string.h>
#include <limits.h>
#include <iconv.h>
#include <errno.h>
#include <stdlib.h>

#include "ftpfs.h"

int convert_charsets(const char* from, const char* to, char** str) {
  iconv_t cd;
  char* s = *str;

  if (!s || !*s)
    return 0;

  if (to && from && (cd = iconv_open(to, from)) != (iconv_t)-1) {
    ICONV_CONST char* ib;
    char* buf;
    char* ob;
    size_t ibl, obl;

    ibl = strlen(s) + 1;
    ib = s;
    obl = MB_LEN_MAX * ibl;
    buf = (char*)malloc(obl * sizeof(char));
    ob = buf;

    do {
      if (iconv(cd, &ib, &ibl, &ob, &obl) == (size_t)-1) {
        DEBUG(2, "iconv return error %d\n", errno);
        if (obl) {
          *ob++ = *ib++;
          ibl--;
          obl--;
        }
      }
    } while (ibl && obl);
    *ob = 0;
    DEBUG(2, "iconv return %s\n", buf);
    iconv_close (cd);
    free(*str);
    *str = buf;
  } else {
    DEBUG(2, "iconv_open return error %d\n", errno);
  }
  
  return 0;
}