merge the formfield patch from ooo-build
[ooovba.git] / sal / systools / win32 / uwinapi / GetLongPathName.cpp
blob2423b03bd02cfab0df3fbe8678021c6390908142
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: GetLongPathName.cpp,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
33 DWORD dwResult = 0; // Assume failure
35 if ( IsBadStringPtr( lpShortPath, MAX_PATH ) )
37 SetLastError( ERROR_INVALID_PARAMETER );
38 return dwResult;
41 // Assume a not existing buffer means a bufsize of zero
42 if ( !lpLongPath )
43 cchBuffer = 0;
45 if ( _tcslen( lpShortPath ) == 2 && lpShortPath[1] == ':' )
47 _tcscpy( lpLongPath, lpShortPath );
48 dwResult = _tcslen( lpLongPath );
50 else
52 HANDLE hFind;
53 WIN32_FIND_DATA aFindFileData;
55 if ( lpShortPath[_tcslen(lpShortPath)-1] == '\\' )
57 TCHAR szFilePath[MAX_PATH];
59 _tcscpy( szFilePath, lpShortPath );
60 _tcscat( szFilePath, TEXT("*.*") );
61 hFind = FindFirstFile( szFilePath, &aFindFileData );;
62 aFindFileData.cFileName[0] = 0;
64 else
66 hFind = FindFirstFile( lpShortPath, &aFindFileData );
67 if ( !IsValidHandle( hFind ) )
69 TCHAR szFilePath[MAX_PATH];
71 _tcscpy( szFilePath, lpShortPath );
72 _tcscat( szFilePath, TEXT("\\*.*") );
73 hFind = FindFirstFile( szFilePath, &aFindFileData );;
74 aFindFileData.cFileName[0] = 0;
78 if ( IsValidHandle( hFind ) )
80 FindClose( hFind );
82 LPCTSTR lpLastSlash = _tcsrchr( lpShortPath, '\\' );
84 if ( lpLastSlash )
86 int nParentLen = lpLastSlash - lpShortPath;
87 LPTSTR lpParentPath = (LPTSTR)_alloca( (nParentLen + 1) * sizeof(TCHAR) );
89 CopyMemory( lpParentPath, lpShortPath, nParentLen * sizeof(TCHAR) );
90 lpParentPath[nParentLen] = 0;
92 dwResult = GetLongPathName( lpParentPath, lpLongPath, cchBuffer );
94 if ( !dwResult )
95 _tcscpy( lpLongPath, lpParentPath );
97 else
99 _tcscpy( lpLongPath, lpShortPath );
100 dwResult = _tcslen( lpLongPath );
103 if ( dwResult < cchBuffer )
105 _tcscat( lpLongPath, TEXT("\\") );
106 _tcscat( lpLongPath, aFindFileData.cFileName );
107 dwResult = _tcslen( lpLongPath );
109 else
110 dwResult += _tcslen( aFindFileData.cFileName ) + 1;
114 return dwResult;