- correct return value of SHGetPathFromIDList[AW]() for virtual
[wine] / dlls / shell32 / pidl.c
1 /*
2  *      pidl Handling
3  *
4  *      Copyright 1998  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  * NOTES
21  *  a pidl == NULL means desktop and is legal
22  *
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "objbase.h"
36 #include "shlguid.h"
37 #include "winerror.h"
38 #include "winnls.h"
39 #include "winternl.h"
40 #include "undocshell.h"
41 #include "shell32_main.h"
42 #include "shellapi.h"
43 #include "shlwapi.h"
44
45 #include "pidl.h"
46 #include "debughlp.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
50 WINE_DECLARE_DEBUG_CHANNEL(shell);
51
52 /* from comctl32.dll */
53 extern LPVOID WINAPI Alloc(INT);
54 extern BOOL WINAPI Free(LPVOID);
55
56 /*************************************************************************
57  * ILGetDisplayNameEx           [SHELL32.186]
58  *
59  * Retrieves the display name of an ItemIDList
60  *
61  * PARAMS
62  *  psf        [I]   Shell Folder to start with, if NULL the desktop is used
63  *  pidl       [I]   ItemIDList relativ to the psf to get the display name for
64  *  path       [O]   Filled in with the display name, assumed to be at least MAX_PATH long
65  *  type       [I]   Type of display name to retrieve
66  *                    0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
67  *                    1 = SHGDN_NORMAL relative to the root folder
68  *                    2 = SHGDN_INFOLDER relative to the root folder, only the last name
69  *
70  * RETURNS
71  *  True if the display name could be retrieved successfully, False otherwise
72  */
73 BOOL WINAPI ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
74 {
75         BOOL ret = FALSE;
76         WCHAR wPath[MAX_PATH];
77
78         TRACE("%p %p %p %ld\n", psf, pidl, path, type);
79
80         if (!pidl || !path)
81           return FALSE;
82
83         ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
84         WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
85         TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
86
87         return ret;
88 }
89
90 BOOL WINAPI ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
91 {
92         LPSHELLFOLDER psfParent, lsf = psf;
93         HRESULT ret = NO_ERROR;
94         LPCITEMIDLIST pidllast;
95         STRRET strret;
96         DWORD flag;
97
98         TRACE("%p %p %p %ld\n", psf, pidl, path, type);
99
100         if (!pidl || !path)
101           return FALSE;
102
103         if (!lsf)
104         {
105           ret = SHGetDesktopFolder(&lsf);
106           if (FAILED(ret))
107             return FALSE;
108         }
109
110         if (type >= 0 && type <= 2)
111         {
112           switch (type)
113           {
114             case ILGDN_FORPARSING:
115               flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
116               break;
117             case ILGDN_NORMAL:
118               flag = SHGDN_NORMAL;
119               break;
120             case ILGDN_INFOLDER:
121               flag = SHGDN_INFOLDER;
122               break;
123             default:
124               FIXME("Unknown type parameter = %lx", type);
125               flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
126               break;
127           }
128           if (!*(LPWORD)pidl || type == ILGDN_FORPARSING)
129           {
130             ret = IShellFolder_GetDisplayNameOf(lsf, pidl, flag, &strret);
131             if (SUCCEEDED(ret))
132             {
133               ret = StrRetToStrNW(path, MAX_PATH, &strret, pidl);
134             }
135           }
136           else
137           {
138             ret = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, &pidllast);
139             if (SUCCEEDED(ret))
140             {
141               ret = IShellFolder_GetDisplayNameOf(psfParent, pidllast, flag, &strret);
142               if (SUCCEEDED(ret))
143               {
144                 ret = StrRetToStrNW(path, MAX_PATH, &strret, pidllast);
145               }
146               IShellFolder_Release(psfParent);
147             }
148           }
149         }
150
151         TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
152
153         if (!psf)
154           IShellFolder_Release(lsf);
155         return SUCCEEDED(ret);
156 }
157
158 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
159 {
160         TRACE_(shell)("%p %p %p %ld\n", psf, pidl, path, type);
161         if (SHELL_OsIsUnicode())
162           return ILGetDisplayNameExW(psf, pidl, path, type);
163         return ILGetDisplayNameExA(psf, pidl, path, type);
164 }
165
166 /*************************************************************************
167  * ILGetDisplayName                     [SHELL32.15]
168  */
169 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
170 {
171         TRACE_(shell)("%p %p\n", pidl, path);
172         if (SHELL_OsIsUnicode())
173           return ILGetDisplayNameExW(NULL, pidl, path, ILGDN_FORPARSING);
174         return ILGetDisplayNameExA(NULL, pidl, path, ILGDN_FORPARSING);
175 }
176
177 /*************************************************************************
178  * ILFindLastID [SHELL32.16]
179  *
180  * NOTES
181  *   observed: pidl=Desktop return=pidl
182  */
183 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
184 {       LPCITEMIDLIST   pidlLast = pidl;
185
186         TRACE("(pidl=%p)\n",pidl);
187
188         while (pidl->mkid.cb)
189         {
190           pidlLast = pidl;
191           pidl = ILGetNext(pidl);
192         }
193         return (LPITEMIDLIST)pidlLast;
194 }
195 /*************************************************************************
196  * ILRemoveLastID [SHELL32.17]
197  *
198  * NOTES
199  *   when pidl=Desktop return=FALSE
200  */
201 BOOL WINAPI ILRemoveLastID(LPCITEMIDLIST pidl)
202 {
203         TRACE_(shell)("pidl=%p\n",pidl);
204
205         if (!pidl || !pidl->mkid.cb)
206           return 0;
207         ILFindLastID(pidl)->mkid.cb = 0;
208         return 1;
209 }
210
211 /*************************************************************************
212  * ILClone [SHELL32.18]
213  *
214  * NOTES
215  *    duplicate an idlist
216  */
217 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
218 { DWORD    len;
219   LPITEMIDLIST  newpidl;
220
221   if (!pidl)
222     return NULL;
223
224   len = ILGetSize(pidl);
225   newpidl = (LPITEMIDLIST)SHAlloc(len);
226   if (newpidl)
227     memcpy(newpidl,pidl,len);
228
229   TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
230   pdump(pidl);
231
232   return newpidl;
233 }
234 /*************************************************************************
235  * ILCloneFirst [SHELL32.19]
236  *
237  * NOTES
238  *  duplicates the first idlist of a complex pidl
239  */
240 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
241 {       DWORD len;
242         LPITEMIDLIST pidlNew = NULL;
243
244         TRACE("pidl=%p \n",pidl);
245         pdump(pidl);
246
247         if (pidl)
248         {
249           len = pidl->mkid.cb;
250           pidlNew = (LPITEMIDLIST) SHAlloc (len+2);
251           if (pidlNew)
252           {
253             memcpy(pidlNew,pidl,len+2);         /* 2 -> mind a desktop pidl */
254
255             if (len)
256               ILGetNext(pidlNew)->mkid.cb = 0x00;
257           }
258         }
259         TRACE("-- newpidl=%p\n",pidlNew);
260
261         return pidlNew;
262 }
263
264 /*************************************************************************
265  * ILLoadFromStream (SHELL32.26)
266  *
267  * NOTES
268  *   the first two bytes are the len, the pidl is following then
269  */
270 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
271 {       WORD            wLen = 0;
272         DWORD           dwBytesRead;
273         HRESULT         ret = E_FAIL;
274
275
276         TRACE_(shell)("%p %p\n", pStream ,  ppPidl);
277
278         if (*ppPidl)
279         { SHFree(*ppPidl);
280           *ppPidl = NULL;
281         }
282
283         IStream_AddRef (pStream);
284
285         if (SUCCEEDED(IStream_Read(pStream, (LPVOID)&wLen, 2, &dwBytesRead)))
286         {
287           TRACE("PIDL length is %d\n", wLen);
288           if (wLen != 0) {
289             *ppPidl = SHAlloc (wLen);
290             if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead))) {
291                 TRACE("Stream read OK\n");
292                 ret = S_OK;
293             } else {
294                 WARN("reading pidl failed\n");
295                 SHFree(*ppPidl);
296                 *ppPidl = NULL;
297             }
298           } else {
299             *ppPidl = NULL;
300             ret = S_OK;
301           }
302         }
303
304         /* we are not yet fully compatible */
305         if (*ppPidl && !pcheck(*ppPidl))
306         {
307           WARN("Check failed\n");
308           SHFree(*ppPidl);
309           *ppPidl = NULL;
310         }
311
312         
313         IStream_Release (pStream);
314         TRACE("done\n");
315         return ret;
316 }
317
318 /*************************************************************************
319  * ILSaveToStream (SHELL32.27)
320  *
321  * NOTES
322  *   the first two bytes are the len, the pidl is following then
323  */
324 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
325 {
326         LPCITEMIDLIST   pidl;
327         WORD            wLen = 0;
328         HRESULT         ret = E_FAIL;
329
330         TRACE_(shell)("%p %p\n", pStream, pPidl);
331
332         IStream_AddRef (pStream);
333
334         pidl = pPidl;
335         while (pidl->mkid.cb)
336         {
337           wLen += sizeof(WORD) + pidl->mkid.cb;
338           pidl = ILGetNext(pidl);
339         }
340
341         if (SUCCEEDED(IStream_Write(pStream, (LPVOID)&wLen, 2, NULL)))
342         {
343           if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
344           { ret = S_OK;
345           }
346         }
347         IStream_Release (pStream);
348
349         return ret;
350 }
351
352 /*************************************************************************
353  * SHILCreateFromPath        [SHELL32.28]
354  *
355  * Create an ItemIDList from a path
356  *
357  * PARAMS
358  *  path       [I]
359  *  ppidl      [O]
360  *  attributes [I/O] requested attributes on call and actual attributes when
361  *                   the function returns
362  *
363  * RETURNS
364  *  NO_ERROR if successful, or an OLE errer code otherwise
365  *
366  * NOTES
367  *  Wrapper for IShellFolder_ParseDisplayName().
368  */
369 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
370 {
371         LPSHELLFOLDER sf;
372         WCHAR lpszDisplayName[MAX_PATH];
373         DWORD pchEaten;
374         HRESULT ret = E_FAIL;
375
376         TRACE_(shell)("%s %p 0x%08lx\n", path, ppidl, attributes ? *attributes : 0);
377
378         if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
379           lpszDisplayName[MAX_PATH-1] = 0;
380
381         if (SUCCEEDED (SHGetDesktopFolder(&sf)))
382         {
383           ret = IShellFolder_ParseDisplayName(sf, 0, NULL, lpszDisplayName, &pchEaten, ppidl, attributes);
384           IShellFolder_Release(sf);
385         }
386         return ret;
387 }
388
389 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
390 {
391         LPSHELLFOLDER sf;
392         DWORD pchEaten;
393         HRESULT ret = E_FAIL;
394
395         TRACE_(shell)("%s %p 0x%08lx\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
396
397         if (SUCCEEDED (SHGetDesktopFolder(&sf)))
398         {
399           ret = IShellFolder_ParseDisplayName(sf, 0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
400           IShellFolder_Release(sf);
401         }
402         return ret;
403 }
404
405 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
406 {
407         if ( SHELL_OsIsUnicode())
408           return SHILCreateFromPathW (path, ppidl, attributes);
409         return SHILCreateFromPathA (path, ppidl, attributes);
410 }
411
412 /*************************************************************************
413  * SHCloneSpecialIDList      [SHELL32.89]
414  *
415  * Create an ItemIDList to one of the special folders.
416
417  * PARAMS
418  *  hwndOwner   [in]
419  *  nFolder             [in]    CSIDL_xxxxx
420  *  fCreate             [in]    Create folder if it does not exist
421  *
422  * RETURNS
423  *  Success: The newly created pidl
424  *  Failure: NULL, if inputs are invalid.
425  *
426  * NOTES
427  *  exported by ordinal.
428  *  Caller is responsible for deallocating the returned ItemIDList with the
429  *  shells IMalloc interface, aka ILFree.
430  */
431 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, DWORD nFolder, BOOL fCreate)
432 {       LPITEMIDLIST ppidl;
433         TRACE_(shell)("(hwnd=%p,csidl=0x%lx,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
434
435         if (fCreate)
436           nFolder |= CSIDL_FLAG_CREATE;
437
438         SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
439         return ppidl;
440 }
441
442 /*************************************************************************
443  * ILGlobalClone             [SHELL32.20]
444  *
445  * Clones an ItemIDList using Alloc.
446  *
447  * PARAMS
448  *  pidl       [I]   ItemIDList to clone
449  *
450  * RETURNS
451  *  Newly allocated ItemIDList.
452  *
453  * NOTES
454  *  exported by ordinal.
455  */
456 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
457 {       DWORD    len;
458         LPITEMIDLIST  newpidl;
459
460         if (!pidl)
461           return NULL;
462
463         len = ILGetSize(pidl);
464         newpidl = (LPITEMIDLIST)Alloc(len);
465         if (newpidl)
466           memcpy(newpidl,pidl,len);
467
468         TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
469         pdump(pidl);
470
471         return newpidl;
472 }
473
474 /*************************************************************************
475  * ILIsEqual [SHELL32.21]
476  *
477  */
478 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
479 {
480         char    szData1[MAX_PATH];
481         char    szData2[MAX_PATH];
482
483         LPCITEMIDLIST pidltemp1 = pidl1;
484         LPCITEMIDLIST pidltemp2 = pidl2;
485
486         TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
487
488         /* explorer reads from registry directly (StreamMRU),
489            so we can only check here */
490         if ((!pcheck (pidl1)) || (!pcheck (pidl2))) return FALSE;
491
492         pdump (pidl1);
493         pdump (pidl2);
494
495         if ( (!pidl1) || (!pidl2) ) return FALSE;
496
497         while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
498         {
499             _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
500             _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
501
502             if (strcasecmp ( szData1, szData2 )!=0 )
503               return FALSE;
504
505             pidltemp1 = ILGetNext(pidltemp1);
506             pidltemp2 = ILGetNext(pidltemp2);
507         }
508
509         if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
510         {
511           return TRUE;
512         }
513
514         return FALSE;
515 }
516 /*************************************************************************
517  * ILIsParent                [SHELL32.23]
518  *
519  * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
520  *
521  * PARAMS
522  *  pidlParent [I]
523  *  pidlChild  [I]
524  *  bImmediate [I]   only return true if the parent is the direct parent
525  *                   of the child
526  *
527  * RETURNS
528  *  True if the parent ItemIDlist is a complete part of the child ItemIdList,
529  *  False otherwise.
530  *
531  * NOTES
532  *  parent = a/b, child = a/b/c -> true, c is in folder a/b
533  *  child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
534  *  child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
535  */
536 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
537 {
538         char    szData1[MAX_PATH];
539         char    szData2[MAX_PATH];
540
541         LPCITEMIDLIST pParent = pidlParent;
542         LPCITEMIDLIST pChild = pidlChild;
543
544         TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
545
546         if (!pParent || !pChild) return FALSE;
547
548         while (pParent->mkid.cb && pChild->mkid.cb)
549         {
550           _ILSimpleGetText(pParent, szData1, MAX_PATH);
551           _ILSimpleGetText(pChild, szData2, MAX_PATH);
552
553           if (strcasecmp ( szData1, szData2 )!=0 )
554             return FALSE;
555
556           pParent = ILGetNext(pParent);
557           pChild = ILGetNext(pChild);
558         }
559
560         if ( pParent->mkid.cb || ! pChild->mkid.cb) /* child shorter or has equal length to parent */
561           return FALSE;
562
563         if ( ILGetNext(pChild)->mkid.cb && bImmediate) /* not immediate descent */
564           return FALSE;
565
566         return TRUE;
567 }
568
569 /*************************************************************************
570  * ILFindChild               [SHELL32.24]
571  *
572  *  Compares elements from pidl1 and pidl2.
573  *
574  * PARAMS
575  *  pidl1      [I]
576  *  pidl2      [I]
577  *
578  * RETURNS
579  *  pidl1 is desktop      pidl2
580  *  pidl1 shorter pidl2   pointer to first different element of pidl2
581  *                        if there was at least one equal element
582  *  pidl2 shorter pidl1   0
583  *  pidl2 equal pidl1     pointer to last 0x00-element of pidl2
584  *
585  * NOTES
586  *  exported by ordinal.
587  */
588 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
589 {
590         char    szData1[MAX_PATH];
591         char    szData2[MAX_PATH];
592
593         LPCITEMIDLIST pidltemp1 = pidl1;
594         LPCITEMIDLIST pidltemp2 = pidl2;
595         LPCITEMIDLIST ret=NULL;
596
597         TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
598
599         /* explorer reads from registry directly (StreamMRU),
600            so we can only check here */
601         if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
602           return FALSE;
603
604         pdump (pidl1);
605         pdump (pidl2);
606
607         if ( _ILIsDesktop(pidl1) )
608         {
609           ret = pidl2;
610         }
611         else
612         {
613           while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
614           {
615             _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
616             _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
617
618             if (strcasecmp(szData1,szData2))
619               break;
620
621             pidltemp1 = ILGetNext(pidltemp1);
622             pidltemp2 = ILGetNext(pidltemp2);
623             ret = pidltemp2;
624           }
625
626           if (pidltemp1->mkid.cb)
627           {
628             ret = NULL; /* elements of pidl1 left*/
629           }
630         }
631         TRACE_(shell)("--- %p\n", ret);
632         return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
633 }
634
635 /*************************************************************************
636  * ILCombine                 [SHELL32.25]
637  *
638  * Concatenates two complex ItemIDLists.
639  *
640  * PARAMS
641  *  pidl1      [I]   first complex ItemIDLists
642  *  pidl2      [I]   complex ItemIDLists to append
643  *
644  * RETURNS
645  *  if both pidl's == NULL      NULL
646  *  if pidl1 == NULL            cloned pidl2
647  *  if pidl2 == NULL            cloned pidl1
648  *  otherwise new pidl with pidl2 appended to pidl1
649  *
650  * NOTES
651  *  exported by ordinal.
652  *  Does not destroy the passed in ItemIDLists!
653  */
654 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
655 {
656         DWORD    len1,len2;
657         LPITEMIDLIST  pidlNew;
658
659         TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
660
661         if(!pidl1 && !pidl2) return NULL;
662
663         pdump (pidl1);
664         pdump (pidl2);
665
666         if(!pidl1)
667         {
668           pidlNew = ILClone(pidl2);
669           return pidlNew;
670         }
671
672         if(!pidl2)
673         {
674           pidlNew = ILClone(pidl1);
675           return pidlNew;
676         }
677
678         len1  = ILGetSize(pidl1)-2;
679         len2  = ILGetSize(pidl2);
680         pidlNew  = SHAlloc(len1+len2);
681
682         if (pidlNew)
683         {
684           memcpy(pidlNew,pidl1,len1);
685           memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
686         }
687
688         /*  TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
689         return pidlNew;
690 }
691 /*************************************************************************
692  *  SHGetRealIDL [SHELL32.98]
693  *
694  * NOTES
695  */
696 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
697 {
698         FIXME("sf=%p pidlSimple=%p pidlReal=%p\n", lpsf, pidlSimple, pidlReal);
699
700         pdump (pidlSimple);
701         return 0;
702 }
703
704 /*************************************************************************
705  *  SHLogILFromFSIL [SHELL32.95]
706  *
707  * NOTES
708  *  pild = CSIDL_DESKTOP        ret = 0
709  *  pild = CSIDL_DRIVES         ret = 0
710  */
711 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
712 {
713         FIXME("(pidl=%p)\n",pidl);
714
715         pdump(pidl);
716
717         return 0;
718 }
719
720 /*************************************************************************
721  * ILGetSize                 [SHELL32.152]
722  *
723  * Gets the byte size of an ItemIDList including zero terminator
724  *
725  * PARAMS
726  *  pidl       [I]   ItemIDList
727  *
728  * RETURNS
729  *  size of pidl in bytes
730  *
731  * NOTES
732  *  exported by ordinal
733  */
734 DWORD WINAPI ILGetSize(LPCITEMIDLIST pidl)
735 {
736         LPCSHITEMID si = &(pidl->mkid);
737         DWORD  len=0;
738
739         if (pidl)
740         { while (si->cb)
741           { len += si->cb;
742             si  = (LPSHITEMID)(((LPBYTE)si)+si->cb);
743           }
744           len += 2;
745         }
746         TRACE("pidl=%p size=%lu\n",pidl, len);
747         return len;
748 }
749
750 /*************************************************************************
751  * ILGetNext                 [SHELL32.153]
752  *
753  * Gets the next ItemID of an ItemIDList
754  *
755  * PARAMS
756  *  pidl       [I]   ItemIDList
757  *
758  * RETURNS
759  *  null -> null
760  *  desktop -> null
761  *  simple pidl -> pointer to 0x0000 element
762  *
763  * NOTES
764  *  exported by ordinal.
765  */
766 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
767 {
768         WORD len;
769
770         TRACE("%p\n", pidl);
771
772         if(pidl)
773         {
774           len =  pidl->mkid.cb;
775           if (len)
776           {
777             pidl = (LPITEMIDLIST) (((LPBYTE)pidl)+len);
778             TRACE("-- %p\n", pidl);
779             return (LPITEMIDLIST)pidl;
780           }
781         }
782         return NULL;
783 }
784
785 /*************************************************************************
786  * ILAppend                  [SHELL32.154]
787  *
788  * Adds the single ItemID item to the ItemIDList indicated by pidl.
789  * If bEnd is FALSE, inserts the item in the front of the list,
790  * otherwise it adds the item to the end. (???)
791  *
792  * PARAMS
793  *  pidl       [I]   ItemIDList to extend
794  *  item       [I]   ItemID to prepend/append
795  *  bEnd       [I]   Indicates if the item should be appended
796  *
797  * NOTES
798  *  Destroys the passed in idlist! (???)
799  */
800 LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
801 {
802         LPITEMIDLIST idlRet;
803
804         WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
805
806         pdump (pidl);
807         pdump (item);
808
809         if (_ILIsDesktop(pidl))
810         {
811            idlRet = ILClone(item);
812            if (pidl)
813              SHFree (pidl);
814            return idlRet;
815         }
816
817         if (bEnd)
818         {
819           idlRet = ILCombine(pidl, item);
820         }
821         else
822         {
823           idlRet = ILCombine(item, pidl);
824         }
825
826         SHFree(pidl);
827         return idlRet;
828 }
829
830 /*************************************************************************
831  * ILFree                    [SHELL32.155]
832  *
833  * Frees memory (if not NULL) allocated by SHMalloc allocator
834  *
835  * PARAMS
836  *  pidl         [I]
837  *
838  * NOTES
839  *  exported by ordinal
840  */
841 DWORD WINAPI ILFree(LPITEMIDLIST pidl)
842 {
843         TRACE("(pidl=0x%08lx)\n",(DWORD)pidl);
844
845         if(!pidl) return FALSE;
846         SHFree(pidl);
847         return TRUE;
848 }
849
850 /*************************************************************************
851  * ILGlobalFree              [SHELL32.156]
852  *
853  * Frees memory (if not NULL) allocated by Alloc allocator
854  *
855  * PARAMS
856  *  pidl         [I]
857  *
858  * NOTES
859  *  exported by ordinal.
860  */
861 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
862 {
863         TRACE("%p\n", pidl);
864
865         if(!pidl) return;
866         Free(pidl);
867 }
868
869 /*************************************************************************
870  * ILCreateFromPathA         [SHELL32.189]
871  *
872  * Creates a complex ItemIDList from a path and returns it.
873  *
874  * PARAMS
875  *  path         [I]
876  *
877  * RETURNS
878  *  the newly created complex ItemIDList or NULL if failed
879  *
880  * NOTES
881  *  exported by ordinal.
882  */
883
884 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
885 {
886         LPITEMIDLIST pidlnew = NULL;
887
888         TRACE_(shell)("%s\n", debugstr_a(path));
889
890         if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
891           return pidlnew;
892         return NULL;
893 }
894
895 /*************************************************************************
896  * ILCreateFromPathW         [SHELL32.190]
897  */
898 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
899 {
900         LPITEMIDLIST pidlnew = NULL;
901
902         TRACE_(shell)("%s\n", debugstr_w(path));
903
904         if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
905           return pidlnew;
906         return NULL;
907 }
908
909 /*************************************************************************
910  * ILCreateFromPath          [SHELL32.157]
911  */
912 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
913 {
914         if ( SHELL_OsIsUnicode())
915           return ILCreateFromPathW (path);
916         return ILCreateFromPathA (path);
917 }
918
919 /*************************************************************************
920  * _ILParsePathW             [internal]
921  *
922  * Creates an ItemIDList from a path and returns it.
923  *
924  * PARAMS
925  *  path         [I]   path to parse and convert into an ItemIDList
926  *  lpFindFile   [I]   pointer to buffer to initialize the FileSystem
927  *                     Bind Data object with
928  *  bBindCtx     [I]   indicates to create a BindContext and assign a
929  *                     FileSystem Bind Data object
930  *  ppidl        [O]   the newly create ItemIDList
931  *  prgfInOut    [I/O] requested attributes on input and actual
932  *                     attributes on return
933  *
934  * RETURNS
935  *  NO_ERROR on success or an OLE error code
936  *
937  * NOTES
938  *  If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
939  *  creates a BindContext object and assigns a FileSystem Bind Data object
940  *  to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
941  *  IShellFolder uses that FileSystem Bind Data object of the BindContext
942  *  to pass data about the current path element to the next object. This
943  *  is used to avoid having to verify the current path element on disk, so
944  *  that creating an ItemIDList from a non-existent path still can work.
945  */
946 static HRESULT WINAPI _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
947                              BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
948 {
949         LPSHELLFOLDER pSF = NULL;
950         LPBC pBC = NULL;
951         HRESULT ret;
952
953         TRACE("%s %p %d (%p)->%p (%p)->0x%lx\n", debugstr_w(path), lpFindFile, bBindCtx,
954                                                  ppidl, ppidl ? *ppidl : NULL,
955                                                  prgfInOut, prgfInOut ? *prgfInOut : 0);
956
957         ret = SHGetDesktopFolder(&pSF);
958         if (FAILED(ret))
959         {
960           return ret;
961         }
962
963         if (lpFindFile || bBindCtx)
964           ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
965
966         if (SUCCEEDED(ret))
967         {
968           ret = IShellFolder_ParseDisplayName(pSF, 0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
969         }
970
971         if (pBC)
972         {
973           IBindCtx_Release(pBC);
974           pBC = NULL;
975         }
976
977         IShellFolder_Release(pSF);
978
979         if (!SUCCEEDED(ret) && ppidl)
980           *ppidl = NULL;
981
982         TRACE("%s %p 0x%lx\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
983
984         return ret;
985 }
986
987 /*************************************************************************
988  * SHSimpleIDListFromPath    [SHELL32.162]
989  *
990  * Creates a simple ItemIDList from a path and returns it. This function
991  * does not fail on non-existent paths.
992  *
993  * PARAMS
994  *  path         [I]   path to parse and convert into an ItemIDList
995  *
996  * RETURNS
997  *  the newly created simple ItemIDList
998  *
999  * NOTES
1000  *  Simple in the name does not mean a relative ItemIDList but rather a
1001  *  fully qualified list, where only the file name is filled in and the
1002  *  directory flag for those ItemID elements this is known about, eg.
1003  *  it is not the last element in the ItemIDList or the actual directory
1004  *  exists on disk.
1005  *  exported by ordinal.
1006  */
1007 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1008 {
1009         LPITEMIDLIST pidl = NULL;
1010         UNICODE_STRING wPath;
1011
1012         TRACE("%s\n", debugstr_a(lpszPath));
1013
1014         RtlCreateUnicodeStringFromAsciiz(&wPath, lpszPath);
1015
1016         _ILParsePathW(wPath.Buffer, NULL, TRUE, &pidl, NULL);
1017         RtlFreeUnicodeString(&wPath);
1018
1019         TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1020         return pidl;
1021 }
1022
1023 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1024 {
1025         LPITEMIDLIST pidl = NULL;
1026
1027         TRACE("%s\n", debugstr_w(lpszPath));
1028
1029         _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1030         TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1031         return pidl;
1032 }
1033
1034 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1035 {
1036         if ( SHELL_OsIsUnicode())
1037           return SHSimpleIDListFromPathW (lpszPath);
1038         return SHSimpleIDListFromPathA (lpszPath);
1039 }
1040
1041 /*************************************************************************
1042  * SHGetSpecialFolderLocation           [SHELL32.@]
1043  *
1044  * gets the folder locations from the registry and creates a pidl
1045  * creates missing reg keys and directories
1046  *
1047  * PARAMS
1048  *   hwndOwner [I]
1049  *   nFolder   [I] CSIDL_xxxxx
1050  *   ppidl     [O] PIDL of a special folder
1051  *
1052  * NOTES
1053  *   In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
1054  *   directory. If the directory is missing it returns a x80070002.
1055  */
1056 HRESULT WINAPI SHGetSpecialFolderLocation(
1057         HWND hwndOwner,
1058         INT nFolder,
1059         LPITEMIDLIST * ppidl)
1060 {
1061         CHAR            szPath[MAX_PATH];
1062         HRESULT         hr = E_INVALIDARG;
1063
1064         TRACE_(shell)("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
1065
1066         if (ppidl)
1067         {
1068           *ppidl = NULL;
1069           switch (nFolder)
1070           {
1071             case CSIDL_DESKTOP:
1072               *ppidl = _ILCreateDesktop();
1073               break;
1074
1075             case CSIDL_DRIVES:
1076               *ppidl = _ILCreateMyComputer();
1077               break;
1078
1079             case CSIDL_NETWORK:
1080               *ppidl = _ILCreateNetwork ();
1081               break;
1082
1083             case CSIDL_CONTROLS:
1084               *ppidl = _ILCreateControl ();
1085               break;
1086
1087             case CSIDL_PRINTERS:
1088               *ppidl = _ILCreatePrinter ();
1089               break;
1090
1091             case CSIDL_BITBUCKET:
1092               *ppidl = _ILCreateBitBucket ();
1093               break;
1094
1095             default:
1096               if (SHGetSpecialFolderPathA(hwndOwner, szPath, nFolder, TRUE))
1097               {
1098                 DWORD attributes=0;
1099                 TRACE_(shell)("Value=%s\n",szPath);
1100                 hr = SHILCreateFromPathA(szPath, ppidl, &attributes);
1101               }
1102           }
1103           if(*ppidl) hr = NOERROR;
1104         }
1105
1106         TRACE_(shell)("-- (new pidl %p)\n",*ppidl);
1107         return hr;
1108 }
1109
1110 /*************************************************************************
1111  * SHGetFolderLocation [SHELL32.@]
1112  *
1113  * NOTES
1114  *  the pidl can be a simple one. since we cant get the path out of the pidl
1115  *  we have to take all data from the pidl
1116  */
1117 HRESULT WINAPI SHGetFolderLocation(
1118         HWND hwnd,
1119         int csidl,
1120         HANDLE hToken,
1121         DWORD dwFlags,
1122         LPITEMIDLIST *ppidl)
1123 {
1124         FIXME("%p 0x%08x %p 0x%08lx %p\n",
1125          hwnd, csidl, hToken, dwFlags, ppidl);
1126         return SHGetSpecialFolderLocation(hwnd, csidl, ppidl);
1127 }
1128
1129 /*************************************************************************
1130  * SHGetDataFromIDListA [SHELL32.247]
1131  *
1132  * NOTES
1133  *  the pidl can be a simple one. since we cant get the path out of the pidl
1134  *  we have to take all data from the pidl
1135  */
1136 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
1137 {
1138         TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1139
1140         pdump(pidl);
1141         if (!psf || !dest )  return E_INVALIDARG;
1142
1143         switch (nFormat)
1144         {
1145           case SHGDFIL_FINDDATA:
1146             {
1147                WIN32_FIND_DATAA * pfd = dest;
1148
1149                if (len < (int)sizeof(WIN32_FIND_DATAA)) return E_INVALIDARG;
1150
1151                ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1152                _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1153                pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1154                pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1155                lstrcpynA(pfd->cFileName,_ILGetTextPointer(pidl), MAX_PATH);
1156                lstrcpynA(pfd->cAlternateFileName,_ILGetSTextPointer(pidl), 14);
1157             }
1158             return NOERROR;
1159
1160           case SHGDFIL_NETRESOURCE:
1161           case SHGDFIL_DESCRIPTIONID:
1162             FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1163             break;
1164
1165           default:
1166             ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1167         }
1168
1169         return E_INVALIDARG;
1170 }
1171 /*************************************************************************
1172  * SHGetDataFromIDListW [SHELL32.248]
1173  *
1174  */
1175 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
1176 {
1177         TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1178
1179         pdump(pidl);
1180
1181         if (! psf || !dest )  return E_INVALIDARG;
1182
1183         switch (nFormat)
1184         {
1185           case SHGDFIL_FINDDATA:
1186             {
1187                WIN32_FIND_DATAW * pfd = dest;
1188
1189                if (len < (int)sizeof(WIN32_FIND_DATAW)) return E_INVALIDARG;
1190
1191                ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1192                _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1193                pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1194                pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1195                if (!MultiByteToWideChar( CP_ACP, 0, _ILGetTextPointer(pidl), -1,
1196                                          pfd->cFileName, MAX_PATH ))
1197                    pfd->cFileName[MAX_PATH-1] = 0;
1198                if (!MultiByteToWideChar( CP_ACP, 0, _ILGetSTextPointer(pidl), -1,
1199                                          pfd->cAlternateFileName, 14 ))
1200                    pfd->cFileName[13] = 0;
1201             }
1202             return NOERROR;
1203           case SHGDFIL_NETRESOURCE:
1204           case SHGDFIL_DESCRIPTIONID:
1205             FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1206             break;
1207
1208           default:
1209             ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1210         }
1211
1212         return E_INVALIDARG;
1213 }
1214
1215 /*************************************************************************
1216  * SHGetPathFromIDListA         [SHELL32.@][NT 4.0: SHELL32.220]
1217  *
1218  * PARAMETERS
1219  *  pidl,   [IN] pidl
1220  *  pszPath [OUT] path
1221  *
1222  * RETURNS
1223  *  path from a passed PIDL.
1224  *
1225  * NOTES
1226  *      NULL returns FALSE
1227  *      desktop pidl gives path to desktop directory back
1228  *      special pidls returning FALSE
1229  */
1230 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1231 {
1232         HRESULT hr;
1233         STRRET str;
1234         LPSHELLFOLDER shellfolder;
1235
1236         TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1237         pdump(pidl);
1238
1239         if (!pidl)
1240                 return FALSE;
1241
1242         hr = SHGetDesktopFolder(&shellfolder);
1243         if (SUCCEEDED (hr)) {
1244             hr = IShellFolder_GetDisplayNameOf(shellfolder, pidl, SHGDN_FORPARSING, &str);
1245             if(SUCCEEDED(hr)) {
1246                 StrRetToStrNA (pszPath, MAX_PATH, &str, pidl);
1247             }
1248             IShellFolder_Release(shellfolder);
1249         }
1250
1251         /* don't allow to return displaynames of the form "::{guid}" */
1252         if (pszPath[0]==':' && pszPath[1]==':') {
1253                 *pszPath = '\0';
1254                 return FALSE;
1255         }
1256
1257         TRACE_(shell)("-- %s, 0x%08lx\n",pszPath, hr);
1258         return SUCCEEDED(hr);
1259 }
1260 /*************************************************************************
1261  * SHGetPathFromIDListW                         [SHELL32.@]
1262  */
1263 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1264 {
1265         HRESULT hr;
1266         STRRET str;
1267         LPSHELLFOLDER shellfolder;
1268
1269         TRACE_(shell)("(pidl=%p,%p)\n", pidl, debugstr_w(pszPath));
1270         pdump(pidl);
1271
1272         if (!pidl) return FALSE;
1273
1274         hr = SHGetDesktopFolder(&shellfolder);
1275         if (SUCCEEDED(hr)) {
1276             hr = IShellFolder_GetDisplayNameOf(shellfolder, pidl, SHGDN_FORPARSING, &str);
1277             if (SUCCEEDED(hr)) {
1278                 StrRetToStrNW(pszPath, MAX_PATH, &str, pidl);
1279             }
1280             IShellFolder_Release(shellfolder);
1281         }
1282
1283         /* don't allow to return displaynames of the form "::{guid}" */
1284         if (pszPath[0]==':' && pszPath[1]==':') {
1285                 *pszPath = '\0';
1286                 return FALSE;
1287         }
1288
1289         TRACE_(shell)("-- %s, 0x%08lx\n",debugstr_w(pszPath), hr);
1290         return SUCCEEDED(hr);
1291 }
1292
1293 /*************************************************************************
1294  * SHGetPathFromIDList          [SHELL32.@][NT 4.0: SHELL32.219]
1295  */
1296 BOOL WINAPI SHGetPathFromIDListAW(LPCITEMIDLIST pidl,LPVOID pszPath)
1297 {
1298         TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1299
1300         if (SHELL_OsIsUnicode())
1301           return SHGetPathFromIDListW(pidl,pszPath);
1302         return SHGetPathFromIDListA(pidl,pszPath);
1303 }
1304
1305 /*************************************************************************
1306  *      SHBindToParent          [shell version 5.0]
1307  */
1308 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1309 {
1310         IShellFolder    * psf;
1311         LPITEMIDLIST    pidlChild, pidlParent;
1312         HRESULT         hr=E_FAIL;
1313
1314         TRACE_(shell)("pidl=%p\n", pidl);
1315         pdump(pidl);
1316
1317         *ppv = NULL;
1318         if (ppidlLast) *ppidlLast = NULL;
1319
1320         if (_ILIsPidlSimple(pidl))
1321         {
1322           /* we are on desktop level */
1323           if (ppidlLast)
1324             *ppidlLast = ILClone(pidl);
1325           hr = SHGetDesktopFolder((IShellFolder**)ppv);
1326         }
1327         else
1328         {
1329           pidlChild =  ILClone(ILFindLastID(pidl));
1330           pidlParent = ILClone(pidl);
1331           ILRemoveLastID(pidlParent);
1332
1333           hr = SHGetDesktopFolder(&psf);
1334
1335           if (SUCCEEDED(hr))
1336             hr = IShellFolder_BindToObject(psf, pidlParent, NULL, riid, ppv);
1337
1338           if (SUCCEEDED(hr) && ppidlLast)
1339             *ppidlLast = pidlChild;
1340           else
1341             ILFree (pidlChild);
1342
1343           SHFree (pidlParent);
1344           if (psf) IShellFolder_Release(psf);
1345         }
1346
1347
1348         TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08lx\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1349         return hr;
1350 }
1351
1352 /**************************************************************************
1353  *
1354  *              internal functions
1355  *
1356  *      ### 1. section creating pidls ###
1357  *
1358  *************************************************************************
1359  *  _ILCreateDesktop()
1360  *  _ILCreateIExplore()
1361  *  _ILCreateMyComputer()
1362  *  _ILCreateDrive()
1363  *  _ILCreateFolder()
1364  *  _ILCreateValue()
1365  */
1366 LPITEMIDLIST _ILCreateDesktop()
1367 {       TRACE("()\n");
1368         return _ILCreate(PT_DESKTOP, NULL, 0);
1369 }
1370
1371 LPITEMIDLIST _ILCreateMyComputer()
1372 {       TRACE("()\n");
1373         return _ILCreate(PT_MYCOMP, &CLSID_MyComputer, sizeof(GUID));
1374 }
1375
1376 LPITEMIDLIST _ILCreateIExplore()
1377 {       TRACE("()\n");
1378         return _ILCreate(PT_MYCOMP, &CLSID_Internet, sizeof(GUID));
1379 }
1380
1381 LPITEMIDLIST _ILCreateControl()
1382 {       TRACE("()\n");
1383         return _ILCreate(PT_SPECIAL, &CLSID_ControlPanel, sizeof(GUID));
1384 }
1385
1386 LPITEMIDLIST _ILCreatePrinter()
1387 {       TRACE("()\n");
1388         return _ILCreate(PT_SPECIAL, &CLSID_Printers, sizeof(GUID));
1389 }
1390
1391 LPITEMIDLIST _ILCreateNetwork()
1392 {       TRACE("()\n");
1393         return _ILCreate(PT_MYCOMP, &CLSID_NetworkPlaces, sizeof(GUID));
1394 }
1395
1396 LPITEMIDLIST _ILCreateBitBucket()
1397 {       TRACE("()\n");
1398         return _ILCreate(PT_MYCOMP, &CLSID_RecycleBin, sizeof(GUID));
1399 }
1400
1401 LPITEMIDLIST _ILCreateDrive( LPCSTR lpszNew)
1402 {       char sTemp[4];
1403         lstrcpynA (sTemp,lpszNew,4);
1404         sTemp[2]='\\';
1405         sTemp[3]=0x00;
1406         TRACE("(%s)\n",sTemp);
1407         return _ILCreate(PT_DRIVE,(LPVOID)&sTemp[0],4);
1408 }
1409
1410 LPITEMIDLIST _ILCreateFolder( WIN32_FIND_DATAA * stffile )
1411 {
1412         char    buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1413         char *  pbuff = buff;
1414         ULONG   len, len1;
1415         LPITEMIDLIST pidl;
1416
1417         TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1418
1419         /* prepare buffer with both names */
1420         len = strlen (stffile->cFileName) + 1;
1421         memcpy (pbuff, stffile->cFileName, len);
1422         pbuff += len;
1423
1424         if (stffile->cAlternateFileName)
1425         {
1426           len1 = strlen (stffile->cAlternateFileName)+1;
1427           memcpy (pbuff, stffile->cAlternateFileName, len1);
1428         }
1429         else
1430         {
1431           len1 = 1;
1432           *pbuff = 0x00;
1433         }
1434
1435         pidl = _ILCreate(PT_FOLDER, (LPVOID)buff, len + len1);
1436
1437         /* set attributes */
1438         if (pidl)
1439         {
1440           LPPIDLDATA pData;
1441           pData = _ILGetDataPointer(pidl);
1442           FileTimeToDosDateTime(&(stffile->ftLastWriteTime),&pData->u.folder.uFileDate,&pData->u.folder.uFileTime);
1443           pData->u.folder.dwFileSize = stffile->nFileSizeLow;
1444           pData->u.folder.uFileAttribs = stffile->dwFileAttributes;
1445         }
1446
1447         return pidl;
1448 }
1449
1450 LPITEMIDLIST _ILCreateValue(WIN32_FIND_DATAA * stffile)
1451 {
1452         char    buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1453         char *  pbuff = buff;
1454         ULONG   len, len1;
1455         LPITEMIDLIST pidl;
1456
1457         TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1458
1459         /* prepare buffer with both names */
1460         len = strlen (stffile->cFileName) + 1;
1461         memcpy (pbuff, stffile->cFileName, len);
1462         pbuff += len;
1463
1464         if (stffile->cAlternateFileName)
1465         {
1466           len1 = strlen (stffile->cAlternateFileName)+1;
1467           memcpy (pbuff, stffile->cAlternateFileName, len1);
1468         }
1469         else
1470         {
1471           len1 = 1;
1472           *pbuff = 0x00;
1473         }
1474
1475         pidl = _ILCreate(PT_VALUE, (LPVOID)buff, len + len1);
1476
1477         /* set attributes */
1478         if (pidl)
1479         {
1480           LPPIDLDATA pData;
1481           pData = _ILGetDataPointer(pidl);
1482           FileTimeToDosDateTime(&(stffile->ftLastWriteTime),&pData->u.folder.uFileDate,&pData->u.folder.uFileTime);
1483           pData->u.folder.dwFileSize = stffile->nFileSizeLow;
1484           pData->u.folder.uFileAttribs=stffile->dwFileAttributes;
1485         }
1486
1487         return pidl;
1488 }
1489
1490 LPITEMIDLIST _ILCreateFromPathA(LPCSTR szPath)
1491 {
1492         HANDLE hFile;
1493         WIN32_FIND_DATAA stffile;
1494         LPITEMIDLIST pidl = NULL;
1495         
1496         hFile = FindFirstFileA(szPath, &stffile);
1497         if (hFile != INVALID_HANDLE_VALUE) 
1498         {
1499           if (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1500             pidl = _ILCreateFolder(&stffile);
1501           else
1502             pidl = _ILCreateValue(&stffile);
1503           FindClose(hFile);
1504         }
1505         return pidl;
1506 }
1507
1508 LPITEMIDLIST _ILCreateSpecial(LPCSTR szGUID)
1509 {
1510         IID iid;
1511
1512         if (!SUCCEEDED(SHCLSIDFromStringA(szGUID, &iid)))
1513         {
1514           ERR("%s is not a GUID\n", szGUID);
1515           return NULL;
1516         }
1517         return _ILCreate(PT_MYCOMP, &iid, sizeof(IID));
1518 }
1519
1520 /**************************************************************************
1521  *  _ILCreate()
1522  *  Creates a new PIDL
1523  *  type = PT_DESKTOP | PT_DRIVE | PT_FOLDER | PT_VALUE
1524  *  pIn = data
1525  *  uInSize = size of data (raw)
1526  */
1527
1528 LPITEMIDLIST _ILCreate(PIDLTYPE type, LPCVOID pIn, UINT uInSize)
1529 {
1530         LPITEMIDLIST   pidlOut = NULL, pidlTemp = NULL;
1531         LPPIDLDATA     pData;
1532         UINT           uSize = 0;
1533         LPSTR   pszDest;
1534
1535         TRACE("(0x%02x %p %i)\n",type,pIn,uInSize);
1536
1537         switch (type)
1538         {
1539           case PT_DESKTOP:
1540             uSize = 0;
1541             break;
1542           case PT_SPECIAL:
1543           case PT_MYCOMP:
1544             uSize = 2 + 2 + sizeof(GUID);
1545             break;
1546           case PT_DRIVE:
1547             uSize = 2 + 23;
1548             break;
1549           case PT_FOLDER:
1550           case PT_VALUE:
1551             uSize = 2 + 12 + uInSize;
1552             break;
1553           default:
1554             FIXME("can't create type: 0x%08x\n",type);
1555             return NULL;
1556         }
1557
1558         if(!(pidlOut = SHAlloc(uSize + 2))) return NULL;
1559         ZeroMemory(pidlOut, uSize + 2);
1560         pidlOut->mkid.cb = uSize;
1561
1562         switch (type)
1563         {
1564           case PT_DESKTOP:
1565             TRACE("- create Desktop\n");
1566             break;
1567
1568           case PT_SPECIAL:
1569           case PT_MYCOMP:
1570             pData = _ILGetDataPointer(pidlOut);
1571             pData->type = type;
1572             memcpy(&(pData->u.mycomp.guid), pIn, uInSize);
1573             TRACE("-- create GUID-pidl %s\n", debugstr_guid(&(pData->u.mycomp.guid)));
1574             break;
1575
1576           case PT_DRIVE:
1577             pData = _ILGetDataPointer(pidlOut);
1578             pData->type = type;
1579             pszDest = _ILGetTextPointer(pidlOut);
1580             memcpy(pszDest, pIn, uInSize);
1581             TRACE("-- create Drive: %s\n",debugstr_a(pszDest));
1582             break;
1583
1584           case PT_FOLDER:
1585           case PT_VALUE:
1586             pData = _ILGetDataPointer(pidlOut);
1587             pData->type = type;
1588             pszDest =  _ILGetTextPointer(pidlOut);
1589             memcpy(pszDest, pIn, uInSize);
1590             TRACE("-- create Value: %s\n",debugstr_a(pszDest));
1591             break;
1592         }
1593
1594         pidlTemp = ILGetNext(pidlOut);
1595         if (pidlTemp)
1596           pidlTemp->mkid.cb = 0x00;
1597
1598         TRACE("-- (pidl=%p, size=%u)\n", pidlOut, uSize);
1599         return pidlOut;
1600 }
1601
1602 /**************************************************************************
1603  *  _ILGetDrive()
1604  *
1605  *  Gets the text for the drive eg. 'c:\'
1606  *
1607  * RETURNS
1608  *  strlen (lpszText)
1609  */
1610 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1611 {       TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1612
1613         if(_ILIsMyComputer(pidl))
1614           pidl = ILGetNext(pidl);
1615
1616         if (pidl && _ILIsDrive(pidl))
1617           return _ILSimpleGetText(pidl, pOut, uSize);
1618
1619         return 0;
1620 }
1621
1622 /**************************************************************************
1623  *
1624  *      ### 2. section testing pidls ###
1625  *
1626  **************************************************************************
1627  *  _ILIsDesktop()
1628  *  _ILIsMyComputer()
1629  *  _ILIsSpecialFolder()
1630  *  _ILIsDrive()
1631  *  _ILIsFolder()
1632  *  _ILIsValue()
1633  *  _ILIsPidlSimple()
1634  */
1635 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1636 {       TRACE("(%p)\n",pidl);
1637         return pidl && pidl->mkid.cb  ? 0 : 1;
1638 }
1639
1640 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1641 {
1642         REFIID iid = _ILGetGUIDPointer(pidl);
1643
1644         TRACE("(%p)\n",pidl);
1645
1646         if (iid)
1647           return IsEqualIID(iid, &CLSID_MyComputer);
1648         return FALSE;
1649 }
1650
1651 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1652 {
1653         LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1654         TRACE("(%p)\n",pidl);
1655         return (pidl && ( (lpPData && (PT_MYCOMP== lpPData->type || PT_SPECIAL== lpPData->type)) ||
1656                           (pidl && pidl->mkid.cb == 0x00)
1657                         ));
1658 }
1659
1660 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1661 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1662         TRACE("(%p)\n",pidl);
1663         return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1664                                     PT_DRIVE1 == lpPData->type ||
1665                                     PT_DRIVE2 == lpPData->type ||
1666                                     PT_DRIVE3 == lpPData->type));
1667 }
1668
1669 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1670 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1671         TRACE("(%p)\n",pidl);
1672         return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1673 }
1674
1675 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1676 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1677         TRACE("(%p)\n",pidl);
1678         return (pidl && lpPData && PT_VALUE == lpPData->type);
1679 }
1680
1681 /**************************************************************************
1682  *      _ILIsPidlSimple
1683  */
1684 BOOL _ILIsPidlSimple ( LPCITEMIDLIST pidl)
1685 {
1686         BOOL ret = TRUE;
1687
1688         if(! _ILIsDesktop(pidl))        /* pidl=NULL or mkid.cb=0 */
1689         {
1690           WORD len = pidl->mkid.cb;
1691           LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((LPBYTE)pidl) + len );
1692           if (pidlnext->mkid.cb)
1693             ret = FALSE;
1694         }
1695
1696         TRACE("%s\n", ret ? "Yes" : "No");
1697         return ret;
1698 }
1699
1700 /**************************************************************************
1701  *
1702  *      ### 3. section getting values from pidls ###
1703  */
1704
1705  /**************************************************************************
1706  *  _ILSimpleGetText
1707  *
1708  * gets the text for the first item in the pidl (eg. simple pidl)
1709  *
1710  * returns the length of the string
1711  */
1712 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1713 {
1714         DWORD           dwReturn=0;
1715         LPSTR           szSrc;
1716         GUID const *    riid;
1717         char szTemp[MAX_PATH];
1718
1719         TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1720
1721         if (!pidl) return 0;
1722
1723         if (szOut)
1724           *szOut = 0;
1725
1726         if (_ILIsDesktop(pidl))
1727         {
1728          /* desktop */
1729           if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1730           {
1731             if (szOut)
1732               lstrcpynA(szOut, szTemp, uOutSize);
1733
1734             dwReturn = strlen (szTemp);
1735           }
1736         }
1737         else if (( szSrc = _ILGetTextPointer(pidl) ))
1738         {
1739           /* filesystem */
1740           if (szOut)
1741             lstrcpynA(szOut, szSrc, uOutSize);
1742
1743           dwReturn = strlen(szSrc);
1744         }
1745         else if (( riid = _ILGetGUIDPointer(pidl) ))
1746         {
1747           /* special folder */
1748           if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
1749           {
1750             if (szOut)
1751               lstrcpynA(szOut, szTemp, uOutSize);
1752
1753             dwReturn = strlen (szTemp);
1754           }
1755         }
1756         else
1757         {
1758           ERR("-- no text\n");
1759         }
1760
1761         TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_a(szOut),dwReturn);
1762         return dwReturn;
1763 }
1764
1765  /**************************************************************************
1766  *  _ILSimpleGetTextW
1767  *
1768  * gets the text for the first item in the pidl (eg. simple pidl)
1769  *
1770  * returns the length of the string
1771  */
1772 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1773 {
1774         DWORD   dwReturn;
1775         char    szTemp[MAX_PATH];
1776
1777         TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1778
1779         dwReturn = _ILSimpleGetText(pidl, szTemp, uOutSize);
1780
1781         if (!MultiByteToWideChar(CP_ACP, 0, szTemp, -1, szOut, MAX_PATH))
1782                 *szOut = 0;
1783
1784         TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_w(szOut),dwReturn);
1785         return dwReturn;
1786 }
1787
1788 /**************************************************************************
1789  *
1790  *      ### 4. getting pointers to parts of pidls ###
1791  *
1792  **************************************************************************
1793  *  _ILGetDataPointer()
1794  */
1795 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1796 {
1797         if(pidl && pidl->mkid.cb != 0x00)
1798           return (LPPIDLDATA) &(pidl->mkid.abID);
1799         return NULL;
1800 }
1801
1802 /**************************************************************************
1803  *  _ILGetTextPointer()
1804  * gets a pointer to the long filename string stored in the pidl
1805  */
1806 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1807 {/*     TRACE(pidl,"(pidl%p)\n", pidl);*/
1808
1809         LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1810
1811         if (pdata)
1812         {
1813           switch (pdata->type)
1814           {
1815             case PT_MYCOMP:
1816             case PT_SPECIAL:
1817               return NULL;
1818
1819             case PT_DRIVE:
1820             case PT_DRIVE1:
1821             case PT_DRIVE2:
1822             case PT_DRIVE3:
1823               return (LPSTR)&(pdata->u.drive.szDriveName);
1824
1825             case PT_FOLDER:
1826             case PT_FOLDER1:
1827             case PT_VALUE:
1828             case PT_IESPECIAL1:
1829             case PT_IESPECIAL2:
1830               return (LPSTR)&(pdata->u.file.szNames);
1831
1832             case PT_WORKGRP:
1833             case PT_COMP:
1834             case PT_NETWORK:
1835             case PT_NETPROVIDER:
1836             case PT_SHARE:
1837               return (LPSTR)&(pdata->u.network.szNames);
1838           }
1839         }
1840         return NULL;
1841 }
1842
1843 /**************************************************************************
1844  *  _ILGetSTextPointer()
1845  * gets a pointer to the short filename string stored in the pidl
1846  */
1847 LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
1848 {/*     TRACE(pidl,"(pidl%p)\n", pidl);*/
1849
1850         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1851
1852         if (pdata)
1853         {
1854           switch (pdata->type)
1855           {
1856             case PT_FOLDER:
1857             case PT_VALUE:
1858             case PT_IESPECIAL1:
1859             case PT_IESPECIAL2:
1860               return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
1861
1862             case PT_WORKGRP:
1863               return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
1864           }
1865         }
1866         return NULL;
1867 }
1868
1869 /**************************************************************************
1870  * _ILGetGUIDPointer()
1871  *
1872  * returns reference to guid stored in some pidls
1873  */
1874 REFIID _ILGetGUIDPointer(LPCITEMIDLIST pidl)
1875 {
1876         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1877
1878         TRACE("%p\n", pidl);
1879
1880         if (pdata)
1881         {
1882           TRACE("pdata->type 0x%04x\n", pdata->type);
1883           switch (pdata->type)
1884           {
1885             case PT_SPECIAL:
1886             case PT_MYCOMP:
1887               return (REFIID) &(pdata->u.mycomp.guid);
1888
1889             default:
1890                 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
1891                 break;
1892           }
1893         }
1894         return NULL;
1895 }
1896
1897 /*************************************************************************
1898  * _ILGetFileDateTime
1899  *
1900  * Given the ItemIdList, get the FileTime
1901  *
1902  * PARAMS
1903  *      pidl        [I] The ItemIDList
1904  *      pFt         [I] the resulted FILETIME of the file
1905  *
1906  * RETURNS
1907  *     True if Successful
1908  *
1909  * NOTES
1910  *
1911  */
1912 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
1913 {
1914     LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1915
1916     if(! pdata) return FALSE;
1917
1918     switch (pdata->type)
1919     {
1920         case PT_FOLDER:
1921             DosDateTimeToFileTime(pdata->u.folder.uFileDate, pdata->u.folder.uFileTime, pFt);
1922             break;
1923         case PT_VALUE:
1924             DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
1925             break;
1926         default:
1927             return FALSE;
1928     }
1929     return TRUE;
1930 }
1931
1932 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1933 {
1934         FILETIME ft,lft;
1935         SYSTEMTIME time;
1936         BOOL ret;
1937
1938         if (_ILGetFileDateTime( pidl, &ft )) {
1939             FileTimeToLocalFileTime(&ft, &lft);
1940             FileTimeToSystemTime (&lft, &time);
1941             ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL,  pOut, uOutSize);
1942         } else {
1943             pOut[0] = '\0';
1944             ret = FALSE;
1945         }
1946         return ret;
1947
1948 }
1949
1950 /*************************************************************************
1951  * _ILGetFileSize
1952  *
1953  * Given the ItemIdList, get the FileSize
1954  *
1955  * PARAMS
1956  *      pidl    [I] The ItemIDList
1957  *      pOut    [I] The buffer to save the result
1958  *      uOutsize [I] The size of the buffer
1959  *
1960  * RETURNS
1961  *     The FileSize
1962  *
1963  * NOTES
1964  *      pOut can be null when no string is needed
1965  *
1966  */
1967 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1968 {
1969         LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1970         DWORD dwSize;
1971
1972         if(! pdata) return 0;
1973
1974         switch (pdata->type)
1975         {
1976           case PT_VALUE:
1977             dwSize = pdata->u.file.dwFileSize;
1978             if (pOut) StrFormatByteSizeA(dwSize, pOut, uOutSize);
1979             return dwSize;
1980         }
1981         if (pOut) *pOut = 0x00;
1982         return 0;
1983 }
1984
1985 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1986 {
1987         char szTemp[MAX_PATH];
1988         const char * pPoint;
1989         LPCITEMIDLIST  pidlTemp=pidl;
1990
1991         TRACE("pidl=%p\n",pidl);
1992
1993         if (!pidl) return FALSE;
1994
1995         pidlTemp = ILFindLastID(pidl);
1996
1997         if (!_ILIsValue(pidlTemp)) return FALSE;
1998         if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH)) return FALSE;
1999
2000         pPoint = PathFindExtensionA(szTemp);
2001
2002         if (! *pPoint) return FALSE;
2003
2004         pPoint++;
2005         lstrcpynA(pOut, pPoint, uOutSize);
2006         TRACE("%s\n",pOut);
2007
2008         return TRUE;
2009 }
2010
2011 /*************************************************************************
2012  * _ILGetFileType
2013  *
2014  * Given the ItemIdList, get the file type description
2015  *
2016  * PARAMS
2017  *      pidl        [I] The ItemIDList (simple)
2018  *      pOut        [I] The buffer to save the result
2019  *      uOutsize    [I] The size of the buffer
2020  *
2021  * RETURNS
2022  *      nothing
2023  *
2024  * NOTES
2025  *      This function copies as much as possible into the buffer.
2026  */
2027 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2028 {
2029         if(_ILIsValue(pidl))
2030         {
2031           char sTemp[64];
2032           if(uOutSize > 0)
2033           {
2034             pOut[0] = 0;
2035           }
2036           if (_ILGetExtension (pidl, sTemp, 64))
2037           {
2038             if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
2039                 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE )))
2040             {
2041               lstrcpynA (pOut, sTemp, uOutSize - 6);
2042               strcat (pOut, "-file");
2043             }
2044           }
2045         }
2046         else
2047         {
2048           lstrcpynA(pOut, "Folder", uOutSize);
2049         }
2050 }
2051
2052 /*************************************************************************
2053  * _ILGetFileAttributes
2054  *
2055  * Given the ItemIdList, get the Attrib string format
2056  *
2057  * PARAMS
2058  *      pidl        [I] The ItemIDList
2059  *      pOut        [I] The buffer to save the result
2060  *      uOutsize    [I] The size of the Buffer
2061  *
2062  * RETURNS
2063  *     Attributes
2064  *
2065  * FIXME
2066  *  return value 0 in case of error is a valid return value
2067  *
2068  */
2069 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2070 {
2071         LPPIDLDATA pData = _ILGetDataPointer(pidl);
2072         WORD wAttrib = 0;
2073         int i;
2074
2075         if(! pData) return 0;
2076
2077         switch(pData->type)
2078         {
2079           case PT_FOLDER:
2080             wAttrib = pData->u.folder.uFileAttribs;
2081             break;
2082           case PT_VALUE:
2083             wAttrib = pData->u.file.uFileAttribs;
2084             break;
2085         }
2086
2087         if(uOutSize >= 6)
2088         {
2089           i=0;
2090           if(wAttrib & FILE_ATTRIBUTE_READONLY)
2091           {
2092             pOut[i++] = 'R';
2093           }
2094           if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2095           {
2096             pOut[i++] = 'H';
2097           }
2098           if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2099           {
2100             pOut[i++] = 'S';
2101           }
2102           if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2103           {
2104             pOut[i++] = 'A';
2105           }
2106           if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2107           {
2108             pOut[i++] = 'C';
2109           }
2110           pOut[i] = 0x00;
2111         }
2112         return wAttrib;
2113 }
2114
2115 /*************************************************************************
2116 * ILFreeaPidl
2117 *
2118 * free a aPidl struct
2119 */
2120 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2121 {
2122         UINT   i;
2123
2124         if (apidl)
2125         {
2126           for (i = 0; i < cidl; i++) SHFree(apidl[i]);
2127           SHFree(apidl);
2128         }
2129 }
2130
2131 /*************************************************************************
2132 * ILCopyaPidl
2133 *
2134 * copies an aPidl struct
2135 */
2136 LPITEMIDLIST* _ILCopyaPidl(LPCITEMIDLIST * apidlsrc, UINT cidl)
2137 {
2138         UINT i;
2139         LPITEMIDLIST * apidldest = (LPITEMIDLIST*)SHAlloc(cidl * sizeof(LPITEMIDLIST));
2140         if(!apidlsrc) return NULL;
2141
2142         for (i = 0; i < cidl; i++)
2143           apidldest[i] = ILClone(apidlsrc[i]);
2144
2145         return apidldest;
2146 }
2147
2148 /*************************************************************************
2149 * _ILCopyCidaToaPidl
2150 *
2151 * creates aPidl from CIDA
2152 */
2153 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, LPIDA cida)
2154 {
2155         UINT i;
2156         LPITEMIDLIST * dst = (LPITEMIDLIST*)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2157
2158         if(!dst) return NULL;
2159
2160         if (pidl)
2161           *pidl = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[0]]));
2162
2163         for (i = 0; i < cida->cidl; i++)
2164           dst[i] = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[i + 1]]));
2165
2166         return dst;
2167 }