2 * PROJECT: ReactOS Applications
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/msconfig_new/fileutils.c
5 * PURPOSE: File Utility Functions
6 * COPYRIGHT: Copyright 2011-2012 Hermes BELUSCA - MAITO <hermes.belusca@sfr.fr>
11 #include "fileutils.h"
14 // NOTE: A function called "FileExists" with the very same prototype
15 // already exists in the PSDK headers (in setupapi.h)
18 MyFileExists(IN LPCWSTR lpszFilePath
,
19 OUT PWIN32_FIND_DATAW pFindData OPTIONAL
)
21 BOOL bIsFound
= FALSE
;
22 WIN32_FIND_DATAW find_data
;
28 dwNumOfChars
= ExpandEnvironmentStringsW(lpszFilePath
, NULL
, 0);
29 lpszCmdLine
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
30 ExpandEnvironmentStringsW(lpszFilePath
, lpszCmdLine
, dwNumOfChars
);
32 search
= FindFirstFileW(lpszCmdLine
, &find_data
);
35 bIsFound
= (search
!= INVALID_HANDLE_VALUE
);
39 if (bIsFound
&& pFindData
)
40 *pFindData
= find_data
;
46 FileQueryFiles(IN LPCWSTR Path
,
47 IN LPCWSTR FileNamesQuery
,
48 IN PQUERY_FILES_TABLE QueryTable
,
51 LRESULT res
= ERROR_SUCCESS
;
52 WIN32_FIND_DATAW find_data
;
56 LPWSTR lpszExpandedQuery
;
59 dwNumOfChars
= wcslen(Path
) + 1 + wcslen(FileNamesQuery
) + 1;
60 lpszQuery
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
61 wcscpy(lpszQuery
, Path
);
62 wcscat(lpszQuery
, L
"\\");
63 wcscat(lpszQuery
, FileNamesQuery
);
65 dwNumOfChars
= ExpandEnvironmentStringsW(lpszQuery
, NULL
, 0);
66 lpszExpandedQuery
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
67 ExpandEnvironmentStringsW(lpszQuery
, lpszExpandedQuery
, dwNumOfChars
);
70 search
= FindFirstFileW(lpszExpandedQuery
, &find_data
);
71 if (search
!= INVALID_HANDLE_VALUE
)
75 PQUERY_FILES_TABLE pTable
= QueryTable
;
76 while (pTable
&& pTable
->QueryRoutine
)
78 pTable
->QueryRoutine(Path
, FileNamesQuery
, lpszExpandedQuery
, &find_data
, Context
, pTable
->EntryContext
);
81 } while (/*res = */ FindNextFileW(search
, &find_data
));
84 res
= ERROR_NO_MORE_FILES
;
88 MemFree(lpszExpandedQuery
);
93 BOOL
BackupIniFile(IN LPCWSTR lpszIniFile
)
96 DWORD dwNumOfChars
= 0;
97 LPWSTR SourceFile
, DestFile
;
98 LPWSTR lpName
, lpPath
;
100 dwNumOfChars
= ExpandEnvironmentStringsW(lpszIniFile
, NULL
, 0);
101 SourceFile
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
102 ExpandEnvironmentStringsW(lpszIniFile
, SourceFile
, dwNumOfChars
);
104 lpName
= wcsrchr(SourceFile
, L
'\\');
105 lpName
= (lpName
? lpName
+ 1 : SourceFile
);
106 dwNumOfChars
= wcslen(L
"%SystemRoot%\\pss\\") + wcslen(lpName
) + 7 + 1;
107 lpPath
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
108 wcscpy(lpPath
, L
"%SystemRoot%\\pss\\");
109 wcscat(lpPath
, lpName
);
110 wcscat(lpPath
, L
".backup");
112 dwNumOfChars
= ExpandEnvironmentStringsW(lpPath
, NULL
, 0);
113 DestFile
= (LPWSTR
)MemAlloc(0, dwNumOfChars
* sizeof(WCHAR
));
114 ExpandEnvironmentStringsW(lpPath
, DestFile
, dwNumOfChars
);
117 Success
= CopyFileW(SourceFile
, DestFile
, TRUE
/* don't overwrite */ /* FALSE */ /* overwrite */);