diff --git a/common/endian.h b/common/endian.h new file mode 100644 index 00000000..f62a7469 --- /dev/null +++ b/common/endian.h @@ -0,0 +1,92 @@ +/* ScummVM Tools + * Copyright (C) 2002-2009 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef COMMON_ENDIAN_H +#define COMMON_ENDIAN_H + +#include "common/scummsys.h" + +#if defined(INVERSE_MKID) +#define MKID_BE(a) ((uint32) \ + (((a) >> 24) & 0x000000FF) | \ + (((a) >> 8) & 0x0000FF00) | \ + (((a) << 8) & 0x00FF0000) | \ + (((a) << 24) & 0xFF000000)) + +#else +# define MKID_BE(a) ((uint32)(a)) +#endif + +static inline uint32 SWAP_32(uint32 a) { + return uint16(((a >> 24) & 0xFF) | ((a >> 8) & 0xFF00) | ((a << 8) & 0xFF0000) | + ((a << 24) & 0xFF000000)); +} + +static inline uint16 SWAP_16(uint16 a) { + return uint16(((a >> 8) & 0xFF) | ((a << 8) & 0xFF00)); +} + + +FORCEINLINE uint16 READ_LE_UINT16(const void *ptr) { + const byte *b = (const byte *)ptr; + return uint16((b[1] << 8) + b[0]); +} +FORCEINLINE uint32 READ_LE_UINT32(const void *ptr) { + const byte *b = (const byte *)ptr; + return (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]); +} +FORCEINLINE void WRITE_LE_UINT16(void *ptr, uint16 value) { + byte *b = (byte *)ptr; + b[0] = (byte)(value >> 0); + b[1] = (byte)(value >> 8); +} +FORCEINLINE void WRITE_LE_UINT32(void *ptr, uint32 value) { + byte *b = (byte *)ptr; + b[0] = (byte)(value >> 0); + b[1] = (byte)(value >> 8); + b[2] = (byte)(value >> 16); + b[3] = (byte)(value >> 24); +} + +FORCEINLINE uint16 READ_BE_UINT16(const void *ptr) { + const byte *b = (const byte *)ptr; + return uint16((b[0] << 8) + b[1]); +} +FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) { + const byte *b = (const byte *)ptr; + return uint32((b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3])); +} +FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) { + byte *b = (byte *)ptr; + b[0] = (byte)(value >> 8); + b[1] = (byte)(value >> 0); +} +FORCEINLINE void WRITE_BE_UINT32(void *ptr, uint32 value) { + byte *b = (byte *)ptr; + b[0] = (byte)(value >> 24); + b[1] = (byte)(value >> 16); + b[2] = (byte)(value >> 8); + b[3] = (byte)(value >> 0); +} + + +#endif diff --git a/common/file.h b/common/file.h index cef683dd..9f03887c 100644 --- a/common/file.h +++ b/common/file.h @@ -23,10 +23,9 @@ #ifndef COMMON_FILE_H #define COMMON_FILE_H -#include "util.h" +#include "common/scummsys.h" -#include -#include +#include "tool_exception.h" /** * Something unexpected happened while reading / writing to a file diff --git a/common/scummsys.h b/common/scummsys.h new file mode 100644 index 00000000..fb3c6eae --- /dev/null +++ b/common/scummsys.h @@ -0,0 +1,112 @@ +/* ScummVM Tools + * Copyright (C) 2002-2009 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef COMMON_SCUMMSYS_H +#define COMMON_SCUMMSYS_H + + +/* + * Some useful types + */ + +typedef unsigned char byte; +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef signed char int8; +typedef signed short int16; +#ifdef __amigaos4__ +#include +#include +#else +typedef unsigned int uint32; +typedef signed int int32; +#endif + +/* + * Various utility macros + */ + +#if defined(_MSC_VER) + + #define scumm_stricmp stricmp + #define scumm_strnicmp _strnicmp + #define snprintf _snprintf + + #define SCUMM_LITTLE_ENDIAN + #pragma once + #pragma warning( disable : 4068 ) /* turn off "unknown pragma" warning */ + #pragma warning( disable : 4996 ) /* turn off warnings about unsafe functions */ + +#elif defined(__MINGW32__) + + #define scumm_stricmp strcasecmp + #define scumm_stricmp stricmp + #define scumm_strnicmp strnicmp + + #define SCUMM_LITTLE_ENDIAN + +#elif defined(UNIX) + #define scumm_stricmp strcasecmp + #define scumm_strnicmp strncasecmp + + #if defined(__DECCXX) /* Assume alpha architecture */ + #define INVERSE_MKID + #define SCUMM_NEED_ALIGNMENT + #endif + +#else + + #error No system type defined + +#endif + + +/* + * GCC specific stuff + */ +#if defined(__GNUC__) + #define GCC_PACK __attribute__((packed)) +#else + #define GCC_PACK +#endif + +#ifndef ARRAYSIZE +#define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0]))) +#endif + +#ifndef FORCEINLINE +#define FORCEINLINE static inline +#endif + +#if defined(__GNUC__) +#define NORETURN_PRE +#define NORETURN_POST __attribute__((__noreturn__)) +#elif defined(_MSC_VER) +#define NORETURN_PRE _declspec(noreturn) +#define NORETURN_POST +#else +#define NORETURN_PRE +#define NORETURN_POST +#endif + + +#endif diff --git a/utils/util.h b/common/util.h similarity index 50% rename from utils/util.h rename to common/util.h index 7706d852..aacd707d 100644 --- a/utils/util.h +++ b/common/util.h @@ -1,5 +1,5 @@ -/* Scumm Tools - * Copyright (C) 2004-2006 The ScummVM Team +/* ScummVM Tools + * Copyright (C) 2002-2009 The ScummVM project * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -23,9 +23,7 @@ #ifndef COMMON_UTIL_H #define COMMON_UTIL_H -#include -#include -#include +#include "common/scummsys.h" namespace Common { @@ -97,88 +95,10 @@ enum Platform { } // End of Common namespace -/* - * Some useful types - */ - -typedef unsigned char byte; -typedef unsigned char uint8; -typedef unsigned short uint16; -typedef signed char int8; -typedef signed short int16; -#ifdef __amigaos4__ -#include -#include -#else -typedef unsigned int uint32; -typedef signed int int32; -#endif - -/* - * Various utility macros - */ - -#if defined(_MSC_VER) - - #define scumm_stricmp stricmp - #define scumm_strnicmp _strnicmp - #define snprintf _snprintf - - #define SCUMM_LITTLE_ENDIAN - #pragma once - #pragma warning( disable : 4068 ) /* turn off "unknown pragma" warning */ - #pragma warning( disable : 4996 ) /* turn off warnings about unsafe functions */ - -#elif defined(__MINGW32__) - - #define scumm_stricmp strcasecmp - #define scumm_stricmp stricmp - #define scumm_strnicmp strnicmp - - #define SCUMM_LITTLE_ENDIAN - -#elif defined(UNIX) - #define scumm_stricmp strcasecmp - #define scumm_strnicmp strncasecmp - - #if defined(__DECCXX) /* Assume alpha architecture */ - #define INVERSE_MKID - #define SCUMM_NEED_ALIGNMENT - #endif - -#else - - #error No system type defined - -#endif - - -/** - * Throw an exception of this type (or subtype of it), if the tool fails fatally. - * This type is intended for general errors - */ -class ToolException : public std::runtime_error { -public: - /** - * Construct an exception, with an appropriate error message - * A return value for the tool should be supplied if none is appropriate - * @todo If the tools are even more C++ized, the tool should decide retcode itself, not by exception - * - * @param error The error message - * @param retcode The return value of the process - */ - ToolException(std::string error, int retcode = -1) : std::runtime_error(error), _retcode(retcode) {} - - int _retcode; -}; - -/** - * Something unexpected happened while reading / writing to a file - * Usually premature end, or that it could not be opened (write / read protected) - */ -class AbortException : public ToolException { -public: - AbortException() : ToolException("Operation was aborted", -2) {} -}; +/* Misc stuff */ +void NORETURN_PRE error(const char *s, ...) NORETURN_POST; +void warning(const char *s, ...); +void debug(int level, const char *s, ...); +void notice(const char *s, ...); #endif diff --git a/compress_saga.cpp b/compress_saga.cpp index 0ce124a5..cd3b08a7 100644 --- a/compress_saga.cpp +++ b/compress_saga.cpp @@ -26,7 +26,7 @@ #include "compress_saga.h" #include "common/md5.h" #include "common/file.h" -#include "utils/util.h" +#include "common/util.h" #include "utils/audiostream.h" #include "utils/voc.h" #include "utils/wave.h" diff --git a/encode_dxa.cpp b/encode_dxa.cpp index 03d2dbd2..39cc2b7c 100644 --- a/encode_dxa.cpp +++ b/encode_dxa.cpp @@ -24,6 +24,7 @@ #include "encode_dxa.h" #include "util.h" +#include #include const uint32 typeDEXA = 0x41584544; diff --git a/tool_exception.h b/tool_exception.h new file mode 100644 index 00000000..24b708f4 --- /dev/null +++ b/tool_exception.h @@ -0,0 +1,57 @@ +/* ScummVM Tools + * Copyright (C) 2002-2009 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef TOOL_EXCEPTION_H +#define TOOL_EXCEPTION_H + +#include +#include + +/** + * Throw an exception of this type (or subtype of it), if the tool fails fatally. + * This type is intended for general errors + */ +class ToolException : public std::runtime_error { +public: + /** + * Construct an exception, with an appropriate error message + * A return value for the tool should be supplied if none is appropriate + * @todo If the tools are even more C++ized, the tool should decide retcode itself, not by exception + * + * @param error The error message + * @param retcode The return value of the process + */ + ToolException(std::string error, int retcode = -1) : std::runtime_error(error), _retcode(retcode) {} + + int _retcode; +}; + +/** + * Something unexpected happened while reading / writing to a file + * Usually premature end, or that it could not be opened (write / read protected) + */ +class AbortException : public ToolException { +public: + AbortException() : ToolException("Operation was aborted", -2) {} +}; + +#endif diff --git a/util.h b/util.h index f6db5fc7..40cadad1 100644 --- a/util.h +++ b/util.h @@ -23,120 +23,18 @@ #ifndef UTIL_H #define UTIL_H +// This header is DEPRECATED. Everything should be gradually migrated away +// from using this to directly including the required headers. + +#include "common/scummsys.h" +#include "common/endian.h" +#include "common/util.h" + #include #include #include #include #include -#include -#if !defined(_MSC_VER) -#include -#endif - -#ifdef WIN32 -#include -#include -#endif - -#include "utils/util.h" -#include -#include - -/* - * GCC specific stuff - */ -#if defined(__GNUC__) - #define GCC_PACK __attribute__((packed)) -#else - #define GCC_PACK -#endif - -#ifndef ARRAYSIZE -#define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0]))) -#endif - -#if defined(INVERSE_MKID) -#define MKID_BE(a) ((uint32) \ - (((a) >> 24) & 0x000000FF) | \ - (((a) >> 8) & 0x0000FF00) | \ - (((a) << 8) & 0x00FF0000) | \ - (((a) << 24) & 0xFF000000)) - -#else -# define MKID_BE(a) ((uint32)(a)) -#endif - -static inline uint32 SWAP_32(uint32 a) { - return uint16(((a >> 24) & 0xFF) | ((a >> 8) & 0xFF00) | ((a << 8) & 0xFF0000) | - ((a << 24) & 0xFF000000)); -} - -static inline uint16 SWAP_16(uint16 a) { - return uint16(((a >> 8) & 0xFF) | ((a << 8) & 0xFF00)); -} - -#ifndef FORCEINLINE -#define FORCEINLINE static inline -#endif - -FORCEINLINE uint16 READ_LE_UINT16(const void *ptr) { - const byte *b = (const byte *)ptr; - return uint16((b[1] << 8) + b[0]); -} -FORCEINLINE uint32 READ_LE_UINT32(const void *ptr) { - const byte *b = (const byte *)ptr; - return (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]); -} -FORCEINLINE void WRITE_LE_UINT16(void *ptr, uint16 value) { - byte *b = (byte *)ptr; - b[0] = (byte)(value >> 0); - b[1] = (byte)(value >> 8); -} -FORCEINLINE void WRITE_LE_UINT32(void *ptr, uint32 value) { - byte *b = (byte *)ptr; - b[0] = (byte)(value >> 0); - b[1] = (byte)(value >> 8); - b[2] = (byte)(value >> 16); - b[3] = (byte)(value >> 24); -} - -FORCEINLINE uint16 READ_BE_UINT16(const void *ptr) { - const byte *b = (const byte *)ptr; - return uint16((b[0] << 8) + b[1]); -} -FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) { - const byte *b = (const byte *)ptr; - return uint32((b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3])); -} -FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) { - byte *b = (byte *)ptr; - b[0] = (byte)(value >> 8); - b[1] = (byte)(value >> 0); -} -FORCEINLINE void WRITE_BE_UINT32(void *ptr, uint32 value) { - byte *b = (byte *)ptr; - b[0] = (byte)(value >> 24); - b[1] = (byte)(value >> 16); - b[2] = (byte)(value >> 8); - b[3] = (byte)(value >> 0); -} - -#if defined(__GNUC__) -#define NORETURN_PRE -#define NORETURN_POST __attribute__((__noreturn__)) -#elif defined(_MSC_VER) -#define NORETURN_PRE _declspec(noreturn) -#define NORETURN_POST -#else -#define NORETURN_PRE -#define NORETURN_POST -#endif - -/* Misc stuff */ -void NORETURN_PRE error(const char *s, ...) NORETURN_POST; -void warning(const char *s, ...); -void debug(int level, const char *s, ...); -void notice(const char *s, ...); #endif