File: utils.c

package info (click to toggle)
qpopper 4.0.5-4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,732 kB
  • ctags: 1,940
  • sloc: ansic: 21,790; makefile: 459; sh: 272; perl: 52
file content (174 lines) | stat: -rw-r--r-- 3,119 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 2003 QUALCOMM Incorporated.  All rights reserved.
 * The file License.txt specifies the terms for use, modification,
 * and redistribution.
 *
 * Revisions:
 *
 *  06/29/00 [rcg]
 *           - Moved to /common/, made consistent with other
 *             string utils.
 *
 *  02/14/00 [rcg]
 *           - Modified StrNCat0 to make len *not* const.
 *
 *  02/10/00 [rcg]
 *           - Modified StrNCat0 to make len const to
 *             agree with prototype in .h
 *
 * 12/1997   [lgl]
 *           - File added.
 */


#ifndef UNIX
#  include <Pilot.h>
#else /* UNIX */

#  include <stdio.h>
#  include <stdlib.h>
#  include <string.h>

#  define StrCompare strcmp
#  define StrCopy    strcpy
#  define StrCat     strcat
#  define StrLen     strlen

#endif /* UNIX */


#include "utils.h"


BOOL CharEQI( char c1, char c2 ) { 
   if ( c1 == c2 )
      return TRUE;
   c1 |= 32;
   return (BOOL) ( BETWEEN ( c1, 'a', 'z' +1 )  &&  c1 == ( c2 | 32 ) );
}


BOOL CharIsAlpha ( char ch ) {
   return (BOOL) BETWEEN ( ( ch | 32 ), 'a', 'z' + 1 );
}


BOOL CharIsSgmlTok ( char ch ) {
  return (BOOL) (   BETWEEN ( ch | 32, 'a', 'z' + 1 )
                 || BETWEEN ( ch, '0', '9' +1 )
                 || ch == '@'
                 || ch == '_'
                 || ch == '.'
                 || ch == '-'
                 || ch == '+'
                 || ch == '$');
}



/*
||  If the compiler doesn't support inline functions, these are better as macros:
*/

BOOL
CharIsAlnum ( char ch ) 
{
   return (BOOL) (   BETWEEN ( ch, '0', '9' +1 )
                  || BETWEEN ( ch | 32, 'a', 'z' +1 ) );

}


int
Constrain ( int nVal, int nMin, int nMax ) 
{
   return ( nVal < nMin ? nMin :
            nVal > nMax ? nMax :
            nVal );
}


/* Return TRUE if s1 and s2 are equal */
int
StrEQI ( CSTR s1, CSTR s2 ) 
{
  if ( s1 == s2 )
    return ( TRUE );

   while ( CharEQI ( *s1, *s2 ) ) {
      if ( *s1 == 0 || *s2 == 0 )
         return ( *s1 == *s2 );
      ++s1;
      ++s2;
   }
   return ( CharEQI ( *s1, *s2 ) );
}


BOOL
StrBeginsI ( char *szPrefix, char *sz ) 
{
   char ch;
   while ( 0 != ( ch = *szPrefix ) ) {
      if ( !CharEQI ( ch, *sz ) )
         return FALSE;

      ++szPrefix;
      ++sz;
   }
   return TRUE;
}


/*
|| Read an integer at (*pszEnt), setting *pszEnt to the character
|| following the integer.
|| Return the value of the integer.
*/
int
ScanInt ( CSTR *pszEnt )  
{
   CSTR szEnt = *pszEnt;
   int n = 0;

   while ( BETWEEN ( *szEnt, '0', '9' + 1 ) ) {
      n = n * 10 + *szEnt - '0';
      ++szEnt;
   }
   *pszEnt = szEnt;
   return n;
}



void
StrNCat0 ( char *dest, const char *src, int len )
{
  while ( *dest )
    dest++; 
  while ( len-- && ( *dest++ = *src++ ) );
  *dest = '\0';
}

char
*StrNDup ( char *src, const int len )
{
    char *local = (char *) malloc ( len+1 );
    if ( local ) {
        strncpy ( local, src, len );
        local[len] = '\0';
    }
    return local;
}

#ifdef UNIX
void
MemSet ( char *dest, unsigned int len, char value )
{
  char *destEnd = dest + len;
  while ( dest != destEnd )
    *dest++ = value;
}
#endif