xref: /trunk/main/sal/systools/win32/uwinapi/GetLongPathName.cpp (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 {
30     DWORD   dwResult = 0;   // Assume failure
31 
32     if ( IsBadStringPtr( lpShortPath, MAX_PATH ) )
33     {
34         SetLastError( ERROR_INVALID_PARAMETER );
35         return dwResult;
36     }
37 
38     // Assume a not existing buffer means a bufsize of zero
39     if ( !lpLongPath )
40         cchBuffer = 0;
41 
42     if ( _tcslen( lpShortPath ) == 2 && lpShortPath[1] == ':' )
43     {
44         _tcscpy( lpLongPath, lpShortPath );
45         dwResult = _tcslen( lpLongPath );
46     }
47     else
48     {
49         HANDLE          hFind;
50         WIN32_FIND_DATA aFindFileData;
51 
52         if ( lpShortPath[_tcslen(lpShortPath)-1] == '\\' )
53         {
54             TCHAR   szFilePath[MAX_PATH];
55 
56             _tcscpy( szFilePath, lpShortPath );
57             _tcscat( szFilePath, TEXT("*.*") );
58             hFind = FindFirstFile( szFilePath, &aFindFileData );;
59             aFindFileData.cFileName[0] = 0;
60         }
61         else
62         {
63             hFind = FindFirstFile( lpShortPath, &aFindFileData );
64             if ( !IsValidHandle( hFind ) )
65             {
66                 TCHAR   szFilePath[MAX_PATH];
67 
68                 _tcscpy( szFilePath, lpShortPath );
69                 _tcscat( szFilePath, TEXT("\\*.*") );
70                 hFind = FindFirstFile( szFilePath, &aFindFileData );;
71                 aFindFileData.cFileName[0] = 0;
72             }
73         }
74 
75         if ( IsValidHandle( hFind ) )
76         {
77             FindClose( hFind );
78 
79             LPCTSTR lpLastSlash = _tcsrchr( lpShortPath, '\\' );
80 
81             if ( lpLastSlash )
82             {
83                 int nParentLen = lpLastSlash - lpShortPath;
84                 LPTSTR  lpParentPath = (LPTSTR)_alloca( (nParentLen + 1) * sizeof(TCHAR) );
85 
86                 CopyMemory( lpParentPath, lpShortPath, nParentLen * sizeof(TCHAR) );
87                 lpParentPath[nParentLen] = 0;
88 
89                 dwResult = GetLongPathName( lpParentPath, lpLongPath, cchBuffer );
90 
91                 if ( !dwResult )
92                     _tcscpy( lpLongPath, lpParentPath );
93             }
94             else
95             {
96                 _tcscpy( lpLongPath, lpShortPath );
97                 dwResult = _tcslen( lpLongPath );
98             }
99 
100             if ( dwResult < cchBuffer )
101             {
102                 _tcscat( lpLongPath, TEXT("\\") );
103                 _tcscat( lpLongPath, aFindFileData.cFileName );
104                 dwResult = _tcslen( lpLongPath );
105             }
106             else
107                 dwResult += _tcslen( aFindFileData.cFileName ) + 1;
108         }
109     }
110 
111     return dwResult;
112 }
113 
114