Reorganized some function into util.c and created prototypes for transcode.

This commit is contained in:
2010-11-06 18:55:53 +01:00
parent 7cbaf96001
commit 629311bb96
7 changed files with 201 additions and 127 deletions

View File

@@ -1,4 +1,4 @@
bin_PROGRAMS=tcfs
tcfs_SOURCES=src/tcfs.c
tcfs_SOURCES=src/tcfs.c src/transcode.c src/util.c
tcfs_LDADD = $(LIBOBJS) $(FUSE_LIBS) $(libavcore_LIBS) $(libavutil_LIBS) $(libavformat_LIBS) $(libavcodec_LIBS)
tcfs_CFLAGS = $(FUSE_CFLAGS) $(libavcore_CFLAGS) $(libavutil_CFLAGS) $(libavformat_CFLAGS) $(libavcodec_CFLAGS) -DFUSE_USE_VERSION=28

View File

@@ -58,4 +58,9 @@
# include <dirent.h>
#endif
#include <libavcore/avcore.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#endif

View File

@@ -5,136 +5,13 @@
*/
#include "common.h"
#include "util.h"
#include "transcode.h"
#include <fuse.h>
#include <fuse_opt.h>
struct options {
char *base;
char *tfmt;
} options;
// Get the filename from a path, i.e. a pointer to the character after the last / in path.
// If path contains no /, returns path.
char *get_file_from_path(const char *path)
{
if(!path)
return NULL;
char *ret = strchr(path, '/');
if(ret == NULL)
return path;
return ret+1;
}
// Get the extension from the file name
char *get_ext_from_file(const char *file)
{
if(!file)
return NULL;
char *ret = strchr(file, '.');
if(!ret)
return file+strlen(file);
if(ret == file)
return file+strlen(file); // return pointer to '\0';
return ret+1;
}
char *strip_ext_from_path(const char *path)
{
if(!path)
return NULL;
char *ret = strdup(path);
char *ext = strchr(ret, '.');
char *file = strchr(ret, '/');
if(!ext || (ext<file)) // no . or . not in last component
return ret;
*ext = '\0';
return ret;
}
// returns 1 if the file is a transcodeable audio file, 0 if it is not and a negative value if an error occured
int is_tc_audio_file(const char *path)
{
if(!path)
return -EINVAL;
struct stat st;
int ret = stat(path, &st);
if(ret == -1)
return -errno;
if(!S_ISREG(st.st_mode))
return 0;
char *file = get_file_from_path(path);
if(!file || (file[0] == '\0'))
return 0;
char *ext = get_ext_from_file(file);
if(!ext)
return 0;
if(strcmp(ext,options.tfmt) == 0)
return 0; // file already has right format
// TODO: proper detection code here
// for testing
if(strcmp(ext,"ogg") == 0)
return 1;
return 0;
}
// Get the path of the transcodeable audio file that
// generates 'path'. Returns NULL when there is no such
// file.
char *get_tc_source_name(const char *path)
{
if(!path)
return NULL;
if(strcmp(get_ext_from_file(get_file_from_path(path)), options.tfmt)!=0) {
return NULL;
}
char *path_no_ext = strip_ext_from_path(path);
if(!path_no_ext)
return NULL;
// TODO : search for transcodable files in path_no_ext.*
// for testing
char *ret;
if(asprintf(&ret, "%s.ogg", path_no_ext) < 0)
return NULL;
return ret;
}
// safely concatenate options.base and path
// the caller must free() the returned string
char *path_cat(const char *path)
{
if(!path)
return NULL;
char *ret = (char*)malloc(strlen(options.base)+strlen(path)+1);
if(!ret)
return NULL;
memcpy(ret, options.base, strlen(options.base));
memcpy(ret+strlen(options.base), path, strlen(path)+1);
return ret;
}
struct options options;
int tcfs_getattr(const char *path, struct stat *stbuf)
{
@@ -364,5 +241,7 @@ int main(int argc, char *argv[])
options.tfmt = "mp3";
}
av_register_all();
return fuse_main(args.argc, args.argv, &tcfs_oper, NULL);
}

21
src/transcode.c Normal file
View File

@@ -0,0 +1,21 @@
/*
Copyright (c) 2010 Matthias Blankertz <matthias@blankertz.org>
This work is licensed under the Open Software License ("OSL") v. 3.0.
*/
#include "common.h"
struct TC_FILE {
};
struct TC_FILE *tc_open(const char *src, const char *tfmt)
{
return NULL;
}
int tc_read(char *buf, size_t size, struct TC_FILE *fd)
{
errno = ENOSYS;
return 0;
}

15
src/transcode.h Normal file
View File

@@ -0,0 +1,15 @@
/*
Copyright (c) 2010 Matthias Blankertz <matthias@blankertz.org>
This work is licensed under the Open Software License ("OSL") v. 3.0.
*/
#ifndef TCFS_TRANSCODE_H
#define TCFS_TRANSCODE_H 1
struct TC_FILE;
struct TC_FILE *tc_open(const char *src, const char *tfmt);
int tc_read(char *buf, size_t size, struct TC_FILE *fd);
#endif

130
src/util.c Normal file
View File

@@ -0,0 +1,130 @@
/*
Copyright (c) 2010 Matthias Blankertz <matthias@blankertz.org>
This work is licensed under the Open Software License ("OSL") v. 3.0.
*/
#include "common.h"
#include "util.h"
// Get the filename from a path, i.e. a pointer to the character after the last / in path.
// If path contains no /, returns path.
const char *get_file_from_path(const char *path)
{
if(!path)
return NULL;
char *ret = strchr(path, '/');
if(ret == NULL)
return path;
return ret+1;
}
// Get the extension from the file name
const char *get_ext_from_file(const char *file)
{
if(!file)
return NULL;
char *ret = strchr(file, '.');
if(!ret)
return file+strlen(file);
if(ret == file)
return file+strlen(file); // return pointer to '\0';
return ret+1;
}
char *strip_ext_from_path(const char *path)
{
if(!path)
return NULL;
char *ret = strdup(path);
char *ext = strchr(ret, '.');
char *file = strchr(ret, '/');
if(!ext || (ext<file)) // no . or . not in last component
return ret;
*ext = '\0';
return ret;
}
// returns 1 if the file is a transcodeable audio file, 0 if it is not and a negative value if an error occured
int is_tc_audio_file(const char *path)
{
if(!path)
return -EINVAL;
struct stat st;
int ret = stat(path, &st);
if(ret == -1)
return -errno;
if(!S_ISREG(st.st_mode))
return 0;
const char *file = get_file_from_path(path);
if(!file || (file[0] == '\0'))
return 0;
const char *ext = get_ext_from_file(file);
if(!ext)
return 0;
if(strcmp(ext,options.tfmt) == 0)
return 0; // file already has right format
// TODO: proper detection code here
// for testing
if(strcmp(ext,"ogg") == 0)
return 1;
return 0;
}
// Get the path of the transcodeable audio file that
// generates 'path'. Returns NULL when there is no such
// file.
char *get_tc_source_name(const char *path)
{
if(!path)
return NULL;
if(strcmp(get_ext_from_file(get_file_from_path(path)), options.tfmt)!=0) {
return NULL;
}
char *path_no_ext = strip_ext_from_path(path);
if(!path_no_ext)
return NULL;
// TODO : search for transcodable files in path_no_ext.*
// for testing
char *ret;
if(asprintf(&ret, "%s.ogg", path_no_ext) < 0)
return NULL;
return ret;
}
// safely concatenate options.base and path
// the caller must free() the returned string
char *path_cat(const char *path)
{
if(!path)
return NULL;
char *ret = (char*)malloc(strlen(options.base)+strlen(path)+1);
if(!ret)
return NULL;
memcpy(ret, options.base, strlen(options.base));
memcpy(ret+strlen(options.base), path, strlen(path)+1);
return ret;
}

24
src/util.h Normal file
View File

@@ -0,0 +1,24 @@
/*
Copyright (c) 2010 Matthias Blankertz <matthias@blankertz.org>
This work is licensed under the Open Software License ("OSL") v. 3.0.
*/
#ifndef TCFS_UTIL_H
#define TCFS_UTIL_H 1
struct options {
char *base;
char *tfmt;
};
extern struct options options;
const char *get_file_from_path(const char *path);
const char *get_ext_from_file(const char *file);
char *strip_ext_from_path(const char *path);
int is_tc_audio_file(const char *path);
char *get_tc_source_name(const char *path);
char *path_cat(const char *path);
#endif