add the 'clean', 'clean-depend', and 'dist-clean' targets as .PHONY targets
[asterisk/asterisk.git] / image.c
diff --git a/image.c b/image.c
old mode 100755 (executable)
new mode 100644 (file)
index 804c2d8..ff67c03
--- a/image.c
+++ b/image.c
@@ -1,16 +1,32 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * Channel Management
- * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999 - 2006, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
  *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Image Management
+ *
+ * \author Mark Spencer <markster@digium.com> 
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "asterisk/translate.h"
 #include "asterisk/cli.h"
 #include "asterisk/lock.h"
-#include "asterisk.h"
 
-static struct ast_imager *list;
-AST_MUTEX_DEFINE_STATIC(listlock);
+static AST_LIST_HEAD_STATIC(imagers, ast_imager);
 
 int ast_image_register(struct ast_imager *img)
 {
        if (option_verbose > 1)
                ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
-       ast_mutex_lock(&listlock);
-       img->next = list;
-       list = img;
-       ast_mutex_unlock(&listlock);
+       AST_LIST_LOCK(&imagers);
+       AST_LIST_INSERT_HEAD(&imagers, img, list);
+       AST_LIST_UNLOCK(&imagers);
        return 0;
 }
 
 void ast_image_unregister(struct ast_imager *img)
 {
-       struct ast_imager *i, *prev = NULL;
-       ast_mutex_lock(&listlock);
-       i = list;
-       while(i) {
+       struct ast_imager *i;
+       
+       AST_LIST_LOCK(&imagers);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&imagers, i, list) {       
                if (i == img) {
-                       if (prev) 
-                               prev->next = i->next;
-                       else
-                               list = i->next;
+                       AST_LIST_REMOVE_CURRENT(&imagers, list);
                        break;
                }
-               prev = i;
-               i = i->next;
        }
-       ast_mutex_unlock(&listlock);
+       AST_LIST_TRAVERSE_SAFE_END
+       AST_LIST_UNLOCK(&imagers);
        if (i && (option_verbose > 1))
                ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
 }
@@ -85,22 +94,22 @@ static int file_exists(char *filename)
        return 0;
 }
 
-static void make_filename(char *buf, int len, char *filename, char *preflang, char *ext)
+static void make_filename(char *buf, int len, char *filename, const char *preflang, char *ext)
 {
        if (filename[0] == '/') {
-               if (preflang && strlen(preflang))
+               if (!ast_strlen_zero(preflang))
                        snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
                else
                        snprintf(buf, len, "%s.%s", filename, ext);
        } else {
-               if (preflang && strlen(preflang))
-                       snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_VAR_DIR, "images", filename, preflang, ext);
+               if (!ast_strlen_zero(preflang))
+                       snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext);
                else
-                       snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_VAR_DIR, "images", filename, ext);
+                       snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext);
        }
 }
 
-struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
+struct ast_frame *ast_read_image(char *filename, const char *preflang, int format)
 {
        struct ast_imager *i;
        char buf[256];
@@ -110,11 +119,9 @@ struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
        int fd;
        int len=0;
        struct ast_frame *f = NULL;
-#if 0 /* We need to have some sort of read-only lock */
-       ast_mutex_lock(&listlock);
-#endif 
-       i = list;
-       while(!found && i) {
+       
+       AST_LIST_LOCK(&imagers);
+       AST_LIST_TRAVERSE(&imagers, i, list) {
                if (i->format & format) {
                        char *stringp=NULL;
                        strncpy(tmp, i->exts, sizeof(tmp)-1);
@@ -134,8 +141,10 @@ struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
                                e = strsep(&stringp, "|");
                        }
                }
-               i = i->next;
+               if (found)
+                       break;  
        }
+
        if (found) {
                fd = open(buf, O_RDONLY);
                if (fd > -1) {
@@ -144,15 +153,15 @@ struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
                                lseek(fd, 0, SEEK_SET);
                                f = found->read_image(fd,len); 
                        } else
-                               ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
+                               ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
                        close(fd);
                } else
                        ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
        } else
                ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
-#if 0
-       ast_mutex_unlock(&listlock);
-#endif 
+       
+       AST_LIST_UNLOCK(&imagers);
+       
        return f;
 }
 
@@ -179,11 +188,8 @@ static int show_image_formats(int fd, int argc, char *argv[])
        if (argc != 3)
                return RESULT_SHOWUSAGE;
        ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
-       i = list;
-       while(i) {
+       AST_LIST_TRAVERSE(&imagers, i, list)
                ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
-               i = i->next;
-       };
        return RESULT_SUCCESS;
 }
 
@@ -199,7 +205,6 @@ struct ast_cli_entry show_images =
 
 int ast_image_init(void)
 {
-       ast_cli_register(&show_images);
-       return 0;
+       return ast_cli_register(&show_images);
 }