notepad: Czech language resources encoding fix.
[wine] / programs / explorer / explorer.c
1 /*
2  * explorer.exe
3  *
4  * Copyright 2005,2006 CodeWeavers, Aric Stewart
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 <windows.h>
22 #include <ctype.h>
23
24 typedef struct parametersTAG {
25     BOOL    explorer_mode;
26     WCHAR   root[MAX_PATH];
27     WCHAR   selection[MAX_PATH];
28 } parameters_struct;
29
30
31 static int CopyPathString(LPWSTR target, LPSTR source)
32 {
33     CHAR temp_buf[MAX_PATH];
34     INT i = 0;
35
36     while (isspace(*source)) source++;
37
38     if (*source == '\"')
39     {
40         source ++;
41         while (*source != '\"')
42         {
43             temp_buf[i] = *source;
44             i++;
45             source++;
46         }
47         temp_buf[i] = 0;
48         source ++;
49         i+=2;
50     }
51     else
52     {
53         while (*source && !isspace(*source))
54         {
55             temp_buf[i] = *source;
56             i++;
57             source++;
58         }
59         temp_buf[i] = 0;
60     }
61     MultiByteToWideChar(CP_ACP,0,temp_buf,-1,target,MAX_PATH);
62     return i;
63 }
64
65
66 static void CopyPathRoot(LPWSTR root, LPWSTR path)
67 {
68     LPWSTR p,p2;
69     INT i = 0;
70
71     p = path;
72     while (*p!=0)
73         p++;
74
75     while (*p!='\\' && p > path)
76         p--;
77
78     if (p == path)
79         return;
80
81     p2 = path;
82     while (p2 != p)
83     {
84         root[i] = *p2;
85         i++;
86         p2++;
87     }
88     root[i] = 0;
89 }
90
91 /*
92  * Command Line parameters are:
93  * [/n]  Opens in single-paned view for each selected items. This is default
94  * [/e,] Uses Windows Explorer View
95  * [/root,object] Specifies the root level of the view
96  * [/select,object] parent folder is opened and specified object is selected
97  */
98 static void ParseCommandLine(LPSTR commandline,parameters_struct *parameters)
99 {
100     LPSTR p;
101     LPSTR p2;
102    
103     p2 = commandline;
104     p = strchr(commandline,'/');
105     while(p)
106     {
107         p++;
108         if (strncmp(p,"n",1)==0)
109         {
110             parameters->explorer_mode = FALSE;
111             p++;
112         }
113         else if (strncmp(p,"e,",2)==0)
114         {
115             parameters->explorer_mode = TRUE;
116             p+=2;
117         }
118         else if (strncmp(p,"root,",5)==0)
119         {
120             p+=5;
121             p+=CopyPathString(parameters->root,p);
122         }
123         else if (strncmp(p,"select,",7)==0)
124         {
125             p+=7;
126             p+=CopyPathString(parameters->selection,p);
127             if (!parameters->root[0])
128                 CopyPathRoot(parameters->root,
129                         parameters->selection);
130         }
131         p2 = p;
132         p = strchr(p,'/');
133     }
134     if (p2 && *p2)
135     {
136         /* left over command line is generally the path to be opened */
137         CopyPathString(parameters->root,p2);
138     }
139 }
140
141 int WINAPI WinMain(HINSTANCE hinstance,
142                    HINSTANCE previnstance,
143                    LPSTR cmdline,
144                    int cmdshow)
145 {
146     STARTUPINFOW si;
147     PROCESS_INFORMATION info;
148     parameters_struct   parameters;
149     BOOL rc;
150     static const WCHAR winefile[] = {'w','i','n','e','f','i','l','e','.','e','x','e',0};
151     static const WCHAR space[] = {' ',0};
152     LPWSTR winefile_commandline = NULL;
153     DWORD len = 0;
154
155     memset(&parameters,0,sizeof(parameters));
156     memset(&si,0,sizeof(STARTUPINFOW));
157
158     ParseCommandLine(cmdline,&parameters);
159     len = lstrlenW(winefile) +1;
160     
161     if (parameters.selection[0])
162     {
163         len += lstrlenW(parameters.selection) + 2;
164         winefile_commandline = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
165
166         lstrcpyW(winefile_commandline,winefile);
167         lstrcatW(winefile_commandline,space);
168         lstrcatW(winefile_commandline,parameters.selection);
169     }
170     else if (parameters.root[0])
171     {
172         len += lstrlenW(parameters.root) + 3;
173         winefile_commandline = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
174
175         lstrcpyW(winefile_commandline,winefile);
176         lstrcatW(winefile_commandline,space);
177         lstrcatW(winefile_commandline,parameters.root);
178         if (winefile_commandline[lstrlenW(winefile_commandline)-1]!='\\')
179         {
180             static const WCHAR slash[] = {'\\',0};
181             lstrcatW(winefile_commandline,slash);
182         }
183     }
184     else
185     {
186         winefile_commandline = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
187         lstrcpyW(winefile_commandline,winefile);
188     }
189
190     rc = CreateProcessW(NULL, winefile_commandline, NULL, NULL, FALSE, 0, NULL,
191                         parameters.root, &si, &info);
192
193     HeapFree(GetProcessHeap(),0,winefile_commandline);
194     
195     if (!rc)
196         return 0;
197
198     WaitForSingleObject(info.hProcess,INFINITE);
199     return 0;
200 }