add --tag and --long-tag
[fdkaac.git] / src / compat_posix.c
CommitLineData
48e2f01c 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
18int64_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
25FILE *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
35void aacenc_getmainargs(int *argc, char ***argv)
36{
37 return;
38}
39
40int 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#ifndef HAVE_ICONV
52char *aacenc_to_utf8(const char *s)
53{
54 return strdup(s);
55}
56#else /* HAVE_ICONV */
57
58#include <sys/types.h>
59#include <stddef.h>
60#include <errno.h>
61#include <iconv.h>
62
63#if HAVE_LOCALCHARSET_H
64#include <localcharset.h>
65#elif HAVE_LANGINFO_H
66#include <langinfo.h>
67static const char *locale_charset(void)
68{
69 return nl_langinfo(CODESET);
70}
71#else
72static const char *locale_charset(void)
73{
74 return 0;
75}
76#endif
77
78static
79int utf8_from_charset(const char *charset, const char *from, char **to)
80{
81 iconv_t cd;
82 size_t fromlen, obsize, ibleft, obleft;
83 char *ip, *op;
84
85 cd = iconv_open("UTF-8", charset);
86 if (cd == (iconv_t)-1)
87 return -1;
88
89 fromlen = strlen(from);
90 ibleft = fromlen;
91 obsize = 2;
92 obleft = obsize - 1;
93 *to = malloc(obsize);
94 ip = (char *)from;
95 op = *to;
96
97 while (ibleft > 0) {
98 if (iconv(cd, &ip, &ibleft, &op, &obleft) != (size_t)-1)
99 break;
100 else {
101 if (errno == E2BIG || obleft == 0) {
102 ptrdiff_t offset = op - *to;
103 obsize *= 2;
104 *to = realloc(*to, obsize);
105 op = *to + offset;
106 obleft = obsize - offset - 1;
107 }
108 if (errno == EILSEQ) {
109 ++ip;
110 --ibleft;
111 *op++ = '?';
112 --obleft;
113 }
114 if (errno != E2BIG && errno != EILSEQ)
115 break;
116 }
117 }
118 iconv_close(cd);
119 *op = 0;
120 if (fromlen > 0 && op == *to) {
121 free(op);
122 return -1;
123 }
124 return 0;
125}
126
127char *aacenc_to_utf8(const char *s)
128{
129 char *result;
130 const char *charset;
131
132 if ((charset = locale_charset()) == 0)
133 charset = "US-ASCII";
134 if (utf8_from_charset(charset, s, &result) < 0)
135 result = strdup(s);
136 return result;
137}
138#endif /* HAVE_ICONV */
This page took 0.015758 seconds and 4 git commands to generate.