]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2013 nu774 | |
3 | * For conditions of distribution and use, see copyright notice in COPYING | |
4 | */ | |
5 | #if HAVE_CONFIG_H | |
6 | # include "config.h" | |
7 | #endif | |
8 | #if HAVE_STDINT_H | |
9 | # include <stdint.h> | |
10 | #endif | |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <stdarg.h> | |
15 | #include <sys/time.h> | |
16 | #include "compat.h" | |
17 | ||
18 | int64_t aacenc_timer(void) | |
19 | { | |
20 | struct timeval tv = { 0 }; | |
21 | gettimeofday(&tv, 0); | |
22 | return (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; | |
23 | } | |
24 | ||
25 | FILE *aacenc_fopen(const char *name, const char *mode) | |
26 | { | |
27 | FILE *fp; | |
28 | if (strcmp(name, "-") == 0) | |
29 | fp = (mode[0] == 'r') ? stdin : stdout; | |
30 | else | |
31 | fp = fopen(name, mode); | |
32 | return fp; | |
33 | } | |
34 | ||
35 | void aacenc_getmainargs(int *argc, char ***argv) | |
36 | { | |
37 | return; | |
38 | } | |
39 | ||
40 | int aacenc_fprintf(FILE *fp, const char *fmt, ...) | |
41 | { | |
42 | va_list ap; | |
43 | int cnt; | |
44 | ||
45 | va_start(ap, fmt); | |
46 | cnt = vfprintf(fp, fmt, ap); | |
47 | va_end(ap); | |
48 | return cnt; | |
49 | } | |
50 | ||
51 | /* | |
52 | * Different from POSIX basename() when path ends with /. | |
53 | * Since we use this only for a regular file, the difference doesn't matter. | |
54 | */ | |
55 | const char *aacenc_basename(const char *path) | |
56 | { | |
57 | const char *p = strrchr(path, '/'); | |
58 | return p ? p + 1: path; | |
59 | } | |
60 | ||
61 | #ifndef HAVE_ICONV | |
62 | char *aacenc_to_utf8(const char *s) | |
63 | { | |
64 | return strdup(s); | |
65 | } | |
66 | #else /* HAVE_ICONV */ | |
67 | ||
68 | #include <sys/types.h> | |
69 | #include <stddef.h> | |
70 | #include <errno.h> | |
71 | #include <iconv.h> | |
72 | ||
73 | #if HAVE_LOCALCHARSET_H | |
74 | #include <localcharset.h> | |
75 | #elif HAVE_LANGINFO_H | |
76 | #include <langinfo.h> | |
77 | static const char *locale_charset(void) | |
78 | { | |
79 | return nl_langinfo(CODESET); | |
80 | } | |
81 | #else | |
82 | static const char *locale_charset(void) | |
83 | { | |
84 | return 0; | |
85 | } | |
86 | #endif | |
87 | ||
88 | static | |
89 | int utf8_from_charset(const char *charset, const char *from, char **to) | |
90 | { | |
91 | iconv_t cd; | |
92 | size_t fromlen, obsize, ibleft, obleft; | |
93 | char *ip, *op; | |
94 | ||
95 | cd = iconv_open("UTF-8", charset); | |
96 | if (cd == (iconv_t)-1) | |
97 | return -1; | |
98 | ||
99 | fromlen = strlen(from); | |
100 | ibleft = fromlen; | |
101 | obsize = 2; | |
102 | obleft = obsize - 1; | |
103 | *to = malloc(obsize); | |
104 | ip = (char *)from; | |
105 | op = *to; | |
106 | ||
107 | while (ibleft > 0) { | |
108 | if (iconv(cd, &ip, &ibleft, &op, &obleft) != (size_t)-1) | |
109 | break; | |
110 | else { | |
111 | if (errno == E2BIG || obleft == 0) { | |
112 | ptrdiff_t offset = op - *to; | |
113 | obsize *= 2; | |
114 | *to = realloc(*to, obsize); | |
115 | op = *to + offset; | |
116 | obleft = obsize - offset - 1; | |
117 | } | |
118 | if (errno == EILSEQ) { | |
119 | ++ip; | |
120 | --ibleft; | |
121 | *op++ = '?'; | |
122 | --obleft; | |
123 | } | |
124 | if (errno != E2BIG && errno != EILSEQ) | |
125 | break; | |
126 | } | |
127 | } | |
128 | iconv_close(cd); | |
129 | *op = 0; | |
130 | if (fromlen > 0 && op == *to) { | |
131 | free(op); | |
132 | return -1; | |
133 | } | |
134 | return 0; | |
135 | } | |
136 | ||
137 | char *aacenc_to_utf8(const char *s) | |
138 | { | |
139 | char *result; | |
140 | const char *charset; | |
141 | ||
142 | if ((charset = locale_charset()) == 0) | |
143 | charset = "US-ASCII"; | |
144 | if (utf8_from_charset(charset, s, &result) < 0) | |
145 | result = strdup(s); | |
146 | return result; | |
147 | } | |
148 | #endif /* HAVE_ICONV */ |