1
/**********************************************************************
2
dlhandler_unix.cpp - Dynamic loader for UNIX (handles file format shared obj.)
4
Copyright (C) 2004 by Chris Morley
5
Some portions Copyright (C) 2004-2005 by Geoffrey R. Hutchison
7
This file is part of the Open Babel project.
8
For more information, see <http://openbabel.sourceforge.net/>
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation version 2 of the License.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
18
***********************************************************************/
20
#include "dlhandler.h"
21
#include "babelconfig.h"
34
OBAPI bool tokenize(vector<string> &, const char *, const char *);
38
#define BUFF_SIZE 32768
41
//Globals for scandir()
43
int matchFiles (SCANDIR_CONST struct dirent *entry_p)
45
return (strstr(entry_p->d_name, DLHandler::getFormatFilePattern()) != 0);
48
bool DLHandler::getConvDirectory(string& convPath)
50
//Need to provide the directory from which this shared library was loaded.
51
//This is the default directory for format shared library files.
54
testPath += OB_MODULE_PATH;
60
int DLHandler::findFiles (std::vector <std::string>& file_list,const std::string& pattern, const std::string& path)
63
vector<string> paths, vs;
64
char buffer[BUFF_SIZE];
67
paths.push_back(path);
69
if (getenv("BABEL_LIBDIR") != NULL)
71
strncpy(buffer,getenv("BABEL_LIBDIR"), BUFF_SIZE - 1);
72
// add a trailing NULL just in case
73
buffer[BUFF_SIZE] = '\0';
75
OpenBabel::tokenize(vs, buffer, "\r\n\t :");
79
for (unsigned int i = 0; i < vs.size(); i++)
80
paths.push_back(vs[i]);
84
if (paths.size() == 0)
85
paths.push_back("./"); // defaults to current directory
87
/* Old method using scandir. Replaced with readdir (below) as for example
88
* Solaris pre 10 doesn't implement scandir.
90
struct dirent **entries_pp;
93
for (unsigned int i = 0; i < paths.size(); i++)
95
currentPath = paths[i];
96
count = scandir (currentPath.c_str(), &entries_pp, SCANDIR_T matchFiles, NULL);
98
for(int i=0; i<count; i++)
100
file_list.push_back(currentPath + getSeparator() + (entries_pp[i])->d_name);
111
struct dirent *entry;
113
for (unsigned int i = 0; i < paths.size(); i++)
115
currentPath=paths[i];
117
if ((dp = opendir(currentPath.c_str())) == NULL)
118
continue; // no big deal, this path causes an error
121
while((entry = readdir(dp)) != NULL)
123
if (matchFiles(entry) != 0)
124
file_list.push_back(currentPath + getSeparator() + (entry)->d_name);
126
closedir(dp); // calls free(dp) -- no memory leak
130
if (file_list.size() == 0)
131
return(-1); // error, didn't find any files at all
132
return file_list.size();
135
bool DLHandler::openLib(const string& lib_name)
137
return dlopen(lib_name.c_str(), RTLD_LAZY) != 0;
140
const char* DLHandler::getFormatFilePattern()
142
return MODULE_EXTENSION;
145
char DLHandler::getSeparator()
150
//! \file dlhandler_unix.cpp
151
//! \brief Dynamic loader for UNIX (handles file format shared obj.)