2 * Win32 5.1 uxtheme ini file processing
4 * Copyright (C) 2004 Kevin Koltzau
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /***********************************************************************
24 * Defines and global variables
27 static const WCHAR szTextFileResource
[] = {
28 'T','E','X','T','F','I','L','E','\0'
31 typedef struct _UXINI_FILE
{
37 /***********************************************************************/
39 /**********************************************************************
42 * Load a theme INI file out of resources from the specified
46 * tf Theme to load INI file out of resources
47 * lpName Resource name of the INI file
50 * INI file, or NULL if not found
52 PUXINI_FILE
UXINI_LoadINI(HMODULE hTheme
, LPCWSTR lpName
) {
54 LPCWSTR lpThemesIni
= NULL
;
58 TRACE("Loading resource INI %s\n", debugstr_w(lpName
));
60 if((hrsc
= FindResourceW(hTheme
, lpName
, szTextFileResource
))) {
61 if(!(lpThemesIni
= LoadResource(hTheme
, hrsc
))) {
62 TRACE("%s resource not found\n", debugstr_w(lpName
));
67 dwIniSize
= SizeofResource(hTheme
, hrsc
) / sizeof(WCHAR
);
68 uf
= HeapAlloc(GetProcessHeap(), 0, sizeof(UXINI_FILE
));
69 uf
->lpIni
= lpThemesIni
;
70 uf
->lpCurLoc
= lpThemesIni
;
71 uf
->lpEnd
= lpThemesIni
+ dwIniSize
;
75 /**********************************************************************
78 * Close an open theme INI file
81 * uf Theme INI file to close
83 void UXINI_CloseINI(PUXINI_FILE uf
)
85 HeapFree(GetProcessHeap(), 0, uf
);
88 /**********************************************************************
91 * Determines if we are at the end of the INI file
94 * uf Theme INI file to test
96 static inline BOOL
UXINI_eof(PUXINI_FILE uf
)
98 return uf
->lpCurLoc
>= uf
->lpEnd
;
101 /**********************************************************************
104 * Check if a character is a space character
107 * c Character to test
109 static inline BOOL
UXINI_isspace(WCHAR c
)
111 if (isspace(c
)) return TRUE
;
112 if (c
=='\r') return TRUE
;
116 /**********************************************************************
119 * Get the next line in the INI file, non NULL terminated
120 * removes whitespace at beginning and end of line, and removes comments
123 * uf INI file to retrieve next line
124 * dwLen Location to store pointer to line length
127 * The section name, non NULL terminated
129 static LPCWSTR
UXINI_GetNextLine(PUXINI_FILE uf
, DWORD
*dwLen
)
135 if(UXINI_eof(uf
)) return NULL
;
136 /* Skip whitespace and empty lines */
137 while(!UXINI_eof(uf
) && (UXINI_isspace(*uf
->lpCurLoc
) || *uf
->lpCurLoc
== '\n')) uf
->lpCurLoc
++;
138 lpLineStart
= uf
->lpCurLoc
;
139 lpLineEnd
= uf
->lpCurLoc
;
140 while(!UXINI_eof(uf
) && *uf
->lpCurLoc
!= '\n' && *uf
->lpCurLoc
!= ';') lpLineEnd
= ++uf
->lpCurLoc
;
141 /* If comment was found, skip the rest of the line */
142 if(*uf
->lpCurLoc
== ';')
143 while(!UXINI_eof(uf
) && *uf
->lpCurLoc
!= '\n') uf
->lpCurLoc
++;
144 len
= (lpLineEnd
- lpLineStart
);
145 if(*lpLineStart
!= ';' && len
== 0)
147 } while(*lpLineStart
== ';');
148 /* Remove whitespace from end of line */
149 while(UXINI_isspace(lpLineStart
[len
-1])) len
--;
155 static inline void UXINI_UnGetToLine(PUXINI_FILE uf
, LPCWSTR lpLine
)
157 uf
->lpCurLoc
= lpLine
;
160 /**********************************************************************
161 * UXINI_GetNextSection
163 * Locate the next section in the ini file, and return pointer to
164 * section name, non NULL terminated. Use dwLen to determine length
167 * uf INI file to search, search starts at current location
168 * dwLen Location to store pointer to section name length
171 * The section name, non NULL terminated
173 LPCWSTR
UXINI_GetNextSection(PUXINI_FILE uf
, DWORD
*dwLen
)
176 while((lpLine
= UXINI_GetNextLine(uf
, dwLen
))) {
177 /* Assuming a ']' ending to the section name */
178 if(lpLine
[0] == '[') {
187 /**********************************************************************
190 * Locate a section with the specified name, search starts
191 * at current location in ini file
194 * uf INI file to search, search starts at current location
195 * lpName Name of the section to locate
198 * TRUE if section was found, FALSE otherwise
200 BOOL
UXINI_FindSection(PUXINI_FILE uf
, LPCWSTR lpName
)
204 while((lpSection
= UXINI_GetNextSection(uf
, &dwLen
))) {
205 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, lpSection
, dwLen
, lpName
, -1) == CSTR_EQUAL
) {
212 /**********************************************************************
215 * Locate the next value in the current section
218 * uf INI file to search, search starts at current location
219 * dwNameLen Location to store pointer to value name length
220 * lpValue Location to store pointer to the value
221 * dwValueLen Location to store pointer to value length
224 * The value name, non NULL terminated
226 LPCWSTR
UXINI_GetNextValue(PUXINI_FILE uf
, DWORD
*dwNameLen
, LPCWSTR
*lpValue
, DWORD
*dwValueLen
)
231 LPCWSTR value
= NULL
;
235 lpLine
= UXINI_GetNextLine(uf
, &dwLen
);
238 if(lpLine
[0] == '[') {
239 UXINI_UnGetToLine(uf
, lpLine
);
242 lpLineEnd
= lpLine
+ dwLen
;
245 while(namelen
< dwLen
&& *lpLine
!= '=') {
249 if(*lpLine
!= '=') return NULL
;
252 /* Remove whitespace from end of name */
253 while(UXINI_isspace(name
[namelen
-1])) namelen
--;
254 /* Remove whitespace from beginning of value */
255 while(UXINI_isspace(*lpLine
) && lpLine
< lpLineEnd
) lpLine
++;
257 vallen
= dwLen
-(value
-name
);
259 *dwNameLen
= namelen
;
260 *dwValueLen
= vallen
;
266 /**********************************************************************
269 * Locate a value by name
272 * uf INI file to search, search starts at current location
273 * lpName Value name to locate
274 * lpValue Location to store pointer to the value
275 * dwValueLen Location to store pointer to value length
278 * The value name, non NULL terminated
280 BOOL
UXINI_FindValue(PUXINI_FILE uf
, LPCWSTR lpName
, LPCWSTR
*lpValue
, DWORD
*dwValueLen
)
285 while((name
= UXINI_GetNextValue(uf
, &namelen
, lpValue
, dwValueLen
))) {
286 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, name
, namelen
, lpName
, -1) == CSTR_EQUAL
) {