mkvtoolnix/avilib-0.6.10/xio.cpp

170 lines
3.6 KiB
C++
Raw Normal View History

2004-02-12 18:40:37 +00:00
/*
* libxio.c
*
* Copyright (C) Lukas Hejtmanek - January 2004
*
* This file is part of transcode, a linux video stream processing tool
*
* transcode is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* transcode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
2004-02-12 18:52:08 +00:00
#include "os.h"
2004-02-12 18:40:37 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
2004-08-29 11:06:00 +00:00
#if defined(HAVE_UNISTD_H)
2004-02-12 18:40:37 +00:00
#include <unistd.h>
2004-08-29 11:06:00 +00:00
#endif
2004-02-12 18:40:37 +00:00
#include <fcntl.h>
#include <sys/stat.h>
#include <stdarg.h>
2004-06-03 14:23:38 +00:00
#include "mm_io.h"
extern "C" {
#define MAX_INSTANCES 4000
static mm_file_io_c *instances[MAX_INSTANCES];
2004-06-03 14:23:38 +00:00
static bool instances_initialized = false;
2004-02-12 18:40:37 +00:00
int
2004-06-03 14:23:38 +00:00
xio_open(const char *pathname,
int flags,
...) {
int i, idx;
open_mode omode;
if (!instances_initialized) {
memset(instances, 0, MAX_INSTANCES * sizeof(mm_io_c *));
instances_initialized = true;
}
idx = -1;
for (i = 0; i < MAX_INSTANCES; i++)
if (instances[i] == NULL) {
idx = i;
break;
}
if (idx == -1)
return -1;
if ((flags & O_CREAT) != 0)
omode = MODE_CREATE;
else if (flags == O_RDONLY)
omode = MODE_READ;
else
omode = MODE_WRITE;
try {
instances[idx] = new mm_file_io_c(pathname, omode);
2004-06-03 14:23:38 +00:00
} catch(...) {
return -1;
}
return idx;
2004-02-12 18:40:37 +00:00
}
ssize_t
2004-06-03 14:23:38 +00:00
xio_read(int fd,
void *buf,
size_t count) {
if ((fd < 0) || (fd >= MAX_INSTANCES) || (instances[fd] == NULL))
return -1;
return instances[fd]->read(buf, count);
2004-02-12 18:40:37 +00:00
}
ssize_t
2004-06-03 14:23:38 +00:00
xio_write(int fd,
const void *buf,
size_t count) {
if ((fd < 0) || (fd >= MAX_INSTANCES) || (instances[fd] == NULL))
return -1;
return instances[fd]->write(buf, count);
2004-02-12 18:40:37 +00:00
}
int
2004-06-03 14:23:38 +00:00
xio_ftruncate(int fd,
2004-06-14 08:35:10 +00:00
int64_t length) {
2004-06-03 14:23:38 +00:00
if ((fd < 0) || (fd >= MAX_INSTANCES) || (instances[fd] == NULL))
return -1;
return instances[fd]->truncate(length);
2004-02-12 18:40:37 +00:00
}
2004-06-14 08:35:10 +00:00
int64_t
2004-06-03 14:23:38 +00:00
xio_lseek(int fd,
2004-06-14 08:35:10 +00:00
int64_t offset,
2004-06-03 14:23:38 +00:00
int whence) {
2004-06-13 15:29:21 +00:00
int64_t expected_pos;
2004-06-03 14:23:38 +00:00
seek_mode smode;
if ((fd < 0) || (fd >= MAX_INSTANCES) || (instances[fd] == NULL))
2004-06-14 08:35:10 +00:00
return (int64_t)-1;
2004-06-13 15:29:21 +00:00
if (whence == SEEK_SET) {
2004-06-03 14:23:38 +00:00
smode = seek_beginning;
2004-06-13 15:29:21 +00:00
expected_pos = offset;
} else if (whence == SEEK_END) {
2004-06-03 14:23:38 +00:00
smode = seek_end;
2004-06-13 15:29:21 +00:00
expected_pos = instances[fd]->get_size() - offset;
} else {
2004-06-03 14:23:38 +00:00
smode = seek_current;
2004-06-13 15:29:21 +00:00
expected_pos = instances[fd]->getFilePointer() + offset;
}
2004-06-03 14:23:38 +00:00
instances[fd]->setFilePointer(offset, smode);
2004-06-13 15:29:21 +00:00
if (instances[fd]->getFilePointer() != expected_pos)
2004-06-14 08:35:10 +00:00
return (int64_t)-1;
2004-06-13 15:29:21 +00:00
return expected_pos;
2004-02-12 18:40:37 +00:00
}
int
2004-06-03 14:23:38 +00:00
xio_close(int fd) {
if ((fd < 0) || (fd >= MAX_INSTANCES) || (instances[fd] == NULL))
return -1;
delete instances[fd];
instances[fd] = NULL;
return 0;
2004-02-12 18:40:37 +00:00
}
int
2004-06-03 14:23:38 +00:00
xio_stat(const char *file_name,
struct stat *buf) {
// Not implemented for mkvtoolnix.
return -1;
2004-02-12 18:40:37 +00:00
}
int
2004-06-03 14:23:38 +00:00
xio_lstat(const char *file_name,
struct stat *buf) {
// Not implemented for mkvtoolnix.
2004-02-12 18:52:08 +00:00
return -1;
2004-02-12 18:40:37 +00:00
}
int
2004-06-03 14:23:38 +00:00
xio_rename(const char *oldpath,
const char *newpath) {
2004-02-12 18:40:37 +00:00
return rename(oldpath, newpath);
}
int
2004-06-03 14:23:38 +00:00
xio_fstat(int fd,
struct stat *buf) {
// Not implemented for mkvtoolnix.
return -1;
}
2004-02-12 18:40:37 +00:00
}