1*87d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*87d2adbcSAndrew Rist * distributed with this work for additional information 6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an 15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 17*87d2adbcSAndrew Rist * specific language governing permissions and limitations 18*87d2adbcSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*87d2adbcSAndrew Rist *************************************************************/ 21*87d2adbcSAndrew Rist 22*87d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir /******************************************* 25cdf0e10cSrcweir Includes 26cdf0e10cSrcweir ******************************************/ 27cdf0e10cSrcweir 28cdf0e10cSrcweir #ifndef _OSL_THREAD_H_ 29cdf0e10cSrcweir #include "osl/thread.h" 30cdf0e10cSrcweir #endif 31cdf0e10cSrcweir 32cdf0e10cSrcweir #ifndef _OSL_FILE_PATH_HELPER_H_ 33cdf0e10cSrcweir #include "file_path_helper.h" 34cdf0e10cSrcweir #endif 35cdf0e10cSrcweir 36cdf0e10cSrcweir #ifndef _OSL_FILE_PATH_HELPER_HXX_ 37cdf0e10cSrcweir #include "file_path_helper.hxx" 38cdf0e10cSrcweir #endif 39cdf0e10cSrcweir 40cdf0e10cSrcweir #ifndef _OSL_UUNXAPI_HXX_ 41cdf0e10cSrcweir #include "uunxapi.hxx" 42cdf0e10cSrcweir #endif 43cdf0e10cSrcweir 44cdf0e10cSrcweir #ifndef _OSL_DIAGNOSE_H_ 45cdf0e10cSrcweir #include <osl/diagnose.h> 46cdf0e10cSrcweir #endif 47cdf0e10cSrcweir 48cdf0e10cSrcweir #ifndef _RTL_USTRING_HXX_ 49cdf0e10cSrcweir #include <rtl/ustring.hxx> 50cdf0e10cSrcweir #endif 51cdf0e10cSrcweir 52cdf0e10cSrcweir /******************************************* 53cdf0e10cSrcweir Constants 54cdf0e10cSrcweir ******************************************/ 55cdf0e10cSrcweir 56cdf0e10cSrcweir const sal_Unicode FPH_CHAR_PATH_SEPARATOR = (sal_Unicode)'\\'; 57cdf0e10cSrcweir const sal_Unicode FPH_CHAR_DOT = (sal_Unicode)'.'; 58cdf0e10cSrcweir const sal_Unicode FPH_CHAR_COLON = (sal_Unicode)':'; 59cdf0e10cSrcweir 60cdf0e10cSrcweir inline const rtl::OUString FPH_PATH_SEPARATOR() 61cdf0e10cSrcweir { return rtl::OUString::createFromAscii("\\"); } 62cdf0e10cSrcweir inline const rtl::OUString FPH_LOCAL_DIR_ENTRY() 63cdf0e10cSrcweir { return rtl::OUString::createFromAscii("."); } 64cdf0e10cSrcweir inline const rtl::OUString FPH_PARENT_DIR_ENTRY() 65cdf0e10cSrcweir { return rtl::OUString::createFromAscii(".."); } 66cdf0e10cSrcweir 67cdf0e10cSrcweir /******************************************* 68cdf0e10cSrcweir * osl_systemPathRemoveSeparator 69cdf0e10cSrcweir ******************************************/ 70cdf0e10cSrcweir 71cdf0e10cSrcweir void SAL_CALL osl_systemPathRemoveSeparator(rtl_uString* pustrPath) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir OSL_PRECOND(pustrPath, "osl_systemPathRemoveSeparator: Invalid parameter"); 74cdf0e10cSrcweir 75cdf0e10cSrcweir // maybe there are more than one separator at end 76cdf0e10cSrcweir // so we run in a loop 77cdf0e10cSrcweir while ((pustrPath->length > 1) && (FPH_CHAR_PATH_SEPARATOR == pustrPath->buffer[pustrPath->length - 1])) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir pustrPath->length--; 80cdf0e10cSrcweir pustrPath->buffer[pustrPath->length] = (sal_Unicode)'\0'; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir OSL_POSTCOND((0 == pustrPath->length) || (1 == pustrPath->length) || \ 84cdf0e10cSrcweir (pustrPath->length > 1 && pustrPath->buffer[pustrPath->length - 1] != FPH_CHAR_PATH_SEPARATOR), \ 85cdf0e10cSrcweir "osl_systemPathRemoveSeparator: Post condition failed"); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir /******************************************* 89cdf0e10cSrcweir osl_systemPathEnsureSeparator 90cdf0e10cSrcweir ******************************************/ 91cdf0e10cSrcweir 92cdf0e10cSrcweir void SAL_CALL osl_systemPathEnsureSeparator(rtl_uString** ppustrPath) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \ 95cdf0e10cSrcweir "osl_systemPathEnsureSeparator: Invalid parameter"); 96cdf0e10cSrcweir 97cdf0e10cSrcweir rtl::OUString path(*ppustrPath); 98cdf0e10cSrcweir sal_Int32 lp = path.getLength(); 99cdf0e10cSrcweir sal_Int32 i = path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR); 100cdf0e10cSrcweir 101cdf0e10cSrcweir if ((lp > 1 && i != (lp - 1)) || ((lp < 2) && i < 0)) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir path += FPH_PATH_SEPARATOR(); 104cdf0e10cSrcweir rtl_uString_assign(ppustrPath, path.pData); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir OSL_POSTCOND(path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR) == (path.getLength() - 1), \ 108cdf0e10cSrcweir "osl_systemPathEnsureSeparator: Post condition failed"); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir /******************************************* 112cdf0e10cSrcweir * osl_systemPathIsRelativePath 113cdf0e10cSrcweir ******************************************/ 114cdf0e10cSrcweir 115cdf0e10cSrcweir sal_Bool SAL_CALL osl_systemPathIsRelativePath(const rtl_uString* pustrPath) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir OSL_PRECOND(pustrPath, "osl_systemPathIsRelativePath: Invalid parameter"); 118cdf0e10cSrcweir return (!osl_systemPathIsAbsolutePath(pustrPath)); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir /****************************************** 122cdf0e10cSrcweir * osl_systemPathIsAbsolutePath 123cdf0e10cSrcweir *****************************************/ 124cdf0e10cSrcweir 125cdf0e10cSrcweir sal_Bool SAL_CALL osl_systemPathIsAbsolutePath(const rtl_uString* pustrPath) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir OSL_PRECOND(pustrPath, "osl_systemPathIsAbsolutePath: Invalid parameter"); 128cdf0e10cSrcweir if (pustrPath->length == 0) 129cdf0e10cSrcweir return sal_False; 130cdf0e10cSrcweir if (pustrPath->buffer[0] == FPH_CHAR_PATH_SEPARATOR) 131cdf0e10cSrcweir return sal_True; 132cdf0e10cSrcweir if (pustrPath->buffer[1] == FPH_CHAR_COLON 133cdf0e10cSrcweir && pustrPath->buffer[2] == FPH_CHAR_PATH_SEPARATOR) 134cdf0e10cSrcweir return sal_True; 135cdf0e10cSrcweir return sal_False; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir /****************************************** 139cdf0e10cSrcweir osl_systemPathMakeAbsolutePath 140cdf0e10cSrcweir *****************************************/ 141cdf0e10cSrcweir 142cdf0e10cSrcweir void SAL_CALL osl_systemPathMakeAbsolutePath( 143cdf0e10cSrcweir const rtl_uString* pustrBasePath, 144cdf0e10cSrcweir const rtl_uString* pustrRelPath, 145cdf0e10cSrcweir rtl_uString** ppustrAbsolutePath) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir rtl::OUString base(rtl_uString_getStr(const_cast<rtl_uString*>(pustrBasePath))); 148cdf0e10cSrcweir rtl::OUString rel(const_cast<rtl_uString*>(pustrRelPath)); 149cdf0e10cSrcweir 150cdf0e10cSrcweir if (base.getLength() > 0) 151cdf0e10cSrcweir osl_systemPathEnsureSeparator(&base.pData); 152cdf0e10cSrcweir 153cdf0e10cSrcweir base += rel; 154cdf0e10cSrcweir 155cdf0e10cSrcweir rtl_uString_acquire(base.pData); 156cdf0e10cSrcweir *ppustrAbsolutePath = base.pData; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir 160cdf0e10cSrcweir /***************************************** 161cdf0e10cSrcweir osl_systemPathGetParent 162cdf0e10cSrcweir ****************************************/ 163cdf0e10cSrcweir 164cdf0e10cSrcweir sal_Int32 SAL_CALL osl_systemPathGetParent(rtl_uString* pustrPath) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir return 0; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir /******************************************* 170cdf0e10cSrcweir osl_systemPathGetFileOrLastDirectoryPart 171cdf0e10cSrcweir ******************************************/ 172cdf0e10cSrcweir 173cdf0e10cSrcweir void SAL_CALL osl_systemPathGetFileNameOrLastDirectoryPart( 174cdf0e10cSrcweir const rtl_uString* pustrPath, 175cdf0e10cSrcweir rtl_uString** ppustrFileNameOrLastDirPart) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir OSL_PRECOND(pustrPath && ppustrFileNameOrLastDirPart, \ 178cdf0e10cSrcweir "osl_systemPathGetFileNameOrLastDirectoryPart: Invalid parameter"); 179cdf0e10cSrcweir 180cdf0e10cSrcweir rtl::OUString path(const_cast<rtl_uString*>(pustrPath)); 181cdf0e10cSrcweir 182cdf0e10cSrcweir osl_systemPathRemoveSeparator(path.pData); 183cdf0e10cSrcweir 184cdf0e10cSrcweir rtl::OUString last_part; 185cdf0e10cSrcweir 186cdf0e10cSrcweir if (path.getLength() > 1 || (1 == path.getLength() && *path.getStr() != FPH_CHAR_PATH_SEPARATOR)) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir sal_Int32 idx_ps = path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR); 189cdf0e10cSrcweir idx_ps++; // always right to increment by one even if idx_ps == -1! 190cdf0e10cSrcweir last_part = rtl::OUString(path.getStr() + idx_ps); 191cdf0e10cSrcweir } 192cdf0e10cSrcweir rtl_uString_assign(ppustrFileNameOrLastDirPart, last_part.pData); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir 195cdf0e10cSrcweir 196cdf0e10cSrcweir /******************************************** 197cdf0e10cSrcweir osl_systemPathIsHiddenFileOrDirectoryEntry 198cdf0e10cSrcweir *********************************************/ 199cdf0e10cSrcweir 200cdf0e10cSrcweir sal_Bool SAL_CALL osl_systemPathIsHiddenFileOrDirectoryEntry( 201cdf0e10cSrcweir const rtl_uString* pustrPath) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir OSL_PRECOND(pustrPath, "osl_systemPathIsHiddenFileOrDirectoryEntry: Invalid parameter"); 204cdf0e10cSrcweir 205cdf0e10cSrcweir sal_Bool is_hidden = sal_False; 206cdf0e10cSrcweir 207cdf0e10cSrcweir if (pustrPath->length > 0) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir rtl::OUString fdp; 210cdf0e10cSrcweir 211cdf0e10cSrcweir osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath, &fdp.pData); 212cdf0e10cSrcweir 213cdf0e10cSrcweir is_hidden = ((fdp.pData->length > 0) && (fdp.pData->buffer[0] == FPH_CHAR_DOT) && 214cdf0e10cSrcweir !osl_systemPathIsLocalOrParentDirectoryEntry(fdp.pData)); 215cdf0e10cSrcweir } 216cdf0e10cSrcweir 217cdf0e10cSrcweir return is_hidden; 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir 221cdf0e10cSrcweir /************************************************ 222cdf0e10cSrcweir osl_systemPathIsLocalOrParentDirectoryEntry 223cdf0e10cSrcweir ************************************************/ 224cdf0e10cSrcweir 225cdf0e10cSrcweir sal_Bool SAL_CALL osl_systemPathIsLocalOrParentDirectoryEntry( 226cdf0e10cSrcweir const rtl_uString* pustrPath) 227cdf0e10cSrcweir { 228cdf0e10cSrcweir OSL_PRECOND(pustrPath, "osl_systemPathIsLocalOrParentDirectoryEntry: Invalid parameter"); 229cdf0e10cSrcweir 230cdf0e10cSrcweir rtl::OUString dirent; 231cdf0e10cSrcweir 232cdf0e10cSrcweir osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath, &dirent.pData); 233cdf0e10cSrcweir 234cdf0e10cSrcweir return ( 235cdf0e10cSrcweir (dirent == FPH_LOCAL_DIR_ENTRY()) || 236cdf0e10cSrcweir (dirent == FPH_PARENT_DIR_ENTRY()) 237cdf0e10cSrcweir ); 238cdf0e10cSrcweir } 239cdf0e10cSrcweir 240cdf0e10cSrcweir /*********************************************** 241cdf0e10cSrcweir Simple iterator for a path list separated by 242cdf0e10cSrcweir the specified character 243cdf0e10cSrcweir **********************************************/ 244cdf0e10cSrcweir 245cdf0e10cSrcweir class path_list_iterator 246cdf0e10cSrcweir { 247cdf0e10cSrcweir public: 248cdf0e10cSrcweir 249cdf0e10cSrcweir /****************************************** 250cdf0e10cSrcweir constructor 251cdf0e10cSrcweir 252cdf0e10cSrcweir after construction get_current_item 253cdf0e10cSrcweir returns the first path in list, no need 254cdf0e10cSrcweir to call reset first 255cdf0e10cSrcweir *****************************************/ 256cdf0e10cSrcweir path_list_iterator(const rtl::OUString& path_list, sal_Unicode list_separator = FPH_CHAR_COLON) : 257cdf0e10cSrcweir m_path_list(path_list), 258cdf0e10cSrcweir m_end(m_path_list.getStr() + m_path_list.getLength() + 1), 259cdf0e10cSrcweir m_separator(list_separator) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir reset(); 262cdf0e10cSrcweir } 263cdf0e10cSrcweir 264cdf0e10cSrcweir /****************************************** 265cdf0e10cSrcweir reset the iterator 266cdf0e10cSrcweir *****************************************/ 267cdf0e10cSrcweir void reset() 268cdf0e10cSrcweir { 269cdf0e10cSrcweir m_path_segment_begin = m_path_segment_end = m_path_list.getStr(); 270cdf0e10cSrcweir advance(); 271cdf0e10cSrcweir } 272cdf0e10cSrcweir 273cdf0e10cSrcweir /****************************************** 274cdf0e10cSrcweir move the iterator to the next position 275cdf0e10cSrcweir *****************************************/ 276cdf0e10cSrcweir void next() 277cdf0e10cSrcweir { 278cdf0e10cSrcweir OSL_PRECOND(!done(), "path_list_iterator: Already done!"); 279cdf0e10cSrcweir 280cdf0e10cSrcweir m_path_segment_begin = ++m_path_segment_end; 281cdf0e10cSrcweir advance(); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir 284cdf0e10cSrcweir /****************************************** 285cdf0e10cSrcweir check if done 286cdf0e10cSrcweir *****************************************/ 287cdf0e10cSrcweir bool done() const 288cdf0e10cSrcweir { 289cdf0e10cSrcweir return (m_path_segment_end >= m_end); 290cdf0e10cSrcweir } 291cdf0e10cSrcweir 292cdf0e10cSrcweir /****************************************** 293cdf0e10cSrcweir return the current item 294cdf0e10cSrcweir *****************************************/ 295cdf0e10cSrcweir rtl::OUString get_current_item() const 296cdf0e10cSrcweir { 297cdf0e10cSrcweir return rtl::OUString( 298cdf0e10cSrcweir m_path_segment_begin, 299cdf0e10cSrcweir (m_path_segment_end - m_path_segment_begin)); 300cdf0e10cSrcweir } 301cdf0e10cSrcweir 302cdf0e10cSrcweir private: 303cdf0e10cSrcweir 304cdf0e10cSrcweir /****************************************** 305cdf0e10cSrcweir move m_path_end to the next separator or 306cdf0e10cSrcweir to the edn of the string 307cdf0e10cSrcweir *****************************************/ 308cdf0e10cSrcweir void advance() 309cdf0e10cSrcweir { 310cdf0e10cSrcweir while (!done() && *m_path_segment_end && (*m_path_segment_end != m_separator)) 311cdf0e10cSrcweir ++m_path_segment_end; 312cdf0e10cSrcweir 313cdf0e10cSrcweir OSL_ASSERT(m_path_segment_end <= m_end); 314cdf0e10cSrcweir } 315cdf0e10cSrcweir 316cdf0e10cSrcweir private: 317cdf0e10cSrcweir rtl::OUString m_path_list; 318cdf0e10cSrcweir const sal_Unicode* m_end; 319cdf0e10cSrcweir const sal_Unicode m_separator; 320cdf0e10cSrcweir const sal_Unicode* m_path_segment_begin; 321cdf0e10cSrcweir const sal_Unicode* m_path_segment_end; 322cdf0e10cSrcweir 323cdf0e10cSrcweir // prevent copy and assignment 324cdf0e10cSrcweir private: 325cdf0e10cSrcweir /****************************************** 326cdf0e10cSrcweir copy constructor 327cdf0e10cSrcweir remember: do not simply copy m_path_begin 328cdf0e10cSrcweir and m_path_end because they point to 329cdf0e10cSrcweir the memory of other.m_path_list! 330cdf0e10cSrcweir *****************************************/ 331cdf0e10cSrcweir path_list_iterator(const path_list_iterator& other); 332cdf0e10cSrcweir 333cdf0e10cSrcweir /****************************************** 334cdf0e10cSrcweir assignment operator 335cdf0e10cSrcweir remember: do not simply copy m_path_begin 336cdf0e10cSrcweir and m_path_end because they point to 337cdf0e10cSrcweir the memory of other.m_path_list! 338cdf0e10cSrcweir *****************************************/ 339cdf0e10cSrcweir path_list_iterator& operator=(const path_list_iterator& other); 340cdf0e10cSrcweir }; 341cdf0e10cSrcweir 342cdf0e10cSrcweir /************************************************ 343cdf0e10cSrcweir osl_searchPath 344cdf0e10cSrcweir ***********************************************/ 345cdf0e10cSrcweir 346cdf0e10cSrcweir sal_Bool SAL_CALL osl_searchPath( 347cdf0e10cSrcweir const rtl_uString* pustrFilePath, 348cdf0e10cSrcweir const rtl_uString* pustrSearchPathList, 349cdf0e10cSrcweir rtl_uString** ppustrPathFound) 350cdf0e10cSrcweir { 351cdf0e10cSrcweir OSL_PRECOND(pustrFilePath && pustrSearchPathList && ppustrPathFound, "osl_searchPath: Invalid parameter"); 352cdf0e10cSrcweir 353cdf0e10cSrcweir bool bfound = false; 354cdf0e10cSrcweir rtl::OUString fp(const_cast<rtl_uString*>(pustrFilePath)); 355cdf0e10cSrcweir rtl::OUString pl = rtl::OUString(const_cast<rtl_uString*>(pustrSearchPathList)); 356cdf0e10cSrcweir path_list_iterator pli(pl); 357cdf0e10cSrcweir 358cdf0e10cSrcweir while (!pli.done()) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir rtl::OUString p = pli.get_current_item(); 361cdf0e10cSrcweir osl::systemPathEnsureSeparator(p); 362cdf0e10cSrcweir p += fp; 363cdf0e10cSrcweir 364cdf0e10cSrcweir if (osl::access(p, F_OK) > -1) 365cdf0e10cSrcweir { 366cdf0e10cSrcweir bfound = true; 367cdf0e10cSrcweir rtl_uString_assign(ppustrPathFound, p.pData); 368cdf0e10cSrcweir break; 369cdf0e10cSrcweir } 370cdf0e10cSrcweir pli.next(); 371cdf0e10cSrcweir } 372cdf0e10cSrcweir return bfound; 373cdf0e10cSrcweir } 374