1 /*
2    Hebcal - A Jewish Calendar Generator
3    Copyright (C) 1994-2004  Danny Sadinoff
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License
7    as published by the Free Software Foundation; either version 2
8    of the License, or (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19    Danny Sadinoff can be reached at
20    danny@sadinoff.com
21  */
22 
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include "danlib.h"
29 #include "myerror.h"
30 
31 
32 /* Some generally useful routines */
33 
initStr(char ** s,size_t size)34 void initStr( char **s, size_t size )
35 {
36     /* allocate space for a string */
37     if ((*s = (char *) malloc ((size + 1) * sizeof (char))) == NULL)
38         die ("\n Memory Error: Couldn't allocate string", "");
39     **s = '\0';
40 }
41 
42 /* istrncasecmp performs a signed, case-insensitive comparison
43    of s1 to s2, for a maximum length of n  bytes. */
istrncasecmp(size_t n,const char * s1,const char * s2)44 int istrncasecmp( size_t n, const char *s1, const char *s2 )
45 {
46     for (;
47          n > 0 &&			/* while not at end of string */
48              (*s1 || *s2);		/* and they both didn't finish simultaneously */
49          n--, s1++, s2++)
50         if (tolower (*s1) != tolower (*s2))
51         {
52             return tolower (*s1) < tolower (*s2) ? -1 : 1;
53         }
54     return 0;
55 }
56 
57 /* find the first element of arr which is a superstring of str, to len
58    places.
59    Return the index of the element.  If there is no such element,
60    Return size.  size is the size of arr.
61  */
lookup_string(const char * str,const char * arr[],int size,int len)62 int lookup_string (const char *str, const char *arr[], int size, int len)
63 {
64     int i;
65 
66     for (i = 0; i < size; i++)
67         if (!(istrncasecmp (len, str, arr[i])))
68             return i;
69     return i;			/* i == size here. */
70 }
71 
72 
73 
isAllNums(const char * s)74 int isAllNums ( const char *s )
75 {
76 
77 /* returns true if a string contains only digits */
78   size_t n = 0;
79   size_t len = strlen( s );
80 
81   for (n = 0;
82        (n < len) && isdigit( (int) s[n] );
83        n++);
84   return (n == len);
85 }
86 
tensDigit(int i)87 static int tensDigit (int i)
88 {
89     return (i % 100) / 10;
90 }
91 
92 /* returns the proper ordinal suffix of a number */
93 const char *
numSuffix(int i)94     numSuffix (int i)
95 {
96     if (tensDigit(i) == 1)
97         return "th";
98     switch (i % 10)
99     {
100     case 1:
101         return "st";
102     case 2:
103         return "nd";
104     case 3:
105         return "rd";
106     default:
107         return "th";
108     }
109 }
110 
hc_itoa(int i)111 char * hc_itoa (int i)
112 {
113     static char ret[7];         /* FIX: eeeviiill */
114     sprintf (ret, "%d", i);
115     return ret;
116 }
117