Build script now builds an oggenc binary with flac support.
[vorbis-lancer-gcc.git] / vorbis-tools-1.2.0 / oggenc / platform.c
blob76494a8e6391e1e6430353d3cf4f422088a6d3f7
1 /* OggEnc
2 **
3 ** This program is distributed under the GNU General Public License, version 2.
4 ** A copy of this license is included with this source.
5 **
6 ** Copyright 2000, Michael Smith <msmith@xiph.org>
7 **
8 ** Portions from Vorbize, (c) Kenneth Arnold <kcarnold-xiph@arnoldnet.net>
9 ** and libvorbis examples, (c) Monty <monty@xiph.org>
11 ** last mod: $Id: platform.c,v 1.2 2005-06-19 02:52:40+09 blacksword Exp $
13 **/
15 /* Platform support routines - win32, OS/2, unix */
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
21 #include "platform.h"
22 #include "encode.h"
23 #include "i18n.h"
24 #include <stdlib.h>
25 #include <ctype.h>
26 #if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
27 #include <fcntl.h>
28 #include <io.h>
29 #include <time.h>
30 #endif
32 #if defined(_WIN32) && defined(_MSC_VER)
34 void setbinmode(FILE *f)
36 _setmode( _fileno(f), _O_BINARY );
38 #endif /* win32 */
40 #ifdef __EMX__
41 void setbinmode(FILE *f)
43 _fsetmode( f, "b");
45 #endif
47 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__)
48 void setbinmode(FILE *f)
50 setmode(fileno(f), O_BINARY);
52 #endif
55 #if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
56 #if 1
57 #include <windows.h>
58 void *timer_start(void)
60 DWORD *start = malloc(sizeof(DWORD));
61 *start = timeGetTime();
62 return (void *)start;
65 double timer_time(void *timer)
67 ogg_int64_t now = (ogg_int64_t)timeGetTime();
68 ogg_int64_t start = (ogg_int64_t)(*(DWORD*)timer);
69 if(now<start)
70 now += 0x100000000;
71 now = now - start;
72 if(now)
73 return (double)(now)/1000.0;
74 else
75 return 1;
79 void timer_clear(void *timer)
81 free((int*)timer);
83 #else
84 void *timer_start(void)
86 time_t *start = malloc(sizeof(time_t));
87 time(start);
88 return (void *)start;
91 double timer_time(void *timer)
93 time_t now = time(NULL);
94 time_t start = *((time_t *)timer);
96 if(now-start)
97 return (double)(now-start);
98 else
99 return 1; /* To avoid division by zero later, for very short inputs */
103 void timer_clear(void *timer)
105 free((time_t *)timer);
107 #endif
109 #else /* unix. Or at least win32 */
111 #include <sys/time.h>
112 #include <unistd.h>
114 void *timer_start(void)
116 struct timeval *start = malloc(sizeof(struct timeval));
117 gettimeofday(start, NULL);
118 return (void *)start;
121 double timer_time(void *timer)
123 struct timeval now;
124 struct timeval start = *((struct timeval *)timer);
126 gettimeofday(&now, NULL);
128 return (double)now.tv_sec - (double)start.tv_sec +
129 ((double)now.tv_usec - (double)start.tv_usec)/1000000.0;
132 void timer_clear(void *timer)
134 free((time_t *)timer);
137 #endif
139 #include <errno.h>
140 #include <string.h>
141 #include <sys/types.h>
142 #include <sys/stat.h>
144 #ifdef _WIN32
146 #include <direct.h>
148 #define PATH_SEPS "/\\"
149 #define mkdir(x,y) _mkdir((x))
151 /* MSVC does this, borland doesn't? */
152 #ifndef __BORLANDC__
153 #define stat _stat
154 #endif
156 #else
158 #define PATH_SEPS "/"
160 #endif
162 int create_directories(char *fn)
164 char *end, *start;
165 struct stat statbuf;
166 char *segment = malloc(strlen(fn)+1);
168 start = fn;
169 #ifdef _WIN32
170 if(strlen(fn) >= 3 && isalpha(fn[0]) && fn[1]==':')
171 start = start+2;
172 #endif
174 while((end = strpbrk(start+1, PATH_SEPS)) != NULL)
176 memcpy(segment, fn, end-fn);
177 segment[end-fn] = 0;
179 if(stat(segment,&statbuf)) {
180 if(errno == ENOENT) {
181 if(mkdir(segment, 0777)) {
182 fprintf(stderr, _("Couldn't create directory \"%s\": %s\n"),
183 segment, strerror(errno));
184 free(segment);
185 return -1;
188 else {
189 fprintf(stderr, _("Error checking for existence of directory %s: %s\n"),
190 segment, strerror(errno));
191 free(segment);
192 return -1;
195 #if defined(_WIN32) && !defined(__BORLANDC__)
196 else if(!(_S_IFDIR & statbuf.st_mode)) {
197 #elif defined(__BORLANDC__)
198 else if(!(S_IFDIR & statbuf.st_mode)) {
199 #else
200 else if(!S_ISDIR(statbuf.st_mode)) {
201 #endif
202 fprintf(stderr, _("Error: path segment \"%s\" is not a directory\n"),
203 segment);
204 free(segment);
205 return -1;
208 start = end+1;
211 free(segment);
212 return 0;