]> jfr.im git - irc/gameservirc.git/blame - gameserv/strings.cpp
More exciting additions to FilePlayerDAO!
[irc/gameservirc.git] / gameserv / strings.cpp
CommitLineData
32ae1ba9 1 /* This file contains functions dealing with strings */
2
3 #include "extern.h"
4 #include "config.h"
5
6 // String functions
7#ifndef HAVE_STRTOK
8char *strtok(char *str, const char *delim);
9#endif
10
11int stricmp(const char *s1, const char *s2);
12int strnicmp(const char *s1, const char *s2, size_t len);
13// String Functions
14
15int stricmp(const char *s1, const char *s2)
16{
17 register int c;
18
19 while ((c = tolower(*s1)) == tolower(*s2)) {
20 if (c == 0)
21 return 0;
22 s1++;
23 s2++;
24 }
25 if (c < tolower(*s2))
26 return -1;
27 return 1;
28}
29
30
31
69db6f3f 32string spaces(int len, const string &seperator)
32ae1ba9 33{
69db6f3f 34 string final;
32ae1ba9 35 int y;
69db6f3f 36 final += seperator;
37
32ae1ba9 38 for (y = 0; y < 30 - len; y++)
69db6f3f 39 final += seperator;
40
32ae1ba9 41 return final;
42}
43
44
45
46int strnicmp(const char *s1, const char *s2, size_t len)
47{
48 register int c;
49
50 if (!len)
51 return 0;
52 while ((c = tolower(*s1)) == tolower(*s2) && len > 0) {
53 if (c == 0 || --len == 0)
54 return 0;
55 s1++;
56 s2++;
57 }
58 if (c < tolower(*s2))
59 return -1;
60 return 1;
61}
62
63#ifndef HAVE_STRTOK
64char *strtok(char *str, const char *delim)
65{
66 static char *current = NULL;
67 char *ret;
68
69 if (str)
70 current = str;
71 if (!current)
72 return NULL;
73 current += strspn(current, delim);
74 ret = *current ? current : NULL;
75 current += strcspn(current, delim);
76 if (!*current)
77 current = NULL;
78 else
79 *current++ = 0;
80 return ret;
81}
82#endif
83
84int isstringnum(char *num)
85{
86 unsigned int x;
87 for (x = 0; x < strlen(num); x++)
88 {
89 if ((int)num[x] < 48 || (int)num[x] > 57)
90 return 0;
91 }
92return 1;
93}
94
95long int stringtoint(char *number)
96{
97 return atol(number);
98}
99
100long int pow(int x, int y)
101{
102 long int value = 0;
103 int count = 0;
104 value += x;
105
106 if (x != 0 && y != 0)
107 {
108 for (count = 1; count <= y - 1; count++)
109 value *= x;
110 }
111 else
112 {
113 return 1;
114 }
115 return value;
116}
117
118long int chartoint(char ch)
119{
120 if (int(ch) >= 48 && int(ch) <= 57)
121 return int(ch) - 48;
122 else
123 return 0;
124}