chiark / gitweb /
Merge remote-tracking branch 'refs/remotes/dgit/dgit/sid'
[bible-kjv.git] / util.c
1 /* -*-C-*-
2 *******************************************************************************
3 *
4 * File:         util.c
5 * RCS:          $Header: /home/matthew/cvs/bible-kjv-4.10/util.c,v 2.2 2005/01/22 00:28:09 matthew Exp $
6 * Description:  Some general-purpose functions.
7 * Author:       Chip Chapin
8 * Created:      Mon Dec 21 13:29:17 1992
9 * Modified:     Mon Apr 26 11:15:51 1993 (Chip Chapin) chip@hpclbis
10 * Language:     C
11 * Package:      N/A
12 * Status:       Experimental (Do Not Distribute)
13 *
14 *******************************************************************************
15 *
16 * Revisions:
17 *
18 * Fri Apr 23 09:49:40 1993 (Chip Chapin) chip@hpclbis
19 *  Added Univ_Int stuff.
20 * Tue Dec 22 11:22:41 1992 (Chip Chapin) chip@hpclbis
21 *  Rewrite without strtok (oops!).
22 * Mon Dec 21 20:43:01 1992 (Chip Chapin) chip@hpclbis
23 *  Initial creation and release with Bible Retrieval System 2.0.
24 *******************************************************************************
25 * $Log: util.c,v $
26 * Revision 2.2  2005/01/22 00:28:09  matthew
27 * tidy up - parentheses around assignment as truth value, clear unused
28 * variables...
29 *
30 * Revision 2.1  2005/01/22 00:20:49  matthew
31 * prototype functions
32 *
33 * Revision 2.0  2003/01/08 15:29:52  matthew
34 * versions collected from the net
35 *
36  * Revision 1.4  93/04/26  11:18:15  11:18:15  chip (Chip Chapin)
37  * Release 4.00
38  * Public release of portable datafile version.
39  * 
40 *
41 */
42
43
44 #include <stdio.h>
45 #include "util.h"
46
47
48 FILE *findfile(char *dfname,char *pathlist)
49 /*----------------------------------------------------------------------
50 |   NAME:
51 |       findfile
52 |
53 |   ALGORITHM:
54 |       Given a file name and a path list, attempt to open the
55 |       file using each possible path until it's opened
56 |       successfully or the paths are exhausted.
57 |
58 |       pathlist -- list of paths separated by blanks or ":".
59 |       Returns a stream pointer (FILE*).
60 |
61 |   HISTORY:
62 |       921221 cc Created by extraction from tsl.c
63 |
64 \*----------------------------------------------------------------------*/
65 {
66 #define PATHSZ 1024
67     char dfpath[PATHSZ];
68     char *dp, *pp, *np;
69     FILE *fp;
70
71     fp = NULL;
72     pp = pathlist;
73     /* Keep trying until we succeed or run out of possible paths */
74     while ((fp == NULL) && *pp) {
75         dp = dfpath;
76         
77         /* copy next item in pathlist into dfpath and */
78         /* advance pathlist pointer past separator    */
79         while (*pp && *pp != ' ' && *pp != ':') *dp++ = *pp++;
80         while (*pp == ' ' || *pp == ':') pp++;
81         
82         /* Ensure a '/' separator between path and filename */
83         if (*(dp-1) != '/')
84             *dp++ = '/';
85         
86         /* Cat dfname onto dfpath, including trailing \0 */
87         for (np=dfname; (*dp++ = *np++); )
88             ;
89         
90         fp=fopen( dfpath, "r");
91     }
92     return fp;
93 } /* findfile */
94
95
96
97 void univ_assign(Univ_Int dst,int src)
98 /*----------------------------------------------------------------------
99 |   NAME:
100 |       univ_assign
101 |
102 |   ALGORITHM:
103 |       Assign value of "src", an int, to "dst", a Univ_Int.
104 |       The Univ_Int is a 4-byte integer with the bytes in a
105 |       pre-defined order for portability.  This facilitates the
106 |       construction of portable data files.
107 |       
108 |       The order of the bytes is as follows:
109 |           
110 |           dst[0] <- highest-order
111 |           dst[1] <- next highest
112 |           dst[2] <- next highest
113 |           dst[3] <- lowest-order
114 |
115 |   HISTORY:
116 |       930423 cc Initial creation.
117 |
118 \*----------------------------------------------------------------------*/
119 {
120     unsigned char b;
121     int i;
122
123     for (i=3; i>=0; i--) {
124         b = src & 0x000000ff;
125         src >>= 8;
126         dst[i] = b;
127     }
128 } /* univ_assign */
129
130
131 void shortuniv_assign(Univ_Int dst,int src)
132 /*----------------------------------------------------------------------
133 |   NAME:
134 |       shortuniv_assign
135 |
136 |   ALGORITHM:
137 |       Assign value of "src", an int, to "dst", a Short_Univ_Int.
138 |       The Short_Univ_Int is a 2-byte integer with the bytes in a
139 |       pre-defined order for portability.  This facilitates the
140 |       construction of portable data files.
141 |       IGNORES src out-of-range errors.
142 |       
143 |       The order of the bytes is as follows:
144 |           
145 |           dst[0] <- highest-order
146 |           dst[1] <- lowest-order
147 |
148 |   HISTORY:
149 |       930423 cc Initial creation.
150 |
151 \*----------------------------------------------------------------------*/
152 {
153     dst[0] = (src >> 8) & 0x000000ff;
154     dst[1] = src & 0x000000ff;
155 } /* shortuniv_assign */
156
157
158 int univ2int(Univ_Int src)
159 /*----------------------------------------------------------------------
160 |   NAME:
161 |       univ2int
162 |
163 |   ALGORITHM:
164 |       Return integer value of "src", where "src" is a Univ_Int
165 |       universal integer.
166 |
167 |   HISTORY:
168 |       930423 cc Initial creation.
169 |
170 \*----------------------------------------------------------------------*/
171 {
172     int result, i;
173
174     result = 0;
175     for (i=0; i<=3; i++) {
176         result <<= 8;
177         result |= src[i];
178     }
179     return result;
180 } /* univ2int */
181
182
183 int shortuniv2int(Short_Univ_Int src)
184 /*----------------------------------------------------------------------
185 |   NAME:
186 |       shortuniv2int
187 |
188 |   ALGORITHM:
189 |       Return integer value of "src", where "src" is a Short_Univ_Int
190 |       universal integer.
191 |
192 |   HISTORY:
193 |       930423 cc Initial creation.
194 |
195 \*----------------------------------------------------------------------*/
196 {
197     int result;
198
199     result = 0;
200     result |= src[0];
201     result <<= 8;
202     result |= src[1];
203     return result;
204 } /* shortuniv2int */
205
206