msi/tests: Fixed a typo.
[wine.git] / dlls / gdi / env.c
blob8e582eb6a57ec9439201b5ab5397507f092da09a
1 /*
2 * Driver Environment functions
4 * Note: This has NOTHING to do with the task/process environment!
6 * Copyright 1997 Marcus Meissner
7 * Copyright 1998 Andreas Mohr
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
26 #include <stdio.h>
27 #include <string.h>
29 #include "gdi.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
34 typedef struct {
35 ATOM atom;
36 HGLOBAL16 handle;
37 } ENVTABLE;
39 static ENVTABLE EnvTable[20];
41 static ENVTABLE *SearchEnvTable(ATOM atom)
43 INT16 i;
45 for (i = 19; i >= 0; i--) {
46 if (EnvTable[i].atom == atom)
47 return &EnvTable[i];
49 return NULL;
52 static ATOM GDI_GetNullPortAtom(void)
54 static ATOM NullPortAtom = 0;
55 if (!NullPortAtom)
57 char NullPort[256];
59 GetProfileStringA( "windows", "nullport", "none",
60 NullPort, sizeof(NullPort) );
61 NullPortAtom = AddAtomA( NullPort );
63 return NullPortAtom;
66 static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
68 char buffer[256];
70 lstrcpynA( buffer, lpPortName, sizeof(buffer) );
72 if (buffer[0] && buffer[strlen(buffer)-1] == ':') buffer[strlen(buffer)-1] = 0;
74 if (add)
75 return AddAtomA(buffer);
76 else
77 return FindAtomA(buffer);
81 /***********************************************************************
82 * GetEnvironment (GDI.133)
84 INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
86 ATOM atom;
87 LPCSTR p;
88 ENVTABLE *env;
89 WORD size;
91 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
93 if (!(atom = PortNameToAtom(lpPortName, FALSE)))
94 return 0;
95 if (atom == GDI_GetNullPortAtom())
96 if (!(atom = FindAtomA((LPCSTR)lpdev)))
97 return 0;
98 if (!(env = SearchEnvTable(atom)))
99 return 0;
100 size = GlobalSize16(env->handle);
101 if (!lpdev) return 0;
102 if (size < nMaxSize) nMaxSize = size;
103 if (!(p = GlobalLock16(env->handle))) return 0;
104 memcpy(lpdev, p, nMaxSize);
105 GlobalUnlock16(env->handle);
106 return nMaxSize;
110 /***********************************************************************
111 * SetEnvironment (GDI.132)
113 INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
115 ATOM atom;
116 BOOL16 nullport = FALSE;
117 LPSTR p;
118 ENVTABLE *env;
119 HGLOBAL16 handle;
121 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
123 if ((atom = PortNameToAtom(lpPortName, FALSE))) {
124 if (atom == GDI_GetNullPortAtom()) {
125 nullport = TRUE;
126 atom = FindAtomA((LPCSTR)lpdev);
128 env = SearchEnvTable(atom);
129 GlobalFree16(env->handle);
130 env->atom = 0;
132 if (nCount) { /* store DEVMODE struct */
133 if (nullport)
134 p = (LPSTR)lpdev;
135 else
136 p = (LPSTR)lpPortName;
138 if ((atom = PortNameToAtom(p, TRUE))
139 && (env = SearchEnvTable(0))
140 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
141 if (!(p = GlobalLock16(handle))) {
142 GlobalFree16(handle);
143 return 0;
145 env->atom = atom;
146 env->handle = handle;
147 memcpy(p, lpdev, nCount);
148 GlobalUnlock16(handle);
149 return handle;
151 else return 0;
153 else return -1;