uxini.c 8.53 KB
Newer Older
Kevin Koltzau's avatar
Kevin Koltzau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Win32 5.1 uxtheme ini file processing
 *
 * Copyright (C) 2004 Kevin Koltzau
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Kevin Koltzau's avatar
Kevin Koltzau committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 */

#include "config.h"

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winnls.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);

/***********************************************************************
 * Defines and global variables
 */

static const WCHAR szTextFileResource[] = {
    'T','E','X','T','F','I','L','E','\0'
};

41 42 43 44 45 46
typedef struct _UXINI_FILE {
    LPCWSTR lpIni;
    LPCWSTR lpCurLoc;
    LPCWSTR lpEnd;
} UXINI_FILE, *PUXINI_FILE;

Kevin Koltzau's avatar
Kevin Koltzau committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/***********************************************************************/

/**********************************************************************
 *      UXINI_LoadINI
 *
 * Load a theme INI file out of resources from the specified
 * theme
 *
 * PARAMS
 *     tf                  Theme to load INI file out of resources
 *     lpName              Resource name of the INI file
 *
 * RETURNS
 *     INI file, or NULL if not found
 */
62
PUXINI_FILE UXINI_LoadINI(HMODULE hTheme, LPCWSTR lpName) {
Kevin Koltzau's avatar
Kevin Koltzau committed
63 64 65 66 67
    HRSRC hrsc;
    LPCWSTR lpThemesIni = NULL;
    PUXINI_FILE uf;
    DWORD dwIniSize;

68 69
    TRACE("Loading resource INI %s\n", debugstr_w(lpName));

70
    if((hrsc = FindResourceW(hTheme, lpName, szTextFileResource))) {
71
        if(!(lpThemesIni = LoadResource(hTheme, hrsc))) {
Kevin Koltzau's avatar
Kevin Koltzau committed
72 73 74 75 76
            TRACE("%s resource not found\n", debugstr_w(lpName));
            return NULL;
        }
    }

77
    dwIniSize = SizeofResource(hTheme, hrsc) / sizeof(WCHAR);
Kevin Koltzau's avatar
Kevin Koltzau committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    uf = HeapAlloc(GetProcessHeap(), 0, sizeof(UXINI_FILE));
    uf->lpIni = lpThemesIni;
    uf->lpCurLoc = lpThemesIni;
    uf->lpEnd = lpThemesIni + dwIniSize;
    return uf;
}

/**********************************************************************
 *      UXINI_CloseINI
 *
 * Close an open theme INI file
 *
 * PARAMS
 *     uf                  Theme INI file to close
 */
void UXINI_CloseINI(PUXINI_FILE uf)
{
    HeapFree(GetProcessHeap(), 0, uf);
}

/**********************************************************************
 *      UXINI_eof
 *
 * Determines if we are at the end of the INI file
 *
 * PARAMS
 *     uf                  Theme INI file to test
 */
static inline BOOL UXINI_eof(PUXINI_FILE uf)
{
    return uf->lpCurLoc >= uf->lpEnd;
}

/**********************************************************************
 *      UXINI_isspace
 *
 * Check if a character is a space character
 *
 * PARAMS
 *     c                   Character to test
 */
static inline BOOL UXINI_isspace(WCHAR c)
{
    if (isspace(c)) return TRUE;
    if (c=='\r') return TRUE;
    return FALSE;
}

/**********************************************************************
 *      UXINI_GetNextLine
 *
 * Get the next line in the INI file, non NULL terminated
 * removes whitespace at beginning and end of line, and removes comments
 *
 * PARAMS
 *     uf                  INI file to retrieve next line
 *     dwLen               Location to store pointer to line length
 *
 * RETURNS
 *     The section name, non NULL terminated
 */
139
static LPCWSTR UXINI_GetNextLine(PUXINI_FILE uf, DWORD *dwLen)
Kevin Koltzau's avatar
Kevin Koltzau committed
140 141 142 143 144 145 146 147 148 149
{
    LPCWSTR lpLineEnd;
    LPCWSTR lpLineStart;
    DWORD len;
    do {
        if(UXINI_eof(uf)) return NULL;
        /* Skip whitespace and empty lines */
        while(!UXINI_eof(uf) && (UXINI_isspace(*uf->lpCurLoc) || *uf->lpCurLoc == '\n')) uf->lpCurLoc++;
        lpLineStart = uf->lpCurLoc;
        lpLineEnd = uf->lpCurLoc;
150
        while(!UXINI_eof(uf) && *uf->lpCurLoc != '\n' && *uf->lpCurLoc != ';') lpLineEnd = ++uf->lpCurLoc;
Kevin Koltzau's avatar
Kevin Koltzau committed
151 152 153 154
        /* If comment was found, skip the rest of the line */
        if(*uf->lpCurLoc == ';')
            while(!UXINI_eof(uf) && *uf->lpCurLoc != '\n') uf->lpCurLoc++;
        len = (lpLineEnd - lpLineStart);
155
        if(*lpLineStart != ';' && len == 0)
Kevin Koltzau's avatar
Kevin Koltzau committed
156 157 158 159 160 161 162 163 164
            return NULL;
    } while(*lpLineStart == ';');
    /* Remove whitespace from end of line */
    while(UXINI_isspace(lpLineStart[len-1])) len--;
    *dwLen = len;

    return lpLineStart;
}

165
static inline void UXINI_UnGetToLine(PUXINI_FILE uf, LPCWSTR lpLine)
Kevin Koltzau's avatar
Kevin Koltzau committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
{
    uf->lpCurLoc = lpLine;
}

/**********************************************************************
 *      UXINI_GetNextSection
 *
 * Locate the next section in the ini file, and return pointer to
 * section name, non NULL terminated. Use dwLen to determine length
 *
 * PARAMS
 *     uf                  INI file to search, search starts at current location
 *     dwLen               Location to store pointer to section name length
 *
 * RETURNS
 *     The section name, non NULL terminated
 */
LPCWSTR UXINI_GetNextSection(PUXINI_FILE uf, DWORD *dwLen)
{
    LPCWSTR lpLine;
    while((lpLine = UXINI_GetNextLine(uf, dwLen))) {
        /* Assuming a ']' ending to the section name */
        if(lpLine[0] == '[') {
            lpLine++;
            *dwLen -= 2;
            break;
        }
    }
    return lpLine;
}

/**********************************************************************
 *      UXINI_FindSection
 *
 * Locate a section with the specified name, search starts
 * at current location in ini file
 *
 * PARAMS
 *     uf                  INI file to search, search starts at current location
 *     lpName              Name of the section to locate
 *
 * RETURNS
 *     TRUE if section was found, FALSE otherwise
 */
BOOL UXINI_FindSection(PUXINI_FILE uf, LPCWSTR lpName)
{
    LPCWSTR lpSection;
    DWORD dwLen;
    while((lpSection = UXINI_GetNextSection(uf, &dwLen))) {
        if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpSection, dwLen, lpName, -1) == CSTR_EQUAL) {
            return TRUE;
        }
    }
    return FALSE;
}

/**********************************************************************
 *      UXINI_GetNextValue
 *
 * Locate the next value in the current section
 *
 * PARAMS
 *     uf                  INI file to search, search starts at current location
 *     dwNameLen            Location to store pointer to value name length
 *     lpValue              Location to store pointer to the value
 *     dwValueLen           Location to store pointer to value length
 *
 * RETURNS
 *     The value name, non NULL terminated
 */
LPCWSTR UXINI_GetNextValue(PUXINI_FILE uf, DWORD *dwNameLen, LPCWSTR *lpValue, DWORD *dwValueLen)
{
    LPCWSTR lpLine;
    LPCWSTR lpLineEnd;
    LPCWSTR name = NULL;
    LPCWSTR value = NULL;
    DWORD vallen = 0;
    DWORD namelen = 0;
    DWORD dwLen;
    lpLine = UXINI_GetNextLine(uf, &dwLen);
    if(!lpLine)
        return NULL;
    if(lpLine[0] == '[') {
        UXINI_UnGetToLine(uf, lpLine);
        return NULL;
    }
    lpLineEnd = lpLine + dwLen;

    name = lpLine;
    while(namelen < dwLen && *lpLine != '=') {
        lpLine++;
        namelen++;
    }
    if(*lpLine != '=') return NULL;
    lpLine++;

    /* Remove whitespace from end of name */
    while(UXINI_isspace(name[namelen-1])) namelen--;
    /* Remove whitespace from beginning of value */
    while(UXINI_isspace(*lpLine) && lpLine < lpLineEnd) lpLine++;
    value = lpLine;
    vallen = dwLen-(value-name);

    *dwNameLen = namelen;
    *dwValueLen = vallen;
    *lpValue = value;

    return name;
}

/**********************************************************************
 *      UXINI_FindValue
 *
 * Locate a value by name
 *
 * PARAMS
 *     uf                   INI file to search, search starts at current location
 *     lpName               Value name to locate
 *     lpValue              Location to store pointer to the value
 *     dwValueLen           Location to store pointer to value length
 *
 * RETURNS
 *     The value name, non NULL terminated
 */
BOOL UXINI_FindValue(PUXINI_FILE uf, LPCWSTR lpName, LPCWSTR *lpValue, DWORD *dwValueLen)
{
    LPCWSTR name;
    DWORD namelen;

    while((name = UXINI_GetNextValue(uf, &namelen, lpValue, dwValueLen))) {
        if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, name, namelen, lpName, -1) == CSTR_EQUAL) {
            return TRUE;
        }
    }
    return FALSE;
}