Reserve extra space for conversions.
[wine] / dlls / commdlg / filetitle.c
1 /*
2  * COMMDLG - File Dialogs
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1996 Albrecht Kleine
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "commdlg.h"
33 #include "cdlg.h"
34 #include "cdlg16.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
39
40 /***********************************************************************
41  *      GetFileTitleA           (COMDLG32.@)
42  *
43  */
44 short WINAPI GetFileTitleA(LPCSTR lpFile, LPSTR lpTitle, WORD cbBuf)
45 {
46     int ret;
47     UNICODE_STRING strWFile;
48     LPWSTR lpWTitle;
49
50     RtlCreateUnicodeStringFromAsciiz(&strWFile, lpFile);
51     lpWTitle = RtlAllocateHeap( GetProcessHeap(), 0, cbBuf*sizeof(WCHAR));
52     ret = GetFileTitleW(strWFile.Buffer, lpWTitle, cbBuf);
53     if (!ret) WideCharToMultiByte( CP_ACP, 0, lpWTitle, -1, lpTitle, cbBuf, NULL, NULL );
54     RtlFreeUnicodeString( &strWFile );
55     RtlFreeHeap( GetProcessHeap(), 0, lpWTitle );
56     return ret;
57 }
58
59
60 /***********************************************************************
61  *      GetFileTitleW           (COMDLG32.@)
62  *
63  */
64 short WINAPI GetFileTitleW(LPCWSTR lpFile, LPWSTR lpTitle, WORD cbBuf)
65 {
66         int i, len;
67         static const WCHAR brkpoint[] = {'*','[',']',0};
68         TRACE("(%p %p %d); \n", lpFile, lpTitle, cbBuf);
69
70         if(lpFile == NULL || lpTitle == NULL)
71                 return -1;
72
73         len = strlenW(lpFile);
74
75         if (len == 0)
76                 return -1;
77
78         if(strpbrkW(lpFile, brkpoint))
79                 return -1;
80
81         len--;
82
83         if(lpFile[len] == '/' || lpFile[len] == '\\' || lpFile[len] == ':')
84                 return -1;
85
86         for(i = len; i >= 0; i--)
87         {
88                 if (lpFile[i] == '/' ||  lpFile[i] == '\\' ||  lpFile[i] == ':')
89                 {
90                         i++;
91                         break;
92                 }
93         }
94
95         if(i == -1)
96                 i++;
97
98         TRACE("---> '%s' \n", debugstr_w(&lpFile[i]));
99
100         len = strlenW(lpFile+i)+1;
101         if(cbBuf < len)
102                 return len;
103
104         strcpyW(lpTitle, &lpFile[i]);
105         return 0;
106 }
107
108
109 /***********************************************************************
110  *      GetFileTitle            (COMMDLG.27)
111  */
112 short WINAPI GetFileTitle16(LPCSTR lpFile, LPSTR lpTitle, UINT16 cbBuf)
113 {
114         return GetFileTitleA(lpFile, lpTitle, cbBuf);
115 }