xref: /reactos/win32ss/printing/base/winspool/utils.c (revision 3435c3b5)
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 BOOL UnicodeToAnsiInPlace(PWSTR pwszField)
11 {
12     /*
13      * This converts an incoming Unicode string to an ANSI string.
14      * It returns FALSE on failure, otherwise it returns TRUE.
15      * It is only useful for "in-place" conversions where the ANSI string goes
16      * back into the same place where the Unicode string came into this function.
17      * It seems that many of the functions involving printing can use this.
18      */
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 TRUE;
33     }
34 
35     cch = wcslen(pwszField);
36     if (cch == 0)
37     {
38         return TRUE;
39     }
40 
41     pszTemp = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
42     if (!pszField)
43     {
44         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
45         ERR("HeapAlloc failed!\n");
46         return FALSE;   // indicates a failure to be handled by caller
47     }
48 
49     WideCharToMultiByte(CP_ACP, 0, pwszField, -1, pszTemp, cch + 1, NULL, NULL);
50     StringCchCopyA(pszField, cch + 1, pszTemp);
51 
52     HeapFree(hProcessHeap, 0, pszTemp);
53 
54     return TRUE;
55 }
56