[PATCH 2/2] winemenubuilder: Use utilities from "wine/heap.h" and "wine/str.h".

Davide Beatrici wine at gitlab.winehq.org
Thu Jun 23 21:44:28 CDT 2022


From: Davide Beatrici <git at davidebeatrici.dev>

---
 programs/winemenubuilder/winemenubuilder.c | 71 +++++++---------------
 1 file changed, 22 insertions(+), 49 deletions(-)

diff --git a/programs/winemenubuilder/winemenubuilder.c b/programs/winemenubuilder/winemenubuilder.c
index 1579ca8dafa..41ed9e2286e 100644
--- a/programs/winemenubuilder/winemenubuilder.c
+++ b/programs/winemenubuilder/winemenubuilder.c
@@ -84,8 +84,10 @@
 #include <wincodec.h>
 
 #include "wine/debug.h"
+#include "wine/heap.h"
 #include "wine/list.h"
 #include "wine/rbtree.h"
+#include "wine/str.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
 
@@ -94,6 +96,11 @@ WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
 #define in_startmenu(csidl)   ((csidl)==CSIDL_STARTMENU || \
                                (csidl)==CSIDL_COMMON_STARTMENU)
 
+#define wchars_to_utf8_chars(wchars) (utf16to8_alloc(wchars, 0))
+#define utf8_chars_to_wchars(chars) (utf8to16_alloc(chars, 0))
+
+#define xwcsdup(wchars) (utf16dup(wchars, 0))
+
 #define IS_OPTION_TRUE(ch) \
     ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
 
@@ -208,7 +215,7 @@ static unsigned short crc16(const WCHAR *string)
 
 static void *xmalloc( size_t size )
 {
-    void *ret = HeapAlloc( GetProcessHeap(), 0, size );
+    void *ret = heap_alloc( size );
     if (!ret)
     {
         ERR( "out of memory\n" );
@@ -219,8 +226,7 @@ static void *xmalloc( size_t size )
 
 static void *xrealloc( void *ptr, size_t size )
 {
-    if (!ptr) return xmalloc( size );
-    ptr = HeapReAlloc( GetProcessHeap(), 0, ptr, size );
+    ptr = heap_realloc( ptr, size );
     if (!ptr)
     {
         ERR( "out of memory\n" );
@@ -229,21 +235,6 @@ static void *xrealloc( void *ptr, size_t size )
     return ptr;
 }
 
-static WCHAR *xwcsdup( const WCHAR *str )
-{
-    WCHAR *ret;
-
-    if (!str) return NULL;
-    ret = xmalloc( (lstrlenW(str) + 1) * sizeof(WCHAR) );
-    lstrcpyW( ret, str );
-    return ret;
-}
-
-static void heap_free( void *ptr )
-{
-    HeapFree( GetProcessHeap(), 0, ptr );
-}
-
 static WCHAR * WINAPIV heap_wprintf(const WCHAR *format, ...)
 {
     va_list args;
@@ -297,29 +288,11 @@ static BOOL create_directories(WCHAR *directory)
     return CreateDirectoryW( directory, NULL ) || GetLastError() == ERROR_ALREADY_EXISTS;
 }
 
-static char* wchars_to_utf8_chars(LPCWSTR string)
-{
-    char *ret;
-    INT size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
-    ret = xmalloc(size);
-    WideCharToMultiByte(CP_UTF8, 0, string, -1, ret, size, NULL, NULL);
-    return ret;
-}
-
-static WCHAR* utf8_chars_to_wchars(LPCSTR string)
-{
-    WCHAR *ret;
-    INT size = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
-    ret = xmalloc(size * sizeof(WCHAR));
-    MultiByteToWideChar(CP_UTF8, 0, string, -1, ret, size);
-    return ret;
-}
-
 static char *wchars_to_xml_text(const WCHAR *string)
 {
     int i, pos;
     char *text = wchars_to_utf8_chars( string );
-    char *ret = xmalloc( 6 * strlen(text) + 1 );
+    char *ret = xmalloc( 6 * utf8size(text) );
 
     for (i = pos = 0; text[i]; i++)
     {
@@ -1056,7 +1029,7 @@ static WCHAR *compute_native_identifier(int exeIndex, LPCWSTR icoPathW, LPCWSTR
     if (basename == NULL) basename = icoPathW;
     else basename++;
     ext = wcsrchr(basename, '.');
-    if (!ext) ext = basename + lstrlenW(basename);
+    if (!ext) ext = basename + utf16len(basename);
 
     return heap_wprintf(L"%04X_%.*s.%d", crc, (int)(ext - basename), basename, exeIndex);
 }
@@ -1200,7 +1173,7 @@ static DWORD register_menus_entry(const WCHAR *menu_file, const WCHAR *windows_f
     if (hkey)
     {
         ret = RegSetValueExW(hkey, menu_file, 0, REG_SZ, (const BYTE*)windows_file,
-                    (lstrlenW(windows_file) + 1) * sizeof(WCHAR));
+                    utf16size(windows_file));
         RegCloseKey(hkey);
     }
     else
@@ -1215,7 +1188,7 @@ static LPSTR escape(LPCWSTR arg)
     WCHAR *escaped_string;
     char *utf8_string;
 
-    escaped_string = xmalloc((4 * lstrlenW(arg) + 1) * sizeof(WCHAR));
+    escaped_string = xmalloc(4 * utf16size(arg));
     for (i = j = 0; arg[i]; i++)
     {
         switch (arg[i])
@@ -1406,7 +1379,7 @@ static BOOL write_menu_file(const WCHAR *windows_link, const WCHAR *link)
     fprintf(tempfile, "</Menu>\n");
 
     menuPath = heap_wprintf(L"%s\\%s", xdg_menu_dir, filename);
-    lstrcpyW(menuPath + lstrlenW(menuPath) - lstrlenW(L".desktop"), L".menu");
+    lstrcpyW(menuPath + utf16len(menuPath) - utf16len(L".desktop"), L".menu");
 
     fclose(tempfile);
     ret = MoveFileExW( tempfilename, menuPath, MOVEFILE_REPLACE_EXISTING );
@@ -1498,7 +1471,7 @@ static BOOL get_link_location( LPCWSTR linkfile, DWORD *loc, WCHAR **relative )
         if (!SHGetSpecialFolderPathW( 0, buffer, locations[i], FALSE ))
             continue;
 
-        len = lstrlenW(buffer);
+        len = utf16len(buffer);
         if (len >= MAX_PATH)
             continue; /* We've just trashed memory! Hopefully we are OK */
 
@@ -1760,10 +1733,10 @@ static WCHAR *freedesktop_mime_type_for_extension(struct list *native_mime_types
     {
         if (PathMatchSpecW( extensionW, mime_type_entry->glob ))
         {
-            if (match == NULL || matchLength < lstrlenW(mime_type_entry->glob))
+            if (match == NULL || matchLength < utf16len(mime_type_entry->glob))
             {
                 match = mime_type_entry->mimeType;
-                matchLength = lstrlenW(mime_type_entry->glob);
+                matchLength = utf16len(mime_type_entry->glob);
             }
         }
     }
@@ -1879,12 +1852,12 @@ static void update_association(LPCWSTR extension, const WCHAR *mimeType, const W
         goto done;
     }
 
-    RegSetValueExW(subkey, L"MimeType", 0, REG_SZ, (const BYTE*) mimeType, (lstrlenW(mimeType) + 1) * sizeof(WCHAR));
-    RegSetValueExW(subkey, L"ProgID", 0, REG_SZ, (const BYTE*) progId, (lstrlenW(progId) + 1) * sizeof(WCHAR));
-    RegSetValueExW(subkey, L"AppName", 0, REG_SZ, (const BYTE*) appName, (lstrlenW(appName) + 1) * sizeof(WCHAR));
-    RegSetValueExW(subkey, L"DesktopFile", 0, REG_SZ, (const BYTE*) desktopFile, (lstrlenW(desktopFile) + 1) * sizeof(WCHAR));
+    RegSetValueExW(subkey, L"MimeType", 0, REG_SZ, (const BYTE*) mimeType, utf16size(mimeType));
+    RegSetValueExW(subkey, L"ProgID", 0, REG_SZ, (const BYTE*) progId, utf16size(progId));
+    RegSetValueExW(subkey, L"AppName", 0, REG_SZ, (const BYTE*) appName, utf16size(appName));
+    RegSetValueExW(subkey, L"DesktopFile", 0, REG_SZ, (const BYTE*) desktopFile, utf16size(desktopFile));
     if (openWithIcon)
-        RegSetValueExW(subkey, L"OpenWithIcon", 0, REG_SZ, (const BYTE*) openWithIcon, (lstrlenW(openWithIcon) + 1) * sizeof(WCHAR));
+        RegSetValueExW(subkey, L"OpenWithIcon", 0, REG_SZ, (const BYTE*) openWithIcon, utf16size(openWithIcon));
     else
         RegDeleteValueW(subkey, L"OpenWithIcon");
 
-- 
GitLab

https://gitlab.winehq.org/wine/wine/-/merge_requests/308



More information about the wine-devel mailing list