diff --git a/lib/framework/cocoa_wrapper.h b/lib/framework/cocoa_wrapper.h index 74bbc45c12d..adca478a5f3 100644 --- a/lib/framework/cocoa_wrapper.h +++ b/lib/framework/cocoa_wrapper.h @@ -37,7 +37,7 @@ void cocoaInit(void); * \returns The index of the selected button. (0-indexed from leftmost argument, rightmost displayed button.) */ int cocoaShowAlert(const char *message, const char *information, unsigned style, - const char *buttonTitles, ...) __attribute__ ((sentinel)); + const char *buttonTitles, ...) __attribute__((sentinel)); void cocoaSelectFileInFinder(const char *filename); void cocoaOpenURL(const char *url); diff --git a/lib/framework/crc.cpp b/lib/framework/crc.cpp index 3d911693700..ae4e633811a 100644 --- a/lib/framework/crc.cpp +++ b/lib/framework/crc.cpp @@ -33,7 +33,7 @@ uint32_t crcSum(uint32_t crc, const void *data_, size_t dataLen) while (dataLen-- > 0) { - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)*data++]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t) * data++]; } return crc; @@ -43,8 +43,8 @@ uint32_t crcSumU16(uint32_t crc, const uint16_t *data, size_t dataLen) { while (dataLen-- > 0) { - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(*data>>8)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)*data++]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(*data >> 8)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t) * data++]; } return crc; @@ -54,14 +54,14 @@ uint32_t crcSumVector2i(uint32_t crc, const Vector2i *data, size_t dataLen) { while (dataLen-- > 0) { - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(data->x>>24)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(data->x>>16)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(data->x>>8)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)data->x]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(data->y>>24)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(data->y>>16)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)(data->y>>8)]; - crc = crc<<8 ^ crcTable[crc>>24 ^ (uint8_t)data++->y]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(data->x >> 24)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(data->x >> 16)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(data->x >> 8)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)data->x]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(data->y >> 24)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(data->y >> 16)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)(data->y >> 8)]; + crc = crc << 8 ^ crcTable[crc>>24 ^ (uint8_t)data++->y]; } return crc; @@ -181,12 +181,12 @@ void Sha256::setZero() std::string Sha256::toString() const { std::string str; - str.resize(Bytes*2); + str.resize(Bytes * 2); char const *hexDigits = "0123456789abcdef"; for (unsigned n = 0; n < Bytes; ++n) { - str[n*2 ] = hexDigits[bytes[n] >> 4]; - str[n*2 + 1] = hexDigits[bytes[n] & 15]; + str[n * 2 ] = hexDigits[bytes[n] >> 4]; + str[n * 2 + 1] = hexDigits[bytes[n] & 15]; } return str; } @@ -194,7 +194,7 @@ std::string Sha256::toString() const void Sha256::fromString(std::string const &s) { setZero(); - unsigned nChars = std::min(Bytes*2, s.size()); + unsigned nChars = std::min(Bytes * 2, s.size()); for (unsigned n = 0; n < nChars; ++n) { unsigned h; @@ -215,6 +215,6 @@ void Sha256::fromString(std::string const &s) { break; } - bytes[n/2] |= h << (n%2? 0 : 4); + bytes[n / 2] |= h << (n % 2 ? 0 : 4); } } diff --git a/lib/framework/debug.cpp b/lib/framework/debug.cpp index bd78c5bf1d2..795621d9e42 100644 --- a/lib/framework/debug.cpp +++ b/lib/framework/debug.cpp @@ -47,7 +47,7 @@ extern void NotifyUserOfError(char *); // will throw up a notifier on error char last_called_script_event[MAX_EVENT_NAME_LEN]; UDWORD traceID = -1; -static debug_callback * callbackRegistry = NULL; +static debug_callback *callbackRegistry = NULL; bool enabled_debug[LOG_LAST]; // global #ifdef DEBUG bool assertEnabled = true; @@ -61,7 +61,8 @@ bool assertEnabled = false; * This list _must_ match the enum in debug.h! * Names must be 8 chars long at max! */ -static const char *code_part_names[] = { +static const char *code_part_names[] = +{ "all", "main", "sound", @@ -112,8 +113,10 @@ static code_part code_part_from_str(const char *str) { unsigned int i; - for (i = 0; i < LOG_LAST; i++) { - if (strcasecmp(code_part_names[i], str) == 0) { + for (i = 0; i < LOG_LAST; i++) + { + if (strcasecmp(code_part_names[i], str) == 0) + { return (code_part)i; } } @@ -127,12 +130,15 @@ static code_part code_part_from_str(const char *str) * \param data Ignored. Use NULL. * \param outputBuffer Buffer containing the preprocessed text to output. */ -void debug_callback_stderr( WZ_DECL_UNUSED void ** data, const char * outputBuffer ) +void debug_callback_stderr(WZ_DECL_UNUSED void **data, const char *outputBuffer) { - if ( outputBuffer[strlen(outputBuffer) - 1] != '\n' ) { - fprintf( stderr, "%s\n", outputBuffer ); - } else { - fprintf( stderr, "%s", outputBuffer ); + if (outputBuffer[strlen(outputBuffer) - 1] != '\n') + { + fprintf(stderr, "%s\n", outputBuffer); + } + else + { + fprintf(stderr, "%s", outputBuffer); } // Make sure that all output is flushed to stderr when requested by the user @@ -150,7 +156,7 @@ void debug_callback_stderr( WZ_DECL_UNUSED void ** data, const char * outputBuff * \param outputBuffer Buffer containing the preprocessed text to output. */ #if defined WIN32 && defined DEBUG -void debug_callback_win32debug(WZ_DECL_UNUSED void ** data, const char * outputBuffer) +void debug_callback_win32debug(WZ_DECL_UNUSED void **data, const char *outputBuffer) { char tmpStr[MAX_LEN_LOG_LINE]; @@ -160,7 +166,7 @@ void debug_callback_win32debug(WZ_DECL_UNUSED void ** data, const char * outputB sstrcat(tmpStr, "\n"); } - OutputDebugStringA( tmpStr ); + OutputDebugStringA(tmpStr); } #endif // WIN32 @@ -171,14 +177,17 @@ void debug_callback_win32debug(WZ_DECL_UNUSED void ** data, const char * outputB * \param data Filehandle to output to. * \param outputBuffer Buffer containing the preprocessed text to output. */ -void debug_callback_file( void ** data, const char * outputBuffer ) +void debug_callback_file(void **data, const char *outputBuffer) { - FILE * logfile = (FILE*)*data; + FILE *logfile = (FILE *)*data; - if ( !strchr( outputBuffer, '\n' ) ) { - fprintf( logfile, "%s\n", outputBuffer ); - } else { - fprintf( logfile, "%s", outputBuffer ); + if (!strchr(outputBuffer, '\n')) + { + fprintf(logfile, "%s\n", outputBuffer); + } + else + { + fprintf(logfile, "%s", outputBuffer); } } @@ -196,7 +205,7 @@ bool debug_callback_file_init(void **data) { WZDebugfilename = (const char *)*data; - FILE* const logfile = fopen(WZDebugfilename, "w"); + FILE *const logfile = fopen(WZDebugfilename, "w"); if (!logfile) { fprintf(stderr, "Could not open %s for appending!\n", WZDebugfilename); @@ -218,10 +227,10 @@ bool debug_callback_file_init(void **data) * * \param data The filehandle to close. */ -void debug_callback_file_exit( void ** data ) +void debug_callback_file_exit(void **data) { - FILE * logfile = (FILE*)*data; - fclose( logfile ); + FILE *logfile = (FILE *)*data; + fclose(logfile); *data = NULL; } @@ -258,20 +267,20 @@ void debug_init(void) /*** Initialize the debug subsystem ***/ #if defined(WZ_CC_MSVC) && defined(DEBUG) int tmpDbgFlag; - _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG ); // Output CRT info to debugger + _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG); // Output CRT info to debugger - tmpDbgFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // Grab current flags + tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Grab current flags # if defined(DEBUG_MEMORY) tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF; // Check every (de)allocation # endif // DEBUG_MEMORY tmpDbgFlag |= _CRTDBG_ALLOC_MEM_DF; // Check allocations tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF; // Check for memleaks - _CrtSetDbgFlag( tmpDbgFlag ); + _CrtSetDbgFlag(tmpDbgFlag); #endif // WZ_CC_MSVC && DEBUG STATIC_ASSERT(ARRAY_SIZE(code_part_names) - 1 == LOG_LAST); // enums start at 0 - memset( enabled_debug, false, sizeof(enabled_debug) ); + memset(enabled_debug, false, sizeof(enabled_debug)); enabled_debug[LOG_ERROR] = true; enabled_debug[LOG_INFO] = true; enabled_debug[LOG_FATAL] = true; @@ -286,14 +295,16 @@ void debug_init(void) void debug_exit(void) { - debug_callback * curCallback = callbackRegistry, * tmpCallback = NULL; + debug_callback *curCallback = callbackRegistry, * tmpCallback = NULL; - while ( curCallback ) + while (curCallback) { - if ( curCallback->exit ) - curCallback->exit( &curCallback->data ); + if (curCallback->exit) + { + curCallback->exit(&curCallback->data); + } tmpCallback = curCallback->next; - free( curCallback ); + free(curCallback); curCallback = tmpCallback; } warning_list.clear(); @@ -301,11 +312,11 @@ void debug_exit(void) } -void debug_register_callback( debug_callback_fn callback, debug_callback_init init, debug_callback_exit exit, void * data ) +void debug_register_callback(debug_callback_fn callback, debug_callback_init init, debug_callback_exit exit, void *data) { - debug_callback * curCallback = callbackRegistry, * tmpCallback = NULL; + debug_callback *curCallback = callbackRegistry, * tmpCallback = NULL; - tmpCallback = (debug_callback*)malloc(sizeof(*tmpCallback)); + tmpCallback = (debug_callback *)malloc(sizeof(*tmpCallback)); tmpCallback->next = NULL; tmpCallback->callback = callback; @@ -314,21 +325,23 @@ void debug_register_callback( debug_callback_fn callback, debug_callback_init in tmpCallback->data = data; if (tmpCallback->init - && !tmpCallback->init(&tmpCallback->data)) + && !tmpCallback->init(&tmpCallback->data)) { fprintf(stderr, "Failed to initialise debug callback, debugfile set to [%s]!\n", WZDebugfilename); free(tmpCallback); return; } - if ( !curCallback ) + if (!curCallback) { callbackRegistry = tmpCallback; return; } - while ( curCallback->next ) + while (curCallback->next) + { curCallback = curCallback->next; + } curCallback->next = tmpCallback; } @@ -338,10 +351,12 @@ bool debug_enable_switch(const char *str) { code_part part = code_part_from_str(str); - if (part != LOG_LAST) { + if (part != LOG_LAST) + { enabled_debug[part] = !enabled_debug[part]; } - if (part == LOG_ALL) { + if (part == LOG_ALL) + { memset(enabled_debug, true, sizeof(enabled_debug)); } return (part != LOG_LAST); @@ -351,9 +366,9 @@ bool debug_enable_switch(const char *str) * * @param str The string to send to debug callbacks. */ -static void printToDebugCallbacks(const char * const str) +static void printToDebugCallbacks(const char *const str) { - debug_callback * curCallback; + debug_callback *curCallback; // Loop over all callbacks, invoking them with the given data string for (curCallback = callbackRegistry; curCallback != NULL; curCallback = curCallback->next) @@ -376,7 +391,7 @@ void _realObjTrace(int id, const char *function, const char *str, ...) printToDebugCallbacks(outputBuffer); } -void _debug( int line, code_part part, const char *function, const char *str, ... ) +void _debug(int line, code_part part, const char *function, const char *str, ...) { va_list ap; static char outputBuffer[MAX_LEN_LOG_LINE]; @@ -410,23 +425,33 @@ void _debug( int line, code_part part, const char *function, const char *str, .. { // Received again the same line repeated++; - if (repeated == next) { - if (repeated > 2) { + if (repeated == next) + { + if (repeated > 2) + { ssprintf(outputBuffer, "last message repeated %u times (total %u repeats)", repeated - prev, repeated); - } else { + } + else + { ssprintf(outputBuffer, "last message repeated %u times", repeated - prev); } printToDebugCallbacks(outputBuffer); prev = repeated; next *= 2; } - } else { + } + else + { // Received another line, cleanup the old - if (repeated > 0 && repeated != prev && repeated != 1) { + if (repeated > 0 && repeated != prev && repeated != 1) + { /* just repeat the previous message when only one repeat occurred */ - if (repeated > 2) { + if (repeated > 2) + { ssprintf(outputBuffer, "last message repeated %u times (total %u repeats)", repeated - prev, repeated); - } else { + } + else + { ssprintf(outputBuffer, "last message repeated %u times", repeated - prev); } printToDebugCallbacks(outputBuffer); @@ -439,12 +464,12 @@ void _debug( int line, code_part part, const char *function, const char *str, .. if (!repeated) { time_t rawtime; - struct tm * timeinfo; + struct tm *timeinfo; char ourtime[15]; //HH:MM:SS - time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - strftime (ourtime,15,"%I:%M:%S",timeinfo); + time(&rawtime); + timeinfo = localtime(&rawtime); + strftime(ourtime, 15, "%I:%M:%S", timeinfo); // Assemble the outputBuffer: ssprintf(outputBuffer, "%-8s|%s: %s", code_part_names[part], ourtime, useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]); @@ -468,26 +493,29 @@ void _debug( int line, code_part part, const char *function, const char *str, .. char wbuf[512]; ssprintf(wbuf, "%s\n\nPlease check the file (%s) in your configuration directory for more details. \ \nDo not forget to upload the %s file, WZdebuginfo.txt and the warzone2100.rpt files in your bug reports at http://developer.wz2100.net/newticket!", useInputBuffer1 ? inputBuffer[1] : inputBuffer[0], WZ_DBGFile, WZ_DBGFile); - MessageBoxA( NULL, wbuf, "Warzone has terminated unexpectedly", MB_OK|MB_ICONERROR); + MessageBoxA(NULL, wbuf, "Warzone has terminated unexpectedly", MB_OK | MB_ICONERROR); #elif defined(WZ_OS_MAC) int clickedIndex = \ - cocoaShowAlert("Warzone has quit unexpectedly.", - "Please check your logs and attach them along with a bug report. Thanks!", - 2, "Show Log Files & Open Bug Reporter", "Ignore", NULL); + cocoaShowAlert("Warzone has quit unexpectedly.", + "Please check your logs and attach them along with a bug report. Thanks!", + 2, "Show Log Files & Open Bug Reporter", "Ignore", NULL); if (clickedIndex == 0) { cocoaOpenURL("http://developer.wz2100.net/newticket"); - if (WZDebugfilename == NULL) { + if (WZDebugfilename == NULL) + { cocoaShowAlert("Unable to open debug log.", "The debug log subsystem has not yet been initialised.", 2, "Continue", NULL); - } else { + } + else + { cocoaSelectFileInFinder(WZDebugfilename); } cocoaOpenUserCrashReportFolder(); } #else - const char* popupBuf = useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]; + const char *popupBuf = useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]; wzFatalDialog(popupBuf); #endif } @@ -499,9 +527,9 @@ void _debug( int line, code_part part, const char *function, const char *str, .. #if defined(WZ_OS_WIN) char wbuf[512]; ssprintf(wbuf, "A non fatal error has occurred.\n\n%s\n\n", useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]); - MessageBoxA( NULL, - wbuf, - "Warzone has detected a problem.", MB_OK|MB_ICONINFORMATION); + MessageBoxA(NULL, + wbuf, + "Warzone has detected a problem.", MB_OK | MB_ICONINFORMATION); #elif defined(WZ_OS_MAC) cocoaShowAlert("Warzone has detected a problem.", inputBuffer[useInputBuffer1 ? 1 : 0], 0, "OK", NULL); #endif @@ -515,7 +543,7 @@ void _debugBacktrace(code_part part) { #ifdef WZ_OS_LINUX void *btv[20]; - unsigned num = backtrace(btv, sizeof(btv)/sizeof(*btv)); + unsigned num = backtrace(btv, sizeof(btv) / sizeof(*btv)); char **btc = backtrace_symbols(btv, num); unsigned i; for (i = 1; i + 2 < num; ++i) // =1: Don't print "src/warzone2100(syncDebugBacktrace+0x16) [0x6312d1]". +2: Don't print last two lines of backtrace such as "/lib/libc.so.6(__libc_start_main+0xe6) [0x7f91e040ea26]", since the address varies (even with the same binary). diff --git a/lib/framework/debug.h b/lib/framework/debug.h index ffaf08b0fbb..a404146e165 100644 --- a/lib/framework/debug.h +++ b/lib/framework/debug.h @@ -66,16 +66,16 @@ extern bool assertEnabled; /** Deals with failure in an assert. Expression is (re-)evaluated for output in the assert() call. */ #define ASSERT_FAILURE(expr, expr_string, location_description, function, ...) \ ( \ - (void)_debug(__LINE__, LOG_INFO, function, __VA_ARGS__), \ - (void)_debug(__LINE__, LOG_INFO, function, "Assert in Warzone: %s (%s), last script event: '%s'", \ - location_description, expr_string, last_called_script_event), \ - ( assertEnabled ? (void)wz_assert(expr) : (void)0 )\ + (void)_debug(__LINE__, LOG_INFO, function, __VA_ARGS__), \ + (void)_debug(__LINE__, LOG_INFO, function, "Assert in Warzone: %s (%s), last script event: '%s'", \ + location_description, expr_string, last_called_script_event), \ + ( assertEnabled ? (void)wz_assert(expr) : (void)0 )\ ) /** * Internal assert helper macro to allow some debug functions to use an alternate calling location. - * Expression is only evaluated once if true, if false it is evaluated another time to provide decent - * feedback on OSes that have good GUI facilities for asserts and lousy backtrace facilities. + * Expression is only evaluated once if true, if false it is evaluated another time to provide decent + * feedback on OSes that have good GUI facilities for asserts and lousy backtrace facilities. * * \param expr Expression to assert on. * \param location_description A string describing the calling location, e.g.: @@ -89,12 +89,12 @@ extern bool assertEnabled; * so unless you have a good reason to, don't depend on it. */ #define ASSERT_HELPER(expr, location_description, function, ...) \ -( \ - (expr) ? /* if (expr) */ \ - (void)0 \ - : /* else */\ - ASSERT_FAILURE(expr, #expr, location_description, function, __VA_ARGS__) \ -) + ( \ + (expr) ? /* if (expr) */ \ + (void)0 \ + : /* else */\ + ASSERT_FAILURE(expr, #expr, location_description, function, __VA_ARGS__) \ + ) /** * @@ -127,7 +127,7 @@ extern bool assertEnabled; * \note BUILD_BUG_ON_ZERO from */ template class StaticAssert; -template<> class StaticAssert{}; +template<> class StaticAssert {}; #define STATIC_ASSERT_EXPR(expr) \ (0*sizeof(StaticAssert<(expr)>)) /** @@ -149,54 +149,54 @@ template<> class StaticAssert{}; /** Debug enums. Must match code_part_names in debug.c */ enum code_part { - LOG_ALL, /* special: sets all to on */ - LOG_MAIN, - LOG_SOUND, - LOG_VIDEO, - LOG_WZ, - LOG_3D, - LOG_TEXTURE, - LOG_NET, - LOG_MEMORY, - LOG_WARNING, /**< special; on in debug mode */ - LOG_ERROR, /**< special; on by default */ - LOG_NEVER, /**< if too verbose for anything but dedicated debugging... */ - LOG_SCRIPT, - LOG_MOVEMENT, - LOG_ATTACK, - LOG_FOG, - LOG_SENSOR, - LOG_GUI, - LOG_MAP, - LOG_SAVE, - LOG_SYNC, - LOG_DEATH, - LOG_LIFE, - LOG_GATEWAY, - LOG_MSG, - LOG_INFO, /**< special; on by default, for both debug & release builds */ - LOG_TERRAIN, - LOG_FEATURE, - LOG_FATAL, /**< special; on by default, for both debug & release builds */ - LOG_INPUT, // mouse / keyboard events - LOG_POPUP, // special, on by default, for both debug & release builds (used for OS dependent popup code) - LOG_CONSOLE, // send console messages to file - LOG_LAST /**< _must_ be last! */ + LOG_ALL, /* special: sets all to on */ + LOG_MAIN, + LOG_SOUND, + LOG_VIDEO, + LOG_WZ, + LOG_3D, + LOG_TEXTURE, + LOG_NET, + LOG_MEMORY, + LOG_WARNING, /**< special; on in debug mode */ + LOG_ERROR, /**< special; on by default */ + LOG_NEVER, /**< if too verbose for anything but dedicated debugging... */ + LOG_SCRIPT, + LOG_MOVEMENT, + LOG_ATTACK, + LOG_FOG, + LOG_SENSOR, + LOG_GUI, + LOG_MAP, + LOG_SAVE, + LOG_SYNC, + LOG_DEATH, + LOG_LIFE, + LOG_GATEWAY, + LOG_MSG, + LOG_INFO, /**< special; on by default, for both debug & release builds */ + LOG_TERRAIN, + LOG_FEATURE, + LOG_FATAL, /**< special; on by default, for both debug & release builds */ + LOG_INPUT, // mouse / keyboard events + LOG_POPUP, // special, on by default, for both debug & release builds (used for OS dependent popup code) + LOG_CONSOLE, // send console messages to file + LOG_LAST /**< _must_ be last! */ }; extern bool enabled_debug[LOG_LAST]; -typedef void (*debug_callback_fn)(void**, const char*); -typedef bool (*debug_callback_init)(void**); -typedef void (*debug_callback_exit)(void**); +typedef void (*debug_callback_fn)(void **, const char *); +typedef bool (*debug_callback_init)(void **); +typedef void (*debug_callback_exit)(void **); struct debug_callback { - debug_callback * next; + debug_callback *next; debug_callback_fn callback; /// Function which does the output debug_callback_init init; /// Setup function debug_callback_exit exit; /// Cleaning function - void * data; /// Used to pass data to the above functions. Eg a filename or handle. + void *data; /// Used to pass data to the above functions. Eg a filename or handle. }; /** @@ -204,12 +204,12 @@ struct debug_callback * * Doesn't register any callbacks! */ -void debug_init( void ); +void debug_init(void); /** * Shutdown the debug system and remove all output callbacks */ -void debug_exit( void ); +void debug_exit(void); /** * Have the stderr output callback flush its output before returning. @@ -226,7 +226,7 @@ extern void debugFlushStderr(void); * \param exit Cleanup function called when unregistering the callback (optional, may be NULL) * \param data Data to be passed to all three functions (optional, may be NULL) */ -void debug_register_callback( debug_callback_fn callback, debug_callback_init init, debug_callback_exit exit, void * data ); +void debug_register_callback(debug_callback_fn callback, debug_callback_init init, debug_callback_exit exit, void *data); void debug_callback_file(void **data, const char *outputBuffer); bool debug_callback_file_init(void **data); @@ -235,7 +235,7 @@ void debug_callback_file_exit(void **data); void debug_callback_stderr(void **data, const char *outputBuffer); #if defined WIN32 && defined DEBUG -void debug_callback_win32debug(void** data, const char* outputBuffer); +void debug_callback_win32debug(void **data, const char *outputBuffer); #endif /** @@ -252,7 +252,7 @@ bool debug_enable_switch(const char *str); * Only outputs if debugging of part was formerly enabled with debug_enable_switch. */ #define debug(part, ...) do { if (enabled_debug[part]) _debug(__LINE__, part, __FUNCTION__, __VA_ARGS__); } while(0) -void _debug( int line, code_part part, const char *function, const char *str, ...) WZ_DECL_FORMAT(printf, 4, 5); +void _debug(int line, code_part part, const char *function, const char *str, ...) WZ_DECL_FORMAT(printf, 4, 5); #define debugBacktrace(part, ...) do { if (enabled_debug[part]) { _debug(__LINE__, part, __FUNCTION__, __VA_ARGS__); _debugBacktrace(part); }} while(0) void _debugBacktrace(code_part part); @@ -267,8 +267,14 @@ extern UDWORD traceID; */ #define objTrace(id, ...) do { if (id == traceID) _realObjTrace(id, __FUNCTION__, __VA_ARGS__); } while(0) void _realObjTrace(int id, const char *function, const char *str, ...) WZ_DECL_FORMAT(printf, 3, 4); -static inline void objTraceEnable(UDWORD id) { traceID = id; } -static inline void objTraceDisable(void) { traceID = (UDWORD)-1; } +static inline void objTraceEnable(UDWORD id) +{ + traceID = id; +} +static inline void objTraceDisable(void) +{ + traceID = (UDWORD) - 1; +} // MSVC specific rotuines to set/clear allocation tracking #if defined(WZ_CC_MSVC) && defined(DEBUG) diff --git a/lib/framework/endian_hack.h b/lib/framework/endian_hack.h index 2bacf26e8f8..95e939eb4c7 100644 --- a/lib/framework/endian_hack.h +++ b/lib/framework/endian_hack.h @@ -27,36 +27,36 @@ #include -static inline void endian_uword(UWORD* p) +static inline void endian_uword(UWORD *p) { STATIC_ASSERT(sizeof(*p) == 2); uint8_t bytes[sizeof(*p)]; memcpy(bytes, p, sizeof(*p)); - *p = bytes[1]<<8 | bytes[0]; + *p = bytes[1] << 8 | bytes[0]; } -static inline void endian_sword(SWORD* p) +static inline void endian_sword(SWORD *p) { STATIC_ASSERT(sizeof(*p) == 2); uint8_t bytes[sizeof(*p)]; memcpy(bytes, p, sizeof(*p)); - *p = bytes[1]<<8 | bytes[0]; + *p = bytes[1] << 8 | bytes[0]; } -static inline void endian_udword(UDWORD* p) +static inline void endian_udword(UDWORD *p) { STATIC_ASSERT(sizeof(*p) == 4); uint8_t bytes[sizeof(*p)]; memcpy(bytes, p, sizeof(*p)); - *p = bytes[3]<<24 | bytes[2]<<16 | bytes[1]<<8 | bytes[0]; + *p = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; } -static inline void endian_sdword(SDWORD* p) +static inline void endian_sdword(SDWORD *p) { STATIC_ASSERT(sizeof(*p) == 4); uint8_t bytes[sizeof(*p)]; memcpy(bytes, p, sizeof(*p)); - *p = bytes[3]<<24 | bytes[2]<<16 | bytes[1]<<8 | bytes[0]; + *p = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; } template @@ -65,7 +65,7 @@ static inline void endian_enum(ENUM *p) STATIC_ASSERT(sizeof(*p) == 4); uint8_t bytes[sizeof(*p)]; memcpy(bytes, p, sizeof(*p)); - *p = ENUM(bytes[3]<<24 | bytes[2]<<16 | bytes[1]<<8 | bytes[0]); + *p = ENUM(bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]); } #endif // ENDIAN_HACK_H diff --git a/lib/framework/file.h b/lib/framework/file.h index 15d35327e20..590689a19c8 100644 --- a/lib/framework/file.h +++ b/lib/framework/file.h @@ -25,15 +25,15 @@ /*! Open a file for reading */ -extern PHYSFS_file* openLoadFile(const char* fileName, bool hard_fail); +extern PHYSFS_file *openLoadFile(const char *fileName, bool hard_fail); /*! Open a file for writing */ -extern PHYSFS_file* openSaveFile(const char* fileName); +extern PHYSFS_file *openSaveFile(const char *fileName); /** Load the file with name pointed to by pFileName into a memory buffer. */ extern bool loadFile(const char *pFileName, // The filename - char **ppFileData, // A buffer containing the file contents - UDWORD *pFileSize); // The size of this buffer + char **ppFileData, // A buffer containing the file contents + UDWORD *pFileSize); // The size of this buffer /** Save the data in the buffer into the given file */ extern bool saveFile(const char *pFileName, const char *pFileData, UDWORD fileSize); @@ -43,7 +43,7 @@ extern bool loadFileToBuffer(const char *pFileName, char *pFileBuffer, UDWORD bu /** Load a file from disk, but returns quietly if no file found. */ extern bool loadFileToBufferNoError(const char *pFileName, char *pFileBuffer, UDWORD bufferSize, - UDWORD *pSize); + UDWORD *pSize); Sha256 findHashOfFile(char const *realFileName); diff --git a/lib/framework/fixedpoint.h b/lib/framework/fixedpoint.h index 6ad78e03ce4..585841074b4 100644 --- a/lib/framework/fixedpoint.h +++ b/lib/framework/fixedpoint.h @@ -40,7 +40,10 @@ static const int DEG_360 = 65536; static const float DEG_1 = (float)65536 / 360.f; -static inline WZ_DECL_CONST float UNDEG(uint16_t angle) { return angle * (360 / 65536.0f); } +static inline WZ_DECL_CONST float UNDEG(uint16_t angle) +{ + return angle * (360 / 65536.0f); +} // Should be a macro (or two separate functions), since we can't do function overloading for float and int, and we don't want to use the float version for anything game-state related. // 65536 / 360 = 8192 / 45, with a bit less overflow risk. diff --git a/lib/framework/frame.cpp b/lib/framework/frame.cpp index bfb9cc2f394..63513d13220 100644 --- a/lib/framework/frame.cpp +++ b/lib/framework/frame.cpp @@ -59,7 +59,7 @@ static uint32_t curTicks = 0; // Number of ticks since execution started static uint32_t lastTicks = 0; /* InitFrameStuff - needs to be called once before frame loop commences */ -static void InitFrameStuff( void ) +static void InitFrameStuff(void) { frameCount = 0.0; curFrames = 0; @@ -153,9 +153,9 @@ bool getMouseWarp() return mousewarp; } -PHYSFS_file* openLoadFile(const char* fileName, bool hard_fail) +PHYSFS_file *openLoadFile(const char *fileName, bool hard_fail) { - PHYSFS_file* fileHandle = PHYSFS_openRead(fileName); + PHYSFS_file *fileHandle = PHYSFS_openRead(fileName); debug(LOG_NEVER, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName); if (!fileHandle) { @@ -205,7 +205,7 @@ static bool loadFile2(const char *pFileName, char **ppFileData, UDWORD *pFileSiz if (AllocateMem) { // Allocate a buffer to store the data and a terminating zero - *ppFileData = (char*)malloc(filesize + 1); + *ppFileData = (char *)malloc(filesize + 1); if (*ppFileData == NULL) { debug(LOG_ERROR, "loadFile2: Out of memory loading %s", pFileName); @@ -261,9 +261,9 @@ static bool loadFile2(const char *pFileName, char **ppFileData, UDWORD *pFileSiz return true; } -PHYSFS_file* openSaveFile(const char* fileName) +PHYSFS_file *openSaveFile(const char *fileName) { - PHYSFS_file* fileHandle = PHYSFS_openWrite(fileName); + PHYSFS_file *fileHandle = PHYSFS_openWrite(fileName); if (!fileHandle) { const char *found = PHYSFS_getRealDir(fileName); @@ -369,7 +369,7 @@ Sha256 findHashOfFile(char const *realFileName) * Accepts string and returns hashed integer. */ /***************************************************************************/ -UDWORD HashString( const char *c ) +UDWORD HashString(const char *c) { UDWORD iHashValue; @@ -379,13 +379,13 @@ UDWORD HashString( const char *c ) for (iHashValue = 0; *c; ++c) { unsigned int i; - iHashValue = ( iHashValue << ONE_EIGHTH ) + *c; + iHashValue = (iHashValue << ONE_EIGHTH) + *c; i = iHashValue & HIGH_BITS; - if ( i != 0 ) + if (i != 0) { - iHashValue = ( iHashValue ^ ( i >> THREE_QUARTERS ) ) & - ~HIGH_BITS; + iHashValue = (iHashValue ^ (i >> THREE_QUARTERS)) & + ~HIGH_BITS; } } return iHashValue; @@ -399,29 +399,33 @@ static inline char upcaseASCII(char c) { // If this is _not_ a lower case character simply return if (c < 'a' || c > 'z') + { return c; + } // Otherwise substract 32 to make the lower case character an upper case one else + { return c - 32; + } } -UDWORD HashStringIgnoreCase( const char *c ) +UDWORD HashStringIgnoreCase(const char *c) { UDWORD iHashValue; assert(c != NULL); assert(*c != 0x0); - for (iHashValue=0; *c; ++c) + for (iHashValue = 0; *c; ++c) { unsigned int i; - iHashValue = ( iHashValue << ONE_EIGHTH ) + upcaseASCII(*c); + iHashValue = (iHashValue << ONE_EIGHTH) + upcaseASCII(*c); i = iHashValue & HIGH_BITS; - if ( i != 0 ) + if (i != 0) { - iHashValue = ( iHashValue ^ ( i >> THREE_QUARTERS ) ) & - ~HIGH_BITS; + iHashValue = (iHashValue ^ (i >> THREE_QUARTERS)) & + ~HIGH_BITS; } } return iHashValue; diff --git a/lib/framework/frame.h b/lib/framework/frame.h index 52435e64441..453c139a06f 100644 --- a/lib/framework/frame.h +++ b/lib/framework/frame.h @@ -123,10 +123,10 @@ extern UDWORD frameGetFrameNumber(void); /** Return framerate of the last second. */ int frameRate(); -extern UDWORD HashString( const char *String ); -extern UDWORD HashStringIgnoreCase( const char *String ); +extern UDWORD HashString(const char *String); +extern UDWORD HashStringIgnoreCase(const char *String); -static inline WZ_DECL_CONST const char * bool2string(bool var) +static inline WZ_DECL_CONST const char *bool2string(bool var) { return (var ? "true" : "false"); } diff --git a/lib/framework/frameresource.cpp b/lib/framework/frameresource.cpp index 461ba476d91..fb991c42979 100644 --- a/lib/framework/frameresource.cpp +++ b/lib/framework/frameresource.cpp @@ -32,7 +32,7 @@ #include "resly.h" // Local prototypes -static RES_TYPE *psResTypes=NULL; +static RES_TYPE *psResTypes = NULL; /* The initial resource directory and the current resource directory */ char aResDir[PATH_MAX]; @@ -45,7 +45,7 @@ static SDWORD resBlockID; static void ResetResourceFile(void); // callback to resload screen. -static RESLOAD_CALLBACK resLoadCallback=NULL; +static RESLOAD_CALLBACK resLoadCallback = NULL; /* set the callback function for the res loader*/ @@ -57,7 +57,7 @@ void resSetLoadCallback(RESLOAD_CALLBACK funcToCall) /* do the callback for the resload display function */ static inline void resDoResLoadCallback(void) { - if(resLoadCallback) + if (resLoadCallback) { resLoadCallback(); } @@ -67,8 +67,8 @@ static inline void resDoResLoadCallback(void) /* Initialise the resource module */ bool resInitialise(void) { - ASSERT( psResTypes == NULL, - "resInitialise: resource module hasn't been shut down??" ); + ASSERT(psResTypes == NULL, + "resInitialise: resource module hasn't been shut down??"); psResTypes = NULL; resBlockID = 0; resLoadCallback = NULL; @@ -82,21 +82,22 @@ bool resInitialise(void) /* Shutdown the resource module */ void resShutDown(void) { - if (psResTypes != NULL) { + if (psResTypes != NULL) + { debug(LOG_WZ, "resShutDown: warning resources still allocated"); resReleaseAll(); } } // force set the base resource directory -void resForceBaseDir(const char* pResDir) +void resForceBaseDir(const char *pResDir) { sstrcpy(aResDir, pResDir); sstrcpy(aCurrResDir, pResDir); } // set the base resource directory -void resSetBaseDir(const char* pResDir) +void resSetBaseDir(const char *pResDir) { sstrcpy(aResDir, pResDir); } @@ -139,16 +140,16 @@ bool resLoad(const char *pResFile, SDWORD blockID) /* Allocate a RES_TYPE structure */ -static RES_TYPE* resAlloc(const char *pType) +static RES_TYPE *resAlloc(const char *pType) { RES_TYPE *psT; #ifdef DEBUG // Check for a duplicate type - for(psT = psResTypes; psT; psT = psT->psNext) + for (psT = psResTypes; psT; psT = psT->psNext) { - ASSERT( strcmp(psT->aType, pType) != 0, - "resAlloc: Duplicate function for type: %s", pType ); + ASSERT(strcmp(psT->aType, pType) != 0, + "resAlloc: Duplicate function for type: %s", pType); } #endif @@ -156,7 +157,7 @@ static RES_TYPE* resAlloc(const char *pType) psT = (RES_TYPE *)malloc(sizeof(RES_TYPE)); if (!psT) { - debug( LOG_FATAL, "resAlloc: Out of memory" ); + debug(LOG_FATAL, "resAlloc: Out of memory"); abort(); return NULL; } @@ -175,7 +176,7 @@ static RES_TYPE* resAlloc(const char *pType) /* Add a buffer load function for a file type */ bool resAddBufferLoad(const char *pType, RES_BUFFERLOAD buffLoad, - RES_FREE release) + RES_FREE release) { RES_TYPE *psT = resAlloc(pType); @@ -197,7 +198,7 @@ bool resAddBufferLoad(const char *pType, RES_BUFFERLOAD buffLoad, /* Add a file name load function for a file type */ bool resAddFileLoad(const char *pType, RES_FILELOAD fileLoad, - RES_FREE release) + RES_FREE release) { RES_TYPE *psT = resAlloc(pType); @@ -253,7 +254,7 @@ void SetLastResourceFilename(const char *pName) // Structure for each file currently in use in the resource ... probably only going to be one ... but we will handle upto MAXLOADEDRESOURCE struct RESOURCEFILE { - char *pBuffer; // a pointer to the data + char *pBuffer; // a pointer to the data UDWORD size; // number of bytes UBYTE type; // what type of resource is it }; @@ -272,9 +273,9 @@ static void ResetResourceFile(void) { UWORD i; - for (i=0;itype=RESFILETYPE_LOADED; - ResData->size=size; - ResData->pBuffer=pBuffer; - return(true); + ResData->type = RESFILETYPE_LOADED; + ResData->size = size; + ResData->pBuffer = pBuffer; + return (true); } @@ -324,24 +330,24 @@ static void FreeResourceFile(RESOURCEFILE *OldResource) { switch (OldResource->type) { - case RESFILETYPE_LOADED: - free(OldResource->pBuffer); - OldResource->pBuffer = NULL; - break; + case RESFILETYPE_LOADED: + free(OldResource->pBuffer); + OldResource->pBuffer = NULL; + break; - default: - debug(LOG_WARNING, "resource not freed"); + default: + debug(LOG_WARNING, "resource not freed"); } // Remove from the list - OldResource->type=RESFILETYPE_EMPTY; + OldResource->type = RESFILETYPE_EMPTY; } -static inline RES_DATA* resDataInit(const char *DebugName, UDWORD DataIDHash, void *pData, UDWORD BlockID) +static inline RES_DATA *resDataInit(const char *DebugName, UDWORD DataIDHash, void *pData, UDWORD BlockID) { - char* resID; + char *resID; // Allocate memory to hold the RES_DATA structure plus the identifying string RES_DATA *psRes = (RES_DATA *)malloc(sizeof(RES_DATA) + strlen(DebugName) + 1); @@ -352,7 +358,7 @@ static inline RES_DATA* resDataInit(const char *DebugName, UDWORD DataIDHash, vo } // Initialize the pointer for our ID string - resID = (char*)(psRes + 1); + resID = (char *)(psRes + 1); // Copy over the identifying string strcpy(resID, DebugName); @@ -375,21 +381,21 @@ static inline RES_DATA* resDataInit(const char *DebugName, UDWORD DataIDHash, vo * \param[in,out] fileName must be at least PATH_MAX bytes large * \param maxlen indicates the maximum buffer size of \c fileName */ -static void makeLocaleFile(char* fileName, size_t maxlen) // given string must have PATH_MAX size +static void makeLocaleFile(char *fileName, size_t maxlen) // given string must have PATH_MAX size { #ifdef ENABLE_NLS - const char * language = getLanguage(); + const char *language = getLanguage(); char localeFile[PATH_MAX]; - if ( language[0] == '\0' || // could not get language - strlen(fileName) + strlen(language) + 1 >= PATH_MAX ) + if (language[0] == '\0' || // could not get language + strlen(fileName) + strlen(language) + 1 >= PATH_MAX) { return; } snprintf(localeFile, sizeof(localeFile), "locale/%s/%s", language, fileName); - if ( PHYSFS_exists(localeFile) ) + if (PHYSFS_exists(localeFile)) { strlcpy(fileName, localeFile, maxlen); debug(LOG_WZ, "Found translated file: %s", fileName); @@ -413,7 +419,7 @@ bool resLoadFile(const char *pType, const char *pFile) UDWORD HashedName, HashedType = HashString(pType); // Find the resource-type - for(psT = psResTypes; psT != NULL; psT = psT->psNext ) + for (psT = psResTypes; psT != NULL; psT = psT->psNext) { if (psT->HashedType == HashedType) { @@ -432,7 +438,7 @@ bool resLoadFile(const char *pType, const char *pFile) HashedName = HashStringIgnoreCase(pFile); for (psRes = psT->psRes; psRes; psRes = psRes->psNext) { - if(psRes->HashedID == HashedName) + if (psRes->HashedID == HashedName) { ASSERT(strcasecmp(psRes->aID, pFile) == 0, "Hash collision \"%s\" vs \"%s\"", psRes->aID, pFile); debug(LOG_WZ, "Duplicate file name: %s (hash %x) for type %s", @@ -482,7 +488,7 @@ bool resLoadFile(const char *pType, const char *pFile) FreeResourceFile(Resource); } - else if(psT->fileLoad) + else if (psT->fileLoad) { // Process data directly from file if (!psT->fileLoad(aFileName, &pData)) @@ -507,7 +513,7 @@ bool resLoadFile(const char *pType, const char *pFile) if (pData != NULL) { // LastResourceFilename may have been changed (e.g. by TEXPAGE loading) - psRes = resDataInit( GetLastResourceFilename(), HashStringIgnoreCase(GetLastResourceFilename()), pData, resBlockID ); + psRes = resDataInit(GetLastResourceFilename(), HashStringIgnoreCase(GetLastResourceFilename()), pData, resBlockID); if (!psRes) { if (psT->release != NULL) @@ -532,22 +538,22 @@ void *resGetDataFromHash(const char *pType, UDWORD HashedID) // Find the correct type UDWORD HashedType = HashString(pType); - for(psT = psResTypes; psT != NULL; psT = psT->psNext ) + for (psT = psResTypes; psT != NULL; psT = psT->psNext) { - if (psT->HashedType==HashedType) + if (psT->HashedType == HashedType) { /* We found it */ break; } } - ASSERT( psT != NULL, "resGetDataFromHash: Unknown type: %s", pType ); + ASSERT(psT != NULL, "resGetDataFromHash: Unknown type: %s", pType); if (psT == NULL) { return NULL; } - for(psRes = psT->psRes; psRes != NULL; psRes = psRes->psNext) + for (psRes = psT->psRes; psRes != NULL; psRes = psRes->psNext) { if (psRes->HashedID == HashedID) { @@ -556,7 +562,7 @@ void *resGetDataFromHash(const char *pType, UDWORD HashedID) } } - ASSERT( psRes != NULL, "resGetDataFromHash: Unknown ID: %0x Type: %s", HashedID, pType ); + ASSERT(psRes != NULL, "resGetDataFromHash: Unknown ID: %0x Type: %s", HashedID, pType); if (psRes == NULL) { return NULL; @@ -571,7 +577,7 @@ void *resGetDataFromHash(const char *pType, UDWORD HashedID) /* Return the resource for a type and ID */ void *resGetData(const char *pType, const char *pID) { - void * data = resGetDataFromHash(pType, HashStringIgnoreCase(pID)); + void *data = resGetDataFromHash(pType, HashStringIgnoreCase(pID)); ASSERT(data != NULL, "resGetData: Unable to find data for %s type %s", pID, pType); return data; } @@ -583,11 +589,11 @@ bool resGetHashfromData(const char *pType, const void *pData, UDWORD *pHash) RES_DATA *psRes; // Find the correct type - UDWORD HashedType=HashString(pType); + UDWORD HashedType = HashString(pType); - for(psT = psResTypes; psT != NULL; psT = psT->psNext ) + for (psT = psResTypes; psT != NULL; psT = psT->psNext) { - if (psT->HashedType==HashedType) + if (psT->HashedType == HashedType) { break; } @@ -595,12 +601,12 @@ bool resGetHashfromData(const char *pType, const void *pData, UDWORD *pHash) if (psT == NULL) { - ASSERT( false, "resGetHashfromData: Unknown type: %x", HashedType ); + ASSERT(false, "resGetHashfromData: Unknown type: %x", HashedType); return false; } // Find the resource - for(psRes = psT->psRes; psRes; psRes = psRes->psNext) + for (psRes = psT->psRes; psRes; psRes = psRes->psNext) { if (psRes->pData == pData) @@ -611,7 +617,7 @@ bool resGetHashfromData(const char *pType, const void *pData, UDWORD *pHash) if (psRes == NULL) { - ASSERT( false, "resGetHashfromData:: couldn't find data for type %x\n", HashedType ); + ASSERT(false, "resGetHashfromData:: couldn't find data for type %x\n", HashedType); return false; } @@ -620,7 +626,7 @@ bool resGetHashfromData(const char *pType, const void *pData, UDWORD *pHash) return true; } -const char* resGetNamefromData(const char* type, const void *data) +const char *resGetNamefromData(const char *type, const void *data) { RES_TYPE *psT; RES_DATA *psRes; @@ -645,12 +651,12 @@ const char* resGetNamefromData(const char* type, const void *data) if (psT == NULL) { - ASSERT( false, "resGetHashfromData: Unknown type: %x", HashedType ); + ASSERT(false, "resGetHashfromData: Unknown type: %x", HashedType); return ""; } // Find the resource in the resource table - for(psRes = psT->psRes; psRes; psRes = psRes->psNext) + for (psRes = psT->psRes; psRes; psRes = psRes->psNext) { if (psRes->pData == data) { @@ -660,7 +666,7 @@ const char* resGetNamefromData(const char* type, const void *data) if (psRes == NULL) { - ASSERT( false, "resGetHashfromData:: couldn't find data for type %x\n", HashedType ); + ASSERT(false, "resGetHashfromData:: couldn't find data for type %x\n", HashedType); return ""; } @@ -674,11 +680,11 @@ bool resPresent(const char *pType, const char *pID) RES_DATA *psRes; // Find the correct type - UDWORD HashedType=HashString(pType); + UDWORD HashedType = HashString(pType); - for(psT = psResTypes; psT != NULL; psT = psT->psNext ) + for (psT = psResTypes; psT != NULL; psT = psT->psNext) { - if (psT->HashedType==HashedType) + if (psT->HashedType == HashedType) { break; } @@ -692,11 +698,11 @@ bool resPresent(const char *pType, const char *pID) } { - UDWORD HashedID=HashStringIgnoreCase(pID); + UDWORD HashedID = HashStringIgnoreCase(pID); - for(psRes = psT->psRes; psRes; psRes = psRes->psNext) + for (psRes = psT->psRes; psRes; psRes = psRes->psNext) { - if (psRes->HashedID==HashedID) + if (psRes->HashedID == HashedID) { /* We found it */ break; @@ -721,7 +727,7 @@ void resReleaseAll(void) resReleaseAllData(); - for(psT = psResTypes; psT != NULL; psT = psNT) + for (psT = psResTypes; psT != NULL; psT = psNT) { psNT = psT->psNext; free(psT); @@ -739,7 +745,7 @@ void resReleaseAllData(void) for (psT = psResTypes; psT != NULL; psT = psT->psNext) { - for(psRes = psT->psRes; psRes != NULL; psRes = psNRes) + for (psRes = psT->psRes; psRes != NULL; psRes = psNRes) { if (psRes->usage == 0) { @@ -766,10 +772,10 @@ void resReleaseBlockData(SDWORD blockID) RES_TYPE *psT, *psNT; RES_DATA *psPRes, *psRes, *psNRes; - for(psT = psResTypes; psT != NULL; psT = psNT) + for (psT = psResTypes; psT != NULL; psT = psNT) { psPRes = NULL; - for(psRes = psT->psRes; psRes; psRes = psNRes) + for (psRes = psT->psRes; psRes; psRes = psNRes) { ASSERT(psRes != NULL, "resReleaseBlockData: null pointer passed into loop"); @@ -780,9 +786,9 @@ void resReleaseBlockData(SDWORD blockID) debug(LOG_NEVER, "resReleaseBlockData: %s resource: %s(%04x) not used", psT->aType, psRes->aID, psRes->HashedID); } - if(psT->release != NULL) + if (psT->release != NULL) { - psT->release( psRes->pData ); + psT->release(psRes->pData); } psNRes = psRes->psNext; diff --git a/lib/framework/frameresource.h b/lib/framework/frameresource.h index b651dd0b392..eb3a41ef1b4 100644 --- a/lib/framework/frameresource.h +++ b/lib/framework/frameresource.h @@ -48,11 +48,11 @@ struct RES_DATA SDWORD blockID; // which of the blocks is it in (so we can clear some of them...) UDWORD HashedID; // hashed version of the name of the id - RES_DATA * psNext; // next entry - most likely to be following on! + RES_DATA *psNext; // next entry - most likely to be following on! UDWORD usage; // Reference count // ID of the resource - filename from the .wrf - e.g. "TRON.PIE" - const char* aID; + const char *aID; }; @@ -71,7 +71,7 @@ struct RES_TYPE UDWORD HashedType; // hashed version of the name of the id - // a null hashedtype indicates end of list RES_FILELOAD fileLoad; // This isn't really used any more ? - RES_TYPE * psNext; + RES_TYPE *psNext; }; @@ -85,8 +85,8 @@ extern bool resInitialise(void); extern void resShutDown(void); /** Set the base resource directory. */ -extern void resSetBaseDir(const char* pResDir); -extern void resForceBaseDir(const char* pResDir); +extern void resSetBaseDir(const char *pResDir); +extern void resForceBaseDir(const char *pResDir); /** Parse the res file. */ bool resLoad(const char *pResFile, SDWORD blockID); @@ -102,11 +102,11 @@ extern void resReleaseAllData(void); /** Add a buffer load and release function for a file type. */ extern bool resAddBufferLoad(const char *pType, RES_BUFFERLOAD buffLoad, - RES_FREE release); + RES_FREE release); /** Add a file name load and release function for a file type. */ extern bool resAddFileLoad(const char *pType, RES_FILELOAD fileLoad, - RES_FREE release); + RES_FREE release); /** Call the load function for a file. */ extern bool resLoadFile(const char *pType, const char *pFile); @@ -126,7 +126,7 @@ extern bool resGetHashfromData(const char *pType, const void *pData, UDWORD *pHa * \return the from the ID string (usually its filename without directory) * \note passing a NULL pointer for either \c type or \c data is valid (the result will be an empty string though) */ -extern const char* resGetNamefromData(const char* type, const void *data); +extern const char *resGetNamefromData(const char *type, const void *data); /** Return last imd resource */ const char *GetLastResourceFilename(void) WZ_DECL_PURE; diff --git a/lib/framework/geometry.cpp b/lib/framework/geometry.cpp index 0917596a4ca..f58eb11b414 100644 --- a/lib/framework/geometry.cpp +++ b/lib/framework/geometry.cpp @@ -3,96 +3,96 @@ #include #include "trig.h" -Affine3F& Affine3F::RotX(uint16_t x) +Affine3F &Affine3F::RotX(uint16_t x) { if (x != 0) { int32_t tmp; const int64_t cra = iCos(x), sra = iSin(x); - tmp = (cra*m[0][1] + sra*m[0][2])>>16; - m[0][2] = (cra*m[0][2] - sra*m[0][1])>>16; + tmp = (cra * m[0][1] + sra * m[0][2]) >> 16; + m[0][2] = (cra * m[0][2] - sra * m[0][1]) >> 16; m[0][1] = tmp; - tmp = (cra*m[1][1] + sra*m[1][2])>>16; - m[1][2] = (cra*m[1][2] - sra*m[1][1])>>16; + tmp = (cra * m[1][1] + sra * m[1][2]) >> 16; + m[1][2] = (cra * m[1][2] - sra * m[1][1]) >> 16; m[1][1] = tmp; - tmp = (cra*m[2][1] + sra*m[2][2])>>16; - m[2][2] = (cra*m[2][2] - sra*m[2][1])>>16; + tmp = (cra * m[2][1] + sra * m[2][2]) >> 16; + m[2][2] = (cra * m[2][2] - sra * m[2][1]) >> 16; m[2][1] = tmp; } return *this; } -Affine3F& Affine3F::RotY(uint16_t y) +Affine3F &Affine3F::RotY(uint16_t y) { if (y != 0) { int32_t tmp; int64_t cra = iCos(y), sra = iSin(y); - - tmp = (cra*m[0][0] - sra*m[0][2])>>16; - m[0][2] = (sra*m[0][0] + cra*m[0][2])>>16; + + tmp = (cra * m[0][0] - sra * m[0][2]) >> 16; + m[0][2] = (sra * m[0][0] + cra * m[0][2]) >> 16; m[0][0] = tmp; - - tmp = (cra*m[1][0] - sra*m[1][2])>>16; - m[1][2] = (sra*m[1][0] + cra*m[1][2])>>16; + + tmp = (cra * m[1][0] - sra * m[1][2]) >> 16; + m[1][2] = (sra * m[1][0] + cra * m[1][2]) >> 16; m[1][0] = tmp; - - tmp = (cra*m[2][0] - sra*m[2][2])>>16; - m[2][2] = (sra*m[2][0] + cra*m[2][2])>>16; + + tmp = (cra * m[2][0] - sra * m[2][2]) >> 16; + m[2][2] = (sra * m[2][0] + cra * m[2][2]) >> 16; m[2][0] = tmp; - + } return *this; } -Affine3F& Affine3F::RotZ(uint16_t z) +Affine3F &Affine3F::RotZ(uint16_t z) { if (z != 0) { int32_t tmp; int64_t cra = iCos(z), sra = iSin(z); - - tmp = (cra*m[0][0] + sra*m[0][1])>>16; - m[0][1] = (cra*m[0][1] - sra*m[0][0])>>16; + + tmp = (cra * m[0][0] + sra * m[0][1]) >> 16; + m[0][1] = (cra * m[0][1] - sra * m[0][0]) >> 16; m[0][0] = tmp; - - tmp = (cra*m[1][0] + sra*m[1][1])>>16; - m[1][1] = (cra*m[1][1] - sra*m[1][0])>>16; + + tmp = (cra * m[1][0] + sra * m[1][1]) >> 16; + m[1][1] = (cra * m[1][1] - sra * m[1][0]) >> 16; m[1][0] = tmp; - - tmp = (cra*m[2][0] + sra*m[2][1])>>16; - m[2][1] = (cra*m[2][1] - sra*m[2][0])>>16; + + tmp = (cra * m[2][0] + sra * m[2][1]) >> 16; + m[2][1] = (cra * m[2][1] - sra * m[2][0]) >> 16; m[2][0] = tmp; } return *this; } -Affine3F& Affine3F::RotZXY(Rotation r) +Affine3F &Affine3F::RotZXY(Rotation r) { return (*this).RotZ(r.direction).RotX(r.pitch).RotY(r.roll); } -Affine3F& Affine3F::RotInvZXY(Rotation r) +Affine3F &Affine3F::RotInvZXY(Rotation r) { - (*this) = (*this)*(Affine3F().RotZ(r.direction).RotX(r.pitch).RotY(r.roll).InvRot()); + (*this) = (*this) * (Affine3F().RotZ(r.direction).RotX(r.pitch).RotY(r.roll).InvRot()); return *this; } -Affine3F& Affine3F::RotYXZ(Rotation r) +Affine3F &Affine3F::RotYXZ(Rotation r) { return (*this).RotY(r.roll).RotX(r.pitch).RotZ(r.direction); } -Affine3F& Affine3F::RotInvYXZ(Rotation r) +Affine3F &Affine3F::RotInvYXZ(Rotation r) { - (*this) = (*this)*(Affine3F().RotY(r.roll).RotX(r.pitch).RotZ(r.direction)); + (*this) = (*this) * (Affine3F().RotY(r.roll).RotX(r.pitch).RotZ(r.direction)); return *this; } -Affine3F& Affine3F::Trans(int32_t x, int32_t y, int32_t z) +Affine3F &Affine3F::Trans(int32_t x, int32_t y, int32_t z) { m[0][3] += x * m[0][0] + y * m[0][1] + z * m[0][2]; m[1][3] += x * m[1][0] + y * m[1][1] + z * m[1][2]; @@ -100,12 +100,12 @@ Affine3F& Affine3F::Trans(int32_t x, int32_t y, int32_t z) return *this; } -Affine3F& Affine3F::Trans(Vector3i tr) +Affine3F &Affine3F::Trans(Vector3i tr) { return (*this).Trans(tr.x, tr.y, tr.z); } -Affine3F& Affine3F::Scale(int32_t s) +Affine3F &Affine3F::Scale(int32_t s) { m[0][0] = ((int64_t)m[0][0] * s) / FP_MULTIPLIER; m[0][1] = ((int64_t)m[0][1] * s) / FP_MULTIPLIER; @@ -119,22 +119,22 @@ Affine3F& Affine3F::Scale(int32_t s) return *this; } -Affine3F& Affine3F::InvRot() +Affine3F &Affine3F::InvRot() { - std::swap(m[0][1],m[1][0]); - std::swap(m[0][2],m[2][0]); - std::swap(m[1][0],m[0][1]); - std::swap(m[1][1],m[1][1]); - std::swap(m[1][2],m[2][1]); - std::swap(m[2][0],m[0][2]); - std::swap(m[2][1],m[1][2]); - std::swap(m[2][2],m[2][2]); + std::swap(m[0][1], m[1][0]); + std::swap(m[0][2], m[2][0]); + std::swap(m[1][0], m[0][1]); + std::swap(m[1][1], m[1][1]); + std::swap(m[1][2], m[2][1]); + std::swap(m[2][0], m[0][2]); + std::swap(m[2][1], m[1][2]); + std::swap(m[2][2], m[2][2]); return (*this); } Vector3i Affine3F::translation() const { - return Vector3i(m[0][3]/FP_MULTIPLIER, m[1][3]/FP_MULTIPLIER, m[2][3]/FP_MULTIPLIER); + return Vector3i(m[0][3] / FP_MULTIPLIER, m[1][3] / FP_MULTIPLIER, m[2][3] / FP_MULTIPLIER); } Vector3i Affine3F::operator*(const Vector3i v) const @@ -146,7 +146,7 @@ Vector3i Affine3F::operator*(const Vector3i v) const return s; } -Affine3F Affine3F::operator*(const Affine3F& rhs) const +Affine3F Affine3F::operator*(const Affine3F &rhs) const { Affine3F tr; #define i64 int64_t @@ -154,7 +154,7 @@ Affine3F Affine3F::operator*(const Affine3F& rhs) const tr.m[0][1] = ((i64)m[0][0] * rhs.m[0][1] + (i64)m[0][1] * rhs.m[1][1] + (i64)m[0][2] * rhs.m[2][1]) / FP_MULTIPLIER; tr.m[0][2] = ((i64)m[0][0] * rhs.m[0][2] + (i64)m[0][1] * rhs.m[1][2] + (i64)m[0][2] * rhs.m[2][2]) / FP_MULTIPLIER; tr.m[0][3] = ((i64)m[0][0] * rhs.m[0][3] + (i64)m[0][1] * rhs.m[1][3] + (i64)m[0][2] * rhs.m[2][3]) / FP_MULTIPLIER + m[0][3]; - + tr.m[1][0] = ((i64)m[1][0] * rhs.m[0][0] + (i64)m[1][1] * rhs.m[1][0] + (i64)m[1][2] * rhs.m[2][0]) / FP_MULTIPLIER; tr.m[1][1] = ((i64)m[1][0] * rhs.m[0][1] + (i64)m[1][1] * rhs.m[1][1] + (i64)m[1][2] * rhs.m[2][1]) / FP_MULTIPLIER; tr.m[1][2] = ((i64)m[1][0] * rhs.m[0][2] + (i64)m[1][1] * rhs.m[1][2] + (i64)m[1][2] * rhs.m[2][2]) / FP_MULTIPLIER; diff --git a/lib/framework/geometry.h b/lib/framework/geometry.h index e406f517aa4..f58d113737a 100644 --- a/lib/framework/geometry.h +++ b/lib/framework/geometry.h @@ -1,17 +1,17 @@ /* * This file is part of Warzone 2100. * Copyright (C) 2013 Warzone 2100 Project - * + * * Warzone 2100 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. - * + * * Warzone 2100 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 Warzone 2100; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA @@ -27,7 +27,8 @@ /** * Fixed point 3D affine transformation. */ -class Affine3F { +class Affine3F +{ /* Compact 4x4 matrix where the 4th row is assumed to be [0 0 0 1] * since we're representing an affine transformation @@ -39,25 +40,26 @@ class Affine3F { static const int INT_BITS = std::numeric_limits::digits - FRAC_BITS; static const int FP_MULTIPLIER = 1 << FRAC_BITS; - Affine3F() { - m[0][0]=FP_MULTIPLIER; m[0][1]=0; m[0][2]=0; m[0][3]=0; - m[1][0]=0; m[1][1]=FP_MULTIPLIER; m[1][2]=0; m[1][3]=0; - m[2][0]=0; m[2][1]=0; m[2][2]=FP_MULTIPLIER; m[2][3]=0; + Affine3F() + { + m[0][0] = FP_MULTIPLIER; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0; + m[1][0] = 0; m[1][1] = FP_MULTIPLIER; m[1][2] = 0; m[1][3] = 0; + m[2][0] = 0; m[2][1] = 0; m[2][2] = FP_MULTIPLIER; m[2][3] = 0; } - Affine3F& RotX(uint16_t); - Affine3F& RotY(uint16_t); - Affine3F& RotZ(uint16_t); - Affine3F& RotZXY(Rotation); - Affine3F& RotInvZXY(Rotation); - Affine3F& RotYXZ(Rotation); - Affine3F& RotInvYXZ(Rotation); - Affine3F& Trans(int32_t,int32_t,int32_t); - Affine3F& Trans(Vector3i); - Affine3F& Scale(int32_t); // Note: parameter assummed to be a scaled integer (scaled by FP_MULTIPLIER) - Affine3F& InvRot(); + Affine3F &RotX(uint16_t); + Affine3F &RotY(uint16_t); + Affine3F &RotZ(uint16_t); + Affine3F &RotZXY(Rotation); + Affine3F &RotInvZXY(Rotation); + Affine3F &RotYXZ(Rotation); + Affine3F &RotInvYXZ(Rotation); + Affine3F &Trans(int32_t, int32_t, int32_t); + Affine3F &Trans(Vector3i); + Affine3F &Scale(int32_t); // Note: parameter assummed to be a scaled integer (scaled by FP_MULTIPLIER) + Affine3F &InvRot(); Vector3i translation() const; Vector3i operator*(const Vector3i) const; - Affine3F operator*(const Affine3F&) const; + Affine3F operator*(const Affine3F &) const; Vector3i InvRot(const Vector3i) const; }; diff --git a/lib/framework/gettext.h b/lib/framework/gettext.h index 209921e654f..9f453d422d9 100644 --- a/lib/framework/gettext.h +++ b/lib/framework/gettext.h @@ -31,10 +31,10 @@ # ifdef DEFAULT_TEXT_DOMAIN # undef gettext # define gettext(Msgid) \ - dgettext (DEFAULT_TEXT_DOMAIN, Msgid) + dgettext (DEFAULT_TEXT_DOMAIN, Msgid) # undef ngettext # define ngettext(Msgid1, Msgid2, N) \ - dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N) + dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N) # endif #else @@ -67,20 +67,20 @@ # define gettext(Msgid) ((const char *) (Msgid)) # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid)) # define dcgettext(Domainname, Msgid, Category) \ - ((void) (Category), dgettext (Domainname, Msgid)) + ((void) (Category), dgettext (Domainname, Msgid)) # define ngettext(Msgid1, Msgid2, N) \ - ((N) == 1 \ - ? ((void) (Msgid2), (const char *) (Msgid1)) \ - : ((void) (Msgid1), (const char *) (Msgid2))) + ((N) == 1 \ + ? ((void) (Msgid2), (const char *) (Msgid1)) \ + : ((void) (Msgid1), (const char *) (Msgid2))) # define dngettext(Domainname, Msgid1, Msgid2, N) \ - ((void) (Domainname), ngettext (Msgid1, Msgid2, N)) + ((void) (Domainname), ngettext (Msgid1, Msgid2, N)) # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \ - ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N)) + ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N)) # define textdomain(Domainname) ((const char *) (Domainname)) # define bindtextdomain(Domainname, Dirname) \ - ((void) (Domainname), (const char *) (Dirname)) + ((void) (Domainname), (const char *) (Dirname)) # define bind_textdomain_codeset(Domainname, Codeset) \ - ((void) (Domainname), (const char *) (Codeset)) + ((void) (Domainname), (const char *) (Codeset)) #endif @@ -102,26 +102,26 @@ The letter 'p' stands for 'particular' or 'special'. */ #ifdef DEFAULT_TEXT_DOMAIN # define pgettext(Msgctxt, Msgid) \ - pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) + pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) #else # define pgettext(Msgctxt, Msgid) \ - pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) + pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) #endif #define dpgettext(Domainname, Msgctxt, Msgid) \ - pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) + pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \ - pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category) + pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category) #ifdef DEFAULT_TEXT_DOMAIN # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \ - npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) + npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) #else # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \ - npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) + npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) #endif #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \ - npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) + npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \ - npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category) + npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category) #ifdef __GNUC__ __inline @@ -131,15 +131,19 @@ inline #endif #endif static const char * -pgettext_aux (const char *domain, - const char *msg_ctxt_id, const char *msgid, - int category) +pgettext_aux(const char *domain, + const char *msg_ctxt_id, const char *msgid, + int category) { - const char *translation = dcgettext (domain, msg_ctxt_id, category); - if (translation == msg_ctxt_id) - return msgid; - else - return translation; + const char *translation = dcgettext(domain, msg_ctxt_id, category); + if (translation == msg_ctxt_id) + { + return msgid; + } + else + { + return translation; + } } #ifdef __GNUC__ @@ -150,17 +154,21 @@ inline #endif #endif static const char * -npgettext_aux (const char *domain, - const char *msg_ctxt_id, const char *msgid, - const char *msgid_plural, unsigned long int n, - int category) +npgettext_aux(const char *domain, + const char *msg_ctxt_id, const char *msgid, + const char *msgid_plural, unsigned long int n, + int category) { - const char *translation = - dcngettext (domain, msg_ctxt_id, msgid_plural, n, category); - if (translation == msg_ctxt_id || translation == msgid_plural) - return (n == 1 ? msgid : msgid_plural); - else - return translation; + const char *translation = + dcngettext(domain, msg_ctxt_id, msgid_plural, n, category); + if (translation == msg_ctxt_id || translation == msgid_plural) + { + return (n == 1 ? msgid : msgid_plural); + } + else + { + return translation; + } } /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID @@ -170,17 +178,17 @@ npgettext_aux (const char *domain, #include #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \ - (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \ - /* || __STDC_VERSION__ >= 199901L */ ) + (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \ + /* || __STDC_VERSION__ >= 199901L */ ) #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS #include #endif #define pgettext_expr(Msgctxt, Msgid) \ - dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES) + dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES) #define dpgettext_expr(Domainname, Msgctxt, Msgid) \ - dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES) + dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES) #ifdef __GNUC__ __inline @@ -190,42 +198,46 @@ inline #endif #endif static const char * -dcpgettext_expr (const char *domain, - const char *msgctxt, const char *msgid, - int category) +dcpgettext_expr(const char *domain, + const char *msgctxt, const char *msgid, + int category) { - size_t msgctxt_len = strlen (msgctxt) + 1; - size_t msgid_len = strlen (msgid) + 1; - const char *translation; + size_t msgctxt_len = strlen(msgctxt) + 1; + size_t msgid_len = strlen(msgid) + 1; + const char *translation; #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS - char msg_ctxt_id[msgctxt_len + msgid_len]; + char msg_ctxt_id[msgctxt_len + msgid_len]; #else - char buf[1024]; - char *msg_ctxt_id = - (msgctxt_len + msgid_len <= sizeof (buf) - ? buf - : (char *) malloc (msgctxt_len + msgid_len)); - if (msg_ctxt_id != NULL) + char buf[1024]; + char *msg_ctxt_id = + (msgctxt_len + msgid_len <= sizeof(buf) + ? buf + : (char *) malloc(msgctxt_len + msgid_len)); + if (msg_ctxt_id != NULL) #endif - { - memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1); - msg_ctxt_id[msgctxt_len - 1] = '\004'; - memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len); - translation = dcgettext (domain, msg_ctxt_id, category); + { + memcpy(msg_ctxt_id, msgctxt, msgctxt_len - 1); + msg_ctxt_id[msgctxt_len - 1] = '\004'; + memcpy(msg_ctxt_id + msgctxt_len, msgid, msgid_len); + translation = dcgettext(domain, msg_ctxt_id, category); #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS - if (msg_ctxt_id != buf) - free (msg_ctxt_id); + if (msg_ctxt_id != buf) + { + free(msg_ctxt_id); + } #endif - if (translation != msg_ctxt_id) - return translation; - } - return msgid; + if (translation != msg_ctxt_id) + { + return translation; + } + } + return msgid; } #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \ - dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES) + dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES) #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \ - dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES) + dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES) #ifdef __GNUC__ __inline @@ -235,37 +247,41 @@ inline #endif #endif static const char * -dcnpgettext_expr (const char *domain, - const char *msgctxt, const char *msgid, - const char *msgid_plural, unsigned long int n, - int category) +dcnpgettext_expr(const char *domain, + const char *msgctxt, const char *msgid, + const char *msgid_plural, unsigned long int n, + int category) { - size_t msgctxt_len = strlen (msgctxt) + 1; - size_t msgid_len = strlen (msgid) + 1; - const char *translation; + size_t msgctxt_len = strlen(msgctxt) + 1; + size_t msgid_len = strlen(msgid) + 1; + const char *translation; #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS - char msg_ctxt_id[msgctxt_len + msgid_len]; + char msg_ctxt_id[msgctxt_len + msgid_len]; #else - char buf[1024]; - char *msg_ctxt_id = - (msgctxt_len + msgid_len <= sizeof (buf) - ? buf - : (char *) malloc (msgctxt_len + msgid_len)); - if (msg_ctxt_id != NULL) + char buf[1024]; + char *msg_ctxt_id = + (msgctxt_len + msgid_len <= sizeof(buf) + ? buf + : (char *) malloc(msgctxt_len + msgid_len)); + if (msg_ctxt_id != NULL) #endif - { - memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1); - msg_ctxt_id[msgctxt_len - 1] = '\004'; - memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len); - translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category); + { + memcpy(msg_ctxt_id, msgctxt, msgctxt_len - 1); + msg_ctxt_id[msgctxt_len - 1] = '\004'; + memcpy(msg_ctxt_id + msgctxt_len, msgid, msgid_len); + translation = dcngettext(domain, msg_ctxt_id, msgid_plural, n, category); #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS - if (msg_ctxt_id != buf) - free (msg_ctxt_id); + if (msg_ctxt_id != buf) + { + free(msg_ctxt_id); + } #endif - if (!(translation == msg_ctxt_id || translation == msgid_plural)) - return translation; - } - return (n == 1 ? msgid : msgid_plural); + if (!(translation == msg_ctxt_id || translation == msgid_plural)) + { + return translation; + } + } + return (n == 1 ? msgid : msgid_plural); } #endif /* _LIBGETTEXT_H */ diff --git a/lib/framework/i18n.cpp b/lib/framework/i18n.cpp index 0258062f07f..7691dcaad9f 100644 --- a/lib/framework/i18n.cpp +++ b/lib/framework/i18n.cpp @@ -91,11 +91,12 @@ */ static const struct { - const char * language; - const char * name; + const char *language; + const char *name; USHORT usPrimaryLanguage; USHORT usSubLanguage; -} map[] = { +} map[] = +{ { "", N_("System locale"), LANG_NEUTRAL, SUBLANG_DEFAULT }, # if defined(ENABLE_NLS) { "ca", LANG_NAME_CATALAN, LANG_CATALAN, SUBLANG_DEFAULT }, @@ -157,7 +158,8 @@ static const struct const char *name; const char *locale; const char *localeFallback; -} map[] = { +} map[] = +{ { "", N_("System locale"), "", "" }, # if defined(ENABLE_NLS) { "ca_ES", LANG_NAME_CATALAN, "ca_ES.UTF-8", "ca" }, @@ -213,7 +215,7 @@ static unsigned int selectedLanguage = 0; * Return the language part of the selected locale */ #if !defined(ENABLE_NLS) -const char* getLanguage(void) +const char *getLanguage(void) { return ""; } @@ -274,7 +276,7 @@ const char *getLanguage(void) #endif -const char* getLanguageName(void) +const char *getLanguageName(void) { const char *language = getLanguage(); unsigned int i; @@ -295,7 +297,7 @@ const char* getLanguageName(void) # if defined(WZ_OS_WIN) static bool setLocaleWindows(USHORT usPrimaryLanguage, USHORT usSubLanguage) { - bool success = SUCCEEDED( SetThreadLocale( MAKELCID( MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT ) ) ); + bool success = SUCCEEDED(SetThreadLocale(MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT))); if (!success) { @@ -316,7 +318,7 @@ static bool setLocaleWindows(USHORT usPrimaryLanguage, USHORT usSubLanguage) * \param locale The locale, NOT just the language part * \note Use this instead of setlocale(), because we need the default radix character */ -static bool setLocaleUnix(const char* locale) +static bool setLocaleUnix(const char *locale) { const char *actualLocale = setlocale(LC_ALL, locale); @@ -404,18 +406,18 @@ void initI18n(void) textdomainDirectory = bindtextdomain(PACKAGE, localeDir); } #else - #ifdef WZ_OS_MAC +#ifdef WZ_OS_MAC { char resourcePath[PATH_MAX]; CFURLRef resourceURL = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()); - if( CFURLGetFileSystemRepresentation( resourceURL, true, (UInt8 *) resourcePath, PATH_MAX) ) + if (CFURLGetFileSystemRepresentation(resourceURL, true, (UInt8 *) resourcePath, PATH_MAX)) { sstrcat(resourcePath, "/locale"); textdomainDirectory = bindtextdomain(PACKAGE, resourcePath); } else { - debug( LOG_ERROR, "Could not change to resources directory." ); + debug(LOG_ERROR, "Could not change to resources directory."); } if (resourceURL != NULL) @@ -425,9 +427,9 @@ void initI18n(void) debug(LOG_INFO, "resourcePath is %s", resourcePath); } - #else +#else textdomainDirectory = bindtextdomain(PACKAGE, LOCALEDIR); - #endif +#endif #endif if (!textdomainDirectory) { diff --git a/lib/framework/i18n.h b/lib/framework/i18n.h index 477ea43d8b2..244a802dd1d 100644 --- a/lib/framework/i18n.h +++ b/lib/framework/i18n.h @@ -49,8 +49,8 @@ // Make xgettext recognize the context #define NP_(Context, String) gettext_noop(String) -extern WZ_DECL_CONST const char* getLanguage(void); -extern WZ_DECL_CONST const char* getLanguageName(void); +extern WZ_DECL_CONST const char *getLanguage(void); +extern WZ_DECL_CONST const char *getLanguageName(void); extern bool setLanguage(const char *name); extern void setNextLanguage(void); extern void initI18n(void); diff --git a/lib/framework/input.h b/lib/framework/input.h index 4f970ba37c0..ec28db6df94 100644 --- a/lib/framework/input.h +++ b/lib/framework/input.h @@ -93,7 +93,7 @@ enum KEY_CODE KEY_M = 'm', KEY_COMMA = ',', KEY_FULLSTOP = '.', - KEY_FORWARDSLASH= '/', + KEY_FORWARDSLASH = '/', KEY_RSHIFT = 303, KEY_RMETA = 309, // TODO Added after Qt branched. KEY_RSUPER = 312, // TODO Added after Qt branched. @@ -129,7 +129,7 @@ enum KEY_CODE KEY_F11 = 292, KEY_F12 = 293, KEY_RCTRL = 305, - KEY_KP_BACKSLASH= 267, ///< Most keypads just have a forward slash. + KEY_KP_BACKSLASH = 267, ///< Most keypads just have a forward slash. KEY_RALT = 307, KEY_HOME = 278, KEY_UPARROW = 273, @@ -163,7 +163,10 @@ struct MousePress enum Action {None, Press, Release}; MousePress(Action action = None, MOUSE_KEY_CODE key = MOUSE_BAD) : action(action), key(key) {} - bool empty() const { return action == None; } + bool empty() const + { + return action == None; + } Action action; MOUSE_KEY_CODE key; diff --git a/lib/framework/lexer_input.cpp b/lib/framework/lexer_input.cpp index 61f79535a53..0ef3dfafab5 100644 --- a/lib/framework/lexer_input.cpp +++ b/lib/framework/lexer_input.cpp @@ -20,52 +20,52 @@ #include "frame.h" #include "lexer_input.h" -int lexer_input(lexerinput_t* input, char* buf, size_t max_size, int nullvalue) +int lexer_input(lexerinput_t *input, char *buf, size_t max_size, int nullvalue) { switch (input->type) { - case LEXINPUT_STDIO: - if (feof(input->input.stdiofile)) - { - buf[0] = EOF; - return nullvalue; - } - else - { - return fread(buf, 1, max_size, input->input.stdiofile); - } - break; + case LEXINPUT_STDIO: + if (feof(input->input.stdiofile)) + { + buf[0] = EOF; + return nullvalue; + } + else + { + return fread(buf, 1, max_size, input->input.stdiofile); + } + break; - case LEXINPUT_PHYSFS: - if (PHYSFS_eof(input->input.physfsfile)) + case LEXINPUT_PHYSFS: + if (PHYSFS_eof(input->input.physfsfile)) + { + buf[0] = EOF; + return nullvalue; + } + else + { + int result = PHYSFS_read(input->input.physfsfile, buf, 1, max_size); + if (result == -1) { buf[0] = EOF; return nullvalue; } - else - { - int result = PHYSFS_read(input->input.physfsfile, buf, 1, max_size); - if (result == -1) - { - buf[0] = EOF; - return nullvalue; - } - return result; - } - break; + return result; + } + break; - case LEXINPUT_BUFFER: - if (input->input.buffer.begin != input->input.buffer.end) - { - buf[0] = *input->input.buffer.begin++; - return 1; - } - else - { - buf[0] = EOF; - return nullvalue; - } - break; + case LEXINPUT_BUFFER: + if (input->input.buffer.begin != input->input.buffer.end) + { + buf[0] = *input->input.buffer.begin++; + return 1; + } + else + { + buf[0] = EOF; + return nullvalue; + } + break; } ASSERT(!"Invalid input type!", "Invalid input type used for lexer (numeric value: %u)", (unsigned int)input->type); diff --git a/lib/framework/lexer_input.h b/lib/framework/lexer_input.h index d32565b944e..0794257170b 100644 --- a/lib/framework/lexer_input.h +++ b/lib/framework/lexer_input.h @@ -34,12 +34,12 @@ struct lexerinput_t { union { - FILE* stdiofile; - PHYSFS_file* physfsfile; + FILE *stdiofile; + PHYSFS_file *physfsfile; struct { - const char* begin; - const char* end; + const char *begin; + const char *end; } buffer; } input; lexinput_type type; @@ -51,12 +51,12 @@ struct lexerinput_t #define YY_EXTRA_TYPE lexerinput_t * -extern int lexer_input(lexerinput_t* input, char* buf, size_t max_size, int nullvalue); +extern int lexer_input(lexerinput_t *input, char *buf, size_t max_size, int nullvalue); #define YY_INPUT(buf, result, max_size) \ -do \ -{ \ - result = lexer_input(yyextra, buf, max_size, YY_NULL); \ -} while(0) + do \ + { \ + result = lexer_input(yyextra, buf, max_size, YY_NULL); \ + } while(0) #endif // __INCLUDED_LIB_FRAMEWORK_LEXER_INPUT_H__ diff --git a/lib/framework/listmacs.h b/lib/framework/listmacs.h index cfd66f162bc..7083df51ae6 100644 --- a/lib/framework/listmacs.h +++ b/lib/framework/listmacs.h @@ -34,55 +34,55 @@ * Append an entry to the end of a linked list */ #define LIST_APPEND(psHead, psEntry, TYPE) \ -{ \ - TYPE *psPrev, *psCurr; \ -\ - /* Find the end of the list */ \ - psPrev = NULL; \ - for(psCurr = (psHead); psCurr; psCurr = psCurr->psNext) \ { \ - psPrev = psCurr; \ - } \ -\ - (psEntry)->psNext = NULL; \ -\ - if (psPrev) \ - { \ - /* Append to the end */ \ - psPrev->psNext = (psEntry); \ - } \ - else \ - { \ - /* If a NULL list got passed, make this item the list */ \ - (psHead) = (psEntry); \ - } \ -} + TYPE *psPrev, *psCurr; \ + \ + /* Find the end of the list */ \ + psPrev = NULL; \ + for(psCurr = (psHead); psCurr; psCurr = psCurr->psNext) \ + { \ + psPrev = psCurr; \ + } \ + \ + (psEntry)->psNext = NULL; \ + \ + if (psPrev) \ + { \ + /* Append to the end */ \ + psPrev->psNext = (psEntry); \ + } \ + else \ + { \ + /* If a NULL list got passed, make this item the list */ \ + (psHead) = (psEntry); \ + } \ + } /** * Remove an entry from a linked list */ #define LIST_REMOVE(psHead, psEntry, TYPE) \ -{ \ - TYPE *psPrev, *psCurr; \ -\ - psPrev = NULL; \ - for(psCurr = (psHead); psCurr; psCurr = psCurr->psNext) \ { \ - if (psCurr == (psEntry)) \ + TYPE *psPrev, *psCurr; \ + \ + psPrev = NULL; \ + for(psCurr = (psHead); psCurr; psCurr = psCurr->psNext) \ { \ - break; \ + if (psCurr == (psEntry)) \ + { \ + break; \ + } \ + psPrev = psCurr; \ } \ - psPrev = psCurr; \ - } \ - ASSERT(psCurr != NULL, "LIST_REMOVE: entry not found"); \ - if (psPrev == NULL) \ - { \ - (psHead) = (psHead)->psNext; \ - } \ - else if (psCurr != NULL) \ - { \ - psPrev->psNext = psCurr->psNext; \ - } \ -} + ASSERT(psCurr != NULL, "LIST_REMOVE: entry not found"); \ + if (psPrev == NULL) \ + { \ + (psHead) = (psHead)->psNext; \ + } \ + else if (psCurr != NULL) \ + { \ + psPrev->psNext = psCurr->psNext; \ + } \ + } #endif // __INCLUDED_LIB_FRAMEWORK_LISTMACS_H__ diff --git a/lib/framework/macros.h b/lib/framework/macros.h index 58f5421f4be..3955d555412 100644 --- a/lib/framework/macros.h +++ b/lib/framework/macros.h @@ -31,10 +31,10 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + WZ_ASSERT_ARRAY_EXPR(x)) #define CLIP(val, min, max) do \ -{ \ - if ((val) < (min)) (val) = (min); \ - else if ((val) > (max)) (val) = (max); \ -} while(0) + { \ + if ((val) < (min)) (val) = (min); \ + else if ((val) > (max)) (val) = (max); \ + } while(0) /* defines for ONEINX diff --git a/lib/framework/math_ext.h b/lib/framework/math_ext.h index d659356b8ff..65330e069ec 100644 --- a/lib/framework/math_ext.h +++ b/lib/framework/math_ext.h @@ -48,9 +48,13 @@ static inline int roundf(float x) { // Ensure that float truncation results in a proper rounding if (x < 0.0f) + { return x - 0.5f; + } else + { return x + 0.5f; + } } @@ -102,8 +106,14 @@ static inline WZ_DECL_CONST float hypotf(float x, float y) */ static inline WZ_DECL_CONST float wrapf(float x, float max) { - while(x < 0.0f) x += max; - while(x >= max) x -= max; + while (x < 0.0f) + { + x += max; + } + while (x >= max) + { + x -= max; + } return x; } @@ -116,8 +126,14 @@ static inline WZ_DECL_CONST float wrapf(float x, float max) */ static inline WZ_DECL_CONST int clip(int x, int min, int max) { - if (x < min) return min; - if (x > max) return max; + if (x < min) + { + return min; + } + if (x > max) + { + return max; + } return x; } @@ -130,8 +146,14 @@ static inline WZ_DECL_CONST int clip(int x, int min, int max) */ static inline WZ_DECL_CONST float clipf(float x, float min, float max) { - if (x < min) return min; - if (x > max) return max; + if (x < min) + { + return min; + } + if (x > max) + { + return max; + } return x; } @@ -142,17 +164,17 @@ static inline void solveDifferential2ndOrder(float *y_, float *dydt_, double acc // Solution is y = g_1 exp(h_1 t) + g_2 exp(h_2 t), where h_i are the solutions to h^2 + f h + a = 0, which are -f/2 +- sqrt(f^2/4 - a). // At t = 0, g_1 = (y' - h_2 y) / (h_1 - h_2) and g_2 = (y' - h_1 y) / (h_2 - h_1). - std::complex d = friction*friction/4 - acceleration; + std::complex d = friction * friction / 4 - acceleration; std::complex sqd = std::sqrt(d); - std::complex h1 = -friction/2 + sqd; - std::complex h2 = -friction/2 - sqd; - std::complex e1 = std::exp(h1*dt); - std::complex e2 = std::exp(h2*dt); + std::complex h1 = -friction / 2 + sqd; + std::complex h2 = -friction / 2 - sqd; + std::complex e1 = std::exp(h1 * dt); + std::complex e2 = std::exp(h2 * dt); std::complex g1 = (dydt - h2 * y) / (h1 - h2); std::complex g2 = (dydt - h1 * y) / (h2 - h1); - *y_ = (g1*e1 + g2*e2).real(); // .imag() should be 0. - *dydt_ = (g1*h1*e1 + g2*h2*e2).real(); // .imag() should be 0. + *y_ = (g1 * e1 + g2 * e2).real(); // .imag() should be 0. + *dydt_ = (g1 * h1 * e1 + g2 * h2 * e2).real(); // .imag() should be 0. } // Windows unfortunately appears to do this, so do this too for compatibility... diff --git a/lib/framework/physfs_ext.h b/lib/framework/physfs_ext.h index 5034abdf59d..3cc0c919b00 100644 --- a/lib/framework/physfs_ext.h +++ b/lib/framework/physfs_ext.h @@ -28,52 +28,53 @@ #define PHYSFS_APPEND 1 #define PHYSFS_PREPEND 0 -static inline bool PHYSFS_writeSLE8(PHYSFS_file* file, int8_t val) +static inline bool PHYSFS_writeSLE8(PHYSFS_file *file, int8_t val) { return (PHYSFS_write(file, &val, sizeof(int8_t), 1) == 1); } -static inline bool PHYSFS_writeULE8(PHYSFS_file* file, uint8_t val) +static inline bool PHYSFS_writeULE8(PHYSFS_file *file, uint8_t val) { return (PHYSFS_write(file, &val, sizeof(uint8_t), 1) == 1); } -static inline bool PHYSFS_readSLE8(PHYSFS_file* file, int8_t* val) +static inline bool PHYSFS_readSLE8(PHYSFS_file *file, int8_t *val) { return (PHYSFS_read(file, val, sizeof(int8_t), 1) == 1); } -static inline bool PHYSFS_readULE8(PHYSFS_file* file, uint8_t* val) +static inline bool PHYSFS_readULE8(PHYSFS_file *file, uint8_t *val) { return (PHYSFS_read(file, val, sizeof(uint8_t), 1) == 1); } -static inline bool PHYSFS_writeSBE8(PHYSFS_file* file, int8_t val) +static inline bool PHYSFS_writeSBE8(PHYSFS_file *file, int8_t val) { return (PHYSFS_write(file, &val, sizeof(int8_t), 1) == 1); } -static inline bool PHYSFS_writeUBE8(PHYSFS_file* file, uint8_t val) +static inline bool PHYSFS_writeUBE8(PHYSFS_file *file, uint8_t val) { return (PHYSFS_write(file, &val, sizeof(uint8_t), 1) == 1); } -static inline bool PHYSFS_readSBE8(PHYSFS_file* file, int8_t* val) +static inline bool PHYSFS_readSBE8(PHYSFS_file *file, int8_t *val) { return (PHYSFS_read(file, val, sizeof(int8_t), 1) == 1); } -static inline bool PHYSFS_readUBE8(PHYSFS_file* file, uint8_t* val) +static inline bool PHYSFS_readUBE8(PHYSFS_file *file, uint8_t *val) { return (PHYSFS_read(file, val, sizeof(uint8_t), 1) == 1); } -static inline bool PHYSFS_writeBEFloat(PHYSFS_file* file, float val) +static inline bool PHYSFS_writeBEFloat(PHYSFS_file *file, float val) { // For the purpose of endian conversions a IEEE754 float can be considered // the same to a 32bit integer. // We're using a union here to prevent type punning of pointers. - union { + union + { float f; uint32_t i; } writeValue; @@ -81,11 +82,11 @@ static inline bool PHYSFS_writeBEFloat(PHYSFS_file* file, float val) return (PHYSFS_writeUBE32(file, writeValue.i) != 0); } -static inline bool PHYSFS_readBEFloat(PHYSFS_file* file, float* val) +static inline bool PHYSFS_readBEFloat(PHYSFS_file *file, float *val) { // For the purpose of endian conversions a IEEE754 float can be considered // the same to a 32bit integer. - uint32_t* readValue = (uint32_t*)val; + uint32_t *readValue = (uint32_t *)val; return (PHYSFS_readUBE32(file, readValue) != 0); } @@ -126,12 +127,13 @@ static inline char *PHYSFS_fgets(char *s, int size, PHYSFS_file *stream) break; } s[i++] = c; - } while (c != '\n' && i < size - 1); + } + while (c != '\n' && i < size - 1); s[i] = '\0'; // Success conditions if (retval == 1 // Read maximum chars or newline - || (i != 0 && PHYSFS_eof(stream) != 0)) /* Read something and stopped at EOF + || (i != 0 && PHYSFS_eof(stream) != 0)) /* Read something and stopped at EOF * (note: i!=0 *should* be redundant) */ { return s; diff --git a/lib/framework/rational.h b/lib/framework/rational.h index bc91b692a3d..b4f88caba70 100644 --- a/lib/framework/rational.h +++ b/lib/framework/rational.h @@ -45,27 +45,78 @@ struct Rational Rational(int numerator, int denominator = 1) { int g = gcd(numerator, denominator); - g *= denominator > 0? 1 : -1; - n = numerator/g; - d = denominator/g; - } - bool operator ==(Rational const &b) const { return n*b.d == b.n*d; } - bool operator !=(Rational const &b) const { return n*b.d != b.n*d; } - bool operator < (Rational const &b) const { return n*b.d < b.n*d; } - bool operator <=(Rational const &b) const { return n*b.d <= b.n*d; } - bool operator > (Rational const &b) const { return n*b.d > b.n*d; } - bool operator >=(Rational const &b) const { return n*b.d >= b.n*d; } - Rational operator +(Rational const &b) const { return Rational(n*b.d + b.n*d, d*b.d); } - Rational operator -(Rational const &b) const { return Rational(n*b.d - b.n*d, d*b.d); } - Rational operator *(Rational const &b) const { return Rational(n*b.n, d*b.d); } - Rational operator /(Rational const &b) const { return Rational(n*b.d, d*b.n); } - Rational operator -() const { return Rational(-n, d); } - Rational &operator +=(Rational const &b) { return *this = *this + b; } - Rational &operator -=(Rational const &b) { return *this = *this - b; } - Rational &operator *=(Rational const &b) { return *this = *this * b; } - Rational &operator /=(Rational const &b) { return *this = *this / b; } - int floor() const { return n >= 0? n/d : (n - (d - 1))/d; } - int ceil() const { return n >= 0? (n + (d - 1))/d : n/d; } + g *= denominator > 0 ? 1 : -1; + n = numerator / g; + d = denominator / g; + } + bool operator ==(Rational const &b) const + { + return n * b.d == b.n * d; + } + bool operator !=(Rational const &b) const + { + return n * b.d != b.n * d; + } + bool operator < (Rational const &b) const + { + return n * b.d < b.n * d; + } + bool operator <=(Rational const &b) const + { + return n * b.d <= b.n * d; + } + bool operator > (Rational const &b) const + { + return n * b.d > b.n * d; + } + bool operator >=(Rational const &b) const + { + return n * b.d >= b.n * d; + } + Rational operator +(Rational const &b) const + { + return Rational(n * b.d + b.n * d, d * b.d); + } + Rational operator -(Rational const &b) const + { + return Rational(n * b.d - b.n * d, d * b.d); + } + Rational operator *(Rational const &b) const + { + return Rational(n * b.n, d * b.d); + } + Rational operator /(Rational const &b) const + { + return Rational(n * b.d, d * b.n); + } + Rational operator -() const + { + return Rational(-n, d); + } + Rational &operator +=(Rational const &b) + { + return *this = *this + b; + } + Rational &operator -=(Rational const &b) + { + return *this = *this - b; + } + Rational &operator *=(Rational const &b) + { + return *this = *this * b; + } + Rational &operator /=(Rational const &b) + { + return *this = *this / b; + } + int floor() const + { + return n >= 0 ? n / d : (n - (d - 1)) / d; + } + int ceil() const + { + return n >= 0 ? (n + (d - 1)) / d : n / d; + } // If int16_t isn't big enough, the comparison operations might overflow. int16_t n; diff --git a/lib/framework/stdio_ext.cpp b/lib/framework/stdio_ext.cpp index 1b3ce7d5aef..8c8f5a8b551 100644 --- a/lib/framework/stdio_ext.cpp +++ b/lib/framework/stdio_ext.cpp @@ -25,16 +25,16 @@ #include #if defined(WZ_CC_MSVC) -int vslcatprintf(char* str, size_t size, const char* format, va_list ap) +int vslcatprintf(char *str, size_t size, const char *format, va_list ap) { size_t str_len; if (str == NULL - || size == 0) + || size == 0) { return vsnprintf(NULL, 0, format, ap); } - + str_len = strlen(str); assert(str_len < size); @@ -43,20 +43,20 @@ int vslcatprintf(char* str, size_t size, const char* format, va_list ap) } -int slcatprintf(char* str, size_t size, const char* format, ...) +int slcatprintf(char *str, size_t size, const char *format, ...) { va_list ap; int count; va_start(ap, format); - count = vslcatprintf(str, size, format, ap); + count = vslcatprintf(str, size, format, ap); va_end(ap); return count; } -int vasprintf(char** strp, const char* format, va_list ap) +int vasprintf(char **strp, const char *format, va_list ap) { int count; va_list aq; @@ -94,13 +94,13 @@ int vasprintf(char** strp, const char* format, va_list ap) } -int asprintf(char** strp, const char* format, ...) +int asprintf(char **strp, const char *format, ...) { va_list ap; int count; va_start(ap, format); - count = vasprintf(strp, format, ap); + count = vasprintf(strp, format, ap); va_end(ap); return count; @@ -109,7 +109,7 @@ int asprintf(char** strp, const char* format, ...) #if defined(WZ_CC_MSVC) -int wz_vsnprintf(char* str, size_t size, const char* format, va_list ap) +int wz_vsnprintf(char *str, size_t size, const char *format, va_list ap) { int count; va_list aq; @@ -121,7 +121,7 @@ int wz_vsnprintf(char* str, size_t size, const char* format, va_list ap) va_end(aq); if (count >= 0 - && str != NULL) + && str != NULL) { // Perfrom the actual string formatting _vsnprintf_s(str, size, _TRUNCATE, format, ap); @@ -132,36 +132,38 @@ int wz_vsnprintf(char* str, size_t size, const char* format, va_list ap) } -int wz_snprintf(char* str, size_t size, const char* format, ...) +int wz_snprintf(char *str, size_t size, const char *format, ...) { va_list ap; int count; va_start(ap, format); - count = vsnprintf(str, size, format, ap); + count = vsnprintf(str, size, format, ap); va_end(ap); return count; } #endif -int vasprintfNull(char** strp, const char* format, va_list ap) +int vasprintfNull(char **strp, const char *format, va_list ap) { int count = vasprintf(strp, format, ap); if (count == -1) // If count == -1, strp is currently undefined. + { strp = NULL; + } return count; } -int asprintfNull(char** strp, const char* format, ...) +int asprintfNull(char **strp, const char *format, ...) { va_list ap; int count; va_start(ap, format); - count = vasprintfNull(strp, format, ap); + count = vasprintfNull(strp, format, ap); va_end(ap); return count; diff --git a/lib/framework/stdio_ext.h b/lib/framework/stdio_ext.h index 7f95bbd5f90..2d42bd5c2ed 100644 --- a/lib/framework/stdio_ext.h +++ b/lib/framework/stdio_ext.h @@ -35,14 +35,14 @@ * string * \return the amount of characters appended to the string */ -extern int vslcatprintf(char* str, size_t size, const char* format, va_list ap); +extern int vslcatprintf(char *str, size_t size, const char *format, va_list ap); /** A variant on snprintf which appends its output string to the given string * The function's interface is similar to vslcatprintf(), so look at that * function's description. */ -extern int slcatprintf(char* str, size_t size, const char* format, ...) WZ_DECL_FORMAT(printf, 3, 4); +extern int slcatprintf(char *str, size_t size, const char *format, ...) WZ_DECL_FORMAT(printf, 3, 4); // These functions are GNU extensions; so make sure they are available on Windows also @@ -66,7 +66,7 @@ extern int slcatprintf(char* str, size_t size, const char* format, ...) WZ_DECL_ * vsprintf. If memory allocation wasn't possible or some other error * occurred, -1 is returned. */ -extern int vasprintf(char** strp, const char* format, va_list ap); +extern int vasprintf(char **strp, const char *format, va_list ap); /** @@ -75,14 +75,14 @@ extern int vasprintf(char** strp, const char* format, va_list ap); * * @see vasprintf() */ -extern int asprintf(char** strp, const char* format, ...) WZ_DECL_FORMAT(printf, 2, 3); +extern int asprintf(char **strp, const char *format, ...) WZ_DECL_FORMAT(printf, 2, 3); #endif #if defined(WZ_CC_MSVC) // Make sure that these functions are available, and work according to the C99 spec on MSVC also -extern int wz_vsnprintf(char* str, size_t size, const char* format, va_list ap); -extern int wz_snprintf(char* str, size_t size, const char* format, ...); +extern int wz_vsnprintf(char *str, size_t size, const char *format, va_list ap); +extern int wz_snprintf(char *str, size_t size, const char *format, ...); // Necessary to prevent conflicting symbols with MSVC's own (incorrect!) implementations of these functions # define vsnprintf wz_vsnprintf @@ -96,24 +96,24 @@ extern int wz_snprintf(char* str, size_t size, const char* format, ...); // A stack-allocating variant of sprintf #define sasprintf(strp, format, ...) \ -do { \ - /* Make sure to evaluate "format" just once */ \ - const char* fmt = format; \ - /* Determine the size of the string we're going to produce */ \ - size_t size = snprintf(NULL, 0, fmt, __VA_ARGS__); \ - \ - /* Let the compiler perform some static type-checking */ \ - char** var = strp; \ - \ - /* Allocate a buffer large enough to hold our string on the stack*/ \ - *var = (char*)alloca(size + 1); \ - /* Print into our newly created string-buffer */ \ - sprintf(*var, fmt, __VA_ARGS__); \ -} while(0) + do { \ + /* Make sure to evaluate "format" just once */ \ + const char* fmt = format; \ + /* Determine the size of the string we're going to produce */ \ + size_t size = snprintf(NULL, 0, fmt, __VA_ARGS__); \ + \ + /* Let the compiler perform some static type-checking */ \ + char** var = strp; \ + \ + /* Allocate a buffer large enough to hold our string on the stack*/ \ + *var = (char*)alloca(size + 1); \ + /* Print into our newly created string-buffer */ \ + sprintf(*var, fmt, __VA_ARGS__); \ + } while(0) /// Equivalent to vasprintf, except that strp is NULL instead of undefined, if the function returns -1. Does not give compiler warnings/-Werrors if not checking the return value. -int vasprintfNull(char** strp, const char* format, va_list ap); +int vasprintfNull(char **strp, const char *format, va_list ap); /// Equivalent to asprintf, except that strp is NULL instead of undefined, if the function returns -1. Does not give compiler warnings/-Werrors if not checking the return value. -int asprintfNull(char** strp, const char* format, ...); +int asprintfNull(char **strp, const char *format, ...); #endif // STDIO_EXT_H diff --git a/lib/framework/string_ext.h b/lib/framework/string_ext.h index 7ccef4aa2f4..4f8e0ce15cc 100644 --- a/lib/framework/string_ext.h +++ b/lib/framework/string_ext.h @@ -38,19 +38,21 @@ # endif # if defined(DEBUG) # define strdup(s) \ - strdup2(s,__FILE__,__LINE__) + strdup2(s,__FILE__,__LINE__) static inline char *strdup2(const char *s, char *fileName, int line) { char *result; (void)debug_MEMCHKOFF(); - result = (char*)malloc(strlen(s) + 1); + result = (char *)malloc(strlen(s) + 1); (void)debug_MEMCHKON(); - if (result == (char*)0) - return (char*)0; - strcpy(result, s); - return result; + if (result == (char *)0) + { + return (char *)0; + } + strcpy(result, s); + return result; } # else // for release builds # define strdup _strdup @@ -68,21 +70,25 @@ static inline char *strdup2(const char *s, char *fileName, int line) * \note This is the same as strnlen(string, maxlen - 1) + 1 when using the * GNU C library. */ -static inline size_t strnlen1(const char* string, size_t maxlen) +static inline size_t strnlen1(const char *string, size_t maxlen) { // Find the first NUL char - const char* end = (const char*)memchr(string, '\0', maxlen); // Cast required for C++ + const char *end = (const char *)memchr(string, '\0', maxlen); // Cast required for C++ if (end != NULL) + { return end - string + 1; + } else + { return maxlen; + } } #ifndef HAVE_VALID_STRLCPY # ifdef HAVE_SYSTEM_STRLCPY - // If the system provides a non-conformant strlcpy we use our own +// If the system provides a non-conformant strlcpy we use our own # ifdef strlcpy # undef strlcpy # endif @@ -116,7 +122,7 @@ static inline size_t strlcpy(char *dest, const char *src, size_t size) #ifndef HAVE_VALID_STRLCAT # ifdef HAVE_SYSTEM_STRLCAT - // If the system provides a non-conformant strlcat we use our own +// If the system provides a non-conformant strlcat we use our own # ifdef strlcat # undef strlcat # endif @@ -151,7 +157,7 @@ static inline size_t strlcat(char *dest, const char *src, size_t size) assert(dlen > 0); - strlcpy(&dest[dlen-1], src, size - dlen); + strlcpy(&dest[dlen - 1], src, size - dlen); } return len; diff --git a/lib/framework/strres.cpp b/lib/framework/strres.cpp index 36b2fcad028..a2b25947f33 100644 --- a/lib/framework/strres.cpp +++ b/lib/framework/strres.cpp @@ -39,13 +39,13 @@ /* A String Resource */ struct STR_RES { - struct TREAP_NODE** psIDTreap; ///< The treap to store string identifiers + struct TREAP_NODE **psIDTreap; ///< The treap to store string identifiers }; /* Initialise the string system */ -STR_RES* strresCreate() +STR_RES *strresCreate() { - STR_RES* const psRes = (STR_RES*)malloc(sizeof(*psRes)); + STR_RES *const psRes = (STR_RES *)malloc(sizeof(*psRes)); if (!psRes) { debug(LOG_FATAL, "Out of memory"); @@ -75,7 +75,7 @@ void strresDestroy(STR_RES *psRes) /* Store a string */ -bool strresStoreString(STR_RES *psRes, const char* pID, const char* pString) +bool strresStoreString(STR_RES *psRes, const char *pID, const char *pString) { ASSERT(psRes != NULL, "Invalid string res pointer"); @@ -90,14 +90,14 @@ bool strresStoreString(STR_RES *psRes, const char* pID, const char* pString) return treapAdd(psRes->psIDTreap, pID, pString); } -const char* strresGetString(const STR_RES* psRes, const char* ID) +const char *strresGetString(const STR_RES *psRes, const char *ID) { ASSERT(psRes != NULL, "Invalid string resource pointer"); return treapFind(psRes->psIDTreap, ID); } /* Load a string resource file */ -bool strresLoad(STR_RES* psRes, const char* fileName) +bool strresLoad(STR_RES *psRes, const char *fileName) { bool retval; lexerinput_t input; @@ -121,7 +121,7 @@ bool strresLoad(STR_RES* psRes, const char* fileName) } /* Get the ID number for a string*/ -const char* strresGetIDfromString(STR_RES *psRes, const char *pString) +const char *strresGetIDfromString(STR_RES *psRes, const char *pString) { ASSERT(psRes != NULL, "Invalid string res pointer"); diff --git a/lib/framework/strres.h b/lib/framework/strres.h index e4fe001876d..19dad970195 100644 --- a/lib/framework/strres.h +++ b/lib/framework/strres.h @@ -27,7 +27,7 @@ struct STR_RES; /* Create a string resource object */ -extern struct STR_RES* strresCreate(void); +extern struct STR_RES *strresCreate(void); /* Release a string resource object */ extern void strresDestroy(struct STR_RES *psRes); @@ -40,12 +40,12 @@ extern void strresDestroy(struct STR_RES *psRes); * @return the string associated with the given @c ID string, or NULL if none * could be found. */ -extern const char* strresGetString(const struct STR_RES* psRes, const char* ID); +extern const char *strresGetString(const struct STR_RES *psRes, const char *ID); /* Load a string resource file */ -extern bool strresLoad(struct STR_RES* psRes, const char* fileName); +extern bool strresLoad(struct STR_RES *psRes, const char *fileName); /* Get the ID string for a string */ -extern const char* strresGetIDfromString(struct STR_RES *psRes, const char *pString); +extern const char *strresGetIDfromString(struct STR_RES *psRes, const char *pString); #endif diff --git a/lib/framework/strresly.h b/lib/framework/strresly.h index 1c6065df87b..9c9aa7ada26 100644 --- a/lib/framework/strresly.h +++ b/lib/framework/strresly.h @@ -29,14 +29,14 @@ extern void strres_set_extra(YY_EXTRA_TYPE user_defined); /* Call the yacc parser */ -extern int strres_parse(void* psStrRes); +extern int strres_parse(void *psStrRes); /* Destroy the lexer */ extern int strres_lex_destroy(void); -void strres_error(const char* msg); +void strres_error(const char *msg); /* Store a string */ -extern bool strresStoreString(struct STR_RES *psRes, const char* pID, const char* pString); +extern bool strresStoreString(struct STR_RES *psRes, const char *pID, const char *pString); #endif diff --git a/lib/framework/treap.cpp b/lib/framework/treap.cpp index 647ee47e15e..37ea23023d2 100644 --- a/lib/framework/treap.cpp +++ b/lib/framework/treap.cpp @@ -36,9 +36,9 @@ struct TREAP_NODE { - const char* key; //< The key to sort the node on + const char *key; //< The key to sort the node on unsigned int priority; //< Treap priority - const char* string; //< The string stored in the treap + const char *string; //< The string stored in the treap TREAP_NODE *psLeft, *psRight; //< The sub trees }; @@ -48,18 +48,24 @@ static int treapStringCmp(const char *key1, const char *key2) int result; result = strcmp(key1, key2); - if (result < 0) + if (result < 0) + { return -1; + } else if (result > 0) + { return 1; + } else + { return 0; + } } -TREAP_NODE** treapCreate() +TREAP_NODE **treapCreate() { - TREAP_NODE** const psTreap = (TREAP_NODE**)malloc(sizeof(*psTreap)); + TREAP_NODE **const psTreap = (TREAP_NODE **)malloc(sizeof(*psTreap)); if (!psTreap) { debug(LOG_FATAL, "Out of memory"); @@ -136,7 +142,7 @@ static void treapAddNode(TREAP_NODE **ppsRoot, TREAP_NODE *psNew) /* Add an object to a treap */ -bool treapAdd(TREAP_NODE** psTreap, const char* key, const char* string) +bool treapAdd(TREAP_NODE **psTreap, const char *key, const char *string) { /* Over-allocate so that we can put the key and the string in the same * chunck of memory as the TREAP_NODE struct. Which means a single @@ -144,7 +150,7 @@ bool treapAdd(TREAP_NODE** psTreap, const char* key, const char* string) */ const size_t key_size = strlen(key) + 1; const size_t string_size = strlen(string) + 1; - TREAP_NODE * const psNew = (TREAP_NODE *)malloc(sizeof(*psNew) + key_size + string_size); + TREAP_NODE *const psNew = (TREAP_NODE *)malloc(sizeof(*psNew) + key_size + string_size); if (psNew == NULL) { @@ -153,8 +159,8 @@ bool treapAdd(TREAP_NODE** psTreap, const char* key, const char* string) return false; } - psNew->key = strcpy((char*)(psNew + 1), key); - psNew->string = strcpy((char*)(psNew + 1) + key_size, string); + psNew->key = strcpy((char *)(psNew + 1), key); + psNew->string = strcpy((char *)(psNew + 1) + key_size, string); psNew->priority = rand(); psNew->psLeft = NULL; @@ -167,7 +173,7 @@ bool treapAdd(TREAP_NODE** psTreap, const char* key, const char* string) /* Recursively find an object in a treap */ -static const char* treapFindRec(TREAP_NODE *psRoot, const char *key) +static const char *treapFindRec(TREAP_NODE *psRoot, const char *key) { if (psRoot == NULL) { @@ -187,7 +193,7 @@ static const char* treapFindRec(TREAP_NODE *psRoot, const char *key) return treapFindRec(psRoot->psRight, key); break; default: - ASSERT( false, "treapFindRec: invalid return from comparison" ); + ASSERT(false, "treapFindRec: invalid return from comparison"); break; } return NULL; @@ -195,16 +201,16 @@ static const char* treapFindRec(TREAP_NODE *psRoot, const char *key) /* Find an object in a treap */ -const char* treapFind(TREAP_NODE** psTreap, const char *key) +const char *treapFind(TREAP_NODE **psTreap, const char *key) { ASSERT(psTreap != NULL, "Invalid treap pointer!"); return treapFindRec(*psTreap, key); } -static const char* treapFindKeyRec(TREAP_NODE const * const psNode, const char * const string) +static const char *treapFindKeyRec(TREAP_NODE const *const psNode, const char *const string) { - const char* key; + const char *key; if (psNode == NULL) { @@ -225,7 +231,7 @@ static const char* treapFindKeyRec(TREAP_NODE const * const psNode, const char * return treapFindKeyRec(psNode->psRight, string); } -const char* treapFindKey(TREAP_NODE** psTreap, const char* string) +const char *treapFindKey(TREAP_NODE **psTreap, const char *string) { ASSERT(psTreap != NULL, "Invalid treap pointer!"); @@ -250,7 +256,7 @@ static void treapDestroyRec(TREAP_NODE *psRoot) /* Destroy a treap and release all the memory associated with it */ -void treapDestroy(TREAP_NODE** psTreap) +void treapDestroy(TREAP_NODE **psTreap) { ASSERT(psTreap != NULL, "Invalid treap pointer!"); diff --git a/lib/framework/treap.h b/lib/framework/treap.h index f0982ca9620..1e776382888 100644 --- a/lib/framework/treap.h +++ b/lib/framework/treap.h @@ -41,19 +41,19 @@ struct TREAP_NODE; * \return On success a non-NULL pointer, which holds the created treap. On * failure NULL is returned. */ -extern struct TREAP_NODE** treapCreate(void); +extern struct TREAP_NODE **treapCreate(void); /* Add an object to a treap */ -extern bool treapAdd(struct TREAP_NODE** psTreap, const char *key, const char* string); +extern bool treapAdd(struct TREAP_NODE **psTreap, const char *key, const char *string); /* Find an object in a treap */ -extern const char* treapFind(struct TREAP_NODE** psTreap, const char *key); +extern const char *treapFind(struct TREAP_NODE **psTreap, const char *key); /** Find the key associated with the given string number */ -extern const char* treapFindKey(struct TREAP_NODE** psTreap, const char* string); +extern const char *treapFindKey(struct TREAP_NODE **psTreap, const char *string); /* Destroy a treap and release all the memory associated with it */ -extern void treapDestroy(struct TREAP_NODE** psTreap); +extern void treapDestroy(struct TREAP_NODE **psTreap); #endif diff --git a/lib/framework/trig.cpp b/lib/framework/trig.cpp index 42bd1ba764c..24a9c8eb58f 100644 --- a/lib/framework/trig.cpp +++ b/lib/framework/trig.cpp @@ -35,7 +35,7 @@ #include // for M_PI #if defined(WZ_CC_MSVC) - #define _USE_MATH_DEFINES +#define _USE_MATH_DEFINES #endif #include @@ -51,35 +51,35 @@ bool trigInitialise(void) uint32_t i; // Generate tables. - STATIC_ASSERT(sizeof(trigSinTable)/sizeof(*trigSinTable) == 0x4001); + STATIC_ASSERT(sizeof(trigSinTable) / sizeof(*trigSinTable) == 0x4001); for (i = 0; i != 0x4001; ++i) { - trigSinTable[i] = (int)(0x10000*sin(i*(M_PI/0x8000)) + 0.5) - !!i; // -!!i = subtract 1, unless i == 0. + trigSinTable[i] = (int)(0x10000 * sin(i * (M_PI / 0x8000)) + 0.5) - !!i; // -!!i = subtract 1, unless i == 0. } - STATIC_ASSERT(sizeof(trigAtanTable)/sizeof(*trigAtanTable) == 0x2001); + STATIC_ASSERT(sizeof(trigAtanTable) / sizeof(*trigAtanTable) == 0x2001); for (i = 0; i != 0x2001; ++i) { - trigAtanTable[i] = (int)(0x8000/M_PI*atan((double)i/0x2000) + 0.5); + trigAtanTable[i] = (int)(0x8000 / M_PI * atan((double)i / 0x2000) + 0.5); } // Check tables are correct. - crc = ~crcSumU16(0, trigSinTable, sizeof(trigSinTable)/sizeof(*trigSinTable)); + crc = ~crcSumU16(0, trigSinTable, sizeof(trigSinTable) / sizeof(*trigSinTable)); ASSERT(crc == 0x6D3C8DB5, "Bad trigSinTable CRC = 0x%08X, sin function is broken.", crc); - crc = ~crcSumU16(0, trigAtanTable, sizeof(trigAtanTable)/sizeof(*trigAtanTable)); + crc = ~crcSumU16(0, trigAtanTable, sizeof(trigAtanTable) / sizeof(*trigAtanTable)); ASSERT(crc == 0xD2797F85, "Bad trigAtanTable CRC = 0x%08X, atan function is broken.", crc); // Test large and small square roots. for (test = 0x0000; test != 0x10000; ++test) { - uint64_t lower = test*test; - uint64_t upper = (test + 1)*(test + 1) - 1; + uint64_t lower = test * test; + uint64_t upper = (test + 1) * (test + 1) - 1; ASSERT((uint32_t)iSqrt(lower) == test, "Sanity check failed, sqrt(%" PRIu64") gave %" PRIu32" instead of %" PRIu64"!", lower, i64Sqrt(lower), test); ASSERT((uint32_t)iSqrt(upper) == test, "Sanity check failed, sqrt(%" PRIu64") gave %" PRIu32" instead of %" PRIu64"!", upper, i64Sqrt(upper), test); } - for (test = 0xFFFE0000; test != 0x00020000; test = (test + 1)&0xFFFFFFFF) + for (test = 0xFFFE0000; test != 0x00020000; test = (test + 1) & 0xFFFFFFFF) { - uint64_t lower = test*test; - uint64_t upper = (test + 1)*(test + 1) - 1; + uint64_t lower = test * test; + uint64_t upper = (test + 1) * (test + 1) - 1; ASSERT((uint32_t)i64Sqrt(lower) == test, "Sanity check failed, sqrt(%" PRIu64") gave %" PRIu32" instead of %" PRIu64"!", lower, i64Sqrt(lower), test); ASSERT((uint32_t)i64Sqrt(upper) == test, "Sanity check failed, sqrt(%" PRIu64") gave %" PRIu32" instead of %" PRIu64"!", upper, i64Sqrt(upper), test); } @@ -91,40 +91,40 @@ int32_t iSin(uint16_t a) { int sign[4] = {1, 1, -1, -1}; bool reverse[4] = {false, true, false, true}; - int q = a>>14; - uint16_t r = a&0x3FFF; + int q = a >> 14; + uint16_t r = a & 0x3FFF; uint16_t rvr = reverse[q] ? 0x4000 - r : r; - return sign[q]*(trigSinTable[rvr] + !!rvr); // +!!rvr = add 1, unless rvr == 0. + return sign[q] * (trigSinTable[rvr] + !!rvr); // +!!rvr = add 1, unless rvr == 0. } int32_t iCos(uint16_t a) { int sign[4] = {1, -1, -1, 1}; bool reverse[4] = {true, false, true, false}; - int q = a>>14; - uint16_t r = a&0x3FFF; + int q = a >> 14; + uint16_t r = a & 0x3FFF; uint16_t rvr = reverse[q] ? 0x4000 - r : r; - return sign[q]*(trigSinTable[rvr] + !!rvr); // +!!rvr = add 1, unless rvr == 0. + return sign[q] * (trigSinTable[rvr] + !!rvr); // +!!rvr = add 1, unless rvr == 0. } int32_t iSinR(uint16_t a, int32_t r) { - return ((int64_t)r*iSin(a))/65536; + return ((int64_t)r * iSin(a)) / 65536; } int32_t iCosR(uint16_t a, int32_t r) { - return ((int64_t)r*iCos(a))/65536; + return ((int64_t)r * iCos(a)) / 65536; } int32_t iSinSR(int32_t a, int32_t s, int32_t r) { - return ((int64_t)r*iSin(((int64_t)a << 16)/s))/65536; + return ((int64_t)r * iSin(((int64_t)a << 16) / s)) / 65536; } int32_t iCosSR(int32_t a, int32_t s, int32_t r) { - return ((int64_t)r*iCos(((int64_t)a << 16)/s))/65536; + return ((int64_t)r * iCos(((int64_t)a << 16) / s)) / 65536; } uint16_t iAtan2(int32_t s, int32_t c) @@ -137,15 +137,15 @@ uint16_t iAtan2(int32_t s, int32_t c) return 0; // All return values equally valid, don't divide by 0. } - switch (((s < 0)<<1) + (c < 0)) + switch (((s < 0) << 1) + (c < 0)) { - case 0: j = s; k = c; d = 0x0000; break; - case 1: j = -c; k = s; d = 0x4000; break; - case 2: j = c; k = -s; d = 0xC000; break; - case 3: j = -s; k = -c; d = 0x8000; break; + case 0: j = s; k = c; d = 0x0000; break; + case 1: j = -c; k = s; d = 0x4000; break; + case 2: j = c; k = -s; d = 0xC000; break; + case 3: j = -s; k = -c; d = 0x8000; break; } - return j < k ? d + trigAtanTable[((int64_t)j*0x2000 + k/2)/k] - : d + 0x4000 - trigAtanTable[((int64_t)k*0x2000 + j/2)/j]; + return j < k ? d + trigAtanTable[((int64_t)j * 0x2000 + k / 2) / k] + : d + 0x4000 - trigAtanTable[((int64_t)k * 0x2000 + j / 2) / j]; } int32_t iSqrt(uint32_t n) @@ -153,7 +153,7 @@ int32_t iSqrt(uint32_t n) uint32_t r = sqrt((double)n); // Calculate square root, rounded down. Excess precision does not change the result. // Check that we got the right result. - ASSERT((int32_t)(r*r - n) <= 0 && (int32_t)((r + 1)*(r + 1) - n) > 0, "Too badly broken sqrt function, iSqrt(%u) = %u.", (unsigned)n, (unsigned)r); + ASSERT((int32_t)(r * r - n) <= 0 && (int32_t)((r + 1) * (r + 1) - n) > 0, "Too badly broken sqrt function, iSqrt(%u) = %u.", (unsigned)n, (unsigned)r); return r; } @@ -171,22 +171,22 @@ int32_t i64Sqrt(uint64_t n) r = sqrtl(n); // Calculate square root, usually rounded down. Excess precision may result in rounding down instead of up, which is fine. } - r -= (int64_t)(r*r - n) > 0; // If we rounded up, subtract 1. + r -= (int64_t)(r * r - n) > 0; // If we rounded up, subtract 1. // Check that we got the right result. - ASSERT((int64_t)(r*r - n) <= 0 && (int64_t)((r + 1)*(r + 1) - n) > 0, "Too badly broken sqrt function, i64Sqrt(%" PRIu64") = %" PRIu64".", n, r); + ASSERT((int64_t)(r * r - n) <= 0 && (int64_t)((r + 1) * (r + 1) - n) > 0, "Too badly broken sqrt function, i64Sqrt(%" PRIu64") = %" PRIu64".", n, r); return r; } int32_t iHypot(int32_t x, int32_t y) { - return i64Sqrt((uint64_t)x*x + (uint64_t)y*y); // Casting from int32_t to uint64_t does sign extend. + return i64Sqrt((uint64_t)x * x + (uint64_t)y * y); // Casting from int32_t to uint64_t does sign extend. } int32_t iHypot3(int32_t x, int32_t y, int32_t z) { - return i64Sqrt((uint64_t)x*x + (uint64_t)y*y + (uint64_t)z*z); // Casting from int32_t to uint64_t does sign extend. + return i64Sqrt((uint64_t)x * x + (uint64_t)y * y + (uint64_t)z * z); // Casting from int32_t to uint64_t does sign extend. } // For testing above functions. @@ -196,9 +196,9 @@ int main() srand(time(NULL)); for (unsigned i = 0; i != 1000000; ++i) { - int32_t s = rand()%2 ? rand() + rand() + rand() + rand() : rand()%101 - 50; - int32_t c = rand()%2 ? rand() + rand() + rand() + rand() : rand()%101 - 50; - int a1 = uint16_t(round(32768/M_PI*atan2(s, c))), a2 = iAtan2(s, c); + int32_t s = rand() % 2 ? rand() + rand() + rand() + rand() : rand() % 101 - 50; + int32_t c = rand() % 2 ? rand() + rand() + rand() + rand() : rand() % 101 - 50; + int a1 = uint16_t(round(32768 / M_PI * atan2(s, c))), a2 = iAtan2(s, c); if (a1 + 1 < a2 || a1 > a2 + 1) { std::printf("32768/π atan2(%d, %d) = %d, iAtan2() = %d\n", s, c, a1, a2); @@ -206,8 +206,8 @@ int main() } for (unsigned a = 0; a < 65536; ++a) { - int s1 = round(65536*sin(a*M_PI/32768)), s2 = iSin(a); - int c1 = round(65536*cos(a*M_PI/32768)), c2 = iCos(a); + int s1 = round(65536 * sin(a * M_PI / 32768)), s2 = iSin(a); + int c1 = round(65536 * cos(a * M_PI / 32768)), c2 = iCos(a); if (s1 != s2 || c1 != c2) { std::printf("a = %d, sin = %d, %d, cos = %d, %d\n", a, s1, s2, c1, c2); diff --git a/lib/framework/trig.h b/lib/framework/trig.h index 3fcb0d9c181..f45021df054 100644 --- a/lib/framework/trig.h +++ b/lib/framework/trig.h @@ -17,7 +17,7 @@ along with Warzone 2100; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -/** +/** * @file * * Interface to trig lookup tables. diff --git a/lib/framework/utf.cpp b/lib/framework/utf.cpp index 1c9995ccbb0..2095ae85c36 100644 --- a/lib/framework/utf.cpp +++ b/lib/framework/utf.cpp @@ -61,7 +61,7 @@ utf_32_char UTF8DecodeChar(const char *utf8_char, const char **next_char) ASSERT_START_OCTECT(*utf8_char); // first octect: 0xxxxxxx: 7 bit (ASCII) - if ((*utf8_char & 0x80) == 0x00) + if ((*utf8_char & 0x80) == 0x00) { // 1 byte long encoding decoded = *((*next_char)++); @@ -141,17 +141,25 @@ size_t UTF16CharacterCount(const uint16_t *utf16) static size_t unicode_utf8_char_length(const utf_32_char unicode_char) { // an ASCII character, which uses 7 bit at most, which is one byte in UTF-8 - if (unicode_char < 0x00000080) - return 1; // stores 7 bits + if (unicode_char < 0x00000080) + { + return 1; // stores 7 bits + } else if (unicode_char < 0x00000800) - return 2; // stores 11 bits + { + return 2; // stores 11 bits + } else if (unicode_char < 0x00010000) - return 3; // stores 16 bits + { + return 3; // stores 16 bits + } /* This encoder can deal with < 0x00200000, but Unicode only ranges * from 0x0 to 0x10FFFF. Thus we don't accept anything else. */ else if (unicode_char < 0x00110000) - return 4; // stores 21 bits + { + return 4; // stores 21 bits + } /* Apparently this character lies outside the 0x0 - 0x10FFFF * Unicode range, so don't accept it. @@ -165,7 +173,7 @@ static size_t unicode_utf8_char_length(const utf_32_char unicode_char) char *UTF8CharacterAtOffset(const char *utf8_string, size_t index) { while (*utf8_string != '\0' - && index != 0) + && index != 0) { // Move to the next character UTF8DecodeChar(utf8_string, &utf8_string); @@ -174,9 +182,11 @@ char *UTF8CharacterAtOffset(const char *utf8_string, size_t index) } if (*utf8_string == '\0') + { return NULL; + } - return (char*)utf8_string; + return (char *)utf8_string; } /** Encodes a single Unicode character to a UTF-8 encoded string. @@ -196,7 +206,7 @@ static char *encode_utf8_char(const utf_32_char unicode_char, char *out_char) char *next_char = out_char; // 7 bits - if (unicode_char < 0x00000080) + if (unicode_char < 0x00000080) { *(next_char++) = unicode_char; } @@ -251,7 +261,7 @@ utf_32_char UTF16DecodeChar(const utf_16_char *utf16_char, const utf_16_char **n // Are we dealing with a surrogate pair if (*utf16_char >= 0xD800 - && *utf16_char <= 0xDFFF) + && *utf16_char <= 0xDFFF) { ASSERT_START_HEXADECT(utf16_char[0]); ASSERT_FINAL_HEXADECT(utf16_char[1]); @@ -287,7 +297,7 @@ static utf_16_char *encode_utf16_char(const utf_32_char unicode_char, utf_16_cha utf_16_char *next_char = out_char; // 16 bits - if (unicode_char < 0x10000) + if (unicode_char < 0x10000) { *(next_char++) = unicode_char; } @@ -313,9 +323,9 @@ static utf_16_char *encode_utf16_char(const utf_32_char unicode_char, utf_16_cha return next_char; } -static size_t utf16_utf8_buffer_length(const utf_16_char* unicode_string) +static size_t utf16_utf8_buffer_length(const utf_16_char *unicode_string) { - const utf_16_char* curChar = unicode_string; + const utf_16_char *curChar = unicode_string; // Determine length of string (in octets) when encoded in UTF-8 size_t length = 0; @@ -330,13 +340,13 @@ static size_t utf16_utf8_buffer_length(const utf_16_char* unicode_string) char *UTF16toUTF8(const utf_16_char *unicode_string, size_t *nbytes) { - const utf_16_char* curChar; + const utf_16_char *curChar; const size_t utf8_length = utf16_utf8_buffer_length(unicode_string); // Allocate memory to hold the UTF-8 encoded string (plus a terminating nul char) - char* utf8_string = (char *)malloc(utf8_length + 1); - char* curOutPos = utf8_string; + char *utf8_string = (char *)malloc(utf8_length + 1); + char *curOutPos = utf8_string; if (utf8_string == NULL) { @@ -362,16 +372,16 @@ char *UTF16toUTF8(const utf_16_char *unicode_string, size_t *nbytes) return utf8_string; } -static size_t utf8_as_utf16_buf_size(const char* utf8_string) +static size_t utf8_as_utf16_buf_size(const char *utf8_string) { - const char* curChar = utf8_string; + const char *curChar = utf8_string; size_t length = 0; while (*curChar != '\0') { const utf_32_char unicode_char = UTF8DecodeChar(curChar, &curChar); - if (unicode_char < 0x10000) + if (unicode_char < 0x10000) { length += 1; } @@ -392,14 +402,14 @@ static size_t utf8_as_utf16_buf_size(const char* utf8_string) return length; } -utf_16_char *UTF8toUTF16(const char* utf8_string, size_t *nbytes) +utf_16_char *UTF8toUTF16(const char *utf8_string, size_t *nbytes) { - const char* curChar = utf8_string; + const char *curChar = utf8_string; const size_t unicode_length = utf8_as_utf16_buf_size(utf8_string); // Allocate memory to hold the UTF-16 encoded string (plus a terminating nul) - utf_16_char* unicode_string = (utf_16_char *)malloc(sizeof(utf_16_char) * (unicode_length + 1)); - utf_16_char* curOutPos = unicode_string; + utf_16_char *unicode_string = (utf_16_char *)malloc(sizeof(utf_16_char) * (unicode_length + 1)); + utf_16_char *curOutPos = unicode_string; if (unicode_string == NULL) { @@ -427,7 +437,7 @@ utf_16_char *UTF8toUTF16(const char* utf8_string, size_t *nbytes) utf_16_char *UTF16CharacterAtOffset(const utf_16_char *utf16_string, size_t index) { while (*utf16_string != '\0' - && index != 0) + && index != 0) { // Move to the next character UTF16DecodeChar(utf16_string, &utf16_string); @@ -436,15 +446,17 @@ utf_16_char *UTF16CharacterAtOffset(const utf_16_char *utf16_string, size_t inde } if (*utf16_string == '\0') + { return NULL; + } - return (utf_16_char*)utf16_string; + return (utf_16_char *)utf16_string; } -static size_t utf32_utf8_buffer_length(const utf_32_char* unicode_string) +static size_t utf32_utf8_buffer_length(const utf_32_char *unicode_string) { - const utf_32_char* curChar; + const utf_32_char *curChar; // Determine length of string (in octets) when encoded in UTF-8 size_t length = 0; @@ -458,13 +470,13 @@ static size_t utf32_utf8_buffer_length(const utf_32_char* unicode_string) char *UTF32toUTF8(const utf_32_char *unicode_string, size_t *nbytes) { - const utf_32_char* curChar; + const utf_32_char *curChar; const size_t utf8_length = utf32_utf8_buffer_length(unicode_string); // Allocate memory to hold the UTF-8 encoded string (plus a terminating nul char) - char* utf8_string = (char *)malloc(utf8_length + 1); - char* curOutPos = utf8_string; + char *utf8_string = (char *)malloc(utf8_length + 1); + char *curOutPos = utf8_string; if (utf8_string == NULL) { @@ -491,12 +503,12 @@ char *UTF32toUTF8(const utf_32_char *unicode_string, size_t *nbytes) utf_32_char *UTF8toUTF32(const char *utf8_string, size_t *nbytes) { - const char* curChar = utf8_string; + const char *curChar = utf8_string; const size_t unicode_length = UTF8CharacterCount(utf8_string); // Allocate memory to hold the UTF-32 encoded string (plus a terminating nul) - utf_32_char* unicode_string = (utf_32_char *)malloc(sizeof(utf_32_char) * (unicode_length + 1)); - utf_32_char* curOutPos = unicode_string; + utf_32_char *unicode_string = (utf_32_char *)malloc(sizeof(utf_32_char) * (unicode_length + 1)); + utf_32_char *curOutPos = unicode_string; if (unicode_string == NULL) { diff --git a/lib/framework/vector.h b/lib/framework/vector.h index 0bdf9bd9d9b..5fe8761f8ce 100644 --- a/lib/framework/vector.h +++ b/lib/framework/vector.h @@ -65,7 +65,12 @@ struct Vector3f }; struct Rotation { - Rotation() { direction = 0; pitch = 0; roll = 0; } + Rotation() + { + direction = 0; + pitch = 0; + roll = 0; + } Rotation(int direction, int pitch, int roll) : direction(direction), pitch(pitch), roll(roll) {} Rotation(Vector3i xyz) : direction(xyz.x), pitch(xyz.y), roll(xyz.z) {} uint16_t direction, pitch, roll; ///< Object rotation in 0..64k range @@ -76,97 +81,267 @@ inline Vector3i::Vector3i(Rotation const &r) : x(r.direction), y(r.pitch), z(r.r inline Vector2i::Vector2i(Vector3i const &r) : x(r.x), y(r.y) {} // removeZ(3d_vector) -> 2d_vector -static inline WZ_DECL_PURE Vector2i removeZ(Vector3i const &a) { return Vector2i(a.x, a.y); } -static inline WZ_DECL_PURE Vector2f removeZ(Vector3f const &a) { return Vector2f(a.x, a.y); } +static inline WZ_DECL_PURE Vector2i removeZ(Vector3i const &a) +{ + return Vector2i(a.x, a.y); +} +static inline WZ_DECL_PURE Vector2f removeZ(Vector3f const &a) +{ + return Vector2f(a.x, a.y); +} // vector == vector -> bool -static inline WZ_DECL_PURE bool operator ==(Vector2i const &a, Vector2i const &b) { return a.x == b.x && a.y == b.y; } -static inline WZ_DECL_PURE bool operator ==(Vector2f const &a, Vector2f const &b) { return a.x == b.x && a.y == b.y; } -static inline WZ_DECL_PURE bool operator ==(Vector3i const &a, Vector3i const &b) { return a.x == b.x && a.y == b.y && a.z == b.z; } -static inline WZ_DECL_PURE bool operator ==(Vector3f const &a, Vector3f const &b) { return a.x == b.x && a.y == b.y && a.z == b.z; } -static inline WZ_DECL_PURE bool operator ==(Rotation const &a, Rotation const &b) { return a.direction == b.direction && a.pitch == b.pitch && a.roll == b.roll; } +static inline WZ_DECL_PURE bool operator ==(Vector2i const &a, Vector2i const &b) +{ + return a.x == b.x && a.y == b.y; +} +static inline WZ_DECL_PURE bool operator ==(Vector2f const &a, Vector2f const &b) +{ + return a.x == b.x && a.y == b.y; +} +static inline WZ_DECL_PURE bool operator ==(Vector3i const &a, Vector3i const &b) +{ + return a.x == b.x && a.y == b.y && a.z == b.z; +} +static inline WZ_DECL_PURE bool operator ==(Vector3f const &a, Vector3f const &b) +{ + return a.x == b.x && a.y == b.y && a.z == b.z; +} +static inline WZ_DECL_PURE bool operator ==(Rotation const &a, Rotation const &b) +{ + return a.direction == b.direction && a.pitch == b.pitch && a.roll == b.roll; +} // vector != vector -> bool -static inline WZ_DECL_PURE bool operator !=(Vector2i const &a, Vector2i const &b) { return a.x != b.x || a.y != b.y; } -static inline WZ_DECL_PURE bool operator !=(Vector2f const &a, Vector2f const &b) { return a.x != b.x || a.y != b.y; } -static inline WZ_DECL_PURE bool operator !=(Vector3i const &a, Vector3i const &b) { return a.x != b.x || a.y != b.y || a.z != b.z; } -static inline WZ_DECL_PURE bool operator !=(Vector3f const &a, Vector3f const &b) { return a.x != b.x || a.y != b.y || a.z != b.z; } -static inline WZ_DECL_PURE bool operator !=(Rotation const &a, Rotation const &b) { return a.direction != b.direction || a.pitch != b.pitch || a.roll != b.roll; } +static inline WZ_DECL_PURE bool operator !=(Vector2i const &a, Vector2i const &b) +{ + return a.x != b.x || a.y != b.y; +} +static inline WZ_DECL_PURE bool operator !=(Vector2f const &a, Vector2f const &b) +{ + return a.x != b.x || a.y != b.y; +} +static inline WZ_DECL_PURE bool operator !=(Vector3i const &a, Vector3i const &b) +{ + return a.x != b.x || a.y != b.y || a.z != b.z; +} +static inline WZ_DECL_PURE bool operator !=(Vector3f const &a, Vector3f const &b) +{ + return a.x != b.x || a.y != b.y || a.z != b.z; +} +static inline WZ_DECL_PURE bool operator !=(Rotation const &a, Rotation const &b) +{ + return a.direction != b.direction || a.pitch != b.pitch || a.roll != b.roll; +} // vector + vector -> vector -static inline WZ_DECL_PURE Vector2i operator +(Vector2i const &a, Vector2i const &b) { return Vector2i(a.x + b.x, a.y + b.y); } -static inline WZ_DECL_PURE Vector2f operator +(Vector2f const &a, Vector2f const &b) { return Vector2f(a.x + b.x, a.y + b.y); } -static inline WZ_DECL_PURE Vector3i operator +(Vector3i const &a, Vector3i const &b) { return Vector3i(a.x + b.x, a.y + b.y, a.z + b.z); } -static inline WZ_DECL_PURE Vector3f operator +(Vector3f const &a, Vector3f const &b) { return Vector3f(a.x + b.x, a.y + b.y, a.z + b.z); } +static inline WZ_DECL_PURE Vector2i operator +(Vector2i const &a, Vector2i const &b) +{ + return Vector2i(a.x + b.x, a.y + b.y); +} +static inline WZ_DECL_PURE Vector2f operator +(Vector2f const &a, Vector2f const &b) +{ + return Vector2f(a.x + b.x, a.y + b.y); +} +static inline WZ_DECL_PURE Vector3i operator +(Vector3i const &a, Vector3i const &b) +{ + return Vector3i(a.x + b.x, a.y + b.y, a.z + b.z); +} +static inline WZ_DECL_PURE Vector3f operator +(Vector3f const &a, Vector3f const &b) +{ + return Vector3f(a.x + b.x, a.y + b.y, a.z + b.z); +} //static inline WZ_DECL_PURE Rotation operator +(Rotation const &a, Rotation const &b) { return Rotation((int16_t)a.direction + (int16_t)b.direction, (int16_t)a.pitch + (int16_t)b.pitch, (int16_t)a.roll + (int16_t)b.roll); } // vector - vector -> vector -static inline WZ_DECL_PURE Vector2i operator -(Vector2i const &a, Vector2i const &b) { return Vector2i(a.x - b.x, a.y - b.y); } -static inline WZ_DECL_PURE Vector2f operator -(Vector2f const &a, Vector2f const &b) { return Vector2f(a.x - b.x, a.y - b.y); } -static inline WZ_DECL_PURE Vector3i operator -(Vector3i const &a, Vector3i const &b) { return Vector3i(a.x - b.x, a.y - b.y, a.z - b.z); } -static inline WZ_DECL_PURE Vector3f operator -(Vector3f const &a, Vector3f const &b) { return Vector3f(a.x - b.x, a.y - b.y, a.z - b.z); } +static inline WZ_DECL_PURE Vector2i operator -(Vector2i const &a, Vector2i const &b) +{ + return Vector2i(a.x - b.x, a.y - b.y); +} +static inline WZ_DECL_PURE Vector2f operator -(Vector2f const &a, Vector2f const &b) +{ + return Vector2f(a.x - b.x, a.y - b.y); +} +static inline WZ_DECL_PURE Vector3i operator -(Vector3i const &a, Vector3i const &b) +{ + return Vector3i(a.x - b.x, a.y - b.y, a.z - b.z); +} +static inline WZ_DECL_PURE Vector3f operator -(Vector3f const &a, Vector3f const &b) +{ + return Vector3f(a.x - b.x, a.y - b.y, a.z - b.z); +} //static inline WZ_DECL_PURE Rotation operator -(Rotation const &a, Rotation const &b) { return Rotation((int16_t)a.direction - (int16_t)b.direction, (int16_t)a.pitch - (int16_t)b.pitch, (int16_t)a.roll - (int16_t)b.roll); } // -vector -> vector -static inline WZ_DECL_PURE Vector2i operator -(Vector2i const &a) { return Vector2i(-a.x, -a.y); } -static inline WZ_DECL_PURE Vector2f operator -(Vector2f const &a) { return Vector2f(-a.x, -a.y); } -static inline WZ_DECL_PURE Vector3i operator -(Vector3i const &a) { return Vector3i(-a.x, -a.y, -a.z); } -static inline WZ_DECL_PURE Vector3f operator -(Vector3f const &a) { return Vector3f(-a.x, -a.y, -a.z); } +static inline WZ_DECL_PURE Vector2i operator -(Vector2i const &a) +{ + return Vector2i(-a.x, -a.y); +} +static inline WZ_DECL_PURE Vector2f operator -(Vector2f const &a) +{ + return Vector2f(-a.x, -a.y); +} +static inline WZ_DECL_PURE Vector3i operator -(Vector3i const &a) +{ + return Vector3i(-a.x, -a.y, -a.z); +} +static inline WZ_DECL_PURE Vector3f operator -(Vector3f const &a) +{ + return Vector3f(-a.x, -a.y, -a.z); +} // vector * scalar -> vector -static inline WZ_DECL_PURE Vector2i operator *(Vector2i const &a, int s) { return Vector2i(a.x*s, a.y*s); } -static inline WZ_DECL_PURE Vector2f operator *(Vector2f const &a, float s) { return Vector2f(a.x*s, a.y*s); } -static inline WZ_DECL_PURE Vector3i operator *(Vector3i const &a, int s) { return Vector3i(a.x*s, a.y*s, a.z*s); } -static inline WZ_DECL_PURE Vector3f operator *(Vector3f const &a, float s) { return Vector3f(a.x*s, a.y*s, a.z*s); } +static inline WZ_DECL_PURE Vector2i operator *(Vector2i const &a, int s) +{ + return Vector2i(a.x * s, a.y * s); +} +static inline WZ_DECL_PURE Vector2f operator *(Vector2f const &a, float s) +{ + return Vector2f(a.x * s, a.y * s); +} +static inline WZ_DECL_PURE Vector3i operator *(Vector3i const &a, int s) +{ + return Vector3i(a.x * s, a.y * s, a.z * s); +} +static inline WZ_DECL_PURE Vector3f operator *(Vector3f const &a, float s) +{ + return Vector3f(a.x * s, a.y * s, a.z * s); +} //static inline WZ_DECL_PURE Rotation operator *(Rotation const &a, int s) { return Rotation((int16_t)a.direction*s, (int16_t)a.pitch*s, (int16_t)a.roll*s); } // vector / scalar -> vector -static inline WZ_DECL_PURE Vector2i operator /(Vector2i const &a, int s) { return Vector2i(a.x/s, a.y/s); } -static inline WZ_DECL_PURE Vector2f operator /(Vector2f const &a, float s) { return Vector2f(a.x/s, a.y/s); } -static inline WZ_DECL_PURE Vector3i operator /(Vector3i const &a, int s) { return Vector3i(a.x/s, a.y/s, a.z/s); } -static inline WZ_DECL_PURE Vector3f operator /(Vector3f const &a, float s) { return Vector3f(a.x/s, a.y/s, a.z/s); } +static inline WZ_DECL_PURE Vector2i operator /(Vector2i const &a, int s) +{ + return Vector2i(a.x / s, a.y / s); +} +static inline WZ_DECL_PURE Vector2f operator /(Vector2f const &a, float s) +{ + return Vector2f(a.x / s, a.y / s); +} +static inline WZ_DECL_PURE Vector3i operator /(Vector3i const &a, int s) +{ + return Vector3i(a.x / s, a.y / s, a.z / s); +} +static inline WZ_DECL_PURE Vector3f operator /(Vector3f const &a, float s) +{ + return Vector3f(a.x / s, a.y / s, a.z / s); +} //static inline WZ_DECL_PURE Rotation operator /(Rotation const &a, int s) { return Rotation((int16_t)a.direction/s, (int16_t)a.pitch/s, (int16_t)a.roll/s); } // vector * vector -> scalar -static inline WZ_DECL_PURE int operator *(Vector2i const &a, Vector2i const &b) { return a.x*b.x + a.y*b.y; } -static inline WZ_DECL_PURE float operator *(Vector2f const &a, Vector2f const &b) { return a.x*b.x + a.y*b.y; } -static inline WZ_DECL_PURE int operator *(Vector3i const &a, Vector3i const &b) { return a.x*b.x + a.y*b.y + a.z*b.z; } -static inline WZ_DECL_PURE float operator *(Vector3f const &a, Vector3f const &b) { return a.x*b.x + a.y*b.y + a.z*b.z; } +static inline WZ_DECL_PURE int operator *(Vector2i const &a, Vector2i const &b) +{ + return a.x * b.x + a.y * b.y; +} +static inline WZ_DECL_PURE float operator *(Vector2f const &a, Vector2f const &b) +{ + return a.x * b.x + a.y * b.y; +} +static inline WZ_DECL_PURE int operator *(Vector3i const &a, Vector3i const &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} +static inline WZ_DECL_PURE float operator *(Vector3f const &a, Vector3f const &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} // normalise(vector) -> scalar -static inline WZ_DECL_PURE Vector2f normalise(Vector2f const &a) { float sq = a*a; if (sq == 0.0f) return Vector2f(0.0f, 0.0f); return a / sqrtf(sq); } -static inline WZ_DECL_PURE Vector3f normalise(Vector3f const &a) { float sq = a*a; if (sq == 0.0f) return Vector3f(0.0f, 0.0f, 0.0f); return a / sqrtf(sq); } +static inline WZ_DECL_PURE Vector2f normalise(Vector2f const &a) +{ + float sq = a * a; + if (sq == 0.0f) + { + return Vector2f(0.0f, 0.0f); + } return a / sqrtf(sq); +} +static inline WZ_DECL_PURE Vector3f normalise(Vector3f const &a) +{ + float sq = a * a; + if (sq == 0.0f) + { + return Vector3f(0.0f, 0.0f, 0.0f); + } return a / sqrtf(sq); +} // iSinCosR(angle, scalar) -> 2d_vector -static inline WZ_DECL_PURE Vector2i iSinCosR(uint16_t a, int32_t r) { return Vector2i(iSinR(a, r), iCosR(a, r)); } +static inline WZ_DECL_PURE Vector2i iSinCosR(uint16_t a, int32_t r) +{ + return Vector2i(iSinR(a, r), iCosR(a, r)); +} // iAtan2(2d_vector) -> angle -static inline WZ_DECL_PURE int iAtan2(Vector2i const &a) { return iAtan2(a.x, a.y); } +static inline WZ_DECL_PURE int iAtan2(Vector2i const &a) +{ + return iAtan2(a.x, a.y); +} // iHypot(vector) -> scalar -static inline WZ_DECL_PURE int iHypot(Vector2i const &a) { return iHypot(a.x, a.y); } -static inline WZ_DECL_PURE int iHypot(Vector3i const &a) { return iHypot3(a.x, a.y, a.z); } +static inline WZ_DECL_PURE int iHypot(Vector2i const &a) +{ + return iHypot(a.x, a.y); +} +static inline WZ_DECL_PURE int iHypot(Vector3i const &a) +{ + return iHypot3(a.x, a.y, a.z); +} // swapYZ(3d_vector) -> 3d_vector -static inline WZ_DECL_PURE Vector3i swapYZ(Vector3i a) { return Vector3i(a.x, a.z, a.y); } -static inline WZ_DECL_PURE Vector3f swapYZ(Vector3f a) { return Vector3f(a.x, a.z, a.y); } +static inline WZ_DECL_PURE Vector3i swapYZ(Vector3i a) +{ + return Vector3i(a.x, a.z, a.y); +} +static inline WZ_DECL_PURE Vector3f swapYZ(Vector3f a) +{ + return Vector3f(a.x, a.z, a.y); +} // vector × vector -> scalar -static inline WZ_DECL_PURE Vector3i crossProduct(Vector3i const &a, Vector3i const &b) { return Vector3i(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); } -static inline WZ_DECL_PURE Vector3f crossProduct(Vector3f const &a, Vector3f const &b) { return Vector3f(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); } +static inline WZ_DECL_PURE Vector3i crossProduct(Vector3i const &a, Vector3i const &b) +{ + return Vector3i(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); +} +static inline WZ_DECL_PURE Vector3f crossProduct(Vector3f const &a, Vector3f const &b) +{ + return Vector3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); +} // vector += vector -static inline Vector2i const &operator +=(Vector2i &a, Vector2i const &b) { return a = a + b; } -static inline Vector2f const &operator +=(Vector2f &a, Vector2f const &b) { return a = a + b; } -static inline Vector3i const &operator +=(Vector3i &a, Vector3i const &b) { return a = a + b; } -static inline Vector3f const &operator +=(Vector3f &a, Vector3f const &b) { return a = a + b; } +static inline Vector2i const &operator +=(Vector2i &a, Vector2i const &b) +{ + return a = a + b; +} +static inline Vector2f const &operator +=(Vector2f &a, Vector2f const &b) +{ + return a = a + b; +} +static inline Vector3i const &operator +=(Vector3i &a, Vector3i const &b) +{ + return a = a + b; +} +static inline Vector3f const &operator +=(Vector3f &a, Vector3f const &b) +{ + return a = a + b; +} // vector -= vector -static inline Vector2i const &operator -=(Vector2i &a, Vector2i const &b) { return a = a - b; } -static inline Vector2f const &operator -=(Vector2f &a, Vector2f const &b) { return a = a - b; } -static inline Vector3i const &operator -=(Vector3i &a, Vector3i const &b) { return a = a - b; } -static inline Vector3f const &operator -=(Vector3f &a, Vector3f const &b) { return a = a - b; } +static inline Vector2i const &operator -=(Vector2i &a, Vector2i const &b) +{ + return a = a - b; +} +static inline Vector2f const &operator -=(Vector2f &a, Vector2f const &b) +{ + return a = a - b; +} +static inline Vector3i const &operator -=(Vector3i &a, Vector3i const &b) +{ + return a = a - b; +} +static inline Vector3f const &operator -=(Vector3f &a, Vector3f const &b) +{ + return a = a - b; +} /*! @@ -178,8 +353,8 @@ static inline Vector3f const &operator -=(Vector3f &a, Vector3f const &b) { retu static inline WZ_DECL_PURE Vector2f Vector2f_Rotate2f(Vector2f v, int angle) { Vector2f result; - result.x = (v.x*iCos(angle) - v.y*iSin(angle)) / 65536; - result.y = (v.x*iSin(angle) + v.y*iCos(angle)) / 65536; + result.x = (v.x * iCos(angle) - v.y * iSin(angle)) / 65536; + result.y = (v.x * iSin(angle) + v.y * iCos(angle)) / 65536; return result; } @@ -215,7 +390,7 @@ static inline WZ_DECL_CONST bool Vector3i_InCircle(const Vector3i v, const Vecto * \param r The radius of the sphere * \return If v falls within the sphere */ -static inline WZ_DECL_CONST bool Vector3i_InSphere (const Vector3i v, const Vector3i c, const unsigned int r) +static inline WZ_DECL_CONST bool Vector3i_InSphere(const Vector3i v, const Vector3i c, const unsigned int r) { Vector3i delta = v - c; // Explictily cast to "unsigned int" because this number never can be diff --git a/lib/framework/wzconfig.cpp b/lib/framework/wzconfig.cpp index a2ab9f7593e..52bc1340210 100644 --- a/lib/framework/wzconfig.cpp +++ b/lib/framework/wzconfig.cpp @@ -38,7 +38,10 @@ void WzConfig::setVector3f(const QString &name, const Vector3f &v) Vector3f WzConfig::vector3f(const QString &name) { Vector3f r(0.0, 0.0, 0.0); - if (!contains(name)) return r; + if (!contains(name)) + { + return r; + } QStringList v = value(name).toStringList(); ASSERT(v.size() == 3, "Bad list of %s", name.toUtf8().constData()); r.x = v[0].toDouble(); @@ -59,7 +62,10 @@ void WzConfig::setVector3i(const QString &name, const Vector3i &v) Vector3i WzConfig::vector3i(const QString &name) { Vector3i r(0, 0, 0); - if (!contains(name)) return r; + if (!contains(name)) + { + return r; + } QStringList v = value(name).toStringList(); ASSERT(v.size() == 3, "Bad list of %s", name.toUtf8().constData()); r.x = v[0].toInt(); @@ -79,7 +85,10 @@ void WzConfig::setVector2i(const QString &name, const Vector2i &v) Vector2i WzConfig::vector2i(const QString &name) { Vector2i r(0, 0); - if (!contains(name)) return r; + if (!contains(name)) + { + return r; + } QStringList v = value(name).toStringList(); ASSERT(v.size() == 2, "Bad list of %s", name.toUtf8().constData()); r.x = v[0].toInt(); diff --git a/lib/framework/wzconfig.h b/lib/framework/wzconfig.h index cc8401cc65e..03c7e5538d8 100644 --- a/lib/framework/wzconfig.h +++ b/lib/framework/wzconfig.h @@ -36,9 +36,15 @@ class WzConfigHack public: WzConfigHack(const QString &fileName) { - if (PHYSFS_exists(fileName.toUtf8().constData())) return; + if (PHYSFS_exists(fileName.toUtf8().constData())) + { + return; + } PHYSFS_file *fileHandle = PHYSFS_openWrite(fileName.toUtf8().constData()); - if (!fileHandle) debug(LOG_ERROR, "%s could not be created: %s", fileName.toUtf8().constData(), PHYSFS_getLastError()); + if (!fileHandle) + { + debug(LOG_ERROR, "%s could not be created: %s", fileName.toUtf8().constData(), PHYSFS_getLastError()); + } PHYSFS_close(fileHandle); } }; diff --git a/lib/framework/wzfs.h b/lib/framework/wzfs.h index 17a75ee793d..2c0877e2e16 100644 --- a/lib/framework/wzfs.h +++ b/lib/framework/wzfs.h @@ -35,45 +35,144 @@ class PhysicsFileSystem : public QAbstractFileEngine void realSetFileName(const QString &file) { name = file; - if (!PHYSFS_exists(name.toUtf8().constData())) return; + if (!PHYSFS_exists(name.toUtf8().constData())) + { + return; + } // Show potential until actually opened flags = QAbstractFileEngine::ExistsFlag | QAbstractFileEngine::ReadOtherPerm | QAbstractFileEngine::WriteOwnerPerm \ - | QAbstractFileEngine::ReadOwnerPerm | QAbstractFileEngine::ReadUserPerm | QAbstractFileEngine::WriteUserPerm; + | QAbstractFileEngine::ReadOwnerPerm | QAbstractFileEngine::ReadUserPerm | QAbstractFileEngine::WriteUserPerm; lastMod = QDateTime::fromTime_t(PHYSFS_getLastModTime(name.toUtf8().constData())); - if (PHYSFS_isDirectory(name.toUtf8().constData())) flags |= QAbstractFileEngine::DirectoryType; - if (PHYSFS_isSymbolicLink(name.toUtf8().constData())) flags |= QAbstractFileEngine::LinkType; - if (!(flags & QAbstractFileEngine::DirectoryType || flags & QAbstractFileEngine::LinkType)) flags |= QAbstractFileEngine::FileType; + if (PHYSFS_isDirectory(name.toUtf8().constData())) + { + flags |= QAbstractFileEngine::DirectoryType; + } + if (PHYSFS_isSymbolicLink(name.toUtf8().constData())) + { + flags |= QAbstractFileEngine::LinkType; + } + if (!(flags & QAbstractFileEngine::DirectoryType || flags & QAbstractFileEngine::LinkType)) + { + flags |= QAbstractFileEngine::FileType; + } } public: - PhysicsFileSystem(QString filename) : fp(NULL), flags(0), name(filename) { realSetFileName(filename); } - virtual ~PhysicsFileSystem() { if (fp) PHYSFS_close(fp); } - bool atEnd() const { return PHYSFS_eof(fp) != 0; } - virtual bool caseSensitive() const { return true; } - virtual bool close() { if (fp) { int retval = PHYSFS_close(fp); fp = NULL; return retval != 0; } else return true; } - QFile::FileError error() const { return QFile::UnspecifiedError; } - QString errorString() const { return QString(PHYSFS_getLastError()); } - virtual bool extension(Extension extension, const ExtensionOption * option = 0, ExtensionReturn * output = 0) { return extension == QAbstractFileEngine::AtEndExtension; } - virtual FileFlags fileFlags(FileFlags type = FileInfoAll) const { return type & flags; } - virtual QDateTime fileTime(FileTime time) const { if (time == QAbstractFileEngine::ModificationTime) return lastMod; else return QDateTime(); } - virtual bool flush() { return PHYSFS_flush(fp) != 0; } - virtual bool isRelativePath() const { return true; } // in physfs, all paths are relative - virtual bool isSequential() const { return true; } - virtual bool mkdir(const QString & dirName, bool createParentDirectories) const { Q_UNUSED(createParentDirectories); return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0; } - virtual qint64 pos() const { return PHYSFS_tell(fp); } - virtual qint64 read(char *data, qint64 maxlen) { return PHYSFS_read(fp, data, 1, maxlen); } - virtual bool remove() { return PHYSFS_delete(name.toUtf8().constData()) != 0; } - virtual bool rmdir(const QString & dirName, bool recurseParentDirectories) const { Q_UNUSED(recurseParentDirectories); return PHYSFS_delete(name.toUtf8().constData()) != 0; } - virtual bool seek(qint64 offset) { return PHYSFS_seek(fp, offset) != 0; } - virtual bool supportsExtension(Extension extension) const { return extension == QAbstractFileEngine::AtEndExtension; } - virtual qint64 write(const char *data, qint64 len) { return PHYSFS_write(fp, data, 1, len); } + PhysicsFileSystem(QString filename) : fp(NULL), flags(0), name(filename) + { + realSetFileName(filename); + } + virtual ~PhysicsFileSystem() + { + if (fp) + { + PHYSFS_close(fp); + } + } + bool atEnd() const + { + return PHYSFS_eof(fp) != 0; + } + virtual bool caseSensitive() const + { + return true; + } + virtual bool close() + { + if (fp) + { + int retval = PHYSFS_close(fp); + fp = NULL; + return retval != 0; + } + else + { + return true; + } + } + QFile::FileError error() const + { + return QFile::UnspecifiedError; + } + QString errorString() const + { + return QString(PHYSFS_getLastError()); + } + virtual bool extension(Extension extension, const ExtensionOption *option = 0, ExtensionReturn *output = 0) + { + return extension == QAbstractFileEngine::AtEndExtension; + } + virtual FileFlags fileFlags(FileFlags type = FileInfoAll) const + { + return type & flags; + } + virtual QDateTime fileTime(FileTime time) const + { + if (time == QAbstractFileEngine::ModificationTime) + { + return lastMod; + } + else + { + return QDateTime(); + } + } + virtual bool flush() + { + return PHYSFS_flush(fp) != 0; + } + virtual bool isRelativePath() const + { + return true; // in physfs, all paths are relative + } + virtual bool isSequential() const + { + return true; + } + virtual bool mkdir(const QString &dirName, bool createParentDirectories) const + { + Q_UNUSED(createParentDirectories); + return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0; + } + virtual qint64 pos() const + { + return PHYSFS_tell(fp); + } + virtual qint64 read(char *data, qint64 maxlen) + { + return PHYSFS_read(fp, data, 1, maxlen); + } + virtual bool remove() + { + return PHYSFS_delete(name.toUtf8().constData()) != 0; + } + virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const + { + Q_UNUSED(recurseParentDirectories); + return PHYSFS_delete(name.toUtf8().constData()) != 0; + } + virtual bool seek(qint64 offset) + { + return PHYSFS_seek(fp, offset) != 0; + } + virtual bool supportsExtension(Extension extension) const + { + return extension == QAbstractFileEngine::AtEndExtension; + } + virtual qint64 write(const char *data, qint64 len) + { + return PHYSFS_write(fp, data, 1, len); + } virtual qint64 size() const { if (!fp) { - if (!PHYSFS_exists(name.toUtf8().constData())) return 0; + if (!PHYSFS_exists(name.toUtf8().constData())) + { + return 0; + } PHYSFS_file *tmp; tmp = PHYSFS_openRead(name.toUtf8().constData()); if (!tmp) @@ -88,7 +187,7 @@ class PhysicsFileSystem : public QAbstractFileEngine return PHYSFS_fileLength(fp); } - virtual void setFileName(const QString & file) + virtual void setFileName(const QString &file) { realSetFileName(file); } @@ -100,7 +199,10 @@ class PhysicsFileSystem : public QAbstractFileEngine { flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType; fp = PHYSFS_openWrite(name.toUtf8().constData()); // will truncate - if (!fp) qWarning("Failed to create %s: %s", name.toUtf8().constData(), PHYSFS_getLastError()); + if (!fp) + { + qWarning("Failed to create %s: %s", name.toUtf8().constData(), PHYSFS_getLastError()); + } } else if (mode & QIODevice::ReadOnly) { @@ -112,19 +214,28 @@ class PhysicsFileSystem : public QAbstractFileEngine { flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType; fp = PHYSFS_openAppend(name.toUtf8().constData()); - if (!fp) qWarning("Failed to append open %s: %s", name.toUtf8().constData(), PHYSFS_getLastError()); + if (!fp) + { + qWarning("Failed to append open %s: %s", name.toUtf8().constData(), PHYSFS_getLastError()); + } } else { qWarning("Bad file open mode: %d", (int)mode); } - if (fp) flags |= QAbstractFileEngine::ExistsFlag; + if (fp) + { + flags |= QAbstractFileEngine::ExistsFlag; + } return fp != NULL; } virtual QString fileName(FileName file = DefaultName) const { - if (file == QAbstractFileEngine::AbsolutePathName) return PHYSFS_getWriteDir(); // hack for QSettings + if (file == QAbstractFileEngine::AbsolutePathName) + { + return PHYSFS_getWriteDir(); // hack for QSettings + } return "wz::" + name; } diff --git a/lib/framework/wzglobal.h b/lib/framework/wzglobal.h index da906a58946..6f24547bcc9 100644 --- a/lib/framework/wzglobal.h +++ b/lib/framework/wzglobal.h @@ -120,11 +120,11 @@ # define WZ_OS_FREEBSD # define WZ_OS_BSD4 #elif defined(__FreeBSD_kernel__) && !defined(__FreeBSD__) - /* We're running a non-FreeBSD system with a FreeBSD kernel. Find out what C - * library we're using to detect the system we're running on. */ +/* We're running a non-FreeBSD system with a FreeBSD kernel. Find out what C + * library we're using to detect the system we're running on. */ # include # if defined(__GLIBC__) - /* We're running GNU/kFreeBSD */ +/* We're running GNU/kFreeBSD */ # define WZ_OS_GNU_kFREEBSD # endif #elif defined(__NetBSD__) @@ -320,7 +320,7 @@ */ #if defined(WZ_CC_GNU) && defined __GNUC__ && defined __GNUC_MINOR__ # define WZ_CC_GNU_PREREQ(maj, min) \ - ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) #else # define WZ_CC_GNU_PREREQ(maj, min) 0 #endif @@ -331,7 +331,7 @@ */ #if defined(WZ_CC_INTEL) && defined __ICC # define WZ_CC_INTEL_PREREQ(maj, min) \ - ((__ICC) >= ((maj) * 100) + (min)) + ((__ICC) >= ((maj) * 100) + (min)) #else # define WZ_CC_INTEL_PREREQ(maj, min) 0 #endif @@ -395,7 +395,7 @@ */ #if WZ_CC_GNU_PREREQ(2,5) && !defined(WZ_CC_INTEL) # define WZ_DECL_FORMAT(archetype, string_index, first_to_check) \ - __attribute__((__format__(archetype, string_index, first_to_check))) + __attribute__((__format__(archetype, string_index, first_to_check))) #else # define WZ_DECL_FORMAT(archetype, string_index, first_to_check) #endif @@ -432,7 +432,7 @@ /*! * \def WZ_DECL_ALWAYS_INLINE * GCC: "Generally, functions are not inlined unless optimization is specified. For functions - * declared inline, this attribute inlines the function even if no optimization level + * declared inline, this attribute inlines the function even if no optimization level * was specified." */ #if WZ_CC_GNU_PREREQ(2,5) @@ -633,10 +633,19 @@ * Asserts that the given string is statically allocated. */ #if defined(__cplusplus) - template - static inline char _WZ_ASSERT_STATIC_STRING_FUNCTION(char const (&)[N]) { return '\0'; } // Regular array. - static inline char *_WZ_ASSERT_STATIC_STRING_FUNCTION(char const *&) { return NULL; } // Eeek, it's a pointer! - static inline char *_WZ_ASSERT_STATIC_STRING_FUNCTION(char *&) { return NULL; } // Eeek, it's a pointer! +template +static inline char _WZ_ASSERT_STATIC_STRING_FUNCTION(char const(&)[N]) +{ + return '\0'; // Regular array. +} +static inline char *_WZ_ASSERT_STATIC_STRING_FUNCTION(char const *&) +{ + return NULL; // Eeek, it's a pointer! +} +static inline char *_WZ_ASSERT_STATIC_STRING_FUNCTION(char *&) +{ + return NULL; // Eeek, it's a pointer! +} # define WZ_ASSERT_STATIC_STRING(_var) STATIC_ASSERT(sizeof(_WZ_ASSERT_STATIC_STRING_FUNCTION(_var)) == sizeof(char)) #elif defined(WZ_CC_GNU) || defined(WZ_CC_INTEL) # define WZ_ASSERT_STATIC_STRING(_var) STATIC_ASSERT(__builtin_types_compatible_p(typeof(_var), char[])) @@ -648,11 +657,20 @@ * Asserts that the given variable is a (statically sized) array, not just a pointer. */ #if defined(__cplusplus) - template - static inline char _WZ_ASSERT_ARRAY_EXPR_FUNCTION(T (&)[N]) { return '\0'; } // Regular array. - static inline char _WZ_ASSERT_ARRAY_EXPR_FUNCTION(void const *) { return '\0'; } // Catch static arrays of unnamed structs. - template - static inline char *_WZ_ASSERT_ARRAY_EXPR_FUNCTION(T *&) { return NULL; } // Eeek, it's a pointer! +template +static inline char _WZ_ASSERT_ARRAY_EXPR_FUNCTION(T(&)[N]) +{ + return '\0'; // Regular array. +} +static inline char _WZ_ASSERT_ARRAY_EXPR_FUNCTION(void const *) +{ + return '\0'; // Catch static arrays of unnamed structs. +} +template +static inline char *_WZ_ASSERT_ARRAY_EXPR_FUNCTION(T *&) +{ + return NULL; // Eeek, it's a pointer! +} # define WZ_ASSERT_ARRAY_EXPR(_var) STATIC_ASSERT_EXPR(sizeof(_WZ_ASSERT_ARRAY_EXPR_FUNCTION(_var)) == sizeof(char)) #elif defined(WZ_CC_GNU) || defined(WZ_CC_INTEL) /* &a[0] degrades to a pointer: a different type from an array */