ciderpress/diskimg/Global.cpp

192 lines
4.9 KiB
C++
Raw Normal View History

2007-03-27 17:47:10 +00:00
/*
* CiderPress
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
* See the file LICENSE for distribution terms.
*/
/*
* Implementation of DiskImgLib globals.
*/
#include "StdAfx.h"
#include "DiskImgPriv.h"
#include "ASPI.h"
/*static*/ bool Global::fAppInitCalled = false;
2014-11-18 05:13:13 +00:00
/*static*/ ASPI* Global::fpASPI = NULL;
2007-03-27 17:47:10 +00:00
/* global constant */
const char* DiskImgLib::kASPIDev = "ASPI:";
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
2007-03-27 17:47:10 +00:00
/*
* Perform one-time DLL initialization.
*/
/*static*/ DIError Global::AppInit(void)
2007-03-27 17:47:10 +00:00
{
NuError nerr;
int32_t major, minor, bug;
2007-03-27 17:47:10 +00:00
if (fAppInitCalled) {
LOGW("DiskImg AppInit already called");
return kDIErrNone;
}
2007-03-27 17:47:10 +00:00
LOGI("Initializing DiskImg library v%d.%d.%d",
kDiskImgVersionMajor, kDiskImgVersionMinor, kDiskImgVersionBug);
2007-03-27 17:47:10 +00:00
#ifdef _WIN32
HMODULE hModule;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
WCHAR fileNameBuf[256];
hModule = ::GetModuleHandle(L"DiskImg4.dll");
2014-11-18 05:13:13 +00:00
if (hModule != NULL &&
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
::GetModuleFileName(hModule, fileNameBuf,
sizeof(fileNameBuf) / sizeof(WCHAR)) != 0)
{
// GetModuleHandle does not increase ref count, so no need to release
LOGD("DiskImg DLL loaded from '%ls'", fileNameBuf);
} else {
LOGW("Unable to get DiskImg DLL filename");
}
2007-03-27 17:47:10 +00:00
#endif
/*
* Make sure we're linked against a compatible version of NufxLib.
*/
nerr = NuGetVersion(&major, &minor, &bug, NULL, NULL);
if (nerr != kNuErrNone) {
LOGE("Unable to get version number from NufxLib.");
return kDIErrNufxLibInitFailed;
}
if (major != kNuVersionMajor || minor < kNuVersionMinor) {
LOGE("Unexpected NufxLib version %d.%d.%d",
major, minor, bug);
return kDIErrNufxLibInitFailed;
}
/*
* Do one-time init over in the DiskImg class.
*/
DiskImg::CalcNibbleInvTables();
2007-03-27 17:47:10 +00:00
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
#if defined(HAVE_WINDOWS_CDROM) && defined(WANT_ASPI)
if (kAlwaysTryASPI || IsWin9x()) {
fpASPI = new ASPI;
if (fpASPI->Init() != kDIErrNone) {
delete fpASPI;
2014-11-18 05:13:13 +00:00
fpASPI = NULL;
}
}
2007-03-27 17:47:10 +00:00
#endif
LOGD("DiskImg HasSPTI=%d HasASPI=%d", GetHasSPTI(), GetHasASPI());
2007-03-27 17:47:10 +00:00
fAppInitCalled = true;
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Perform cleanup at application shutdown time.
*/
/*static*/ DIError Global::AppCleanup(void)
2007-03-27 17:47:10 +00:00
{
LOGI("DiskImgLib cleanup");
delete fpASPI;
return kDIErrNone;
2007-03-27 17:47:10 +00:00
}
/*
* Simple getters.
*
* SPTI is enabled if we're in Win2K *and* ASPI isn't loaded. If ASPI is
* loaded, it can interfere with SPTI, so we want to stick with one or
* the other.
*/
#ifdef _WIN32
2014-11-18 05:13:13 +00:00
/*static*/ bool Global::GetHasSPTI(void) { return !IsWin9x() && fpASPI == NULL; }
/*static*/ bool Global::GetHasASPI(void) { return fpASPI != NULL; }
2007-03-27 17:47:10 +00:00
/*static*/ unsigned long Global::GetASPIVersion(void) {
2014-11-18 05:13:13 +00:00
assert(fpASPI != NULL);
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
#ifdef WANT_ASPI
return fpASPI->GetVersion();
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
#else
return 123456789;
#endif
2007-03-27 17:47:10 +00:00
}
#else
/*static*/ bool Global::GetHasSPTI(void) { return false; }
/*static*/ bool Global::GetHasASPI(void) { return false; }
/*static*/ unsigned long Global::GetASPIVersion(void) { assert(false); return 0; }
#endif
/*
* Return current library versions.
*/
/*static*/ void Global::GetVersion(int32_t* pMajor, int32_t* pMinor,
int32_t* pBug)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
if (pMajor != NULL)
*pMajor = kDiskImgVersionMajor;
2014-11-18 05:13:13 +00:00
if (pMinor != NULL)
*pMinor = kDiskImgVersionMinor;
2014-11-18 05:13:13 +00:00
if (pBug != NULL)
*pBug = kDiskImgVersionBug;
2007-03-27 17:47:10 +00:00
}
/*
* Pointer to debug message handler function.
*/
2014-11-18 05:13:13 +00:00
/*static*/ Global::DebugMsgHandler Global::gDebugMsgHandler = NULL;
2007-03-27 17:47:10 +00:00
/*
* Change the debug message handler. The previous handler is returned.
*/
Global::DebugMsgHandler Global::SetDebugMsgHandler(DebugMsgHandler handler)
2007-03-27 17:47:10 +00:00
{
DebugMsgHandler oldHandler;
2007-03-27 17:47:10 +00:00
oldHandler = gDebugMsgHandler;
gDebugMsgHandler = handler;
return oldHandler;
2007-03-27 17:47:10 +00:00
}
/*
* Send a debug message to the debug message handler.
*
* Even if _DEBUG_MSGS is disabled we can still get here from the NuFX error
* handler.
*/
/*static*/ void Global::PrintDebugMsg(const char* file, int line, const char* fmt, ...)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
if (gDebugMsgHandler == NULL) {
/*
* This can happen if the app decides to bail with an exit()
* call. I'm not sure what's zapping the pointer.
*
* We get here on "-install" or "-uninstall", which really
* should be using a more Windows-friendly exit strategy.
*/
DebugBreak();
return;
}
char buf[512];
2007-03-27 17:47:10 +00:00
va_list args;
va_start(args, fmt);
#if defined(HAVE_VSNPRINTF)
(void) vsnprintf(buf, sizeof(buf), fmt, args);
2007-03-27 17:47:10 +00:00
#elif defined(HAVE__VSNPRINTF)
(void) _vsnprintf(buf, sizeof(buf), fmt, args);
2007-03-27 17:47:10 +00:00
#else
# error "hosed"
#endif
va_end(args);
buf[sizeof(buf)-1] = '\0';
2007-03-27 17:47:10 +00:00
(*gDebugMsgHandler)(file, line, buf);
2007-03-27 17:47:10 +00:00
}