]>
Commit | Line | Data |
---|---|---|
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 | |
8 | char *strtok(char *str, const char *delim); | |
9 | #endif | |
10 | ||
11 | int stricmp(const char *s1, const char *s2); | |
12 | int strnicmp(const char *s1, const char *s2, size_t len); | |
13 | // String Functions | |
14 | ||
15 | int 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 | ||
32 | char *spaces(int len, char *seperator) | |
33 | { | |
34 | char *final; | |
35 | final = new char[30]; | |
36 | int y; | |
37 | strcpy(final, seperator); | |
38 | for (y = 0; y < 30 - len; y++) | |
39 | strcat(final, seperator); | |
40 | return final; | |
41 | } | |
42 | ||
43 | ||
44 | ||
45 | int strnicmp(const char *s1, const char *s2, size_t len) | |
46 | { | |
47 | register int c; | |
48 | ||
49 | if (!len) | |
50 | return 0; | |
51 | while ((c = tolower(*s1)) == tolower(*s2) && len > 0) { | |
52 | if (c == 0 || --len == 0) | |
53 | return 0; | |
54 | s1++; | |
55 | s2++; | |
56 | } | |
57 | if (c < tolower(*s2)) | |
58 | return -1; | |
59 | return 1; | |
60 | } | |
61 | ||
62 | #ifndef HAVE_STRTOK | |
63 | char *strtok(char *str, const char *delim) | |
64 | { | |
65 | static char *current = NULL; | |
66 | char *ret; | |
67 | ||
68 | if (str) | |
69 | current = str; | |
70 | if (!current) | |
71 | return NULL; | |
72 | current += strspn(current, delim); | |
73 | ret = *current ? current : NULL; | |
74 | current += strcspn(current, delim); | |
75 | if (!*current) | |
76 | current = NULL; | |
77 | else | |
78 | *current++ = 0; | |
79 | return ret; | |
80 | } | |
81 | #endif | |
82 | ||
83 | int isstringnum(char *num) | |
84 | { | |
85 | unsigned int x; | |
86 | for (x = 0; x < strlen(num); x++) | |
87 | { | |
88 | if ((int)num[x] < 48 || (int)num[x] > 57) | |
89 | return 0; | |
90 | } | |
91 | return 1; | |
92 | } | |
93 | ||
94 | long int stringtoint(char *number) | |
95 | { | |
96 | return atol(number); | |
97 | } | |
98 | ||
99 | long int pow(int x, int y) | |
100 | { | |
101 | long int value = 0; | |
102 | int count = 0; | |
103 | value += x; | |
104 | ||
105 | if (x != 0 && y != 0) | |
106 | { | |
107 | for (count = 1; count <= y - 1; count++) | |
108 | value *= x; | |
109 | } | |
110 | else | |
111 | { | |
112 | return 1; | |
113 | } | |
114 | return value; | |
115 | } | |
116 | ||
117 | long int chartoint(char ch) | |
118 | { | |
119 | if (int(ch) >= 48 && int(ch) <= 57) | |
120 | return int(ch) - 48; | |
121 | else | |
122 | return 0; | |
123 | } |