/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2010 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstd
 * Version: 1.07
 * First release date:   2010-11-08
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstd library
 * 
 * mzstd 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 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstd 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 program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#include "mzstdGlobals.h"
#include "mzFileUtils.h"
#include "mzLogger.h"

VOID WINAPI initMzstdLibrary(HINSTANCE hInstance) {
    InitLogger(DEFAULT_LOGGER_FILENAME);
    ::hInstance = hInstance;
}

VOID WINAPI cleanupMzstdLibrary() {
    CleanupLogger();
}


CString WINAPI GetSystemErrorMessage(DWORD errorCode) {
	LPVOID msgbuf;
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                    NULL, errorCode, 0, (LPTSTR)&msgbuf, 0, NULL);
    CString msg((WCHAR*)msgbuf);
	LocalFree(msgbuf);
    msg.trim();
    return msg;
}

CString WINAPI GetLastSystemErrorMessage() {
    return GetSystemErrorMessage(GetLastError());
}

CString WINAPI GetDetailedSystemErrorMessage(DWORD errorCode, WCHAR* prependString) {
    CString msg = FormatMessage(L"System error code:\n    %s   %ddx\nSystem error message:\n    %s", L"ERROR_*", errorCode, GetSystemErrorMessage(errorCode).getBuffer());
    if (prependString) {
        msg.prepend(prependString);
    }
    return msg;
}

CString WINAPI GetDetailedLastSystemErrorMessage(WCHAR* prependString) {
    return GetDetailedSystemErrorMessage(GetLastError(), prependString);
}
