~ubuntu-branches/ubuntu/feisty/openbabel/feisty

« back to all changes in this revision

Viewing changes to src/dlhandler_unix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-05-14 19:46:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060514194601-h3j1wovvc42cigxl
Tags: 2.0.1-1
* New upstream release. (Closes: #341628)
* debian/patches/04_zipstream_fix.diff: Removed, applied upstream.
* debian/rules (DEB_MAKE_CHECK_TARGET): Readded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
dlhandler_unix.cpp - Dynamic loader for UNIX (handles file format shared obj.)
 
3
 
 
4
Copyright (C) 2004 by Chris Morley
 
5
Some portions Copyright (C) 2004-2005 by Geoffrey R. Hutchison
 
6
 
 
7
This file is part of the Open Babel project.
 
8
For more information, see <http://openbabel.sourceforge.net/>
 
9
 
 
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.
 
13
 
 
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
***********************************************************************/
 
19
 
 
20
#include "dlhandler.h"
 
21
#include "babelconfig.h"
 
22
 
 
23
#include <unistd.h>
 
24
#include <dirent.h>
 
25
#include <stdio.h>
 
26
#include <sys/stat.h>
 
27
#include <dlfcn.h>
 
28
 
 
29
#include <iostream>
 
30
 
 
31
using namespace std;
 
32
 
 
33
namespace OpenBabel {
 
34
OBAPI bool tokenize(vector<string> &, const char *, const char *);
 
35
}
 
36
 
 
37
#ifndef BUFF_SIZE
 
38
#define BUFF_SIZE 32768
 
39
#endif
 
40
 
 
41
//Globals for scandir()
 
42
 
 
43
int matchFiles (SCANDIR_CONST struct dirent *entry_p)
 
44
{
 
45
  return (strstr(entry_p->d_name, DLHandler::getFormatFilePattern()) != 0);
 
46
}
 
47
 
 
48
bool DLHandler::getConvDirectory(string& convPath)
 
49
{
 
50
  //Need to provide the directory from which this shared library was loaded.
 
51
  //This is the default directory for format shared library files.
 
52
  
 
53
  string testPath;
 
54
  testPath += OB_MODULE_PATH;
 
55
  convPath = testPath;
 
56
  
 
57
  return true;
 
58
}
 
59
 
 
60
int DLHandler::findFiles (std::vector <std::string>& file_list,const std::string& pattern, const std::string& path)
 
61
{
 
62
  string currentPath;
 
63
  vector<string> paths, vs;
 
64
  char buffer[BUFF_SIZE];
 
65
  
 
66
  if (!path.empty())
 
67
    paths.push_back(path);
 
68
  
 
69
  if (getenv("BABEL_LIBDIR") != NULL)
 
70
    {
 
71
      strncpy(buffer,getenv("BABEL_LIBDIR"), BUFF_SIZE - 1);
 
72
      // add a trailing NULL just in case
 
73
      buffer[BUFF_SIZE] = '\0';
 
74
      
 
75
      OpenBabel::tokenize(vs, buffer, "\r\n\t :");
 
76
      
 
77
      if (vs.size() > 0)
 
78
        {
 
79
          for (unsigned int i = 0; i < vs.size(); i++)
 
80
            paths.push_back(vs[i]);
 
81
        }
 
82
    }
 
83
  
 
84
  if (paths.size() == 0)
 
85
    paths.push_back("./"); // defaults to current directory
 
86
 
 
87
  /* Old method using scandir. Replaced with readdir (below) as for example
 
88
   * Solaris pre 10 doesn't implement scandir.
 
89
   
 
90
   struct dirent **entries_pp;
 
91
   int count;
 
92
   
 
93
   for (unsigned int i = 0; i < paths.size(); i++)
 
94
   {
 
95
   currentPath = paths[i];
 
96
   count = scandir (currentPath.c_str(), &entries_pp, SCANDIR_T matchFiles, NULL);
 
97
   
 
98
   for(int i=0; i<count; i++)
 
99
   {
 
100
   file_list.push_back(currentPath + getSeparator() + (entries_pp[i])->d_name);
 
101
   free(entries_pp[i]);
 
102
   }
 
103
   }
 
104
   
 
105
   if (entries_pp)
 
106
   free(entries_pp);
 
107
   * 
 
108
   */
 
109
  
 
110
  DIR *dp;
 
111
  struct dirent *entry;
 
112
  
 
113
  for (unsigned int i = 0; i < paths.size(); i++)
 
114
    {
 
115
      currentPath=paths[i];
 
116
      
 
117
      if ((dp = opendir(currentPath.c_str())) == NULL)
 
118
        continue; // no big deal, this path causes an error
 
119
      else 
 
120
        {
 
121
          while((entry = readdir(dp)) != NULL) 
 
122
            {
 
123
              if (matchFiles(entry) != 0) 
 
124
                file_list.push_back(currentPath + getSeparator() + (entry)->d_name);
 
125
            }
 
126
          closedir(dp); // calls free(dp) -- no memory leak
 
127
        }
 
128
    }
 
129
  
 
130
  if (file_list.size() == 0)
 
131
    return(-1); // error, didn't find any files at all
 
132
  return file_list.size();
 
133
}
 
134
 
 
135
bool DLHandler::openLib(const string& lib_name)
 
136
{
 
137
  return dlopen(lib_name.c_str(), RTLD_LAZY) != 0;
 
138
}
 
139
 
 
140
const char* DLHandler::getFormatFilePattern()
 
141
{
 
142
  return MODULE_EXTENSION;
 
143
}
 
144
 
 
145
char DLHandler::getSeparator()
 
146
{
 
147
  return '/';
 
148
}
 
149
 
 
150
//! \file dlhandler_unix.cpp
 
151
//! \brief Dynamic loader for UNIX (handles file format shared obj.)