WINEMENUBUILDER: convert to unicode

Mike McCormack mike at codeweavers.com
Thu Feb 17 01:33:27 CST 2005


ChangeLog:
Dmitry Timoshkov <dmitry at codeweavers.com>
* convert to unicode
-------------- next part --------------
Index: programs/winemenubuilder/winemenubuilder.c
===================================================================
RCS file: /home/wine/wine/programs/winemenubuilder/winemenubuilder.c,v
retrieving revision 1.23
diff -u -p -r1.23 winemenubuilder.c
--- programs/winemenubuilder/winemenubuilder.c	16 Feb 2005 16:04:05 -0000	1.23
+++ programs/winemenubuilder/winemenubuilder.c	17 Feb 2005 07:32:09 -0000
@@ -4,6 +4,7 @@
  * Copyright 1997 Marcus Meissner
  * Copyright 1998 Juergen Schmied
  * Copyright 2003 Mike McCormack for CodeWeavers
+ * Copyright 2004 Dmitry Timoshkov
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -54,6 +55,7 @@
 #include <objidl.h>
 #include <shlguid.h>
 
+#include "wine/unicode.h"
 #include "wine/debug.h"
 #include "wine.xpm"
 
@@ -123,7 +125,7 @@ typedef struct
  * FIXME: should not use stdio
  */
 
-static BOOL SaveIconResAsXPM(const BITMAPINFO *pIcon, const char *szXPMFileName, const char *comment)
+static BOOL SaveIconResAsXPM(const BITMAPINFO *pIcon, const char *szXPMFileName, LPCWSTR commentW)
 {
     FILE *fXPMFile;
     int nHeight;
@@ -136,6 +138,7 @@ static BOOL SaveIconResAsXPM(const BITMA
     BOOL aColorUsed[256] = {0};
     int nColorsUsed = 0;
     int i,j;
+    char *comment;
 
     if (!((pIcon->bmiHeader.biBitCount == 4) || (pIcon->bmiHeader.biBitCount == 8)))
         return FALSE;
@@ -143,6 +146,10 @@ static BOOL SaveIconResAsXPM(const BITMA
     if (!(fXPMFile = fopen(szXPMFileName, "w")))
         return FALSE;
 
+    i = WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, NULL, 0, NULL, NULL);
+    comment = malloc(i);
+    WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, comment, i, NULL, NULL);
+
     nHeight = pIcon->bmiHeader.biHeight / 2;
     nXORWidthBytes = 4 * ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount / 32)
                           + ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount % 32) > 0));
@@ -206,33 +213,35 @@ static BOOL SaveIconResAsXPM(const BITMA
 #undef MASK
 #undef COLOR
 
+    free(comment);
     fclose(fXPMFile);
     return TRUE;
 
  error:
+    free(comment);
     fclose(fXPMFile);
     unlink( szXPMFileName );
     return FALSE;
 }
 
-static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCSTR lpszType, LPSTR lpszName, LONG lParam)
+static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
 {
     ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
 
     if (!sEnumRes->nIndex--)
     {
-        *sEnumRes->pResInfo = FindResourceA(hModule, lpszName, (LPSTR)RT_GROUP_ICON);
+        *sEnumRes->pResInfo = FindResourceW(hModule, lpszName, (LPCWSTR)RT_GROUP_ICON);
         return FALSE;
     }
     else
         return TRUE;
 }
 
-static BOOL ExtractFromEXEDLL(const char *szFileName, int nIndex, const char *szXPMFileName)
+static BOOL ExtractFromEXEDLL(LPCWSTR szFileName, int nIndex, const char *szXPMFileName)
 {
     HMODULE hModule;
     HRSRC hResInfo;
-    char *lpName = NULL;
+    LPCWSTR lpName = NULL;
     HGLOBAL hResData;
     GRPICONDIR *pIconDir;
     BITMAPINFO *pIcon;
@@ -241,10 +250,11 @@ static BOOL ExtractFromEXEDLL(const char
     int nMaxBits = 0;
     int i;
 
-    hModule = LoadLibraryExA(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
+    hModule = LoadLibraryExW(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
     if (!hModule)
     {
-        WINE_ERR("LoadLibraryExA (%s) failed, error %ld\n", szFileName, GetLastError());
+        WINE_ERR("LoadLibraryExW (%s) failed, error %ld\n",
+                 wine_dbgstr_w(szFileName), GetLastError());
         return FALSE;
     }
 
@@ -252,14 +262,14 @@ static BOOL ExtractFromEXEDLL(const char
     {
         hResInfo = FindResourceA(hModule, MAKEINTRESOURCEA(-nIndex), (LPSTR)RT_GROUP_ICON);
         WINE_TRACE("FindResourceA (%s) called, return %p, error %ld\n",
-                   szFileName, hResInfo, GetLastError());
+                    wine_dbgstr_w(szFileName), hResInfo, GetLastError());
     }
     else
     {
         hResInfo=NULL;
         sEnumRes.pResInfo = &hResInfo;
         sEnumRes.nIndex = nIndex;
-        EnumResourceNamesA(hModule, (LPSTR)RT_GROUP_ICON, &EnumResNameProc, (LONG) &sEnumRes);
+        EnumResourceNamesW(hModule, (LPCWSTR)RT_GROUP_ICON, EnumResNameProc, (LONG_PTR)&sEnumRes);
     }
 
     if (!hResInfo)
@@ -289,14 +299,14 @@ static BOOL ExtractFromEXEDLL(const char
           }
           if ((pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth) > nMax)
           {
-              lpName = MAKEINTRESOURCEA(pIconDir->idEntries[i].nID);
+              lpName = MAKEINTRESOURCEW(pIconDir->idEntries[i].nID);
               nMax = pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth;
           }
         }
 
     FreeResource(hResData);
 
-    if (!(hResInfo = FindResourceA(hModule, lpName, (LPSTR)RT_ICON)))
+    if (!(hResInfo = FindResourceW(hModule, lpName, (LPWSTR)RT_ICON)))
     {
         WINE_ERR("Second FindResourceA failed, error %ld\n", GetLastError());
         goto error2;
@@ -331,15 +341,12 @@ static BOOL ExtractFromEXEDLL(const char
 }
 
 /* get the Unix file name for a given path, allocating the string */
-inline static char *get_unix_file_name( LPCSTR dos )
+inline static char *get_unix_file_name( LPCWSTR dosW )
 {
-    WCHAR dosW[MAX_PATH];
-
-    MultiByteToWideChar(CP_ACP, 0, dos, -1, dosW, MAX_PATH);
     return wine_get_unix_file_name( dosW );
 }
 
-static int ExtractFromICO(const char *szFileName, const char *szXPMFileName)
+static int ExtractFromICO(LPCWSTR szFileName, const char *szXPMFileName)
 {
     FILE *fICOFile;
     ICONDIR iconDir;
@@ -440,38 +447,45 @@ static unsigned short crc16(const char* 
 }
 
 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
-static char *extract_icon( const char *path, int index)
+static char *extract_icon( LPCWSTR path, int index)
 {
     int nodefault = 1;
     unsigned short crc;
     char *iconsdir, *ico_path, *ico_name, *xpm_path;
     char* s;
     HKEY hkey;
+    int n;
 
     /* Where should we save the icon? */
-    WINE_TRACE("path=[%s] index=%d\n",path,index);
+    WINE_TRACE("path=[%s] index=%d\n", wine_dbgstr_w(path), index);
     iconsdir=NULL;  /* Default is no icon */
     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
     {
+        static const WCHAR IconsDirW[] = {'I','c','o','n','s','D','i','r',0};
+        LPWSTR iconsdirW;
         DWORD size = 0;
 
-        if (RegQueryValueExA(hkey, "IconsDir", 0, NULL, NULL, &size)==0)
+        if (!RegQueryValueExW(hkey, IconsDirW, 0, NULL, NULL, &size))
         {
-            iconsdir = HeapAlloc(GetProcessHeap(), 0, size);
-            RegQueryValueExA(hkey, "IconsDir", 0, NULL, iconsdir, &size);
+            iconsdirW = HeapAlloc(GetProcessHeap(), 0, size);
+            RegQueryValueExW(hkey, IconsDirW, 0, NULL, (LPBYTE)iconsdirW, &size);
 
-            s = get_unix_file_name(iconsdir);
+            s = get_unix_file_name(iconsdirW);
             if (s)
+                iconsdir = s;
+            else
             {
-                HeapFree(GetProcessHeap(), 0, iconsdir);
-                iconsdir=s;
+                int n = WideCharToMultiByte(CP_UNIXCP, 0, iconsdirW, -1, NULL, 0, NULL, NULL);
+                iconsdir = HeapAlloc(GetProcessHeap(), 0, n);
+                WideCharToMultiByte(CP_UNIXCP, 0, iconsdirW, -1, iconsdir, n, NULL, NULL);
             }
+            HeapFree(GetProcessHeap(), 0, iconsdirW);
         }
         else
         {
-            char path[MAX_PATH];
+            WCHAR path[MAX_PATH];
 
-            if (GetTempPath(sizeof(path),path))
+            if (GetTempPathW(MAX_PATH,path))
             {
                 s = get_unix_file_name(path);
                 if (s)
@@ -494,8 +508,9 @@ static char *extract_icon( const char *p
     }
 
     /* Determine the icon base name */
-    ico_path=HeapAlloc(GetProcessHeap(), 0, lstrlenA(path)+1);
-    strcpy(ico_path, path);
+    n = WideCharToMultiByte(CP_UNIXCP, 0, path, -1, NULL, 0, NULL, NULL);
+    ico_path = HeapAlloc(GetProcessHeap(), 0, n);
+    WideCharToMultiByte(CP_UNIXCP, 0, path, -1, ico_path, n, NULL, NULL);
     s=ico_name=ico_path;
     while (*s!='\0') {
         if (*s=='/' || *s=='\\') {
@@ -524,7 +539,7 @@ static char *extract_icon( const char *p
     if (ExtractFromICO( path, xpm_path))
         goto end;
     if (!nodefault)
-        if (create_default_icon( xpm_path, path ))
+        if (create_default_icon( xpm_path, ico_path ))
             goto end;
 
     HeapFree( GetProcessHeap(), 0, xpm_path );
@@ -577,15 +592,28 @@ static BOOL DeferToRunOnce(LPWSTR link)
 }
 
 /* This escapes \ in filenames */
-static LPSTR escape(LPCSTR arg)
+static LPSTR escape(LPCWSTR arg)
 {
     LPSTR narg, x;
+    LPCWSTR esc;
+    int len = 0, n;
+
+    esc = arg;
+    while((esc = strchrW(esc, '\\')))
+    {
+        esc++;
+        len++;
+    }
+
+    len += WideCharToMultiByte(CP_UNIXCP, 0, arg, -1, NULL, 0, NULL, NULL);
+    narg = HeapAlloc(GetProcessHeap(), 0, len);
 
-    narg = HeapAlloc(GetProcessHeap(),0,2*strlen(arg)+2);
     x = narg;
     while (*arg)
     {
-        *x++ = *arg;
+        n = WideCharToMultiByte(CP_UNIXCP, 0, arg, 1, x, len, NULL, NULL);
+        x += n;
+        len -= n;
         if (*arg == '\\')
             *x++='\\'; /* escape \ */
         arg++;
@@ -712,12 +740,13 @@ static BOOL GetLinkLocation( LPCWSTR lin
     return FALSE;
 }
 
-static BOOL InvokeShellLinker( IShellLinkA *sl, LPCWSTR link )
+static BOOL InvokeShellLinker( IShellLinkW *sl, LPCWSTR link )
 {
-    char *link_name, *p, *icon_name = NULL, *work_dir = NULL;
+    char *link_name, *icon_name = NULL, *work_dir = NULL;
     char *escaped_path = NULL, *escaped_args = NULL;
-    CHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
-    CHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH];
+    char *escaped_description = NULL;
+    WCHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
+    WCHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH];
     int iIconId = 0, r;
     DWORD csidl = -1, ofs = 0;
 
@@ -739,31 +768,31 @@ static BOOL InvokeShellLinker( IShellLin
     }
 
     szWorkDir[0] = 0;
-    IShellLinkA_GetWorkingDirectory( sl, szWorkDir, MAX_PATH );
-    WINE_TRACE("workdir    : %s\n", szWorkDir);
+    IShellLinkW_GetWorkingDirectory( sl, szWorkDir, MAX_PATH );
+    WINE_TRACE("workdir    : %s\n", wine_dbgstr_w(szWorkDir));
 
     szDescription[0] = 0;
-    IShellLinkA_GetDescription( sl, szDescription, INFOTIPSIZE );
-    WINE_TRACE("description: %s\n", szDescription);
+    IShellLinkW_GetDescription( sl, szDescription, INFOTIPSIZE );
+    WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription));
 
     szPath[0] = 0;
-    IShellLinkA_GetPath( sl, szPath, MAX_PATH, NULL, SLGP_RAWPATH );
-    WINE_TRACE("path       : %s\n", szPath);
+    IShellLinkW_GetPath( sl, szPath, MAX_PATH, NULL, SLGP_RAWPATH );
+    WINE_TRACE("path       : %s\n", wine_dbgstr_w(szPath));
 
     szArgs[0] = 0;
     IShellLinkA_GetArguments( sl, szArgs, INFOTIPSIZE );
-    WINE_TRACE("args       : %s\n", szArgs);
+    WINE_TRACE("args       : %s\n", wine_dbgstr_w(szArgs));
 
     szIconPath[0] = 0;
-    IShellLinkA_GetIconLocation( sl, szIconPath, MAX_PATH, &iIconId );
-    WINE_TRACE("icon file  : %s\n", szIconPath );
+    IShellLinkW_GetIconLocation( sl, szIconPath, MAX_PATH, &iIconId );
+    WINE_TRACE("icon file  : %s\n", wine_dbgstr_w(szIconPath));
 
     if( !szPath[0] )
     {
         LPITEMIDLIST pidl = NULL;
-        IShellLinkA_GetIDList( sl, &pidl );
-        if( pidl && SHGetPathFromIDListA( pidl, szPath ) );
-            WINE_TRACE("pidl path  : %s\n", szPath );
+        IShellLinkW_GetIDList( sl, &pidl );
+        if( pidl && SHGetPathFromIDListW( pidl, szPath ) );
+            WINE_TRACE("pidl path  : %s\n", wine_dbgstr_w(szPath));
     }
 
     /* extract the icon */
@@ -782,10 +811,13 @@ static BOOL InvokeShellLinker( IShellLin
     /* check the path */
     if( szPath[0] )
     {
+        static const WCHAR exeW[] = {'.','e','x','e',0};
+        WCHAR *p;
+
         /* check for .exe extension */
-        if (!(p = strrchr( szPath, '.' ))) return FALSE;
-        if (strchr( p, '\\' ) || strchr( p, '/' )) return FALSE;
-        if (strcasecmp( p, ".exe" )) return FALSE;
+        if (!(p = strrchrW( szPath, '.' ))) return FALSE;
+        if (strchrW( p, '\\' ) || strchrW( p, '/' )) return FALSE;
+        if (lstrcmpiW( p, exeW )) return FALSE;
 
         /* convert app working dir */
         if (szWorkDir[0])
@@ -793,11 +825,11 @@ static BOOL InvokeShellLinker( IShellLin
     }
     else
     {
+        static const WCHAR startW[] = {'\\','s','t','a','r','t','.','e','x','e',0};
         /* if there's no path... try run the link itself */
-        WideCharToMultiByte( CP_ACP, 0, link, -1, szArgs, MAX_PATH, NULL, NULL );
-        GetWindowsDirectoryA(szPath, MAX_PATH);
-        strncat(szPath, "\\command\\start.exe",
-                MAX_PATH - GetWindowsDirectoryA(NULL, 0));
+        lstrcpynW(szArgs, link, MAX_PATH);
+        GetSystemDirectoryW(szPath, MAX_PATH);
+        lstrcatW(szPath, startW);
     }
 
     link_name = cleanup_link( &link[ofs] );
@@ -811,16 +843,18 @@ static BOOL InvokeShellLinker( IShellLin
     escaped_path = escape(szPath);
     if (szArgs)
         escaped_args = escape(szArgs);
+    escaped_description = escape(szDescription);
 
     r = fork_and_wait("wineshelllink", link_name, escaped_path,
                       in_desktop_dir(csidl), escaped_args, icon_name,
-                      work_dir ? work_dir : "", szDescription );
+                      work_dir ? work_dir : "", escaped_description );
 
     HeapFree( GetProcessHeap(), 0, icon_name );
     HeapFree( GetProcessHeap(), 0, work_dir );
     HeapFree( GetProcessHeap(), 0, link_name );
     HeapFree( GetProcessHeap(), 0, escaped_args );
     HeapFree( GetProcessHeap(), 0, escaped_path );
+    HeapFree( GetProcessHeap(), 0, escaped_description );
 
     if (r)
     {
@@ -832,9 +866,9 @@ static BOOL InvokeShellLinker( IShellLin
 }
 
 
-static BOOL Process_Link( LPWSTR linkname, BOOL bAgain )
+static BOOL Process_Link( LPCWSTR linkname, BOOL bAgain )
 {
-    IShellLinkA *sl;
+    IShellLinkW *sl;
     IPersistFile *pf;
     HRESULT r;
     WCHAR fullname[MAX_PATH];
@@ -863,14 +897,14 @@ static BOOL Process_Link( LPWSTR linknam
     }
 
     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
-                          &IID_IShellLink, (LPVOID *) &sl );
+                          &IID_IShellLinkW, (LPVOID *) &sl );
     if( FAILED( r ) )
     {
         WINE_ERR("No IID_IShellLink\n");
         return 1;
     }
 
-    r = IShellLinkA_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
+    r = IShellLinkW_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
     if( FAILED( r ) )
     {
         WINE_ERR("No IID_IPersistFile\n");
@@ -890,7 +924,7 @@ static BOOL Process_Link( LPWSTR linknam
     }
 
     IPersistFile_Release( pf );
-    IShellLinkA_Release( sl );
+    IShellLinkW_Release( sl );
 
     CoUninitialize();
 


More information about the wine-patches mailing list