[c47208]: oggenc / platform.c Maximize Restore History

Download this file

platform.c    183 lines (143 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* OggEnc
**
** This program is distributed under the GNU General Public License, version 2.
** A copy of this license is included with this source.
**
** Copyright 2000, Michael Smith <msmith@xiph.org>
**
** Portions from Vorbize, (c) Kenneth Arnold <kcarnold-xiph@arnoldnet.net>
** and libvorbis examples, (c) Monty <monty@xiph.org>
**/
/* Platform support routines - win32, OS/2, unix */
#include "platform.h"
#include "encode.h"
#include "i18n.h"
#include <stdlib.h>
#include <ctype.h>
#if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
#include <fcntl.h>
#include <io.h>
#include <time.h>
#endif
#if defined(_WIN32) && defined(_MSC_VER)
void setbinmode(FILE *f)
{
_setmode( _fileno(f), _O_BINARY );
}
#endif /* win32 */
#ifdef __EMX__
void setbinmode(FILE *f)
{
_fsetmode( f, "b");
}
#endif
#if defined(__WATCOMC__) || defined(__BORLANDC__)
void setbinmode(FILE *f)
{
setmode(fileno(f), O_BINARY);
}
#endif
#if defined(_WIN32) || defined(__EMX__) || defined(__WATCOMC__)
void *timer_start(void)
{
time_t *start = malloc(sizeof(time_t));
time(start);
return (void *)start;
}
double timer_time(void *timer)
{
time_t now = time(NULL);
time_t start = *((time_t *)timer);
if(now-start)
return (double)(now-start);
else
return 1; /* To avoid division by zero later, for very short inputs */
}
void timer_clear(void *timer)
{
free((time_t *)timer);
}
#else /* unix. Or at least win32 */
#include <sys/time.h>
#include <unistd.h>
void *timer_start(void)
{
struct timeval *start = malloc(sizeof(struct timeval));
gettimeofday(start, NULL);
return (void *)start;
}
double timer_time(void *timer)
{
struct timeval now;
struct timeval start = *((struct timeval *)timer);
gettimeofday(&now, NULL);
return (double)now.tv_sec - (double)start.tv_sec +
((double)now.tv_usec - (double)start.tv_usec)/1000000.0;
}
void timer_clear(void *timer)
{
free((time_t *)timer);
}
#endif
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#define PATH_SEPS "/\\"
#define mkdir(x,y) _mkdir((x))
/* MSVC does this, borland doesn't? */
#ifndef __BORLANDC__
#define stat _stat
#endif
#else
#define PATH_SEPS "/"
#endif
int create_directories(char *fn)
{
char *end, *start;
struct stat statbuf;
char *segment = malloc(strlen(fn)+1);
start = fn;
#ifdef _WIN32
if(strlen(fn) >= 3 && isalpha(fn[0]) && fn[1]==':')
start = start+2;
#endif
while((end = strpbrk(start+1, PATH_SEPS)) != NULL)
{
memcpy(segment, fn, end-fn);
segment[end-fn] = 0;
if(stat(segment,&statbuf)) {
if(errno == ENOENT) {
if(mkdir(segment, 0777)) {
fprintf(stderr, _("Couldn't create directory \"%s\": %s\n"),
segment, strerror(errno));
free(segment);
return -1;
}
}
else {
fprintf(stderr, _("Error checking for existence of directory %s: %s\n"),
segment, strerror(errno));
free(segment);
return -1;
}
}
#if defined(_WIN32) && !defined(__BORLANDC__)
else if(!(_S_IFDIR & statbuf.st_mode)) {
#elif defined(__BORLANDC__)
else if(!(S_IFDIR & statbuf.st_mode)) {
#else
else if(!S_ISDIR(statbuf.st_mode)) {
#endif
fprintf(stderr, _("Error: path segment \"%s\" is not a directory\n"),
segment);
free(segment);
return -1;
}
start = end+1;
}
free(segment);
return 0;
}