1 |
dashley |
71 |
//$Header$ |
2 |
|
|
//------------------------------------------------------------------------------------------------- |
3 |
|
|
//This file is part of "David T. Ashley's Shared Source Code", a set of shared components |
4 |
|
|
//integrated into many of David T. Ashley's projects. |
5 |
|
|
//------------------------------------------------------------------------------------------------- |
6 |
|
|
//This source code and any program in which it is compiled/used is provided under the MIT License, |
7 |
|
|
//reproduced below. |
8 |
|
|
//------------------------------------------------------------------------------------------------- |
9 |
|
|
//Permission is hereby granted, free of charge, to any person obtaining a copy of |
10 |
|
|
//this software and associated documentation files(the "Software"), to deal in the |
11 |
|
|
//Software without restriction, including without limitation the rights to use, |
12 |
|
|
//copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
13 |
|
|
//Software, and to permit persons to whom the Software is furnished to do so, |
14 |
|
|
//subject to the following conditions : |
15 |
|
|
// |
16 |
|
|
//The above copyright notice and this permission notice shall be included in all |
17 |
|
|
//copies or substantial portions of the Software. |
18 |
|
|
// |
19 |
|
|
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
|
|
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
|
|
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
22 |
|
|
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
|
|
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
|
|
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
|
|
//SOFTWARE. |
26 |
|
|
//------------------------------------------------------------------------------------------------- |
27 |
|
|
#define MODULE_STRFUNCS |
28 |
|
|
|
29 |
|
|
#include <string.h> |
30 |
|
|
|
31 |
|
|
#include "strfuncs.h" |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
//Converts from an unsigned char quantity to a hexadecimal digit, with |
35 |
|
|
//upper-case/lower-case option. Only the lowest four bits are considered. |
36 |
|
|
DECMOD_STRFUNCS |
37 |
|
|
char Strfuncs_NibbleToHexadecimalChar(unsigned char arg, int use_upper_case) |
38 |
|
|
{ |
39 |
|
|
char rv; |
40 |
|
|
|
41 |
|
|
switch (arg & 0x0F) |
42 |
|
|
{ |
43 |
|
|
case 0: |
44 |
|
|
rv = '0'; |
45 |
|
|
break; |
46 |
|
|
case 1: |
47 |
|
|
rv = '1'; |
48 |
|
|
break; |
49 |
|
|
case 2: |
50 |
|
|
rv = '2'; |
51 |
|
|
break; |
52 |
|
|
case 3: |
53 |
|
|
rv = '3'; |
54 |
|
|
break; |
55 |
|
|
case 4: |
56 |
|
|
rv = '4'; |
57 |
|
|
break; |
58 |
|
|
case 5: |
59 |
|
|
rv = '5'; |
60 |
|
|
break; |
61 |
|
|
case 6: |
62 |
|
|
rv = '6'; |
63 |
|
|
break; |
64 |
|
|
case 7: |
65 |
|
|
rv = '7'; |
66 |
|
|
break; |
67 |
|
|
case 8: |
68 |
|
|
rv = '8'; |
69 |
|
|
break; |
70 |
|
|
case 9: |
71 |
|
|
rv = '9'; |
72 |
|
|
break; |
73 |
|
|
case 10: |
74 |
|
|
rv = 'A'; |
75 |
|
|
break; |
76 |
|
|
case 11: |
77 |
|
|
rv = 'B'; |
78 |
|
|
break; |
79 |
|
|
case 12: |
80 |
|
|
rv = 'C'; |
81 |
|
|
break; |
82 |
|
|
case 13: |
83 |
|
|
rv = 'D'; |
84 |
|
|
break; |
85 |
|
|
case 14: |
86 |
|
|
rv = 'E'; |
87 |
|
|
break; |
88 |
|
|
case 15: |
89 |
|
|
rv = 'F'; |
90 |
|
|
break; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
//If upper-case is not desired, convert to lower. |
94 |
|
|
if (!use_upper_case && (rv >= 'A')) |
95 |
|
|
rv += ('a'-'A'); |
96 |
|
|
|
97 |
|
|
return(rv); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
//Given a single unsigned long number, assigns eight consecutive characters of a |
102 |
|
|
//string to be the hexadecimal digits of the number. The eight characters must |
103 |
|
|
//already be allocated in the caller's area. An option is provided to use |
104 |
|
|
//upper-case or lower-case. The caller is responsible for null termination. |
105 |
|
|
DECMOD_STRFUNCS |
106 |
|
|
void Strfuncs_UlToHexString(unsigned long num, |
107 |
|
|
char *output, |
108 |
|
|
int use_upper_case) |
109 |
|
|
{ |
110 |
|
|
unsigned i; |
111 |
|
|
|
112 |
|
|
for (i=0; i<8; i++) |
113 |
|
|
{ |
114 |
|
|
output[7-i] = Strfuncs_NibbleToHexadecimalChar((unsigned char)num, use_upper_case); |
115 |
|
|
num >>= 4; |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
//Returns true if substr is a substring of arg. An empty string is a substring |
121 |
|
|
//of anything except a NULL pointer, and a NULL pointer is a substring of |
122 |
|
|
//anything. |
123 |
|
|
DECMOD_STRFUNCS |
124 |
|
|
int Strfuncs_IsSubstring(const char *substr, const char *arg) |
125 |
|
|
{ |
126 |
|
|
size_t i; |
127 |
|
|
|
128 |
|
|
if (!substr) |
129 |
|
|
{ |
130 |
|
|
return(1); |
131 |
|
|
//A NULL pointer is a substring of anything. |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
if (!arg) |
135 |
|
|
{ |
136 |
|
|
return(0); |
137 |
|
|
//arg is NULL but substr is not, no substring. |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
//Will not be here unless both pointers are valid. These are the |
141 |
|
|
//non-degenerate cases. |
142 |
|
|
i = 0; |
143 |
|
|
while(1) |
144 |
|
|
{ |
145 |
|
|
if (!substr[i]) |
146 |
|
|
{ |
147 |
|
|
return(1); |
148 |
|
|
//Ran out of substr chars before arg chars. Is a substring. |
149 |
|
|
} |
150 |
|
|
if (!arg[i]) |
151 |
|
|
{ |
152 |
|
|
return(0); |
153 |
|
|
//Ran out of arg characters with substr chars still remaining. |
154 |
|
|
//Definitely not a substring. |
155 |
|
|
} |
156 |
|
|
//Have exhausted neither string. Not a substring if lack of equality. |
157 |
|
|
if (substr[i] != arg[i]) |
158 |
|
|
{ |
159 |
|
|
return(0); |
160 |
|
|
//Definitely not a substring. |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
//Can't make a determination. Advance to next character. |
164 |
|
|
i++; |
165 |
|
|
} |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
|
169 |
|
|
//Returns version control string for file. |
170 |
|
|
// |
171 |
|
|
DECMOD_STRFUNCS |
172 |
|
|
const char *StrfuncsCversion(void) |
173 |
|
|
{ |
174 |
|
|
return ("$Header$"); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
|
178 |
|
|
//Returns version control string for associated .H file. |
179 |
|
|
// |
180 |
|
|
DECMOD_STRFUNCS |
181 |
|
|
const char *StrfuncsHversion(void) |
182 |
|
|
{ |
183 |
|
|
return (STRFUNCS_H_VERSION); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
//End of strfuncs.c. |