- changed STRRET definition
[wine] / dlls / shell32 / debughlp.c
1 /*
2  * Helper functions for debugging
3  *
4  * Copyright 1998, 2002 Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "pidl.h"
26 #include "shlguid.h"
27 #include "wine/debug.h"
28 #include "debughlp.h"
29 #include "docobj.h"
30 #include "shell32_main.h"
31
32
33 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
34
35 LPITEMIDLIST  _dbg_ILGetNext(LPITEMIDLIST pidl)
36 {
37         WORD len;
38
39         if(pidl)
40         {
41           len =  pidl->mkid.cb;
42           if (len)
43           {
44             pidl = (LPITEMIDLIST) (((LPBYTE)pidl)+len);
45             return pidl;
46           }
47         }
48         return NULL;
49 }
50
51 BOOL _dbg_ILIsDesktop(LPCITEMIDLIST pidl)
52 {
53         return ( !pidl || (pidl && pidl->mkid.cb == 0x00) );
54 }
55
56 LPPIDLDATA _dbg_ILGetDataPointer(LPITEMIDLIST pidl)
57 {
58         if(pidl && pidl->mkid.cb != 0x00)
59           return (LPPIDLDATA) &(pidl->mkid.abID);
60         return NULL;
61 }
62
63 LPSTR _dbg_ILGetTextPointer(LPCITEMIDLIST pidl)
64 {
65         LPPIDLDATA pdata =_dbg_ILGetDataPointer(pidl);
66
67         if (pdata)
68         {
69           switch (pdata->type)
70           {
71             case PT_MYCOMP:
72             case PT_SPECIAL:
73               return NULL;
74
75             case PT_DRIVE:
76             case PT_DRIVE1:
77             case PT_DRIVE2:
78             case PT_DRIVE3:
79               return (LPSTR)&(pdata->u.drive.szDriveName);
80
81             case PT_FOLDER:
82             case PT_FOLDER1:
83             case PT_VALUE:
84             case PT_IESPECIAL1:
85             case PT_IESPECIAL2:
86               return (LPSTR)&(pdata->u.file.szNames);
87
88             case PT_WORKGRP:
89             case PT_COMP:
90             case PT_NETWORK:
91             case PT_SHARE:
92               return (LPSTR)&(pdata->u.network.szNames);
93           }
94         }
95         return NULL;
96 }
97
98 LPSTR _dbg_ILGetSTextPointer(LPCITEMIDLIST pidl)
99 {
100         LPPIDLDATA pdata =_dbg_ILGetDataPointer(pidl);
101
102         if (pdata)
103         {
104           switch (pdata->type)
105           {
106             case PT_FOLDER:
107             case PT_VALUE:
108             case PT_IESPECIAL1:
109             case PT_IESPECIAL2:
110               return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
111
112             case PT_WORKGRP:
113               return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
114           }
115         }
116         return NULL;
117 }
118
119 REFIID _dbg_ILGetGUIDPointer(LPCITEMIDLIST pidl)
120 {
121         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
122
123         if (pdata)
124         {
125           switch (pdata->type)
126           {
127             case PT_SPECIAL:
128             case PT_MYCOMP:
129               return (REFIID) &(pdata->u.mycomp.guid);
130
131             default:
132                 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
133                 break;
134           }
135         }
136         return NULL;
137 }
138
139 DWORD _dbg_ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
140 {
141         DWORD           dwReturn=0;
142         LPSTR           szSrc;
143         GUID const *    riid;
144         char szTemp[MAX_PATH];
145
146         if (!pidl) return 0;
147
148         if (szOut)
149           *szOut = 0;
150
151         if (_dbg_ILIsDesktop(pidl))
152         {
153          /* desktop */
154           if (szOut) strncpy(szOut, "Desktop", uOutSize);
155           dwReturn = strlen ("Desktop");
156         }
157         else if (( szSrc = _dbg_ILGetTextPointer(pidl) ))
158         {
159           /* filesystem */
160           if (szOut) strncpy(szOut, szSrc, uOutSize);
161           dwReturn = strlen(szSrc);
162         }
163         else if (( riid = _dbg_ILGetGUIDPointer(pidl) ))
164         {
165           if (szOut)
166             sprintf( szOut, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
167                  riid->Data1, riid->Data2, riid->Data3,
168                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
169                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
170           dwReturn = strlen (szTemp);
171         }
172         else
173         {
174           ERR("-- no text\n");
175         }
176         return dwReturn;
177 }
178
179
180
181
182 void pdump (LPCITEMIDLIST pidl)
183 {
184         LPITEMIDLIST pidltemp = pidl;
185
186         if (!TRACE_ON(pidl)) return;
187
188         if (! pidltemp)
189         {
190           MESSAGE ("-------- pidl=NULL (Desktop)\n");
191         }
192         else
193         {
194           MESSAGE ("-------- pidl=%p\n", pidl);
195           if (pidltemp->mkid.cb)
196           {
197             do
198             {
199               DWORD dwAttrib = 0;
200               LPPIDLDATA pData   = _dbg_ILGetDataPointer(pidltemp);
201               DWORD type         = pData->type;
202               LPSTR szLongName   = _dbg_ILGetTextPointer(pidltemp);
203               LPSTR szShortName  = _dbg_ILGetSTextPointer(pidltemp);
204               char szName[MAX_PATH];
205
206               _dbg_ILSimpleGetText(pidltemp, szName, MAX_PATH);
207               if( PT_FOLDER == type)
208                 dwAttrib = pData->u.folder.uFileAttribs;
209               else if( PT_VALUE == type)
210                 dwAttrib = pData->u.file.uFileAttribs;
211
212               MESSAGE ("-- pidl=%p size=%u type=%lx attr=0x%08lx name=%s (%s,%s)\n",
213                        pidltemp, pidltemp->mkid.cb,type,dwAttrib,szName,debugstr_a(szLongName), debugstr_a(szShortName));
214
215               pidltemp = _dbg_ILGetNext(pidltemp);
216
217             } while (pidltemp->mkid.cb);
218           }
219           else
220           {
221             MESSAGE ("empty pidl (Desktop)\n");
222           }
223           pcheck(pidl);
224         }
225 }
226 #define BYTES_PRINTED 32
227 BOOL pcheck (LPCITEMIDLIST pidl)
228 {
229         DWORD type, ret=TRUE;
230         LPITEMIDLIST pidltemp = pidl;
231
232         if (pidltemp && pidltemp->mkid.cb)
233         { do
234           { type   = _dbg_ILGetDataPointer(pidltemp)->type;
235             switch (type)
236             { case PT_DESKTOP:
237               case PT_MYCOMP:
238               case PT_SPECIAL:
239               case PT_DRIVE:
240               case PT_DRIVE1:
241               case PT_DRIVE2:
242               case PT_DRIVE3:
243               case PT_FOLDER:
244               case PT_VALUE:
245               case PT_FOLDER1:
246               case PT_WORKGRP:
247               case PT_COMP:
248               case PT_NETWORK:
249               case PT_IESPECIAL1:
250               case PT_IESPECIAL2:
251               case PT_SHARE:
252                 break;
253               default:
254               {
255                 char szTemp[BYTES_PRINTED*4 + 1];
256                 int i;
257                 unsigned char c;
258
259                 memset(szTemp, ' ', BYTES_PRINTED*4 + 1);
260                 for ( i = 0; (i<pidltemp->mkid.cb) && (i<BYTES_PRINTED); i++)
261                 {
262                   c = ((unsigned char *)pidltemp)[i];
263
264                   szTemp[i*3+0] = ((c>>4)>9)? (c>>4)+55 : (c>>4)+48;
265                   szTemp[i*3+1] = ((0x0F&c)>9)? (0x0F&c)+55 : (0x0F&c)+48;
266                   szTemp[i*3+2] = ' ';
267                   szTemp[i+BYTES_PRINTED*3]  =  (c>=0x20 && c <=0x80) ? c : '.';
268                 }
269                 szTemp[BYTES_PRINTED*4] = 0x00;
270                 ERR("unknown IDLIST type size=%u type=%lx\n%s\n",pidltemp->mkid.cb,type, szTemp);
271                 ret = FALSE;
272               }
273             }
274             pidltemp = _dbg_ILGetNext(pidltemp);
275           } while (pidltemp->mkid.cb);
276         }
277         return ret;
278 }
279
280 static char shdebugstr_buf[100];
281
282
283 static struct {
284         REFIID  riid;
285         char    *name;
286 } InterfaceDesc[] = {
287         {&IID_IUnknown,                 "IID_IUnknown"},
288         {&IID_IShellView,               "IID_IShellView"},
289         {&IID_IOleCommandTarget,        "IID_IOleCommandTarget"},
290         {&IID_IDropTarget,              "IID_IDropTarget"},
291         {&IID_IDropSource,              "IID_IDropSource"},
292         {&IID_IViewObject,              "IID_IViewObject"},
293         {&IID_IContextMenu,             "IID_IContextMenu"},
294         {&IID_IShellExtInit,            "IID_IShellExtInit"},
295         {&IID_IShellFolder,             "IID_IShellFolder"},
296         {&IID_IShellFolder2,            "IID_IShellFolder2"},
297         {&IID_IPersist,                 "IID_IPersist"},
298         {&IID_IPersistFolder,           "IID_IPersistFolder"},
299         {&IID_IPersistFolder2,          "IID_IPersistFolder2"},
300         {&IID_IPersistFolder3,          "IID_IPersistFolder3"},
301         {&IID_IExtractIconA,            "IID_IExtractIconA"},
302         {&IID_IDataObject,              "IID_IDataObject"},
303         {&IID_IDataObject,              "IID_IDataObject"},
304         {NULL,NULL}};
305
306 const char * shdebugstr_guid( const struct _GUID *id )
307 {
308         int i;
309         char* name = NULL;
310         char clsidbuf[100];
311
312         if (!id) {
313           strcpy (shdebugstr_buf, "(null)");
314         } else {
315             for (i=0;InterfaceDesc[i].riid && !name;i++) {
316                 if (IsEqualIID(InterfaceDesc[i].riid, id)) name = InterfaceDesc[i].name;
317             }
318             if (!name) {
319                 if (HCR_GetClassName(id, clsidbuf, 100))
320                     name = clsidbuf;
321             }
322
323             sprintf( shdebugstr_buf, "\n\t{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x} (%s)",
324                  id->Data1, id->Data2, id->Data3,
325                  id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
326                  id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], name ? name : "unknown" );
327         }
328         return shdebugstr_buf;
329 }