3 ** This program is distributed under the GNU General Public License, version 2.
4 ** A copy of this license is included with this source.
6 ** Copyright 2000, Michael Smith <msmith@xiph.org>
8 ** Portions from Vorbize, (c) Kenneth Arnold <kcarnold-xiph@arnoldnet.net>
9 ** and libvorbis examples, (c) Monty <monty@xiph.org>
12 /* Platform support routines - win32, OS/2, unix */
23 #if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
34 #if defined(_WIN32) && defined(_MSC_VER)
36 void setbinmode(FILE *f
)
38 _setmode( _fileno(f
), _O_BINARY
);
43 void setbinmode(FILE *f
)
49 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__)
50 void setbinmode(FILE *f
)
52 setmode(fileno(f
), O_BINARY
);
57 #if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
58 void *timer_start(void)
60 time_t *start
= malloc(sizeof(time_t));
65 double timer_time(void *timer
)
67 time_t now
= time(NULL
);
68 time_t start
= *((time_t *)timer
);
71 return (double)(now
-start
);
73 return 1; /* To avoid division by zero later, for very short inputs */
77 void timer_clear(void *timer
)
79 free((time_t *)timer
);
82 #else /* unix. Or at least win32 */
87 void *timer_start(void)
89 struct timeval
*start
= malloc(sizeof(struct timeval
));
90 gettimeofday(start
, NULL
);
94 double timer_time(void *timer
)
97 struct timeval start
= *((struct timeval
*)timer
);
99 gettimeofday(&now
, NULL
);
101 return (double)now
.tv_sec
- (double)start
.tv_sec
+
102 ((double)now
.tv_usec
- (double)start
.tv_usec
)/1000000.0;
105 void timer_clear(void *timer
)
107 free((time_t *)timer
);
114 #include <sys/types.h>
115 #include <sys/stat.h>
121 #define PATH_SEPS "/\\"
122 #define mkdir(x,y) _mkdir((x))
124 /* MSVC does this, borland doesn't? */
131 #define PATH_SEPS "/"
135 int create_directories(char *fn
, int isutf8
)
139 char *segment
= malloc(strlen(fn
)+1);
141 wchar_t seg
[MAX_PATH
+1];
146 if(strlen(fn
) >= 3 && isalpha(fn
[0]) && fn
[1]==':')
150 while((end
= strpbrk(start
+1, PATH_SEPS
)) != NULL
)
153 memcpy(segment
, fn
, end
-fn
);
158 MultiByteToWideChar(CP_UTF8
, 0, segment
, -1, seg
, MAX_PATH
+1);
159 rv
= _wstat(seg
,&statbuf
);
162 rv
= stat(segment
,&statbuf
);
164 if(errno
== ENOENT
) {
170 rv
= mkdir(segment
, 0777);
172 fprintf(stderr
, _("Couldn't create directory \"%s\": %s\n"),
173 segment
, strerror(errno
));
179 fprintf(stderr
, _("Error checking for existence of directory %s: %s\n"),
180 segment
, strerror(errno
));
185 #if defined(_WIN32) && !defined(__BORLANDC__)
186 else if(!(_S_IFDIR
& statbuf
.st_mode
)) {
187 #elif defined(__BORLANDC__)
188 else if(!(S_IFDIR
& statbuf
.st_mode
)) {
190 else if(!S_ISDIR(statbuf
.st_mode
)) {
192 fprintf(stderr
, _("Error: path segment \"%s\" is not a directory\n"),
208 FILE *oggenc_fopen(char *fn
, char *mode
, int isutf8
)
211 wchar_t wfn
[MAX_PATH
+1];
213 MultiByteToWideChar(CP_UTF8
, 0, fn
, -1, wfn
, MAX_PATH
+1);
214 MultiByteToWideChar(CP_ACP
, 0, mode
, -1, wmode
, 32);
215 return _wfopen(wfn
, wmode
);
217 return fopen(fn
, mode
);
221 parse_for_utf8(int argc
, char **argv
)
223 extern struct option long_options
[];
225 int option_index
= 1;
227 while((ret
= getopt_long(argc
, argv
, "A:a:b:B:c:C:d:G:hkl:m:M:n:N:o:P:q:QrR:s:t:vX:",
228 long_options
, &option_index
)) != -1)
233 if(!strcmp(long_options
[option_index
].name
, "utf8")) {
245 typedef WINSHELLAPI LPWSTR
* (APIENTRY
*tCommandLineToArgvW
)(LPCWSTR lpCmdLine
, int*pNumArgs
);
247 void get_args_from_ucs16(int *argc
, char ***argv
)
252 utf8
= parse_for_utf8(*argc
, *argv
);
253 optind
= 1; /* Reset getopt_long */
255 /* If command line is already UTF-8, don't convert */
259 vi
.dwOSVersionInfoSize
= sizeof(vi
);
262 /* We only do NT4 and more recent.*/
263 /* It would be relatively easy to add NT3.5 support. Is anyone still using NT3? */
264 /* It would be relatively hard to add 9x support. Fortunately, 9x is
265 a lost cause for unicode support anyway. */
266 if (vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
&& vi
.dwMajorVersion
>= 4) {
267 const char utf8flag
[] = "--utf8";
272 char **newargv
= NULL
;
273 LPWSTR
*ucs16argv
= NULL
;
274 tCommandLineToArgvW pCommandLineToArgvW
= NULL
;
277 hLib
= LoadLibrary("shell32.dll");
280 pCommandLineToArgvW
= (tCommandLineToArgvW
)GetProcAddress(hLib
, "CommandLineToArgvW");
281 if (!pCommandLineToArgvW
)
284 ucs16argv
= pCommandLineToArgvW(GetCommandLineW(), &newargc
);
288 for (a
=0; a
<newargc
; a
++) {
289 count
= WideCharToMultiByte(CP_UTF8
, 0, ucs16argv
[a
], -1,
290 NULL
, 0, NULL
, NULL
);
296 sizeofargs
+= strlen(utf8flag
) + 1;
298 newargv
= malloc(((newargc
+ 2) * sizeof(char *)) + sizeofargs
);
299 argptr
= (char *)(&newargv
[newargc
+2]);
301 for (a
=0; a
<newargc
; a
++) {
302 count
= WideCharToMultiByte(CP_UTF8
, 0, ucs16argv
[a
], -1,
303 argptr
, sizeofargs
, NULL
, NULL
);
312 count
= strlen(utf8flag
) + 1;
313 strcpy(argptr
, utf8flag
);
326 if (ucs16argv
!= NULL
)
327 GlobalFree(ucs16argv
);