|
|
1.1 root 1: /* 1.1.1.4 ! root 2: Hatari - misc.c ! 3: ! 4: This file is distributed under the GNU Public License, version 2 or at ! 5: your option any later version. Read the file gpl.txt for details. 1.1 root 6: 7: Misc functions 8: */ 1.1.1.4 ! root 9: static char rcsid[] = "Hatari $Id: misc.c,v 1.7 2003/04/05 22:25:02 thothy Exp $"; ! 10: ! 11: #include <ctype.h> 1.1 root 12: 13: #include "main.h" 14: #include "debug.h" 15: #include "errlog.h" 16: #include "file.h" 17: #include "memAlloc.h" 18: #include "misc.h" 19: 20: long RandomNum; 21: 1.1.1.2 root 22: 23: /*-----------------------------------------------------------------------*/ 1.1 root 24: /* 25: Fill end of string out with spaces 26: */ 27: void Misc_PadStringWithSpaces(char *pszString,int nChars) 28: { 29: int i; 30: 31: for(i=nChars; i>=(int)strlen(pszString); i--) { 32: pszString[i] = ' '; 33: } 34: pszString[nChars] = '\0'; 35: } 36: 1.1.1.2 root 37: 38: /*-----------------------------------------------------------------------*/ 1.1 root 39: /* 40: Remove any spaces from string 41: */ 42: void Misc_RemoveSpacesFromString(char *pszSrcString, char *pszDestString) 43: { 44: int i=0,j=0; 45: 1.1.1.2 root 46: /* Copy string */ 1.1 root 47: while(pszSrcString[i]!='\0') { 1.1.1.2 root 48: if (pszSrcString[i]!=' ') { /* Copy character if not a white-space */ 1.1 root 49: pszDestString[j] = pszSrcString[i]; 50: j++; 51: } 52: i++; 53: } 54: 1.1.1.2 root 55: pszDestString[j] = '\0'; /* Term */ 1.1 root 56: } 57: 1.1.1.2 root 58: 59: /*-----------------------------------------------------------------------*/ 1.1 root 60: /* 61: Remove 'white-space' from beginning of text string 62: */ 63: void Misc_RemoveWhiteSpace(char *pszString,int Length) 64: { 65: while( (*pszString==' ') || (*pszString=='\t') ) { 1.1.1.2 root 66: /* Copy line left one character */ 1.1 root 67: memmove(pszString,pszString+1,Length-1); 68: } 69: } 70: 1.1.1.2 root 71: 72: /*-----------------------------------------------------------------------*/ 1.1 root 73: /* 1.1.1.4 ! root 74: Convert a string to uppercase. ! 75: */ ! 76: void Misc_strupr(char *pString) ! 77: { ! 78: while(*pString) ! 79: { ! 80: *pString = toupper(*pString); ! 81: pString++; ! 82: } ! 83: } ! 84: ! 85: ! 86: /*-----------------------------------------------------------------------*/ ! 87: /* 1.1 root 88: Find working directory, and store to 'szWorkingDir' 1.1.1.3 root 89: Note: I'm not sure if this function is still usefull in Hatari - Thothy 1.1 root 90: */ 1.1.1.3 root 91: void Misc_FindWorkingDirectory(char *prgname) 1.1 root 92: { 1.1.1.3 root 93: char szSrcDir[256], szSrcName[128], szSrcExt[32]; 1.1 root 94: 1.1.1.3 root 95: /* Find name of program */ 96: strncpy(szWorkingDir, prgname, MAX_FILENAME_LENGTH); 97: File_splitpath(szWorkingDir, szSrcDir, szSrcName, szSrcExt); 98: File_makepath(szWorkingDir, szSrcDir, "", ""); 99: /* And remove trailing backslash */ 1.1 root 100: if (strlen(szWorkingDir)>0) { 101: if (szWorkingDir[strlen(szWorkingDir)-1]=='/') 102: szWorkingDir[strlen(szWorkingDir)-1]='\0'; 103: } 1.1.1.3 root 104: 1.1 root 105: } 106: 1.1.1.2 root 107: 108: /*-----------------------------------------------------------------------*/ 1.1 root 109: /* 110: Limit integer between min/max range 111: */ 112: int Misc_LimitInt(int Value, int MinRange, int MaxRange) 113: { 114: if (Value<MinRange) 115: Value = MinRange; 116: else if (Value>MaxRange) 117: Value = MaxRange; 118: 119: return(Value); 120: } 121: 1.1.1.2 root 122: 123: /*-----------------------------------------------------------------------*/ 1.1 root 124: /* 125: Convert value to 2-digit BCD 126: */ 127: unsigned char Misc_ConvertToBCD(unsigned short int Value) 128: { 1.1.1.4 ! root 129: return (((Value/10))<<4) | (Value%10); 1.1 root 130: } 131: 1.1.1.2 root 132: 133: /*-----------------------------------------------------------------------*/ 1.1 root 134: /* 135: See own random number(must be !=0) 136: */ 137: void Misc_SeedRandom(unsigned long Seed) 138: { 139: RandomNum = Seed; 140: } 141: 1.1.1.2 root 142: 143: /*-----------------------------------------------------------------------*/ 1.1 root 144: /* 145: Get mext random number 146: */ 147: long Misc_NextLongRand(long Seed) 148: { 149: unsigned long Lo, Hi; 150: 151: Lo = 16807 * (long)(Seed & 0xffff); 152: Hi = 16807 * (long)((unsigned long)Seed >> 16); 153: Lo += (Hi & 0x7fff) << 16; 154: if (Lo > 2147483647L) { 155: Lo &= 2147483647L; 156: ++Lo; 157: } 158: Lo += Hi >> 15; 159: if (Lo > 2147483647L) { 160: Lo &= 2147483647L; 161: ++Lo; 162: } 163: return((long)Lo); 164: } 165: 1.1.1.2 root 166: 167: /*-----------------------------------------------------------------------*/ 1.1 root 168: /* 169: Get own random number 170: */ 171: long Misc_GetRandom(void) 172: { 173: RandomNum = Misc_NextLongRand(RandomNum); 174: return(RandomNum); 175: } 176:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.