Added some new Rtl* tests.
[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 <string.h>
23
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winternl.h"
27 #include "commdlg.h"
28 #include "cdlg.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
33
34 /***********************************************************************
35  *      GetFileTitleA           (COMDLG32.@)
36  *
37  */
38 short WINAPI GetFileTitleA(LPCSTR lpFile, LPSTR lpTitle, UINT cbBuf)
39 {
40     int ret;
41     UNICODE_STRING strWFile;
42     LPWSTR lpWTitle;
43
44     RtlCreateUnicodeStringFromAsciiz(&strWFile, lpFile);
45     lpWTitle = RtlAllocateHeap( GetProcessHeap(), 0, cbBuf*sizeof(WCHAR));
46     ret = GetFileTitleW(strWFile.Buffer, lpWTitle, cbBuf);
47     if (!ret) WideCharToMultiByte( CP_ACP, 0, lpWTitle, -1, lpTitle, cbBuf, NULL, NULL );
48     RtlFreeUnicodeString( &strWFile );
49     RtlFreeHeap( GetProcessHeap(), 0, lpWTitle );
50     return ret;
51 }
52
53
54 /***********************************************************************
55  *      GetFileTitleW           (COMDLG32.@)
56  *
57  */
58 short WINAPI GetFileTitleW(LPCWSTR lpFile, LPWSTR lpTitle, UINT cbBuf)
59 {
60         int i, len;
61         static const WCHAR brkpoint[] = {'*','[',']',0};
62         TRACE("(%p %p %d); \n", lpFile, lpTitle, cbBuf);
63
64         if(lpFile == NULL || lpTitle == NULL)
65                 return -1;
66
67         len = strlenW(lpFile);
68
69         if (len == 0)
70                 return -1;
71
72         if(strpbrkW(lpFile, brkpoint))
73                 return -1;
74
75         len--;
76
77         if(lpFile[len] == '/' || lpFile[len] == '\\' || lpFile[len] == ':')
78                 return -1;
79
80         for(i = len; i >= 0; i--)
81         {
82                 if (lpFile[i] == '/' ||  lpFile[i] == '\\' ||  lpFile[i] == ':')
83                 {
84                         i++;
85                         break;
86                 }
87         }
88
89         if(i == -1)
90                 i++;
91
92         TRACE("---> '%s' \n", debugstr_w(&lpFile[i]));
93
94         len = strlenW(lpFile+i)+1;
95         if(cbBuf < len)
96                 return len;
97
98         strcpyW(lpTitle, &lpFile[i]);
99         return 0;
100 }
101
102
103 /***********************************************************************
104  *      GetFileTitle            (COMMDLG.27)
105  */
106 short WINAPI GetFileTitle16(LPCSTR lpFile, LPSTR lpTitle, UINT16 cbBuf)
107 {
108         return GetFileTitleA(lpFile, lpTitle, cbBuf);
109 }