gst-indent
[platform/upstream/gst-plugins-good.git] / ext / ladspa / search.c
1 /* search.c
2
3    Free software by Richard W.E. Furse. Do with as you will. No
4    warranty. */
5
6 /*****************************************************************************/
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <dirent.h>
13 #include <dlfcn.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <glib.h>
20
21 /*****************************************************************************/
22
23 #include "ladspa.h"
24 #include "utils.h"
25
26 /*****************************************************************************/
27
28 /* Search just the one directory. */
29 static void
30     LADSPADirectoryPluginSearch
31     (const char *pcDirectory,
32     LADSPAPluginSearchCallbackFunction fCallbackFunction)
33 {
34
35   char *pcFilename;
36   DIR *psDirectory;
37   LADSPA_Descriptor_Function fDescriptorFunction;
38   long lDirLength;
39   long iNeedSlash;
40   struct dirent *psDirectoryEntry;
41   void *pvPluginHandle;
42
43   lDirLength = strlen (pcDirectory);
44   if (!lDirLength)
45     return;
46   if (pcDirectory[lDirLength - 1] == '/')
47     iNeedSlash = 0;
48   else
49     iNeedSlash = 1;
50
51   psDirectory = opendir (pcDirectory);
52   if (!psDirectory)
53     return;
54
55   while (1) {
56
57     psDirectoryEntry = readdir (psDirectory);
58     if (!psDirectoryEntry) {
59       closedir (psDirectory);
60       return;
61     }
62
63     pcFilename = malloc (lDirLength + strlen (psDirectoryEntry->d_name)
64         + 1 + iNeedSlash);
65     strcpy (pcFilename, pcDirectory);
66     if (iNeedSlash)
67       strcat (pcFilename, "/");
68     strcat (pcFilename, psDirectoryEntry->d_name);
69
70     pvPluginHandle = dlopen (pcFilename, RTLD_LAZY);
71     if (pvPluginHandle) {
72       /* This is a file and the file is a shared library! */
73
74       dlerror ();
75       fDescriptorFunction
76           = (LADSPA_Descriptor_Function) dlsym (pvPluginHandle,
77           "ladspa_descriptor");
78       if (dlerror () == NULL && fDescriptorFunction) {
79         /* We've successfully found a ladspa_descriptor function. Pass
80            it to the callback function. */
81         fCallbackFunction (pcFilename, pvPluginHandle, fDescriptorFunction);
82       } else {
83         /* It was a library, but not a LADSPA one. Unload it. */
84         dlclose (pcFilename);
85       }
86     }
87     free (pcFilename);
88   }
89 }
90
91 /*****************************************************************************/
92
93 void
94 LADSPAPluginSearch (LADSPAPluginSearchCallbackFunction fCallbackFunction)
95 {
96
97   char *pcBuffer;
98   const char *pcEnd;
99   const char *pcLADSPAPath;
100   const char *pcStart;
101
102   /* thomasvs: I'm sorry, but I'm going to add glib stuff here.
103    * I'm appending logical values for LADSPA_PATH here
104    */
105
106   pcLADSPAPath = g_strdup_printf ("%s:/usr/lib/ladspa:/usr/local/lib/ladspa",
107       getenv ("LADSPA_PATH"));
108
109   if (!pcLADSPAPath) {
110 /*    fprintf(stderr, */
111 /*          "Warning: You do not have a LADSPA_PATH " */
112 /*          "environment variable set.\n"); */
113     return;
114   }
115
116   pcStart = pcLADSPAPath;
117   while (*pcStart != '\0') {
118     pcEnd = pcStart;
119     while (*pcEnd != ':' && *pcEnd != '\0')
120       pcEnd++;
121
122     pcBuffer = malloc (1 + pcEnd - pcStart);
123     if (pcEnd > pcStart)
124       strncpy (pcBuffer, pcStart, pcEnd - pcStart);
125     pcBuffer[pcEnd - pcStart] = '\0';
126
127     LADSPADirectoryPluginSearch (pcBuffer, fCallbackFunction);
128     free (pcBuffer);
129
130     pcStart = pcEnd;
131     if (*pcStart == ':')
132       pcStart++;
133   }
134 }
135
136 /*****************************************************************************/
137
138 /* EOF */