Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / fsutils.cc @ 636cbdd5

History | View | Annotate | Download (2.45 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  FSUTILS.CC - part of
3
//                     OMNeT++/OMNEST
4
//             Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#include "platmisc.h"
19
#include "fileutil.h"
20
#include "fsutils.h"
21
#include "onstartup.h"
22
#include "cexception.h"
23
24
NAMESPACE_BEGIN
25
26
std::string makeLibFileName(const char *libname, const char *prefix, const char *suffix)
27
{
28
     bool hasDir = strchr(libname, '/')!=NULL || strchr(libname, '\\')!=NULL;
29
     std::string dir, fileNameOnly;
30
     splitFileName(libname, dir, fileNameOnly);
31
     bool hasExt = strchr(fileNameOnly.c_str(), '.')!=NULL;
32
33
     std::string libFileName;
34
     if (hasExt)
35
         libFileName = libname;  // when an exact file name is given, leave it unchanged
36
     else if (hasDir)
37
         libFileName = dir + "/" + prefix + fileNameOnly + suffix;
38
     else
39
         libFileName = std::string(prefix) + fileNameOnly + suffix;
40
     return libFileName;
41
}
42
43
static bool opp_loadlibrary(const char *libname)
44
{
45
#if HAVE_DLOPEN
46
     std::string libFileName = makeLibFileName(libname, "lib", SHARED_LIB_SUFFIX);
47
     if (!dlopen(libFileName.c_str(), RTLD_NOW|RTLD_GLOBAL))
48
         throw std::runtime_error(std::string("Cannot load library '")+libFileName+"': "+dlerror());
49
     return true;
50
51
#elif defined(_WIN32)
52
# ifdef __GNUC__
53
     std::string libFileName = makeLibFileName(libname, "lib", ".dll"); // MinGW
54
# else
55
     std::string libFileName = makeLibFileName(libname, "", ".dll"); // Visual C++
56
# endif
57
     if (!LoadLibrary((char *)libFileName.c_str()))
58
         throw std::runtime_error(std::string("Cannot load library '")+libFileName+"': "+opp_getWindowsError(GetLastError()));
59
     return true;
60
61
#else
62
     throw std::runtime_error(std::string("Cannot load library '")+libname+"': dlopen() syscall not available");
63
#endif
64
}
65
66
void loadExtensionLibrary(const char *lib)
67
{
68
    try {
69
        opp_loadlibrary(lib);
70
        ExecuteOnStartup::executeAll();
71
    }
72
    catch (std::runtime_error e) {
73
        throw cRuntimeError(e.what());
74
    }
75
}
76
77
NAMESPACE_END