[WINSPOOL] GetPrinterDriverA(): Refactor failure handling (#2832)
[reactos.git] / win32ss / printing / base / winspool / utils.c
1 /*
2 * PROJECT: ReactOS Spooler API
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Utility Functions related to Print Processors
5 * COPYRIGHT: Copyright 2020 Doug Lyons (douglyons@douglyons.com)
6 */
7
8 #include "precomp.h"
9
10 /*
11 * Converts an incoming Unicode string to an ANSI string.
12 * It is only useful for "in-place" conversions where the ANSI string goes
13 * back into the same place where the Unicode string came into this function.
14 *
15 * It returns an error code.
16 */
17 // TODO: It seems that many of the functions involving printing could use this.
18 DWORD UnicodeToAnsiInPlace(PWSTR pwszField)
19 {
20 PSTR pszTemp;
21 DWORD cch;
22
23 /*
24 * Map the incoming Unicode pwszField string to an ANSI one here so that we can do
25 * in-place conversion. We read the Unicode input and then we write back the ANSI
26 * conversion into the same buffer for use with our GetPrinterDriverA function
27 */
28 PSTR pszField = (PSTR)pwszField;
29
30 if (!pwszField)
31 {
32 return ERROR_SUCCESS;
33 }
34
35 cch = wcslen(pwszField);
36 if (cch == 0)
37 {
38 return ERROR_SUCCESS;
39 }
40
41 pszTemp = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
42 if (!pszTemp)
43 {
44 ERR("HeapAlloc failed!\n");
45 return ERROR_NOT_ENOUGH_MEMORY;
46 }
47
48 WideCharToMultiByte(CP_ACP, 0, pwszField, -1, pszTemp, cch + 1, NULL, NULL);
49 StringCchCopyA(pszField, cch + 1, pszTemp);
50
51 HeapFree(hProcessHeap, 0, pszTemp);
52
53 return ERROR_SUCCESS;
54 }