1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 /*******************************************
32 ******************************************/
34 #ifndef _OSL_FILE_PATH_HELPER_H_
35 #include "file_path_helper.h"
38 #ifndef _OSL_FILE_PATH_HELPER_HXX_
39 #include "file_path_helper.hxx"
42 #ifndef _OSL_UUNXAPI_HXX_
43 #include "uunxapi.hxx"
46 #ifndef _OSL_DIAGNOSE_H_
47 #include <osl/diagnose.h>
50 #ifndef _RTL_USTRING_HXX_
51 #include <rtl/ustring.hxx>
54 /*******************************************
56 ******************************************/
58 const sal_Unicode FPH_CHAR_PATH_SEPARATOR
= (sal_Unicode
)'/';
59 const sal_Unicode FPH_CHAR_DOT
= (sal_Unicode
)'.';
60 const sal_Unicode FPH_CHAR_COLON
= (sal_Unicode
)':';
62 inline const rtl::OUString
FPH_PATH_SEPARATOR()
63 { return rtl::OUString(FPH_CHAR_PATH_SEPARATOR
); }
64 inline const rtl::OUString
FPH_LOCAL_DIR_ENTRY()
65 { return rtl::OUString(FPH_CHAR_PATH_SEPARATOR
); }
66 inline const rtl::OUString
FPH_PARENT_DIR_ENTRY()
67 { return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("..")); }
69 /*******************************************
70 * osl_systemPathRemoveSeparator
71 ******************************************/
73 void SAL_CALL
osl_systemPathRemoveSeparator(rtl_uString
* pustrPath
)
75 OSL_PRECOND(0 != pustrPath
, "osl_systemPathRemoveSeparator: Invalid parameter");
78 // maybe there are more than one separator at end
79 // so we run in a loop
80 while ((pustrPath
->length
> 1) && (FPH_CHAR_PATH_SEPARATOR
== pustrPath
->buffer
[pustrPath
->length
- 1]))
83 pustrPath
->buffer
[pustrPath
->length
] = (sal_Unicode
)'\0';
86 OSL_POSTCOND((0 == pustrPath
->length
) || (1 == pustrPath
->length
) || \
87 (pustrPath
->length
> 1 && pustrPath
->buffer
[pustrPath
->length
- 1] != FPH_CHAR_PATH_SEPARATOR
), \
88 "osl_systemPathRemoveSeparator: Post condition failed");
92 /*******************************************
93 osl_systemPathEnsureSeparator
94 ******************************************/
96 void SAL_CALL
osl_systemPathEnsureSeparator(rtl_uString
** ppustrPath
)
98 OSL_PRECOND((0 != ppustrPath
) && (0 != *ppustrPath
), "osl_systemPathEnsureSeparator: Invalid parameter");
99 if ((0 != ppustrPath
) && (0 != *ppustrPath
))
101 rtl::OUString
path(*ppustrPath
);
102 sal_Int32 lp
= path
.getLength();
103 sal_Int32 i
= path
.lastIndexOf(FPH_CHAR_PATH_SEPARATOR
);
105 if ((lp
> 1 && i
!= (lp
- 1)) || ((lp
< 2) && i
< 0))
107 path
+= FPH_PATH_SEPARATOR();
108 rtl_uString_assign(ppustrPath
, path
.pData
);
111 OSL_POSTCOND(path
.lastIndexOf(FPH_CHAR_PATH_SEPARATOR
) == (path
.getLength() - 1), \
112 "osl_systemPathEnsureSeparator: Post condition failed");
116 /*******************************************
117 * osl_systemPathIsRelativePath
118 ******************************************/
120 sal_Bool SAL_CALL
osl_systemPathIsRelativePath(const rtl_uString
* pustrPath
)
122 OSL_PRECOND(0 != pustrPath
, "osl_systemPathIsRelativePath: Invalid parameter");
123 return ((0 == pustrPath
) || (0 == pustrPath
->length
) || (pustrPath
->buffer
[0] != FPH_CHAR_PATH_SEPARATOR
));
126 /******************************************
127 osl_systemPathMakeAbsolutePath
128 *****************************************/
130 void SAL_CALL
osl_systemPathMakeAbsolutePath(
131 const rtl_uString
* pustrBasePath
,
132 const rtl_uString
* pustrRelPath
,
133 rtl_uString
** ppustrAbsolutePath
)
135 rtl::OUString
base(rtl_uString_getStr(const_cast<rtl_uString
*>(pustrBasePath
)));
136 rtl::OUString
rel(const_cast<rtl_uString
*>(pustrRelPath
));
139 osl_systemPathEnsureSeparator(&base
.pData
);
143 rtl_uString_acquire(base
.pData
);
144 *ppustrAbsolutePath
= base
.pData
;
148 /*******************************************
149 osl_systemPathGetFileOrLastDirectoryPart
150 ******************************************/
152 void SAL_CALL
osl_systemPathGetFileNameOrLastDirectoryPart(
153 const rtl_uString
* pustrPath
,
154 rtl_uString
** ppustrFileNameOrLastDirPart
)
156 OSL_PRECOND(pustrPath
&& ppustrFileNameOrLastDirPart
, \
157 "osl_systemPathGetFileNameOrLastDirectoryPart: Invalid parameter");
159 rtl::OUString
path(const_cast<rtl_uString
*>(pustrPath
));
161 osl_systemPathRemoveSeparator(path
.pData
);
163 rtl::OUString last_part
;
165 if (path
.getLength() > 1 || (1 == path
.getLength() && *path
.getStr() != FPH_CHAR_PATH_SEPARATOR
))
167 sal_Int32 idx_ps
= path
.lastIndexOf(FPH_CHAR_PATH_SEPARATOR
);
168 idx_ps
++; // always right to increment by one even if idx_ps == -1!
169 last_part
= rtl::OUString(path
.getStr() + idx_ps
);
171 rtl_uString_assign(ppustrFileNameOrLastDirPart
, last_part
.pData
);
175 /********************************************
176 osl_systemPathIsHiddenFileOrDirectoryEntry
177 *********************************************/
179 sal_Bool SAL_CALL
osl_systemPathIsHiddenFileOrDirectoryEntry(
180 const rtl_uString
* pustrPath
)
182 OSL_PRECOND(0 != pustrPath
, "osl_systemPathIsHiddenFileOrDirectoryEntry: Invalid parameter");
183 if ((0 == pustrPath
) || (0 == pustrPath
->length
))
187 osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath
, &fdp
.pData
);
189 return ((fdp
.pData
->length
> 0) &&
190 (fdp
.pData
->buffer
[0] == FPH_CHAR_DOT
) &&
191 !osl_systemPathIsLocalOrParentDirectoryEntry(fdp
.pData
));
195 /************************************************
196 osl_systemPathIsLocalOrParentDirectoryEntry
197 ************************************************/
199 sal_Bool SAL_CALL
osl_systemPathIsLocalOrParentDirectoryEntry(
200 const rtl_uString
* pustrPath
)
202 OSL_PRECOND(pustrPath
, "osl_systemPathIsLocalOrParentDirectoryEntry: Invalid parameter");
204 rtl::OUString dirent
;
206 osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath
, &dirent
.pData
);
209 (dirent
== FPH_LOCAL_DIR_ENTRY()) ||
210 (dirent
== FPH_PARENT_DIR_ENTRY())
214 /***********************************************
215 Simple iterator for a path list separated by
216 the specified character
217 **********************************************/
219 class path_list_iterator
223 /******************************************
226 after construction get_current_item
227 returns the first path in list, no need
229 *****************************************/
230 path_list_iterator(const rtl::OUString
& path_list
, sal_Unicode list_separator
= FPH_CHAR_COLON
) :
231 m_path_list(path_list
),
232 m_end(m_path_list
.getStr() + m_path_list
.getLength() + 1),
233 m_separator(list_separator
)
238 /******************************************
240 *****************************************/
243 m_path_segment_begin
= m_path_segment_end
= m_path_list
.getStr();
247 /******************************************
248 move the iterator to the next position
249 *****************************************/
252 OSL_PRECOND(!done(), "path_list_iterator: Already done!");
254 m_path_segment_begin
= ++m_path_segment_end
;
258 /******************************************
260 *****************************************/
263 return (m_path_segment_end
>= m_end
);
266 /******************************************
267 return the current item
268 *****************************************/
269 rtl::OUString
get_current_item() const
271 return rtl::OUString(
272 m_path_segment_begin
,
273 (m_path_segment_end
- m_path_segment_begin
));
278 /******************************************
279 move m_path_end to the next separator or
280 to the edn of the string
281 *****************************************/
284 while (!done() && *m_path_segment_end
&& (*m_path_segment_end
!= m_separator
))
285 ++m_path_segment_end
;
287 OSL_ASSERT(m_path_segment_end
<= m_end
);
291 rtl::OUString m_path_list
;
292 const sal_Unicode
* m_end
;
293 const sal_Unicode m_separator
;
294 const sal_Unicode
* m_path_segment_begin
;
295 const sal_Unicode
* m_path_segment_end
;
297 // prevent copy and assignment
299 /******************************************
301 remember: do not simply copy m_path_begin
302 and m_path_end because they point to
303 the memory of other.m_path_list!
304 *****************************************/
305 path_list_iterator(const path_list_iterator
& other
);
307 /******************************************
309 remember: do not simply copy m_path_begin
310 and m_path_end because they point to
311 the memory of other.m_path_list!
312 *****************************************/
313 path_list_iterator
& operator=(const path_list_iterator
& other
);
316 /************************************************
318 ***********************************************/
320 sal_Bool SAL_CALL
osl_searchPath(
321 const rtl_uString
* pustrFilePath
,
322 const rtl_uString
* pustrSearchPathList
,
323 rtl_uString
** ppustrPathFound
)
325 OSL_PRECOND(pustrFilePath
&& pustrSearchPathList
&& ppustrPathFound
, "osl_searchPath: Invalid parameter");
328 rtl::OUString
fp(const_cast<rtl_uString
*>(pustrFilePath
));
329 rtl::OUString pl
= rtl::OUString(const_cast<rtl_uString
*>(pustrSearchPathList
));
330 path_list_iterator
pli(pl
);
334 rtl::OUString p
= pli
.get_current_item();
335 osl::systemPathEnsureSeparator(p
);
338 if (osl::access(p
, F_OK
) > -1)
341 rtl_uString_assign(ppustrPathFound
, p
.pData
);
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */