File: modulemgr.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (209 lines) | stat: -rw-r--r-- 4,338 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * XPLC - Cross-Platform Lightweight Components
 * Copyright (C) 2002-2004, Net Integration Technologies, Inc.
 * Copyright (C) 2002-2004, Pierre Phaneuf
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <assert.h>
#include "modulemgr.h"
#include <xplc/IModuleLoader.h>

#include "config.h"

#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif

#if !defined(WIN32)
# if HAVE_DIRENT_H
#  include <dirent.h>
#  define NAMLEN(dirent) strlen((dirent)->d_name)
# else
#  define dirent direct
#  define NAMLEN(dirent) (dirent)->d_namlen
#  if HAVE_SYS_NDIR_H
#   include <sys/ndir.h>
#  endif
#  if HAVE_SYS_DIR_H
#   include <sys/dir.h>
#  endif
#  if HAVE_NDIR_H
#   include <ndir.h>
#  endif
# endif
#else
# include <io.h>
#endif

#include <stdio.h>

UUID_MAP_BEGIN(ModuleManagerFactory)
  UUID_MAP_ENTRY(IObject)
  UUID_MAP_ENTRY(IModuleManagerFactory)
  UUID_MAP_END

UUID_MAP_BEGIN(ModuleManager)
  UUID_MAP_ENTRY(IObject)
  UUID_MAP_ENTRY(IServiceHandler)
  UUID_MAP_END

struct ModuleNode {
  ModuleNode* next;
  IModule* module;
  ModuleNode(IModule* aModule, ModuleNode* aNext):
    next(aNext), module(aModule) {
    assert(module);
  }
  ~ModuleNode() {
    if(module)
      module->release();
  }
};

#if defined(SOLARIS) || defined(MACOS)
#define PATH_MAX 4096
#endif

IServiceHandler* ModuleManagerFactory::createModuleManager(const char* directory) {
#if !defined(WIN32)
  DIR* dir;
  struct dirent* ent;
  char fname[PATH_MAX];
  IServiceManager* servmgr = XPLC_getServiceManager();
  IModuleLoader* loader;
  ModuleNode* modules = 0;

  if(!servmgr)
    return 0;

  loader = mutate<IModuleLoader>(servmgr->getObject(XPLC_moduleLoader));
  servmgr->release();
  if(!loader)
    return 0;

  dir = opendir(directory);
  if(!dir) {
    loader->release();
    return 0;
  }

  rewinddir(dir);
  while((ent = readdir(dir))) {
    IModule* module;

    snprintf(fname, PATH_MAX, "%s/%s", directory, ent->d_name);

    module = loader->loadModule(fname);
    if(module) {
      ModuleNode* node = new ModuleNode(module, modules);

      if(node)
        modules = node;
    }
  }

  loader->release();

  closedir(dir);

  return new ModuleManager(modules);

#else

  intptr_t dir;
  _finddata_t ent;
  char fname[4096];
  char pattern[4096];
  IServiceManager* servmgr = XPLC_getServiceManager();
  IModuleLoader* loader;
  ModuleNode* modules = 0;

  if(!servmgr)
    return 0;

  loader = mutate<IModuleLoader>(servmgr->getObject(XPLC_moduleLoader));
  servmgr->release();
  if(!loader)
    return 0;

  snprintf(pattern, sizeof(pattern), "%s/*.*", directory);

  dir = _findfirst(pattern, &ent);

  if(!dir) {
    loader->release();
    return 0;
  }

  do {
    IModule* module;

    _snprintf(fname, sizeof(fname), "%s/%s", directory, ent.name);

    module = loader->loadModule(fname);
    if(module) {
      ModuleNode* node = new ModuleNode(module, modules);

      if(node)
        modules = node;
    }
  } while(_findnext(dir, &ent) == 0);

  loader->release();

  _findclose(dir);

  return new ModuleManager(modules);
#endif
}

ModuleManager::ModuleManager(ModuleNode* aModules):
  modules(aModules) {
}

IObject* ModuleManager::getObject(const UUID& cid) {
  ModuleNode* node = modules;

  while(node) {
    IObject* obj = node->module->getObject(cid);

    if(obj)
      return obj;

    node = node->next;
  }

  return 0;
}

ModuleManager::~ModuleManager() {
  ModuleNode* node = modules;

  while(node) {
    ModuleNode* next = node->next;

    delete node;

    node = next;
  }
}