cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_sysfilesystem.c (3835B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23#ifdef SDL_FILESYSTEM_WINDOWS
     24
     25/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     26/* System dependent filesystem routines                                */
     27
     28#include "../../core/windows/SDL_windows.h"
     29#include <shlobj.h>
     30
     31#include "SDL_assert.h"
     32#include "SDL_error.h"
     33#include "SDL_stdinc.h"
     34#include "SDL_filesystem.h"
     35
     36char *
     37SDL_GetBasePath(void)
     38{
     39    TCHAR path[MAX_PATH];
     40    const DWORD len = GetModuleFileName(NULL, path, SDL_arraysize(path));
     41    size_t i;
     42
     43    SDL_assert(len < SDL_arraysize(path));
     44
     45    if (len == 0) {
     46        WIN_SetError("Couldn't locate our .exe");
     47        return NULL;
     48    }
     49
     50    for (i = len-1; i > 0; i--) {
     51        if (path[i] == '\\') {
     52            break;
     53        }
     54    }
     55
     56    SDL_assert(i > 0); /* Should have been an absolute path. */
     57    path[i+1] = '\0';  /* chop off filename. */
     58    return WIN_StringToUTF8(path);
     59}
     60
     61char *
     62SDL_GetPrefPath(const char *org, const char *app)
     63{
     64    /*
     65     * Vista and later has a new API for this, but SHGetFolderPath works there,
     66     *  and apparently just wraps the new API. This is the new way to do it:
     67     *
     68     *     SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
     69     *                          NULL, &wszPath);
     70     */
     71
     72    WCHAR path[MAX_PATH];
     73    char *retval = NULL;
     74    WCHAR* worg = NULL;
     75    WCHAR* wapp = NULL;
     76    size_t new_wpath_len = 0;
     77    BOOL api_result = FALSE;
     78
     79    if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
     80        WIN_SetError("Couldn't locate our prefpath");
     81        return NULL;
     82    }
     83
     84    worg = WIN_UTF8ToString(org);
     85    if (worg == NULL) {
     86        SDL_OutOfMemory();
     87        return NULL;
     88    }
     89
     90    wapp = WIN_UTF8ToString(app);
     91    if (wapp == NULL) {
     92        SDL_free(worg);
     93        SDL_OutOfMemory();
     94        return NULL;
     95    }
     96
     97    new_wpath_len = lstrlenW(worg) + lstrlenW(wapp) + lstrlenW(path) + 3;
     98
     99    if ((new_wpath_len + 1) > MAX_PATH) {
    100        SDL_free(worg);
    101        SDL_free(wapp);
    102        WIN_SetError("Path too long.");
    103        return NULL;
    104    }
    105
    106    lstrcatW(path, L"\\");
    107    lstrcatW(path, worg);
    108    SDL_free(worg);
    109
    110    api_result = CreateDirectoryW(path, NULL);
    111    if (api_result == FALSE) {
    112        if (GetLastError() != ERROR_ALREADY_EXISTS) {
    113            SDL_free(wapp);
    114            WIN_SetError("Couldn't create a prefpath.");
    115            return NULL;
    116        }
    117    }
    118
    119    lstrcatW(path, L"\\");
    120    lstrcatW(path, wapp);
    121    SDL_free(wapp);
    122
    123    api_result = CreateDirectoryW(path, NULL);
    124    if (api_result == FALSE) {
    125        if (GetLastError() != ERROR_ALREADY_EXISTS) {
    126            WIN_SetError("Couldn't create a prefpath.");
    127            return NULL;
    128        }
    129    }
    130
    131    lstrcatW(path, L"\\");
    132
    133    retval = WIN_StringToUTF8(path);
    134
    135    return retval;
    136}
    137
    138#endif /* SDL_FILESYSTEM_WINDOWS */
    139
    140/* vi: set ts=4 sw=4 expandtab: */