Sync Apps to wine HEAD (1 of 2)
[reactos.git] / rosapps / applications / winfile / winefile.c
index a51c80b..2caff8e 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "winefile.h"
 #include "resource.h"
+#include "wine/unicode.h"
 
 #ifdef _NO_EXTENSIONS
 #undef _LEFT_FILES
@@ -196,19 +197,27 @@ static const TCHAR sQMarks[] = {'?','?','?','\0'};
 static const TCHAR sWINEFILEFRAME[] = {'W','F','S','_','F','r','a','m','e','\0'};
 static const TCHAR sWINEFILETREE[] = {'W','F','S','_','T','r','e','e','\0'};
 
-static const TCHAR sLongHexFmt[] = {'%','I','6','4','X','\0'};
-static const TCHAR sLongNumFmt[] = {'%','I','6','4','u','\0'};
+static void format_longlong(LPWSTR ret, ULONGLONG val)
+{
+    WCHAR buffer[65], *p = &buffer[64];
+
+    *p = 0;
+    do {
+        *(--p) = '0' + val % 10;
+       val /= 10;
+    } while (val);
+    lstrcpyW( ret, p );
+}
 
 
 /* load resource string */
-static LPTSTR load_string(LPTSTR buffer, UINT id)
+static LPTSTR load_string(LPTSTR buffer, DWORD size, UINT id)
 {
-       LoadString(Globals.hInstance, id, buffer, BUFFER_LEN);
-
+       LoadString(Globals.hInstance, id, buffer, size);
        return buffer;
 }
 
-#define RS(b, i) load_string(b, i)
+#define RS(b, i) load_string(b, sizeof(b)/sizeof(b[0]), i)
 
 
 /* display error message for the specified WIN32 error code */
@@ -300,52 +309,6 @@ static inline void choose_font(HWND hwnd)
         ReleaseDC(hwnd, hdc);
 }
 
-#ifdef __WINE__
-
-#ifdef UNICODE
-
-/* call vswprintf() in msvcrt.dll */
-/*TODO: fix swprintf() in non-msvcrt mode, so that this dynamic linking function can be removed */
-static int msvcrt_swprintf(WCHAR* buffer, const WCHAR* fmt, ...)
-{
-       static int (__cdecl *pvswprintf)(WCHAR*, const WCHAR*, va_list) = NULL;
-       va_list ap;
-       int ret;
-
-       if (!pvswprintf) {
-               HMODULE hModMsvcrt = LoadLibraryA("msvcrt");
-               pvswprintf = (int(__cdecl*)(WCHAR*,const WCHAR*,va_list)) GetProcAddress(hModMsvcrt, "vswprintf");
-       }
-
-       va_start(ap, fmt);
-       ret = (*pvswprintf)(buffer, fmt, ap);
-       va_end(ap);
-
-       return ret;
-}
-
-static LPCWSTR my_wcsrchr(LPCWSTR str, WCHAR c)
-{
-       LPCWSTR p = str;
-
-       while(*p)
-               ++p;
-
-       do {
-               if (--p < str)
-                       return NULL;
-       } while(*p != c);
-
-       return p;
-}
-
-#define _tcsrchr my_wcsrchr
-#else  /* UNICODE */
-#define _tcsrchr strrchr
-#endif /* UNICODE */
-
-#endif /* __WINE__ */
-
 
 /* allocate and initialise a directory entry */
 static Entry* alloc_entry(void)
@@ -965,7 +928,7 @@ static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrde
                ((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;
 
                hr = IShellFolder_BindToObject(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
-               if (!SUCCEEDED(hr))
+               if (FAILED(hr))
                        break;
 
                read_directory(entry, NULL, sortOrder, hwnd);
@@ -1071,7 +1034,7 @@ static void read_directory_shell(Entry* dir, HWND hwnd)
                        memset(pidls, 0, sizeof(pidls));
 
                        hr = IEnumIDList_Next(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
-                       if (!SUCCEEDED(hr))
+                       if (FAILED(hr))
                                break;
 
                        if (hr == S_FALSE)
@@ -1089,7 +1052,7 @@ static void read_directory_shell(Entry* dir, HWND hwnd)
                                memset(&entry->data, 0, sizeof(WIN32_FIND_DATA));
                                entry->bhfi_valid = FALSE;
 
-                               attribs = ~SFGAO_FILESYSTEM;    /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
+                               attribs = ~SFGAO_FILESYSTEM;    /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
 
                                hr = IShellFolder_GetAttributesOf(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
 
@@ -1220,8 +1183,8 @@ static int compareExt(const void* arg1, const void* arg2)
        name1 = fd1->cFileName;
        name2 = fd2->cFileName;
 
-       ext1 = _tcsrchr(name1, '.');
-       ext2 = _tcsrchr(name2, '.');
+       ext1 = strrchrW(name1, '.');
+       ext2 = strrchrW(name2, '.');
 
        if (ext1)
                ext1++;
@@ -1407,9 +1370,9 @@ static Entry* read_tree(Root* root, LPCTSTR path, LPITEMIDLIST pidl, LPTSTR drv,
                root->drive_type = DRIVE_UNKNOWN;
                drv[0] = '\\';
                drv[1] = '\0';
-               load_string(root->volname, IDS_DESKTOP);
+               load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_DESKTOP);
                root->fs_flags = 0;
-               load_string(root->fs, IDS_SHELL);
+               load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_SHELL);
 
                return read_tree_shell(root, pidl, sortOrder, hwnd);
        }
@@ -1422,9 +1385,9 @@ static Entry* read_tree(Root* root, LPCTSTR path, LPITEMIDLIST pidl, LPTSTR drv,
                root->drive_type = GetDriveType(path);
 
                lstrcat(drv, sSlash);
-               load_string(root->volname, IDS_ROOT_FS);
+               load_string(root->volname, sizeof(root->volname)/sizeof(root->volname[0]), IDS_ROOT_FS);
                root->fs_flags = 0;
-               load_string(root->fs, IDS_UNIXFS);
+               load_string(root->fs, sizeof(root->fs)/sizeof(root->fs[0]), IDS_UNIXFS);
 
                lstrcpy(root->path, sSlash);
 
@@ -1509,7 +1472,7 @@ static ChildWnd* alloc_child_window(LPCTSTR path, LPITEMIDLIST pidl, HWND hwnd)
 
 #ifdef _SHELL_FOLDERS
        if (root->entry.etype == ET_SHELL)
-               load_string(root->entry.data.cFileName, IDS_DESKTOP);
+               load_string(root->entry.data.cFileName, sizeof(root->entry.data.cFileName)/sizeof(root->entry.data.cFileName[0]), IDS_DESKTOP);
        else
 #endif
                wsprintf(root->entry.data.cFileName, RS(b1,IDS_TITLEFMT), drv, root->fs);
@@ -2033,7 +1996,6 @@ static INT_PTR CALLBACK PropertiesDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wpa
                        static const TCHAR sByteFmt[] = {'%','s',' ','B','y','t','e','s','\0'};
                        TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
                        LPWIN32_FIND_DATA pWFD;
-                       ULONGLONG size;
 
                        dlg = (struct PropertiesDialog*) lparam;
                        pWFD = (LPWIN32_FIND_DATA) &dlg->entry.data;
@@ -2045,8 +2007,7 @@ static INT_PTR CALLBACK PropertiesDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wpa
                        format_date(&pWFD->ftLastWriteTime, b1, COL_DATE|COL_TIME);
                        SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_LASTCHANGE), b1);
 
-                       size = ((ULONGLONG)pWFD->nFileSizeHigh << 32) | pWFD->nFileSizeLow;
-                       _stprintf(b1, sLongNumFmt, size);
+                        format_longlong( b1, ((ULONGLONG)pWFD->nFileSizeHigh << 32) | pWFD->nFileSizeLow );
                        wsprintf(b2, sByteFmt, b1);
                        SetWindowText(GetDlgItem(hwnd, IDC_STATIC_PROP_SIZE), b2);
 
@@ -2908,17 +2869,18 @@ static void format_bytes(LPTSTR buffer, LONGLONG bytes)
        static const TCHAR sFmtGB[] = {'%', '.', '1', 'f', ' ', 'G', 'B', '\0'};
        static const TCHAR sFmtMB[] = {'%', '.', '1', 'f', ' ', 'M', 'B', '\0'};
        static const TCHAR sFmtkB[] = {'%', '.', '1', 'f', ' ', 'k', 'B', '\0'};
+       static const TCHAR sFmtB[]  = {'%', 'u', 0};
 
        float fBytes = (float)bytes;
 
        if (bytes >= 1073741824)        /* 1 GB */
-               _stprintf(buffer, sFmtGB, fBytes/1073741824.f+.5f);
+               sprintfW(buffer, sFmtGB, fBytes/1073741824.f+.5f);
        else if (bytes >= 1048576)      /* 1 MB */
-               _stprintf(buffer, sFmtMB, fBytes/1048576.f+.5f);
+               sprintfW(buffer, sFmtMB, fBytes/1048576.f+.5f);
        else if (bytes >= 1024)         /* 1 kB */
-               _stprintf(buffer, sFmtkB, fBytes/1024.f+.5f);
+               sprintfW(buffer, sFmtkB, fBytes/1024.f+.5f);
        else
-               _stprintf(buffer, sLongNumFmt, bytes);
+               sprintfW(buffer, sFmtB, (DWORD)bytes);
 }
 
 static void set_space_status(void)
@@ -3142,7 +3104,7 @@ static BOOL is_registered_type(LPCTSTR ext)
 
 static enum FILE_TYPE get_file_type(LPCTSTR filename)
 {
-       LPCTSTR ext = _tcsrchr(filename, '.');
+       LPCTSTR ext = strrchrW(filename, '.');
        if (!ext)
                ext = sEmpty;
 
@@ -3355,11 +3317,7 @@ static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWi
                if (!(attrs&FILE_ATTRIBUTE_DIRECTORY))
 #endif
                {
-                       ULONGLONG size;
-
-                        size = ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow;
-
-                       _stprintf(buffer, sLongNumFmt, size);
+                       format_longlong( buffer, ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow );
 
                        if (calcWidthCol == -1)
                                output_number(pane, dis, col, buffer);
@@ -3398,10 +3356,15 @@ static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWi
 
 #ifndef _NO_EXTENSIONS
        if (entry->bhfi_valid) {
-            ULONGLONG index = ((ULONGLONG)entry->bhfi.nFileIndexHigh << 32) | entry->bhfi.nFileIndexLow;
-
                if (visible_cols & COL_INDEX) {
-                       _stprintf(buffer, sLongHexFmt, index);
+                        static const TCHAR fmtlow[] = {'%','X',0};
+                        static const TCHAR fmthigh[] = {'%','X','%','0','8','X',0};
+
+                        if (entry->bhfi.nFileIndexHigh)
+                            wsprintf(buffer, fmthigh,
+                                     entry->bhfi.nFileIndexHigh, entry->bhfi.nFileIndexLow );
+                        else
+                            wsprintf(buffer, fmtlow, entry->bhfi.nFileIndexLow );
 
                        if (calcWidthCol == -1)
                                output_text(pane, dis, col, buffer, DT_RIGHT);
@@ -3818,8 +3781,15 @@ static void refresh_child(ChildWnd* child)
        scan_entry(child, &child->root.entry, 0, child->hwnd);
 
 #ifdef _SHELL_FOLDERS
+
        if (child->root.entry.etype == ET_SHELL)
-               entry = read_tree(&child->root, NULL, get_path_pidl(path,child->hwnd), drv, child->sortOrder, child->hwnd);
+       {
+               LPITEMIDLIST local_pidl = get_path_pidl(path,child->hwnd);
+               if (local_pidl)
+                       entry = read_tree(&child->root, NULL, local_pidl , drv, child->sortOrder, child->hwnd);
+               else
+                       entry = NULL;
+       }
        else
 #endif
                entry = read_tree(&child->root, path, NULL, drv, child->sortOrder, child->hwnd);
@@ -3865,7 +3835,7 @@ static void create_drive_bar(void)
 #endif
 #ifdef _SHELL_FOLDERS
        /* insert shell namespace button */
-       load_string(b1, IDS_SHELL);
+       load_string(b1, sizeof(b1)/sizeof(b1[0]), IDS_SHELL);
        b1[lstrlen(b1)+1] = '\0';
        SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b1);
 
@@ -4720,18 +4690,18 @@ static void InitInstance(HINSTANCE hinstance)
        /* load column strings */
        col = 1;
 
-       load_string(g_pos_names[col++], IDS_COL_NAME);
-       load_string(g_pos_names[col++], IDS_COL_SIZE);
-       load_string(g_pos_names[col++], IDS_COL_CDATE);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_NAME);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SIZE);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_CDATE);
 #ifndef _NO_EXTENSIONS
-       load_string(g_pos_names[col++], IDS_COL_ADATE);
-       load_string(g_pos_names[col++], IDS_COL_MDATE);
-       load_string(g_pos_names[col++], IDS_COL_IDX);
-       load_string(g_pos_names[col++], IDS_COL_LINKS);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ADATE);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_MDATE);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_IDX);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_LINKS);
 #endif
-       load_string(g_pos_names[col++], IDS_COL_ATTR);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_ATTR);
 #ifndef _NO_EXTENSIONS
-       load_string(g_pos_names[col++], IDS_COL_SEC);
+       load_string(g_pos_names[col++], sizeof(g_pos_names[col])/sizeof(g_pos_names[col][0]), IDS_COL_SEC);
 #endif
 }