update dev300-m58
[ooovba.git] / testshl2 / source / dynamicregister.cxx
blob95af83dfe8c9f71c685a0dbdbfa72f92d808d8d9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dynamicregister.cxx,v $
10 * $Revision: 1.11 $
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"
41 #include <unistd.h>
43 #if defined(WIN32)
44 #include <direct.h> /* w.g. _chdir() */
45 #endif
47 namespace fixes
49 void changedir(const char* _sPath)
51 #if defined(WIN32)
52 // chdir(_sPath) is marked depricated since Visual C++ 2005
53 // use _chdir instead
54 ::_chdir(_sPath);
55 #else
56 ::chdir(_sPath);
57 #endif
61 // -----------------------------------------------------------------------------
63 DynamicLibraryHelper::DynamicLibraryHelper(rtl::OUString const& _suDLLName, GetOpt & _aOptions)
64 :m_pModule(new ::osl::Module()),
65 m_suDLLName(_suDLLName),
66 m_aOptions(_aOptions)
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);
77 if (nPos != -1)
79 m_suAbsolutePath = m_suAbsolutePathFile.copy(0, nPos);
80 m_suFilename = m_suAbsolutePathFile.copy(nPos + 1);
82 else
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());
87 exit(1);
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();
100 else
103 // PLEASE DON'T CHANGE THIS STUPID STRUCTURE, JUST ADD YOUR ENVIRONMENT
104 #if defined(LINUX)
105 loadLibraryFromAbsolutePath();
106 // will fail if load local
108 #elif defined(SOLARIS)
109 loadLibraryFromAbsolutePath();
110 // will also be right if load local
112 #elif defined(WIN32)
113 loadLibraryFromAbsolutePath();
114 // will fail if load local
116 #elif defined(MACOSX)
117 loadLibraryFromLocalPath();
118 // will fail if local absolute
119 #else
120 // default is load absolute
121 loadLibraryFromAbsolutePath();
122 #endif
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());
134 // check filename
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()
155 sal_Int32 nPos;
156 rtl::OUString suPathSeparator = rtl::OUString( rtl::OUString::createFromAscii("/"));
157 #if defined(WIN32)
158 suPathSeparator = rtl::OUString( rtl::OUString::createFromAscii("\\"));
159 #endif
160 rtl::OUString suSystemPathFile;
161 osl::FileBase::getSystemPathFromFileURL(m_suAbsolutePathFile, suSystemPathFile);
163 nPos = suSystemPathFile.lastIndexOf(suPathSeparator);
164 rtl::OUString suCurrentDirPath;
165 if (nPos != -1)
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.");
204 delete m_pModule;
205 if (getOptions().hasOpt("-verbose"))
207 fprintf(stderr, " [done].\n");