|
|
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.5 ! root 9: char Misc_rcsid[] = "Hatari $Id: misc.c,v 1.9 2004/04/19 08:53:34 thothy Exp $";
1.1.1.4 root 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:
1.1.1.5 ! root 20:
! 21: static long RandomNum;
1.1 root 22:
1.1.1.2 root 23:
24: /*-----------------------------------------------------------------------*/
1.1 root 25: /*
26: Fill end of string out with spaces
27: */
28: void Misc_PadStringWithSpaces(char *pszString,int nChars)
29: {
30: int i;
31:
32: for(i=nChars; i>=(int)strlen(pszString); i--) {
33: pszString[i] = ' ';
34: }
35: pszString[nChars] = '\0';
36: }
37:
1.1.1.2 root 38:
39: /*-----------------------------------------------------------------------*/
1.1 root 40: /*
41: Remove any spaces from string
42: */
43: void Misc_RemoveSpacesFromString(char *pszSrcString, char *pszDestString)
44: {
45: int i=0,j=0;
46:
1.1.1.2 root 47: /* Copy string */
1.1 root 48: while(pszSrcString[i]!='\0') {
1.1.1.2 root 49: if (pszSrcString[i]!=' ') { /* Copy character if not a white-space */
1.1 root 50: pszDestString[j] = pszSrcString[i];
51: j++;
52: }
53: i++;
54: }
55:
1.1.1.2 root 56: pszDestString[j] = '\0'; /* Term */
1.1 root 57: }
58:
1.1.1.2 root 59:
60: /*-----------------------------------------------------------------------*/
1.1 root 61: /*
62: Remove 'white-space' from beginning of text string
63: */
64: void Misc_RemoveWhiteSpace(char *pszString,int Length)
65: {
66: while( (*pszString==' ') || (*pszString=='\t') ) {
1.1.1.2 root 67: /* Copy line left one character */
1.1 root 68: memmove(pszString,pszString+1,Length-1);
69: }
70: }
71:
1.1.1.2 root 72:
73: /*-----------------------------------------------------------------------*/
1.1 root 74: /*
1.1.1.4 root 75: Convert a string to uppercase.
76: */
77: void Misc_strupr(char *pString)
78: {
79: while(*pString)
80: {
81: *pString = toupper(*pString);
82: pString++;
83: }
84: }
85:
86:
87: /*-----------------------------------------------------------------------*/
88: /*
1.1 root 89: Limit integer between min/max range
90: */
91: int Misc_LimitInt(int Value, int MinRange, int MaxRange)
92: {
93: if (Value<MinRange)
94: Value = MinRange;
95: else if (Value>MaxRange)
96: Value = MaxRange;
97:
98: return(Value);
99: }
100:
1.1.1.2 root 101:
102: /*-----------------------------------------------------------------------*/
1.1 root 103: /*
104: Convert value to 2-digit BCD
105: */
106: unsigned char Misc_ConvertToBCD(unsigned short int Value)
107: {
1.1.1.4 root 108: return (((Value/10))<<4) | (Value%10);
1.1 root 109: }
110:
1.1.1.2 root 111:
112: /*-----------------------------------------------------------------------*/
1.1 root 113: /*
114: See own random number(must be !=0)
115: */
116: void Misc_SeedRandom(unsigned long Seed)
117: {
118: RandomNum = Seed;
119: }
120:
1.1.1.2 root 121:
122: /*-----------------------------------------------------------------------*/
1.1 root 123: /*
124: Get mext random number
125: */
1.1.1.5 ! root 126: static long Misc_NextLongRand(long Seed)
1.1 root 127: {
128: unsigned long Lo, Hi;
129:
130: Lo = 16807 * (long)(Seed & 0xffff);
131: Hi = 16807 * (long)((unsigned long)Seed >> 16);
132: Lo += (Hi & 0x7fff) << 16;
133: if (Lo > 2147483647L) {
134: Lo &= 2147483647L;
135: ++Lo;
136: }
137: Lo += Hi >> 15;
138: if (Lo > 2147483647L) {
139: Lo &= 2147483647L;
140: ++Lo;
141: }
142: return((long)Lo);
143: }
144:
1.1.1.2 root 145:
146: /*-----------------------------------------------------------------------*/
1.1 root 147: /*
148: Get own random number
149: */
150: long Misc_GetRandom(void)
151: {
152: RandomNum = Misc_NextLongRand(RandomNum);
153: return(RandomNum);
154: }
155:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.