/[dtapublic]/projs/trunk/shared_source/c_datd/strfuncs.c
ViewVC logotype

Diff of /projs/trunk/shared_source/c_datd/strfuncs.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.70  
changed lines
  Added in v.71

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25