1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dynamicregister.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_testshl2.hxx"
34 #include "testshl/dynamicregister.hxx"
35 #include <osl/process.h>
36 // #include <osl/mutex.hxx>
37 #include <rtl/string.hxx>
38 #include <rtl/ustring.hxx>
39 #include "testshl/filehelper.hxx"
44 #include <direct.h> /* w.g. _chdir() */
49 void changedir(const char* _sPath
)
52 // chdir(_sPath) is marked depricated since Visual C++ 2005
61 // -----------------------------------------------------------------------------
63 DynamicLibraryHelper::DynamicLibraryHelper(rtl::OUString
const& _suDLLName
, GetOpt
& _aOptions
)
64 :m_pModule(new ::osl::Module()),
65 m_suDLLName(_suDLLName
),
68 // create and load the module (shared library)
69 m_suAbsolutePathFile
= FileHelper::convertPath( _suDLLName
);
71 // due to some problems on mac OS
72 // we split the absolute pathname to path and filename
73 // change to the path and load the filename direct
74 // then change back to the old path.
75 rtl::OUString suPathSeparator
= rtl::OUString( rtl::OUString::createFromAscii("/"));
76 sal_Int32 nPos
= m_suAbsolutePathFile
.lastIndexOf(suPathSeparator
);
79 m_suAbsolutePath
= m_suAbsolutePathFile
.copy(0, nPos
);
80 m_suFilename
= m_suAbsolutePathFile
.copy(nPos
+ 1);
84 // Should never happen.
85 rtl::OString sPath
= rtl::OUStringToOString(m_suAbsolutePathFile
, RTL_TEXTENCODING_ASCII_US
);
86 fprintf(stderr
, "There is a problem with path '%s'.\n", sPath
.getStr());
90 if (getOptions().hasOpt("-absolutepath"))
92 fprintf(stderr
, "Hint: Use absolute path to load test library.\n");
93 loadLibraryFromAbsolutePath();
95 else if (getOptions().hasOpt("-localpath"))
97 fprintf(stderr
, "Hint: make a chdir() to the test library, then try to load the test library without given path.\n");
98 loadLibraryFromLocalPath();
103 // PLEASE DON'T CHANGE THIS STUPID STRUCTURE, JUST ADD YOUR ENVIRONMENT
105 loadLibraryFromAbsolutePath();
106 // will fail if load local
108 #elif defined(SOLARIS)
109 loadLibraryFromAbsolutePath();
110 // will also be right if load local
113 loadLibraryFromAbsolutePath();
114 // will fail if load local
116 #elif defined(MACOSX)
117 loadLibraryFromLocalPath();
118 // will fail if local absolute
120 // default is load absolute
121 loadLibraryFromAbsolutePath();
126 void DynamicLibraryHelper::showFilenameIfVerbose()
128 if (getOptions().hasOpt("-verbose"))
130 rtl::OString sFilename
= rtl::OUStringToOString(m_suFilename
, RTL_TEXTENCODING_ASCII_US
);
131 rtl::OString sPath
= rtl::OUStringToOString(m_suAbsolutePath
, RTL_TEXTENCODING_ASCII_US
);
132 fprintf(stderr
, "Try to load '%s' from '%s'.\n", sFilename
.getStr(), sPath
.getStr());
138 void DynamicLibraryHelper::realLoadLibrary(rtl::OUString
const& _suLibToLoad
)
140 if (! m_pModule
->load(_suLibToLoad
, SAL_LOADMODULE_LAZY
| SAL_LOADMODULE_GLOBAL
))
142 rtl::OString sDLLName
= rtl::OUStringToOString(m_suDLLName
, RTL_TEXTENCODING_ASCII_US
);
143 fprintf(stderr
, "warning: Can't load module '%s'.\n", sDLLName
.getStr());
147 void DynamicLibraryHelper::loadLibraryFromAbsolutePath()
149 showFilenameIfVerbose();
150 realLoadLibrary(m_suAbsolutePathFile
);
153 void DynamicLibraryHelper::loadLibraryFromLocalPath()
156 rtl::OUString suPathSeparator
= rtl::OUString( rtl::OUString::createFromAscii("/"));
158 suPathSeparator
= rtl::OUString( rtl::OUString::createFromAscii("\\"));
160 rtl::OUString suSystemPathFile
;
161 osl::FileBase::getSystemPathFromFileURL(m_suAbsolutePathFile
, suSystemPathFile
);
163 nPos
= suSystemPathFile
.lastIndexOf(suPathSeparator
);
164 rtl::OUString suCurrentDirPath
;
167 // the filename only, no '/' in the path
168 rtl::OUString suNewPath
= suSystemPathFile
.copy(0, nPos
);
169 if (suNewPath
.getLength() > 0)
171 rtl::OString sPath
= rtl::OUStringToOString(suNewPath
, RTL_TEXTENCODING_ASCII_US
);
172 osl_getProcessWorkingDir( &suCurrentDirPath
.pData
);
174 fixes::changedir(sPath
.getStr());
176 // curNewDirPath should be suPath, small self test
177 rtl::OUString curNewDirPath
;
178 osl_getProcessWorkingDir( &curNewDirPath
.pData
);
179 if (! curNewDirPath
.equals(m_suAbsolutePath
))
181 fprintf(stderr
, "There is a problem with path '%s'.\n", sPath
.getStr());
186 showFilenameIfVerbose();
187 realLoadLibrary(m_suFilename
);
189 // change back to old directory
190 if (suCurrentDirPath
.getLength() > 0)
192 rtl::OString sCurrentDirPath
= rtl::OUStringToOString(suCurrentDirPath
, RTL_TEXTENCODING_ASCII_US
);
193 fixes::changedir(sCurrentDirPath
.getStr());
197 DynamicLibraryHelper::~DynamicLibraryHelper()
199 if (getOptions().hasOpt("-verbose"))
201 fprintf(stderr
, "Dtor DynamicLibraryHelper.\n");
202 fprintf(stderr
, "Delete loaded module.");
205 if (getOptions().hasOpt("-verbose"))
207 fprintf(stderr
, " [done].\n");