ciderpress/nufxlib/MiscStuff.c

121 lines
2.9 KiB
C
Raw Normal View History

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
/*
* Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved.
* This is free software; you can redistribute it and/or modify it under the
* terms of the BSD License, see the file COPYING-LIB.
*
* Misc stuff (shared between nufxlib and nulib2). This is a collection
* of standard functions that aren't available in libc on this system.
*/
#include "SysDefs.h"
#include "MiscStuff.h"
#include <ctype.h>
#ifndef HAVE_STRERROR
/*
* Return a pointer to the appropriate string in the system table, or NULL
* if the value is out of bounds.
*/
const char*
Nu_strerror(int errnum)
{
extern int sys_nerr;
extern char *sys_errlist[];
if (errnum < 0 || errnum > sys_nerr)
return NULL;
return sys_errlist[errnum];
}
#endif
#ifndef HAVE_MEMMOVE
/*
* Move a block of memory. Unlike memcpy, this is expected to work
* correctly with overlapping blocks.
*
* This is a straightforward implementation. A much faster implementation,
* from BSD, is available in the PGP 2.6.2 distribution, but this should
* suffice for those few systems that don't have memmove.
*/
void*
Nu_memmove(void* dst, const void* src, size_t n)
{
void* retval = dst;
char* srcp = (char*)src;
char* dstp = (char*)dst;
/* you can normally get away with this if n==0 */
Assert(dst != NULL);
Assert(src != NULL);
if (dstp == srcp || !n) {
/* nothing to do */
} else if (dstp > srcp) {
/* start from the end */
(char*)dstp += n-1;
(char*)srcp += n-1;
while (n--)
*dstp-- = *srcp--;
} else {
/* start from the front */
while (n--)
*dstp++ = *srcp++;
}
return retval;
}
#endif
#ifndef HAVE_STRTOUL
/*
* Perform strtol, but on an unsigned long.
*
* On systems that have strtol but don't have strtoul, the strtol
* function doesn't clamp the return value, making it similar in
* function to strtoul. The comparison is not exact, however,
* because strtoul is expected to lots of fancy things (like set
* errno to ERANGE).
*
* For our purposes here, strtol does all we need it to. Someday
* we should replace this with a "real" version.
*/
unsigned long
Nu_strtoul(const char *nptr, char **endptr, int base)
{
return strtol(nptr, endptr, base);
}
#endif
#ifndef HAVE_STRCASECMP
/*
* Compare two strings, case-insensitive.
*/
int
Nu_strcasecmp(const char *str1, const char *str2)
{
while (*str1 && *str2 && toupper(*str1) == toupper(*str2))
str1++, str2++;
return (toupper(*str1) - toupper(*str2));
}
#endif
#ifndef HAVE_STRNCASECMP
/*
* Compare two strings, case-insensitive, stopping after "n" chars.
*/
int
Nu_strncasecmp(const char *str1, const char *str2, size_t n)
{
while (n && *str1 && *str2 && toupper(*str1) == toupper(*str2))
str1++, str2++, n--;
if (n)
return (toupper(*str1) - toupper(*str2));
else
return 0; /* no mismatch in first n chars */
}
#endif