source: trunk/kLdr/kLdrHlpStr.c@ 2944

Last change on this file since 2944 was 2944, checked in by bird, 19 years ago

split up kLdrHlp.c and kLdr.c to make it more flexible (like using the module interpreters without the dynamic loader bit and similar).

  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/* $Id: kLdrHlpStr.c 2944 2007-01-13 15:55:40Z bird $ */
2/** @file
3 *
4 * kLdr - The Dynamic Loader, String Helper Functions.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
7 *
8 *
9 * This file is part of kLdr.
10 *
11 * kLdr is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLdr is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLdr; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <kLdr.h>
32#include "kLdrHlp.h"
33
34
35/**
36 * Converts an signed integer to an ascii string.
37 *
38 * @returns psz.
39 * @param psz Pointer to the output buffer.
40 * @param cch The size of the output buffer.
41 * @param lVal The value.
42 * @param iBase The base to format it. (2,8,10 or 16)
43 */
44char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase)
45{
46 static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
47 char *pszRet = psz;
48
49 if (cch >= (lVal < 0 ? 3U : 2U) && psz)
50 {
51 /* prefix */
52 if (lVal < 0)
53 {
54 *psz++ = '-';
55 cch--;
56 lVal = -lVal;
57 }
58
59 /* the digits */
60 do
61 {
62 *psz++ = s_szDigits[lVal % iBase];
63 cch--;
64 lVal /= iBase;
65 } while (lVal && cch > 1);
66
67 /* overflow indicator */
68 if (lVal)
69 psz[-1] = '+';
70 }
71 else if (!pszRet)
72 return pszRet;
73 else if (cch < 1 || !pszRet)
74 return pszRet;
75 else
76 *psz++ = '+';
77 *psz = '\0';
78
79 return pszRet;
80}
81
82
83int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb)
84{
85 const uint8_t *pb1 = (const uint8_t *)pv1;
86 const uint8_t *pb2 = (const uint8_t *)pv2;
87 while (cb-- > 0)
88 {
89 if (*pb1 != *pb2)
90 {
91 const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
92 const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
93 if (u1 != u2)
94 return (int)*pb1 - (int)*pb2;
95 }
96 pb1++;
97 pb2++;
98 }
99 return 0;
100}
101
102
103int kLdrHlpStrIComp(const char *pv1, const char *pv2)
104{
105 const uint8_t *pb1 = (const uint8_t *)pv1;
106 const uint8_t *pb2 = (const uint8_t *)pv2;
107 for (;;)
108 {
109 if (*pb1 != *pb2)
110 {
111 const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
112 const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
113 if (u1 != u2)
114 return (int)*pb1 - (int)*pb2;
115 }
116 if (!*pb1)
117 break;
118 pb1++;
119 pb2++;
120 }
121 return 0;
122}
123
124
125#ifdef kLdrHlpStrChr_needed
126char *kLdrHlpStrChr(const char *psz, int ch)
127{
128 while (*psz)
129 {
130 if (*psz == ch)
131 return (char *)psz;
132 psz++;
133 }
134 return NULL;
135}
136#endif
137
138
139#ifdef kLdrHlpMemChr_needed
140void *kLdrHlpMemChr(const void *pv, int ch, size_t cb)
141{
142 const uint8_t *pb = (const uint8_t *)pv;
143 const uint8_t b = (uint8_t)ch;
144 while (cb-- > 0)
145 {
146 if (*pb == b)
147 return (void *)pb;
148 pb++;
149 }
150 return NULL;
151}
152#endif
153
154
155#ifdef kLdrHlpMemMove_needed
156void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb)
157{
158 uint8_t *pbDst = (uint8_t *)pv1;
159 const uint8_t *pbSrc = (const uint8_t *)pv2;
160 while (cb-- > 0)
161 {
162 const uint8_t b = *pbSrc++;
163 *pbDst++ = b;
164 }
165 return pv1;
166}
167#endif
168
169
170#ifdef kLdrHlpStrNComp_needed
171int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch)
172{
173 while (cch-- > 0)
174 {
175 if (*psz1 != *psz2)
176 return (int)*psz1 - (int)*psz2;
177 if (!*psz1)
178 break;
179 psz1++;
180 psz2++;
181 }
182 return 0;
183}
184#endif
185
Note: See TracBrowser for help on using the repository browser.