File: util.c

package info (click to toggle)
bible-kjv 4.41
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 4,760 kB
  • sloc: ansic: 3,589; makefile: 336; sh: 238; perl: 37
file content (206 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* -*-C-*-
*******************************************************************************
*
* File:         util.c
* RCS:          $Header: /home/matthew/cvs/bible-kjv-4.10/util.c,v 2.2 2005/01/22 00:28:09 matthew Exp $
* Description:  Some general-purpose functions.
* Author:       Chip Chapin
* Created:      Mon Dec 21 13:29:17 1992
* Modified:     Mon Apr 26 11:15:51 1993 (Chip Chapin) chip@hpclbis
* Language:     C
* Package:      N/A
* Status:       Experimental (Do Not Distribute)
*
*******************************************************************************
*
* Revisions:
*
* Fri Apr 23 09:49:40 1993 (Chip Chapin) chip@hpclbis
*  Added Univ_Int stuff.
* Tue Dec 22 11:22:41 1992 (Chip Chapin) chip@hpclbis
*  Rewrite without strtok (oops!).
* Mon Dec 21 20:43:01 1992 (Chip Chapin) chip@hpclbis
*  Initial creation and release with Bible Retrieval System 2.0.
*******************************************************************************
* $Log: util.c,v $
* Revision 2.2  2005/01/22 00:28:09  matthew
* tidy up - parentheses around assignment as truth value, clear unused
* variables...
*
* Revision 2.1  2005/01/22 00:20:49  matthew
* prototype functions
*
* Revision 2.0  2003/01/08 15:29:52  matthew
* versions collected from the net
*
 * Revision 1.4  93/04/26  11:18:15  11:18:15  chip (Chip Chapin)
 * Release 4.00
 * Public release of portable datafile version.
 * 
*
*/


#include <stdio.h>
#include "util.h"


FILE *findfile(char *dfname,char *pathlist)
/*----------------------------------------------------------------------
|   NAME:
|       findfile
|
|   ALGORITHM:
|       Given a file name and a path list, attempt to open the
|       file using each possible path until it's opened
|       successfully or the paths are exhausted.
|
|       pathlist -- list of paths separated by blanks or ":".
|       Returns a stream pointer (FILE*).
|
|   HISTORY:
|       921221 cc Created by extraction from tsl.c
|
\*----------------------------------------------------------------------*/
{
#define PATHSZ 1024
    char dfpath[PATHSZ];
    char *dp, *pp, *np;
    FILE *fp;

    fp = NULL;
    pp = pathlist;
    /* Keep trying until we succeed or run out of possible paths */
    while ((fp == NULL) && *pp) {
	dp = dfpath;
	
	/* copy next item in pathlist into dfpath and */
	/* advance pathlist pointer past separator    */
	while (*pp && *pp != ' ' && *pp != ':') *dp++ = *pp++;
	while (*pp == ' ' || *pp == ':') pp++;
	
	/* Ensure a '/' separator between path and filename */
	if (*(dp-1) != '/')
	    *dp++ = '/';
	
	/* Cat dfname onto dfpath, including trailing \0 */
	for (np=dfname; (*dp++ = *np++); )
	    ;
	
	fp=fopen( dfpath, "r");
    }
    return fp;
} /* findfile */



void univ_assign(Univ_Int dst,int src)
/*----------------------------------------------------------------------
|   NAME:
|       univ_assign
|
|   ALGORITHM:
|       Assign value of "src", an int, to "dst", a Univ_Int.
|       The Univ_Int is a 4-byte integer with the bytes in a
|       pre-defined order for portability.  This facilitates the
|       construction of portable data files.
|       
|       The order of the bytes is as follows:
|           
|           dst[0] <- highest-order
|           dst[1] <- next highest
|           dst[2] <- next highest
|           dst[3] <- lowest-order
|
|   HISTORY:
|       930423 cc Initial creation.
|
\*----------------------------------------------------------------------*/
{
    unsigned char b;
    int i;

    for (i=3; i>=0; i--) {
	b = src & 0x000000ff;
	src >>= 8;
	dst[i] = b;
    }
} /* univ_assign */


void shortuniv_assign(Short_Univ_Int dst,int src)
/*----------------------------------------------------------------------
|   NAME:
|       shortuniv_assign
|
|   ALGORITHM:
|       Assign value of "src", an int, to "dst", a Short_Univ_Int.
|       The Short_Univ_Int is a 2-byte integer with the bytes in a
|       pre-defined order for portability.  This facilitates the
|       construction of portable data files.
|       IGNORES src out-of-range errors.
|       
|       The order of the bytes is as follows:
|           
|           dst[0] <- highest-order
|           dst[1] <- lowest-order
|
|   HISTORY:
|       930423 cc Initial creation.
|
\*----------------------------------------------------------------------*/
{
    dst[0] = (src >> 8) & 0x000000ff;
    dst[1] = src & 0x000000ff;
} /* shortuniv_assign */


int univ2int(Univ_Int src)
/*----------------------------------------------------------------------
|   NAME:
|       univ2int
|
|   ALGORITHM:
|       Return integer value of "src", where "src" is a Univ_Int
|       universal integer.
|
|   HISTORY:
|       930423 cc Initial creation.
|
\*----------------------------------------------------------------------*/
{
    int result, i;

    result = 0;
    for (i=0; i<=3; i++) {
	result <<= 8;
	result |= src[i];
    }
    return result;
} /* univ2int */


int shortuniv2int(Short_Univ_Int src)
/*----------------------------------------------------------------------
|   NAME:
|       shortuniv2int
|
|   ALGORITHM:
|       Return integer value of "src", where "src" is a Short_Univ_Int
|       universal integer.
|
|   HISTORY:
|       930423 cc Initial creation.
|
\*----------------------------------------------------------------------*/
{
    int result;

    result = 0;
    result |= src[0];
    result <<= 8;
    result |= src[1];
    return result;
} /* shortuniv2int */