Recognizes if input is ogg or not.
[xiph.git] / vorbis-tools / oggenc / platform.c
blobf59c085482b8193e87ca2d21edb012c3320ce9f2
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>
10 **/
12 /* Platform support routines - win32, OS/2, unix */
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
18 #include "platform.h"
19 #include "encode.h"
20 #include "i18n.h"
21 #include <stdlib.h>
22 #include <ctype.h>
23 #if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
24 #include <fcntl.h>
25 #include <io.h>
26 #include <time.h>
27 #endif
29 #if defined(_WIN32) && defined(_MSC_VER)
31 void setbinmode(FILE *f)
33 _setmode( _fileno(f), _O_BINARY );
35 #endif /* win32 */
37 #ifdef __EMX__
38 void setbinmode(FILE *f)
40 _fsetmode( f, "b");
42 #endif
44 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__)
45 void setbinmode(FILE *f)
47 setmode(fileno(f), O_BINARY);
49 #endif
52 #if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
53 void *timer_start(void)
55 time_t *start = malloc(sizeof(time_t));
56 time(start);
57 return (void *)start;
60 double timer_time(void *timer)
62 time_t now = time(NULL);
63 time_t start = *((time_t *)timer);
65 if(now-start)
66 return (double)(now-start);
67 else
68 return 1; /* To avoid division by zero later, for very short inputs */
72 void timer_clear(void *timer)
74 free((time_t *)timer);
77 #else /* unix. Or at least win32 */
79 #include <sys/time.h>
80 #include <unistd.h>
82 void *timer_start(void)
84 struct timeval *start = malloc(sizeof(struct timeval));
85 gettimeofday(start, NULL);
86 return (void *)start;
89 double timer_time(void *timer)
91 struct timeval now;
92 struct timeval start = *((struct timeval *)timer);
94 gettimeofday(&now, NULL);
96 return (double)now.tv_sec - (double)start.tv_sec +
97 ((double)now.tv_usec - (double)start.tv_usec)/1000000.0;
100 void timer_clear(void *timer)
102 free((time_t *)timer);
105 #endif
107 #include <errno.h>
108 #include <string.h>
109 #include <sys/types.h>
110 #include <sys/stat.h>
112 #ifdef _WIN32
114 #include <direct.h>
116 #define PATH_SEPS "/\\"
117 #define mkdir(x,y) _mkdir((x))
119 /* MSVC does this, borland doesn't? */
120 #ifndef __BORLANDC__
121 #define stat _stat
122 #endif
124 #else
126 #define PATH_SEPS "/"
128 #endif
130 int create_directories(char *fn)
132 char *end, *start;
133 struct stat statbuf;
134 char *segment = malloc(strlen(fn)+1);
136 start = fn;
137 #ifdef _WIN32
138 if(strlen(fn) >= 3 && isalpha(fn[0]) && fn[1]==':')
139 start = start+2;
140 #endif
142 while((end = strpbrk(start+1, PATH_SEPS)) != NULL)
144 memcpy(segment, fn, end-fn);
145 segment[end-fn] = 0;
147 if(stat(segment,&statbuf)) {
148 if(errno == ENOENT) {
149 if(mkdir(segment, 0777)) {
150 fprintf(stderr, _("Couldn't create directory \"%s\": %s\n"),
151 segment, strerror(errno));
152 free(segment);
153 return -1;
156 else {
157 fprintf(stderr, _("Error checking for existence of directory %s: %s\n"),
158 segment, strerror(errno));
159 free(segment);
160 return -1;
163 #if defined(_WIN32) && !defined(__BORLANDC__)
164 else if(!(_S_IFDIR & statbuf.st_mode)) {
165 #elif defined(__BORLANDC__)
166 else if(!(S_IFDIR & statbuf.st_mode)) {
167 #else
168 else if(!S_ISDIR(statbuf.st_mode)) {
169 #endif
170 fprintf(stderr, _("Error: path segment \"%s\" is not a directory\n"),
171 segment);
172 free(segment);
173 return -1;
176 start = end+1;
179 free(segment);
180 return 0;