123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- #define BUILDING_OPENDOORS
- #include <string.h>
- #include <stdio.h>
- #include "OpenDoor.h"
- #include "ODStr.h"
- #include "ODUtil.h"
- #include "ODGen.h"
- void ODStringCopy(char *pszDest, CONST char *pszSource, INT nSizeofDest)
- {
- ASSERT(pszDest != NULL);
- ASSERT(pszSource != NULL);
- ASSERT(nSizeofDest > 0);
-
-
- strncpy(pszDest, pszSource, nSizeofDest);
-
-
- pszDest[nSizeofDest - 1] = '\0';
- }
- char *ODStringCToPascal(char *psPascalString, BYTE btMaxPascalLength,
- char *pszCString)
- {
- BYTE btCStringLength = strlen(pszCString);
- ASSERT(psPascalString != NULL);
- ASSERT(btMaxPascalLength > 0);
- ASSERT(pszCString != NULL);
- memcpy((char *)psPascalString + 1,
- pszCString, *psPascalString = (btCStringLength < btMaxPascalLength)
- ? btCStringLength : btMaxPascalLength);
- return(psPascalString);
- }
- char *ODStringPascalToC(char *pszCString, char *psPascalString,
- BYTE btMaxLength)
- {
- ASSERT(pszCString != NULL);
- ASSERT(psPascalString != NULL);
- ASSERT(btMaxLength > 0);
- if(*(BYTE *)psPascalString <= btMaxLength)
- {
- memcpy(pszCString, (char *)psPascalString + 1, *psPascalString);
- pszCString[(int)psPascalString[0]] = '\0';
- }
- else
- {
- pszCString[0] = '\0';
- }
- return(pszCString);
- }
- BOOL ODStringHasTail(char *pszFullString, char *pszTail)
- {
- INT nTailLength = strlen(pszTail);
- INT nFullStringLength = strlen(pszFullString);
- ASSERT(pszFullString != NULL);
- ASSERT(pszTail != NULL);
- if(nFullStringLength < nTailLength)
- {
- return(FALSE);
- }
- return(stricmp(pszFullString + (nFullStringLength - nTailLength), pszTail) == 0);
- }
- tODResult ODMakeFilename(char *pszOut, CONST char *pszPath,
- CONST char *pszFilename, INT nMaxOutSize)
- {
-
- ASSERT(pszPath != NULL);
- ASSERT(pszFilename != NULL);
- ASSERT(pszOut != NULL);
- ASSERT(pszFilename != pszOut);
- ASSERT(nMaxOutSize > 0);
-
-
- if((INT)(strlen(pszPath) + strlen(pszFilename) + 1) > nMaxOutSize - 1)
- {
- return(kODRCFilenameTooLong);
- }
-
- if(pszPath != pszOut)
- {
- strcpy(pszOut, pszPath);
- }
-
- #ifdef ODPLAT_NIX
- #else
- if(pszOut[strlen(pszOut) - 1] != DIRSEP && strlen(pszOut) > 0)
- {
- strcat(pszOut, DIRSEP_STR);
- }
- #endif
-
- strcat(pszOut, pszFilename);
- return(kODRCSuccess);
- }
- DWORD ODFileSize(FILE *pfFile)
- {
- DWORD dwOriginal;
- DWORD dwFileSize;
- ASSERT(pfFile != NULL);
- dwOriginal = ftell(pfFile);
- fseek(pfFile, 0L, SEEK_END);
- dwFileSize = ftell(pfFile);
- fseek(pfFile, dwOriginal, SEEK_SET);
- return(dwFileSize);
- }
- DWORD ODDWordShiftLeft(DWORD dwValue, BYTE btDistance)
- {
- WORD wUpper;
- WORD wLower;
- wLower = (WORD)dwValue;
- wUpper = *(WORD *)(((BYTE *)(&dwValue)) + 2);
- while(btDistance--)
- {
- wUpper <<= 1;
- wUpper |= (wLower & 0x8000) >> 15;
- wLower <<= 1;
- }
- dwValue = wLower;
- *(WORD *)(((BYTE *)(&dwValue)) + 2) = wUpper;
- return(dwValue);
- }
- DWORD ODDWordShiftRight(DWORD dwValue, BYTE btDistance)
- {
- WORD wUpper;
- WORD wLower;
- wLower = (WORD)dwValue;
- wUpper = *(WORD *)(((BYTE *)(&dwValue)) + 2);
- while(btDistance--)
- {
- wLower >>= 1;
- wLower |= (wUpper & 0x0001) << 15;
- wUpper >>= 1;
- }
- dwValue=wLower;
- *(WORD *)(((BYTE *)(&dwValue)) + 2) = wUpper;
- return(dwValue);
- }
- BOOL ODDWordDivide(DWORD *pdwQuotient, DWORD *pdwRemainder,
- DWORD dwDividend, DWORD dwDivisor)
- {
- INT nTimes = 0;
- DWORD dwQuotient;
- DWORD dwRemainder;
-
- ASSERT(dwDivisor != 0);
-
-
-
- if(dwDivisor == 0L)
- {
- return(FALSE);
- }
-
- dwRemainder = dwDividend;
-
- dwQuotient = 0L;
-
- while(dwRemainder >= dwDivisor)
- {
- dwDivisor = ODDWordShiftLeft(dwDivisor, 1);
- ++nTimes;
- }
-
- do
- {
- dwQuotient = ODDWordShiftLeft(dwQuotient, 1);
-
- if(dwRemainder >= dwDivisor)
- {
-
- dwRemainder -= dwDivisor;
-
- dwQuotient |= 1L;
- }
-
- dwDivisor = ODDWordShiftRight(dwDivisor, 1);
-
- } while(nTimes--);
-
- if(pdwQuotient != NULL)
- {
- *pdwQuotient = dwQuotient;
- }
-
- if(pdwRemainder != NULL)
- {
- *pdwRemainder = dwRemainder;
- }
- return(TRUE);
- }
- DWORD ODDWordMultiply(DWORD dwMultiplicand, DWORD dwMultiplier)
- {
- DWORD dwResult = 0;
-
- while(dwMultiplier != 0)
- {
-
- if(dwMultiplier & 0x00000001)
- {
-
- dwResult += dwMultiplicand;
- }
-
- dwMultiplicand = ODDWordShiftLeft(dwMultiplicand, 1);
-
- dwMultiplier = ODDWordShiftRight(dwMultiplier, 1);
- }
-
- return(dwResult);
- }
|