Bug 6112: file.c list macro conversion (drumkilla)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7912 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Olle Johansson 20 years ago
parent 8d42b1eb74
commit efe79bd677

@ -50,6 +50,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/lock.h" #include "asterisk/lock.h"
#include "asterisk/app.h" #include "asterisk/app.h"
#include "asterisk/pbx.h" #include "asterisk/pbx.h"
#include "asterisk/linkedlists.h"
struct ast_format { struct ast_format {
/*! Name of format */ /*! Name of format */
@ -79,7 +80,7 @@ struct ast_format {
/*! Retrieve file comment */ /*! Retrieve file comment */
char * (*getcomment)(struct ast_filestream *); char * (*getcomment)(struct ast_filestream *);
/*! Link */ /*! Link */
struct ast_format *next; AST_LIST_ENTRY(ast_format) list;
}; };
struct ast_filestream { struct ast_filestream {
@ -99,9 +100,7 @@ struct ast_filestream {
struct ast_channel *owner; struct ast_channel *owner;
}; };
AST_MUTEX_DEFINE_STATIC(formatlock); static AST_LIST_HEAD_STATIC(formats, ast_format);
static struct ast_format *formats = NULL;
int ast_format_register(const char *name, const char *exts, int format, int ast_format_register(const char *name, const char *exts, int format,
struct ast_filestream * (*open)(FILE *f), struct ast_filestream * (*open)(FILE *f),
@ -115,23 +114,21 @@ int ast_format_register(const char *name, const char *exts, int format,
char * (*getcomment)(struct ast_filestream *)) char * (*getcomment)(struct ast_filestream *))
{ {
struct ast_format *tmp; struct ast_format *tmp;
if (ast_mutex_lock(&formatlock)) { if (AST_LIST_LOCK(&formats)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return -1; return -1;
} }
tmp = formats; AST_LIST_TRAVERSE(&formats, tmp, list) {
while(tmp) {
if (!strcasecmp(name, tmp->name)) { if (!strcasecmp(name, tmp->name)) {
ast_mutex_unlock(&formatlock); AST_LIST_UNLOCK(&formats);
ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", name); ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", name);
return -1; return -1;
} }
tmp = tmp->next;
} }
tmp = malloc(sizeof(struct ast_format)); tmp = malloc(sizeof(struct ast_format));
if (!tmp) { if (!tmp) {
ast_log(LOG_WARNING, "Out of memory\n"); ast_log(LOG_WARNING, "Out of memory\n");
ast_mutex_unlock(&formatlock); AST_LIST_UNLOCK(&formats);
return -1; return -1;
} }
ast_copy_string(tmp->name, name, sizeof(tmp->name)); ast_copy_string(tmp->name, name, sizeof(tmp->name));
@ -146,9 +143,8 @@ int ast_format_register(const char *name, const char *exts, int format,
tmp->close = close; tmp->close = close;
tmp->format = format; tmp->format = format;
tmp->getcomment = getcomment; tmp->getcomment = getcomment;
tmp->next = formats; AST_LIST_INSERT_HEAD(&formats, tmp, list);
formats = tmp; AST_LIST_UNLOCK(&formats);
ast_mutex_unlock(&formatlock);
if (option_verbose > 1) if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", name, exts); ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", name, exts);
return 0; return 0;
@ -156,29 +152,30 @@ int ast_format_register(const char *name, const char *exts, int format,
int ast_format_unregister(const char *name) int ast_format_unregister(const char *name)
{ {
struct ast_format *tmp, *tmpl = NULL; struct ast_format *tmp;
if (ast_mutex_lock(&formatlock)) { int res = -1;
if (AST_LIST_LOCK(&formats)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return -1; return -1;
} }
tmp = formats; AST_LIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
while(tmp) {
if (!strcasecmp(name, tmp->name)) { if (!strcasecmp(name, tmp->name)) {
if (tmpl) AST_LIST_REMOVE_CURRENT(&formats, list);
tmpl->next = tmp->next;
else
formats = tmp->next;
free(tmp); free(tmp);
ast_mutex_unlock(&formatlock); res = 0;
if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
return 0;
} }
tmpl = tmp;
tmp = tmp->next;
} }
ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name); AST_LIST_TRAVERSE_SAFE_END
return -1; AST_LIST_UNLOCK(&formats);
if (tmp) {
if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
} else
ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
return res;
} }
int ast_stopstream(struct ast_channel *tmp) int ast_stopstream(struct ast_channel *tmp)
@ -360,11 +357,11 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
if (action == ACTION_OPEN) if (action == ACTION_OPEN)
ret = -1; ret = -1;
/* Check for a specific format */ /* Check for a specific format */
if (ast_mutex_lock(&formatlock)) { if (AST_LIST_LOCK(&formats)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return res; return res;
} }
for (f = formats; f; f = f->next) { AST_LIST_TRAVERSE(&formats, f, list) {
if (!fmt || exts_compare(f->exts, fmt)) { if (!fmt || exts_compare(f->exts, fmt)) {
char *stringp=NULL; char *stringp=NULL;
exts = ast_strdupa(f->exts); exts = ast_strdupa(f->exts);
@ -446,7 +443,7 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
} }
} }
ast_mutex_unlock(&formatlock); AST_LIST_UNLOCK(&formats);
if ((action == ACTION_EXISTS) || (action == ACTION_OPEN)) if ((action == ACTION_EXISTS) || (action == ACTION_OPEN))
res = ret ? ret : -1; res = ret ? ret : -1;
return res; return res;
@ -836,12 +833,15 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
struct ast_filestream *fs = NULL; struct ast_filestream *fs = NULL;
char *fn; char *fn;
if (ast_mutex_lock(&formatlock)) { if (AST_LIST_LOCK(&formats)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return NULL; return NULL;
} }
for (f = formats; f && !fs; f = f->next) { AST_LIST_TRAVERSE(&formats, f, list) {
if (fs)
break;
if (!exts_compare(f->exts, type)) if (!exts_compare(f->exts, type))
continue; continue;
@ -868,7 +868,7 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
free(fn); free(fn);
} }
ast_mutex_unlock(&formatlock); AST_LIST_UNLOCK(&formats);
if (!fs) if (!fs)
ast_log(LOG_WARNING, "No such format '%s'\n", type); ast_log(LOG_WARNING, "No such format '%s'\n", type);
@ -886,7 +886,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
char *buf = NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
if (ast_mutex_lock(&formatlock)) { if (AST_LIST_LOCK(&formats)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return NULL; return NULL;
} }
@ -901,7 +901,10 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
myflags |= O_WRONLY | O_CREAT; myflags |= O_WRONLY | O_CREAT;
for (f = formats; f && !fs; f = f->next) { AST_LIST_TRAVERSE(&formats, f, list) {
if (fs)
break;
if (!exts_compare(f->exts, type)) if (!exts_compare(f->exts, type))
continue; continue;
@ -983,7 +986,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
free(fn); free(fn);
} }
ast_mutex_unlock(&formatlock); AST_LIST_UNLOCK(&formats);
if (!fs) if (!fs)
ast_log(LOG_WARNING, "No such format '%s'\n", type); ast_log(LOG_WARNING, "No such format '%s'\n", type);
@ -1259,18 +1262,16 @@ static int show_file_formats(int fd, int argc, char *argv[])
return RESULT_SHOWUSAGE; return RESULT_SHOWUSAGE;
ast_cli(fd, FORMAT, "Format", "Name", "Extensions"); ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
if (ast_mutex_lock(&formatlock)) { if (AST_LIST_LOCK(&formats)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return -1; return -1;
} }
f = formats; AST_LIST_TRAVERSE(&formats, f, list) {
while(f) {
ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts); ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
f = f->next;
count_fmt++; count_fmt++;
}; }
ast_mutex_unlock(&formatlock); AST_LIST_UNLOCK(&formats);
ast_cli(fd, "%d file formats registered.\n", count_fmt); ast_cli(fd, "%d file formats registered.\n", count_fmt);
return RESULT_SUCCESS; return RESULT_SUCCESS;
#undef FORMAT #undef FORMAT

Loading…
Cancel
Save