summaryrefslogtreecommitdiff
path: root/sal/osl/unx/file_path_helper.cxx
diff options
context:
space:
mode:
authorTino Rachui <tra@openoffice.org>2002-12-14 12:20:24 +0000
committerTino Rachui <tra@openoffice.org>2002-12-14 12:20:24 +0000
commitffbab40f7fde9557ec7585dc5b4f4daa7dff4dc5 (patch)
tree5319ff3cbd13ceed80b5a5bc5154659e665a0792 /sal/osl/unx/file_path_helper.cxx
parent5147906241c19e94cf0ae0f3794ceff2c8b81fe0 (diff)
#104563#fixed osl_systemPathGetFileNameOrLastDirectoryPart
Diffstat (limited to 'sal/osl/unx/file_path_helper.cxx')
-rw-r--r--sal/osl/unx/file_path_helper.cxx285
1 files changed, 164 insertions, 121 deletions
diff --git a/sal/osl/unx/file_path_helper.cxx b/sal/osl/unx/file_path_helper.cxx
index 25e9ca931356..3f5375fb9c61 100644
--- a/sal/osl/unx/file_path_helper.cxx
+++ b/sal/osl/unx/file_path_helper.cxx
@@ -2,9 +2,9 @@
*
* $RCSfile: file_path_helper.cxx,v $
*
- * $Revision: 1.1 $
+ * $Revision: 1.2 $
*
- * last change: $Author: tra $ $Date: 2002-11-29 10:38:29 $
+ * last change: $Author: tra $ $Date: 2002-12-14 13:20:24 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
@@ -63,10 +63,18 @@
Includes
******************************************/
- #ifndef _OSL_PATH_HELPER_H_
+ #ifndef _OSL_FILE_PATH_HELPER_H_
#include "file_path_helper.h"
#endif
+ #ifndef _OSL_FILE_PATH_HELPER_HXX_
+ #include "file_path_helper.hxx"
+ #endif
+
+ #ifndef _OSL_UUNXAPI_HXX_
+ #include "uunxapi.hxx"
+ #endif
+
#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif
@@ -81,6 +89,7 @@
const sal_Unicode FPH_CHAR_PATH_SEPARATOR = (sal_Unicode)'/';
const sal_Unicode FPH_CHAR_DOT = (sal_Unicode)'.';
+ const sal_Unicode FPH_CHAR_COLON = (sal_Unicode)':';
const rtl::OUString FPH_PATH_SEPARATOR = rtl::OUString::createFromAscii("/");
const rtl::OUString FPH_LOCAL_DIR_ENTRY = rtl::OUString::createFromAscii(".");
@@ -102,24 +111,14 @@
pustrPath->buffer[pustrPath->length] = (sal_Unicode)'\0';
}
- OSL_POSTCOND((pustrPath->length > 0) && pustrPath->buffer[pustrPath->length - 1] != FPH_CHAR_PATH_SEPARATOR, \
+ OSL_POSTCOND((0 == pustrPath->length) || \
+ (1 == pustrPath->length && pustrPath->buffer[0] == FPH_CHAR_PATH_SEPARATOR) || \
+ ((pustrPath->length > 0) && pustrPath->buffer[pustrPath->length - 1] != FPH_CHAR_PATH_SEPARATOR), \
"osl_systemPathRemoveSeparator: Post condition failed");
}
/*******************************************
osl_systemPathEnsureSeparator
- Adds a trailing path separator to the
- given system path if not already there
- and if the path is not the root path '/'
-
- @param pustrPath [inout] a system path
- if the path is not the root path
- '/' and has no trailing separator
- a separator will be added
- pustrPath must not be NULL
-
- @returns nothing
-
******************************************/
void SAL_CALL osl_systemPathEnsureSeparator(rtl_uString** ppustrPath)
@@ -129,12 +128,12 @@
rtl::OUString path(*ppustrPath);
sal_Int32 lp = path.getLength();
+ sal_Int32 i = path.lastIndexOf(FPH_PATH_SEPARATOR);
- if ((lp > 1) && (path.lastIndexOf(FPH_PATH_SEPARATOR) != (lp - 1)))
+ if ((lp > 1 && i != (lp - 1)) || ((lp < 2) && i < 0))
{
path += FPH_PATH_SEPARATOR;
- rtl_uString_acquire(path.pData);
- *ppustrPath = path.pData;
+ rtl_uString_assign(ppustrPath, path.pData);
}
OSL_POSTCOND(path.lastIndexOf(FPH_PATH_SEPARATOR) == (path.getLength() - 1), \
@@ -163,36 +162,6 @@
/******************************************
osl_systemPathMakeAbsolutePath
- Append a relative path to a base path
-
- @param pustrBasePath [in] a system
- path that will be considered as
- base path
- pustrBasePath must not be NULL
-
- @param pustrRelPath [in] a system path
- that will be considered as
- relative path
- pustrBasePath must not be NULL
-
- @param ppustrAbsolutePath [out] the
- resulting path which is a
- concatination of the base and
- the relative path
- if base path is empty the
- resulting absolute path is the
- relative path
- if relative path is empty the
- resulting absolute path is the
- base path
- if base and relative path are
- empty the resulting absolute
- path is also empty
- ppustrAbsolutePath must not be
- NULL and *ppustrAbsolutePath
- must be 0 or point to a valid
- rtl_uString
-
*****************************************/
void SAL_CALL osl_systemPathMakeAbsolutePath(
@@ -215,27 +184,6 @@
/*****************************************
osl_systemPathGetParent
- Replaces the last occurrance of a path
- separator with '\0' and returns the
- position where the '/' was replaced
-
- @param pustrPath [inout] a system
- path, the last separator of
- this path will be replaced by
- a '\0'
- if the path is the root path
- '/' or the path is considered
- as to have no parent, e.g.
- '/NoParent' or 'NoParent' or
- the path is empty no
- replacement will be made
- pustrPath must not be NULL
-
- @returns the position of the last path
- separator that was replaced
- or 0 if no replacement took
- place
-
****************************************/
sal_Int32 SAL_CALL osl_systemPathGetParent(rtl_uString* pustrPath)
@@ -243,29 +191,10 @@
return 0;
}
- /*****************************************
+ /*******************************************
osl_systemPathGetFileOrLastDirectoryPart
- Returns the file or the directory part
- of the given path
-
- @param pustrPath [in] a system path
-
- @param ppustrFileOrDirPart [out] on
- return receives the last part
- of the given directory or the
- file name
- if pustrPath is the root path
- '/' an empty string will be
- returned
- if pustrPath has a trailing
- '/' the last part before the
- '/' will be returned else
- the part after the last '/'
- will be returned
-
- @returns nothing
+ ******************************************/
- ****************************************/
void SAL_CALL osl_systemPathGetFileNameOrLastDirectoryPart(
const rtl_uString* pustrPath,
rtl_uString** ppustrFileNameOrLastDirPart)
@@ -273,31 +202,24 @@
OSL_PRECOND(pustrPath && ppustrFileNameOrLastDirPart, \
"osl_systemPathGetFileNameOrLastDirectoryPart: Invalid parameter");
- sal_Unicode* p = rtl_uString_getStr(const_cast<rtl_uString*>(pustrPath));
- sal_Int32 i = rtl_ustr_lastIndexOfChar(p, FPH_CHAR_PATH_SEPARATOR);
+ rtl::OUString path(const_cast<rtl_uString*>(pustrPath));
- if (-1 != i)
- p += (i + 1);
+ osl_systemPathRemoveSeparator(path.pData);
- rtl_uString_newFromStr(ppustrFileNameOrLastDirPart, p);
+ rtl::OUString last_part;
+
+ if (path.getLength() > 1 || (1 == path.getLength() && *path.getStr() != FPH_CHAR_PATH_SEPARATOR))
+ {
+ sal_Int32 idx_ps = path.lastIndexOf(FPH_PATH_SEPARATOR);
+ idx_ps++; // always right to increment by one even if idx_ps == -1!
+ last_part = rtl::OUString(path.getStr() + idx_ps);
+ }
+ rtl_uString_assign(ppustrFileNameOrLastDirPart, last_part.pData);
}
/********************************************
osl_systemPathIsHiddenFileOrDirectoryEntry
- Returns sal_True if the last part of
- given system path is not '.' or '..'
- alone and starts with a '.'
-
- @param pustrPath [in] a system path,
- must not be 0
-
- @returns sal_True if the last part of
- the given system path starts
- with '.' or sal_False the last
- part is '.' or '..' alone or
- doesn't start with a dot
-
*********************************************/
sal_Bool SAL_CALL osl_systemPathIsHiddenFileOrDirectoryEntry(
@@ -323,23 +245,12 @@
/************************************************
osl_systemPathIsLocalOrParentDirectoryEntry
- Returns sal_True if the last part of the given
- system path is the local directory entry '.'
- or the parent directory entry '..'
-
- @param pustrPath [in] a system path,
- must not be 0
-
- @returns sal_True if the last part of the
- given system path is '.' or '..'
- else sal_False
-
************************************************/
sal_Bool SAL_CALL osl_systemPathIsLocalOrParentDirectoryEntry(
const rtl_uString* pustrPath)
{
- OSL_PRECOND(pustrPath, "osl_systemPathIsLocalOrParentDirectoryEntry: Invalid argument");
+ OSL_PRECOND(pustrPath, "osl_systemPathIsLocalOrParentDirectoryEntry: Invalid parameter");
rtl::OUString dirent;
@@ -348,5 +259,137 @@ sal_Bool SAL_CALL osl_systemPathIsLocalOrParentDirectoryEntry(
return ((dirent == FPH_LOCAL_DIR_ENTRY) || (dirent == FPH_PARENT_DIR_ENTRY));
}
+/***********************************************
+ Simple iterator for a path list separated by
+ the specified character
+ **********************************************/
+
+class path_list_iterator
+{
+public:
+
+ /******************************************
+ constructor
+
+ after construction get_current_item
+ returns the first path in list, no need
+ to call reset first
+ *****************************************/
+ path_list_iterator(const rtl::OUString& path_list, sal_Unicode list_separator = FPH_CHAR_COLON) :
+ m_path_list(path_list),
+ m_end(m_path_list.getStr() + m_path_list.getLength() + 1),
+ m_separator(list_separator)
+ {
+ reset();
+ }
+
+ /******************************************
+ reset the iterator
+ *****************************************/
+ void reset()
+ {
+ m_path_segment_begin = m_path_segment_end = m_path_list.getStr();
+ advance();
+ }
+
+ /******************************************
+ move the iterator to the next position
+ *****************************************/
+ void next()
+ {
+ OSL_PRECOND(!done(), "path_list_iterator: Already done!");
+
+ m_path_segment_begin = ++m_path_segment_end;
+ advance();
+ }
+
+ /******************************************
+ check if done
+ *****************************************/
+ bool done() const
+ {
+ return (m_path_segment_end >= m_end);
+ }
+
+ /******************************************
+ return the current item
+ *****************************************/
+ rtl::OUString get_current_item() const
+ {
+ return rtl::OUString(
+ m_path_segment_begin,
+ (m_path_segment_end - m_path_segment_begin));
+ }
+
+private:
+
+ /******************************************
+ move m_path_end to the next separator or
+ to the edn of the string
+ *****************************************/
+ void advance()
+ {
+ while (!done() && *m_path_segment_end && (*m_path_segment_end != m_separator))
+ ++m_path_segment_end;
+ OSL_ASSERT(m_path_segment_end <= m_end);
+ }
+
+private:
+ rtl::OUString m_path_list;
+ const sal_Unicode* m_end;
+ const sal_Unicode m_separator;
+ const sal_Unicode* m_path_segment_begin;
+ const sal_Unicode* m_path_segment_end;
+
+// prevent copy and assignment
+private:
+ /******************************************
+ copy constructor
+ remember: do not simply copy m_path_begin
+ and m_path_end because they point to
+ the memory of other.m_path_list!
+ *****************************************/
+ path_list_iterator(const path_list_iterator& other);
+
+ /******************************************
+ assignment operator
+ remember: do not simply copy m_path_begin
+ and m_path_end because they point to
+ the memory of other.m_path_list!
+ *****************************************/
+ path_list_iterator& operator=(const path_list_iterator& other);
+};
+
+ /************************************************
+ osl_searchPath
+ ***********************************************/
+sal_Bool SAL_CALL osl_searchPath(
+ const rtl_uString* pustrFilePath,
+ const rtl_uString* pustrSearchPathList,
+ rtl_uString** ppustrPathFound)
+{
+ OSL_PRECOND(pustrFilePath && pustrSearchPathList && ppustrPathFound, "osl_searchPath: Invalid parameter");
+
+ bool bfound = false;
+ rtl::OUString fp(const_cast<rtl_uString*>(pustrFilePath));
+ rtl::OUString pl = rtl::OUString(const_cast<rtl_uString*>(pustrSearchPathList));
+ path_list_iterator pli(pl);
+
+ while (!pli.done())
+ {
+ rtl::OUString p = pli.get_current_item();
+ osl::systemPathEnsureSeparator(p);
+ p += fp;
+
+ if (osl::access(p, F_OK) > -1)
+ {
+ bfound = true;
+ rtl_uString_assign(ppustrPathFound, p.pData);
+ break;
+ }
+ pli.next();
+ }
+ return bfound;
+}