123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- #include "util.h"
- #ifdef PAL_HAS_NATIVEMIDI
- #include "midi.h"
- #endif
- void
- trim(
- char *str
- )
- {
- int pos = 0;
- char *dest = str;
-
-
-
- while (str[pos] <= ' ' && str[pos] > 0)
- pos++;
- while (str[pos])
- {
- *(dest++) = str[pos];
- pos++;
- }
- *(dest--) = '\0';
-
-
-
- while (dest >= str && *dest <= ' ' && *dest > 0)
- *(dest--) = '\0';
- }
- char *
- va(
- const char *format,
- ...
- )
- {
- static char string[256];
- va_list argptr;
- va_start(argptr, format);
- vsnprintf(string, 256, format, argptr);
- va_end(argptr);
- return string;
- }
- static int glSeed = 0;
- static void
- lsrand(
- unsigned int iInitialSeed
- )
- {
-
-
-
- glSeed = 1664525L * iInitialSeed + 1013904223L;
- }
- static int
- lrand(
- void
- )
- {
- if (glSeed == 0)
- lsrand((unsigned int)time(NULL));
- glSeed = 1664525L * glSeed + 1013904223L;
- return ((glSeed >> 1) + 1073741824L);
- }
- int
- RandomLong(
- int from,
- int to
- )
- {
- if (to <= from)
- return from;
- return from + lrand() / (INT_MAX / (to - from + 1));
- }
- float
- RandomFloat(
- float from,
- float to
- )
- {
- if (to <= from)
- return from;
- return from + (float)lrand() / (INT_MAX / (to - from));
- }
- void
- UTIL_Delay(
- unsigned int ms
- )
- {
- unsigned int t = SDL_GetTicks() + ms;
- while (SDL_PollEvent(NULL));
- while (SDL_GetTicks() < t)
- {
- SDL_Delay(1);
- while (SDL_PollEvent(NULL));
- }
- #ifdef PAL_HAS_NATIVEMIDI
- MIDI_CheckLoop();
- #endif
- }
- void
- TerminateOnError(
- const char *fmt,
- ...
- )
- {
- va_list argptr;
- char string[256];
- extern VOID PAL_Shutdown(VOID);
-
- va_start(argptr, fmt);
- vsnprintf(string, sizeof(string), fmt, argptr);
- va_end(argptr);
- fprintf(stderr, "\nFATAL ERROR: %s\n", string);
- #ifdef _WIN32
- MessageBoxA(0, string, "FATAL ERROR", MB_ICONERROR);
- #endif
- #ifdef __linux__
- system(va("beep; xmessage -center \"FATAL ERROR: %s\"", string));
- #endif
- #if defined(__SYMBIAN32__)
- UTIL_WriteLog(LOG_DEBUG,"[0x%08x][%s][%s] - %s",(long)TerminateOnError,"TerminateOnError",__FILE__, string);
- SDL_Delay(3000);
- #endif
- #ifdef _DEBUG
- assert(!"TerminateOnError()");
- #endif
- PAL_Shutdown();
- #if defined (NDS)
- while (1);
- #else
- exit(255);
- #endif
- }
- void *
- UTIL_malloc(
- size_t buffer_size
- )
- {
-
- void *buffer;
-
- if (buffer_size == 0)
- TerminateOnError("UTIL_malloc() called with invalid buffer size: %d\n", buffer_size);
- buffer = malloc(buffer_size);
-
- if (buffer == NULL)
- TerminateOnError("UTIL_malloc() failure for %d bytes (out of memory?)\n", buffer_size);
- return buffer;
- }
- void *
- UTIL_calloc(
- size_t n,
- size_t size
- )
- {
-
- void *buffer;
-
- if (n == 0 || size == 0)
- TerminateOnError ("UTIL_calloc() called with invalid parameters\n");
- buffer = calloc(n, size);
-
- if (buffer == NULL)
- TerminateOnError("UTIL_calloc() failure for %d bytes (out of memory?)\n", size * n);
- return buffer;
- }
- FILE *
- UTIL_OpenRequiredFile(
- LPCSTR lpszFileName
- )
- {
- FILE *fp;
- fp = fopen(va("%s%s", PAL_PREFIX, lpszFileName), "rb");
- if (fp == NULL)
- {
- TerminateOnError("File not found: %s!\n", lpszFileName);
- }
- return fp;
- }
- VOID
- UTIL_CloseFile(
- FILE *fp
- )
- {
- if (fp != NULL)
- {
- fclose(fp);
- }
- }
- #ifdef ENABLE_LOG
- static FILE *pLogFile = NULL;
- FILE *
- UTIL_OpenLog(
- VOID
- )
- {
- if ((pLogFile = fopen(_PATH_LOG, "a+")) == NULL)
- {
- return NULL;
- }
- return pLogFile;
- }
- VOID
- UTIL_CloseLog(
- VOID
- )
- {
- if (pLogFile != NULL)
- {
- fclose(pLogFile);
- }
- }
- VOID
- UTIL_WriteLog(
- int Priority,
- const char *Fmt,
- ...
- )
- {
- va_list vaa;
- time_t lTime;
- struct tm *curTime;
- char szDateBuf[260];
- time(&lTime);
- if ((Priority < LOG_EMERG) || (Priority >= LOG_LAST_PRIORITY))
- {
- return;
- }
- curTime = localtime(&lTime);
- strftime(szDateBuf, 128, "%Y-%m-%d %H:%M:%S", curTime);
- szDateBuf[strlen(szDateBuf) - 1] = '\0';
- va_start(vaa,Fmt);
- fprintf(pLogFile, "[%s]", szDateBuf);
- vfprintf(pLogFile, Fmt, vaa);
- fprintf(pLogFile, "\n");
- fflush(pLogFile);
- va_end(vaa);
- }
- #endif
|