nulib2/nulib2/MiscUtils.c

139 lines
3.0 KiB
C
Raw Permalink Normal View History

2000-05-23 01:55:31 +00:00
/*
Distinguish Unicode and Mac OS Roman strings NufxLib has historically made no effort to distinguish between the character set used for filenames on the local disk, and for filenames stored within the archive. Now all Unicode filename strings use the UNICHAR type and have "UNI" in the name, and all Mac OS Roman strings have "MOR" in the name. (The naming convention makes it obvious when you're assigning the wrong thing; on Linux both formats are char*, so the compiler won't tell you if you get it wrong.) The distinction is necessary because filesystems generally support Unicode these days, but on Windows you need to use a separate set of wide-character file I/O functions. (On Linux it all works with "narrow" strings, and the UTF-8 encoding is interpreted by applications.) The character set used for NuFX archive filenames is MOR, matching what GS/OS + HFS supported, and we want to be able to convert back and forth between MOR and a Unicode representation. This change updates the various character types and string names, adds conversion functions, and updates NuLib2 for proper execution on Linux. It does not include the (probably extensive) changes required for Windows UTF-16 support. Instead, the conversion functions are no-ops, which should result in NuLib2 for Windows continuing to behave in the same slightly broken way. This adds "test-names", which exercises Unicode filenames a bit. It will not pass on Win32. Also, tweaked the Linux makefiles to have explicit dependencies, rather than empty space and an expectation that "makedepend" exists. Also, minor source code cleanups. While this probably doesn't affect binary compatibility -- it's mainly a matter of naming and string interpretation -- there's enough going on that it should be considered an API revision, so this updates the version to 3.0.0.
2014-12-24 19:14:32 +00:00
* NuLib2
2007-02-19 23:11:55 +00:00
* Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved.
2000-05-23 01:55:31 +00:00
* This is free software; you can redistribute it and/or modify it under the
2007-02-19 23:11:55 +00:00
* terms of the BSD License, see the file COPYING.
2000-05-23 01:55:31 +00:00
*
* Misc support functions.
*/
Distinguish Unicode and Mac OS Roman strings NufxLib has historically made no effort to distinguish between the character set used for filenames on the local disk, and for filenames stored within the archive. Now all Unicode filename strings use the UNICHAR type and have "UNI" in the name, and all Mac OS Roman strings have "MOR" in the name. (The naming convention makes it obvious when you're assigning the wrong thing; on Linux both formats are char*, so the compiler won't tell you if you get it wrong.) The distinction is necessary because filesystems generally support Unicode these days, but on Windows you need to use a separate set of wide-character file I/O functions. (On Linux it all works with "narrow" strings, and the UTF-8 encoding is interpreted by applications.) The character set used for NuFX archive filenames is MOR, matching what GS/OS + HFS supported, and we want to be able to convert back and forth between MOR and a Unicode representation. This change updates the various character types and string names, adds conversion functions, and updates NuLib2 for proper execution on Linux. It does not include the (probably extensive) changes required for Windows UTF-16 support. Instead, the conversion functions are no-ops, which should result in NuLib2 for Windows continuing to behave in the same slightly broken way. This adds "test-names", which exercises Unicode filenames a bit. It will not pass on Win32. Also, tweaked the Linux makefiles to have explicit dependencies, rather than empty space and an expectation that "makedepend" exists. Also, minor source code cleanups. While this probably doesn't affect binary compatibility -- it's mainly a matter of naming and string interpretation -- there's enough going on that it should be considered an API revision, so this updates the version to 3.0.0.
2014-12-24 19:14:32 +00:00
#include "NuLib2.h"
2000-05-23 01:55:31 +00:00
/*
* Similar to perror(), but takes the error as an argument, and knows
* about NufxLib errors as well as system errors.
*
2014-12-22 02:17:23 +00:00
* If "format" is NULL, just the error message itself is printed.
2000-05-23 01:55:31 +00:00
*/
void ReportError(NuError err, const char* format, ...)
2000-05-23 01:55:31 +00:00
{
const char* msg;
va_list args;
2014-12-22 02:17:23 +00:00
Assert(format != NULL);
va_start(args, format);
/* print the message, if any */
2014-12-22 02:17:23 +00:00
if (format != NULL) {
fprintf(stderr, "%s: ERROR: ", gProgName);
vfprintf(stderr, format, args);
}
/* print the error code data, if any */
if (err == kNuErrNone)
fprintf(stderr, "\n");
else {
2014-12-22 02:17:23 +00:00
if (format != NULL)
fprintf(stderr, ": ");
2014-12-22 02:17:23 +00:00
msg = NULL;
if (err >= 0)
msg = strerror(err);
2014-12-22 02:17:23 +00:00
if (msg == NULL)
msg = NuStrError(err);
2014-12-22 02:17:23 +00:00
if (msg == NULL)
fprintf(stderr, "(unknown err=%d)\n", err);
else
fprintf(stderr, "%s\n", msg);
}
va_end(args);
2000-05-23 01:55:31 +00:00
}
/*
* Memory allocation wrappers.
*
* Under gcc these would be macros, but not all compilers can handle that.
*/
#ifndef USE_DMALLOC
void* Malloc(size_t size)
2000-05-23 01:55:31 +00:00
{
void* _result;
Assert(size > 0);
_result = malloc(size);
2014-12-22 02:17:23 +00:00
if (_result == NULL) {
ReportError(kNuErrMalloc, "malloc(%u) failed", (unsigned int) size);
DebugAbort(); /* leave a core dump if we're built for it */
}
DebugFill(_result, size);
return _result;
2000-05-23 01:55:31 +00:00
}
void* Calloc(size_t size)
2000-05-23 01:55:31 +00:00
{
void* _cresult = Malloc(size);
memset(_cresult, 0, size);
return _cresult;
2000-05-23 01:55:31 +00:00
}
void* Realloc(void* ptr, size_t size)
2000-05-23 01:55:31 +00:00
{
void* _result;
2014-12-22 02:17:23 +00:00
Assert(ptr != NULL); /* disallow this usage */
Assert(size > 0); /* disallow this usage */
_result = realloc(ptr, size);
2014-12-22 02:17:23 +00:00
if (_result == NULL) {
ReportError(kNuErrMalloc, "realloc(%u) failed", (unsigned int) size);
DebugAbort(); /* leave a core dump if we're built for it */
}
return _result;
2000-05-23 01:55:31 +00:00
}
void Free(void* ptr)
2000-05-23 01:55:31 +00:00
{
2014-12-22 02:17:23 +00:00
if (ptr != NULL)
free(ptr);
2000-05-23 01:55:31 +00:00
}
#endif
/*
* This gets called when a buffer DataSource is no longer needed.
*/
NuResult FreeCallback(NuArchive* pArchive, void* args)
{
DBUG(("+++ free callback 0x%08lx\n", (long) args));
Free(args);
return kNuOK;
}
Distinguish Unicode and Mac OS Roman strings NufxLib has historically made no effort to distinguish between the character set used for filenames on the local disk, and for filenames stored within the archive. Now all Unicode filename strings use the UNICHAR type and have "UNI" in the name, and all Mac OS Roman strings have "MOR" in the name. (The naming convention makes it obvious when you're assigning the wrong thing; on Linux both formats are char*, so the compiler won't tell you if you get it wrong.) The distinction is necessary because filesystems generally support Unicode these days, but on Windows you need to use a separate set of wide-character file I/O functions. (On Linux it all works with "narrow" strings, and the UTF-8 encoding is interpreted by applications.) The character set used for NuFX archive filenames is MOR, matching what GS/OS + HFS supported, and we want to be able to convert back and forth between MOR and a Unicode representation. This change updates the various character types and string names, adds conversion functions, and updates NuLib2 for proper execution on Linux. It does not include the (probably extensive) changes required for Windows UTF-16 support. Instead, the conversion functions are no-ops, which should result in NuLib2 for Windows continuing to behave in the same slightly broken way. This adds "test-names", which exercises Unicode filenames a bit. It will not pass on Win32. Also, tweaked the Linux makefiles to have explicit dependencies, rather than empty space and an expectation that "makedepend" exists. Also, minor source code cleanups. While this probably doesn't affect binary compatibility -- it's mainly a matter of naming and string interpretation -- there's enough going on that it should be considered an API revision, so this updates the version to 3.0.0.
2014-12-24 19:14:32 +00:00
/*
* Convert Mac OS Roman to Unicode. The caller must free the string
* returned.
*
* Returns NULL if stringMOR is NULL or if the conversion fails.
*/
UNICHAR* CopyMORToUNI(const char* stringMOR)
{
size_t uniLen;
UNICHAR* uniBuf;
if (stringMOR == NULL) {
return NULL;
}
uniLen = NuConvertMORToUNI(stringMOR, NULL, 0);
if (uniLen == (size_t) -1) {
return NULL;
}
uniBuf = (UNICHAR*) malloc(uniLen);
NuConvertMORToUNI(stringMOR, uniBuf, uniLen);
return uniBuf;
}