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: file_path_helper.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 /*******************************************
33 ******************************************/
35 #ifndef _OSL_THREAD_H_
36 #include "osl/thread.h"
39 #ifndef _OSL_FILE_PATH_HELPER_H_
40 #include "file_path_helper.h"
43 #ifndef _OSL_FILE_PATH_HELPER_HXX_
44 #include "file_path_helper.hxx"
47 #ifndef _OSL_UUNXAPI_HXX_
48 #include "uunxapi.hxx"
51 #ifndef _OSL_DIAGNOSE_H_
52 #include <osl/diagnose.h>
55 #ifndef _RTL_USTRING_HXX_
56 #include <rtl/ustring.hxx>
59 /*******************************************
61 ******************************************/
63 const sal_Unicode FPH_CHAR_PATH_SEPARATOR
= (sal_Unicode
)'\\';
64 const sal_Unicode FPH_CHAR_DOT
= (sal_Unicode
)'.';
65 const sal_Unicode FPH_CHAR_COLON
= (sal_Unicode
)':';
67 inline const rtl::OUString
FPH_PATH_SEPARATOR()
68 { return rtl::OUString::createFromAscii("\\"); }
69 inline const rtl::OUString
FPH_LOCAL_DIR_ENTRY()
70 { return rtl::OUString::createFromAscii("."); }
71 inline const rtl::OUString
FPH_PARENT_DIR_ENTRY()
72 { return rtl::OUString::createFromAscii(".."); }
74 /*******************************************
75 * osl_systemPathRemoveSeparator
76 ******************************************/
78 void SAL_CALL
osl_systemPathRemoveSeparator(rtl_uString
* pustrPath
)
80 OSL_PRECOND(pustrPath
, "osl_systemPathRemoveSeparator: Invalid parameter");
82 // maybe there are more than one separator at end
83 // so we run in a loop
84 while ((pustrPath
->length
> 1) && (FPH_CHAR_PATH_SEPARATOR
== pustrPath
->buffer
[pustrPath
->length
- 1]))
87 pustrPath
->buffer
[pustrPath
->length
] = (sal_Unicode
)'\0';
90 OSL_POSTCOND((0 == pustrPath
->length
) || (1 == pustrPath
->length
) || \
91 (pustrPath
->length
> 1 && pustrPath
->buffer
[pustrPath
->length
- 1] != FPH_CHAR_PATH_SEPARATOR
), \
92 "osl_systemPathRemoveSeparator: Post condition failed");
95 /*******************************************
96 osl_systemPathEnsureSeparator
97 ******************************************/
99 void SAL_CALL
osl_systemPathEnsureSeparator(rtl_uString
** ppustrPath
)
101 OSL_PRECOND(ppustrPath
&& (NULL
!= *ppustrPath
), \
102 "osl_systemPathEnsureSeparator: Invalid parameter");
104 rtl::OUString
path(*ppustrPath
);
105 sal_Int32 lp
= path
.getLength();
106 sal_Int32 i
= path
.lastIndexOf(FPH_CHAR_PATH_SEPARATOR
);
108 if ((lp
> 1 && i
!= (lp
- 1)) || ((lp
< 2) && i
< 0))
110 path
+= FPH_PATH_SEPARATOR();
111 rtl_uString_assign(ppustrPath
, path
.pData
);
114 OSL_POSTCOND(path
.lastIndexOf(FPH_CHAR_PATH_SEPARATOR
) == (path
.getLength() - 1), \
115 "osl_systemPathEnsureSeparator: Post condition failed");
118 /*******************************************
119 * osl_systemPathIsRelativePath
120 ******************************************/
122 sal_Bool SAL_CALL
osl_systemPathIsRelativePath(const rtl_uString
* pustrPath
)
124 OSL_PRECOND(pustrPath
, "osl_systemPathIsRelativePath: Invalid parameter");
125 return (!osl_systemPathIsAbsolutePath(pustrPath
));
128 /******************************************
129 * osl_systemPathIsAbsolutePath
130 *****************************************/
132 sal_Bool SAL_CALL
osl_systemPathIsAbsolutePath(const rtl_uString
* pustrPath
)
134 OSL_PRECOND(pustrPath
, "osl_systemPathIsAbsolutePath: Invalid parameter");
135 if (pustrPath
->length
== 0)
137 if (pustrPath
->buffer
[0] == FPH_CHAR_PATH_SEPARATOR
)
139 if (pustrPath
->buffer
[1] == FPH_CHAR_COLON
140 && pustrPath
->buffer
[2] == FPH_CHAR_PATH_SEPARATOR
)
145 /******************************************
146 osl_systemPathMakeAbsolutePath
147 *****************************************/
149 void SAL_CALL
osl_systemPathMakeAbsolutePath(
150 const rtl_uString
* pustrBasePath
,
151 const rtl_uString
* pustrRelPath
,
152 rtl_uString
** ppustrAbsolutePath
)
154 rtl::OUString
base(rtl_uString_getStr(const_cast<rtl_uString
*>(pustrBasePath
)));
155 rtl::OUString
rel(const_cast<rtl_uString
*>(pustrRelPath
));
157 if (base
.getLength() > 0)
158 osl_systemPathEnsureSeparator(&base
.pData
);
162 rtl_uString_acquire(base
.pData
);
163 *ppustrAbsolutePath
= base
.pData
;
167 /*****************************************
168 osl_systemPathGetParent
169 ****************************************/
171 sal_Int32 SAL_CALL
osl_systemPathGetParent(rtl_uString
* pustrPath
)
176 /*******************************************
177 osl_systemPathGetFileOrLastDirectoryPart
178 ******************************************/
180 void SAL_CALL
osl_systemPathGetFileNameOrLastDirectoryPart(
181 const rtl_uString
* pustrPath
,
182 rtl_uString
** ppustrFileNameOrLastDirPart
)
184 OSL_PRECOND(pustrPath
&& ppustrFileNameOrLastDirPart
, \
185 "osl_systemPathGetFileNameOrLastDirectoryPart: Invalid parameter");
187 rtl::OUString
path(const_cast<rtl_uString
*>(pustrPath
));
189 osl_systemPathRemoveSeparator(path
.pData
);
191 rtl::OUString last_part
;
193 if (path
.getLength() > 1 || (1 == path
.getLength() && *path
.getStr() != FPH_CHAR_PATH_SEPARATOR
))
195 sal_Int32 idx_ps
= path
.lastIndexOf(FPH_CHAR_PATH_SEPARATOR
);
196 idx_ps
++; // always right to increment by one even if idx_ps == -1!
197 last_part
= rtl::OUString(path
.getStr() + idx_ps
);
199 rtl_uString_assign(ppustrFileNameOrLastDirPart
, last_part
.pData
);
203 /********************************************
204 osl_systemPathIsHiddenFileOrDirectoryEntry
205 *********************************************/
207 sal_Bool SAL_CALL
osl_systemPathIsHiddenFileOrDirectoryEntry(
208 const rtl_uString
* pustrPath
)
210 OSL_PRECOND(pustrPath
, "osl_systemPathIsHiddenFileOrDirectoryEntry: Invalid parameter");
212 sal_Bool is_hidden
= sal_False
;
214 if (pustrPath
->length
> 0)
218 osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath
, &fdp
.pData
);
220 is_hidden
= ((fdp
.pData
->length
> 0) && (fdp
.pData
->buffer
[0] == FPH_CHAR_DOT
) &&
221 !osl_systemPathIsLocalOrParentDirectoryEntry(fdp
.pData
));
228 /************************************************
229 osl_systemPathIsLocalOrParentDirectoryEntry
230 ************************************************/
232 sal_Bool SAL_CALL
osl_systemPathIsLocalOrParentDirectoryEntry(
233 const rtl_uString
* pustrPath
)
235 OSL_PRECOND(pustrPath
, "osl_systemPathIsLocalOrParentDirectoryEntry: Invalid parameter");
237 rtl::OUString dirent
;
239 osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath
, &dirent
.pData
);
242 (dirent
== FPH_LOCAL_DIR_ENTRY()) ||
243 (dirent
== FPH_PARENT_DIR_ENTRY())
247 /***********************************************
248 Simple iterator for a path list separated by
249 the specified character
250 **********************************************/
252 class path_list_iterator
256 /******************************************
259 after construction get_current_item
260 returns the first path in list, no need
262 *****************************************/
263 path_list_iterator(const rtl::OUString
& path_list
, sal_Unicode list_separator
= FPH_CHAR_COLON
) :
264 m_path_list(path_list
),
265 m_end(m_path_list
.getStr() + m_path_list
.getLength() + 1),
266 m_separator(list_separator
)
271 /******************************************
273 *****************************************/
276 m_path_segment_begin
= m_path_segment_end
= m_path_list
.getStr();
280 /******************************************
281 move the iterator to the next position
282 *****************************************/
285 OSL_PRECOND(!done(), "path_list_iterator: Already done!");
287 m_path_segment_begin
= ++m_path_segment_end
;
291 /******************************************
293 *****************************************/
296 return (m_path_segment_end
>= m_end
);
299 /******************************************
300 return the current item
301 *****************************************/
302 rtl::OUString
get_current_item() const
304 return rtl::OUString(
305 m_path_segment_begin
,
306 (m_path_segment_end
- m_path_segment_begin
));
311 /******************************************
312 move m_path_end to the next separator or
313 to the edn of the string
314 *****************************************/
317 while (!done() && *m_path_segment_end
&& (*m_path_segment_end
!= m_separator
))
318 ++m_path_segment_end
;
320 OSL_ASSERT(m_path_segment_end
<= m_end
);
324 rtl::OUString m_path_list
;
325 const sal_Unicode
* m_end
;
326 const sal_Unicode m_separator
;
327 const sal_Unicode
* m_path_segment_begin
;
328 const sal_Unicode
* m_path_segment_end
;
330 // prevent copy and assignment
332 /******************************************
334 remember: do not simply copy m_path_begin
335 and m_path_end because they point to
336 the memory of other.m_path_list!
337 *****************************************/
338 path_list_iterator(const path_list_iterator
& other
);
340 /******************************************
342 remember: do not simply copy m_path_begin
343 and m_path_end because they point to
344 the memory of other.m_path_list!
345 *****************************************/
346 path_list_iterator
& operator=(const path_list_iterator
& other
);
349 /************************************************
351 ***********************************************/
353 sal_Bool SAL_CALL
osl_searchPath(
354 const rtl_uString
* pustrFilePath
,
355 const rtl_uString
* pustrSearchPathList
,
356 rtl_uString
** ppustrPathFound
)
358 OSL_PRECOND(pustrFilePath
&& pustrSearchPathList
&& ppustrPathFound
, "osl_searchPath: Invalid parameter");
361 rtl::OUString
fp(const_cast<rtl_uString
*>(pustrFilePath
));
362 rtl::OUString pl
= rtl::OUString(const_cast<rtl_uString
*>(pustrSearchPathList
));
363 path_list_iterator
pli(pl
);
367 rtl::OUString p
= pli
.get_current_item();
368 osl::systemPathEnsureSeparator(p
);
371 if (osl::access(p
, F_OK
) > -1)
374 rtl_uString_assign(ppustrPathFound
, p
.pData
);