1 /* zzrepsub.f -- translated by f2c (version 19980913).
2    You must link the resulting object file with the libraries:
3 	-lf2c -lm   (in that order)
4 */
5 
6 #include "f2c.h"
7 
8 /* Table of constant values */
9 
10 static integer c__3 = 3;
11 
12 /* $Procedure      ZZREPSUB ( Replace one substring with another ) */
zzrepsub_(char * in,integer * left,integer * right,char * string,char * out,ftnlen in_len,ftnlen string_len,ftnlen out_len)13 /* Subroutine */ int zzrepsub_(char *in, integer *left, integer *right, char *
14 	string, char *out, ftnlen in_len, ftnlen string_len, ftnlen out_len)
15 {
16     /* System generated locals */
17     integer i__1, i__2;
18 
19     /* Builtin functions */
20     integer i_len(char *, ftnlen);
21     /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);
22 
23     /* Local variables */
24     integer next, i__, inlen;
25     extern integer sumai_(integer *, integer *);
26     integer remain, myleft, strlen, outlen, myrght, end, use[3];
27 
28 /* $ Abstract */
29 
30 /*     SPICE Private routine intended solely for the support of SPICE */
31 /*     routines.  Users should not call this routine directly due */
32 /*     to the volatile nature of this routine. */
33 
34 /*     Replace the substring (LEFT:RIGHT) with a string of any length. */
35 
36 /* $ Disclaimer */
37 
38 /*     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
39 /*     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
40 /*     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
41 /*     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
42 /*     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
43 /*     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
44 /*     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
45 /*     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
46 /*     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
47 /*     SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
48 
49 /*     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
50 /*     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
51 /*     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
52 /*     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
53 /*     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
54 /*     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
55 
56 /*     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
57 /*     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
58 /*     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
59 /*     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
60 
61 /* $ Required_Reading */
62 
63 /*     None. */
64 
65 /* $ Keywords */
66 
67 /*     ASSIGNMENT */
68 /*     CHARACTER */
69 /*     STRING */
70 
71 /* $ Declarations */
72 /* $ Brief_I/O */
73 
74 /*     VARIABLE  I/O  DESCRIPTION */
75 /*     --------  ---  -------------------------------------------------- */
76 /*     IN         I   Input string. */
77 /*     LEFT, */
78 /*     RIGHT      I   Ends of substring to be replaced. */
79 /*     STRING     I   Replacement string. */
80 /*     OUT        O   Resulting string. */
81 
82 /* $ Detailed_Input */
83 
84 /*     IN         is an arbitrary character string. */
85 
86 /*     LEFT, */
87 /*     RIGHT      are the ends of the substring to be replaced. */
88 /*                Legitimate substrings satisfy the following */
89 /*                conditions */
90 
91 /*                    RIGHT > LEFT - 2 */
92 /*                    LEFT  > 1 */
93 /*                    RIGHT < LEN(STRING) + 1 */
94 
95 /*                This allows users to refer to zero-length substrings */
96 /*                (null substrings) of IN. */
97 
98 /*     STRING     is the replacement string. Essentially, the */
99 /*                substring (LEFT:RIGHT) is removed from the */
100 /*                input string, and STRING is inserted at the */
101 /*                point of removal. */
102 
103 /* $ Detailed_Output */
104 
105 /*     OUT        is the resulting string. OUT may overwrite IN. */
106 
107 /* $ Parameters */
108 
109 /*     None. */
110 
111 /* $ Exceptions */
112 
113 /*     1) If RIGHT is one less than LEFT, the substring to */
114 /*        replace will be the null substring.  In this case, */
115 /*        STRING will be inserted between IN(:RIGHT) and IN(LEFT:). */
116 
117 /*     2) If LEFT is smaller than one, it's treated as 1. */
118 
119 /*     3) If RIGHT is greater than the length of the input string, */
120 /*        it is treated as being the length of the string. */
121 
122 /*     4) If RIGHT is less than LEFT-1, no substitution is made. */
123 
124 /*     5) Whenever the output string is too small to hold the result, */
125 /*        the result is truncated on the right. */
126 
127 /* $ Files */
128 
129 /*     None. */
130 
131 /* $ Particulars */
132 
133 /*     Ideally, replacement could be done with simple concatenation, */
134 
135 /*        OUT = IN(1:LEFT-1) // STRING // IN(RIGHT+1: ) */
136 
137 /*     but the Fortran 77 standard makes this illegal for strings of */
138 /*     unknown length. */
139 
140 /*     This private routine is basically just a copy of the SPICE */
141 /*     routine REPSUB with all error handling removed and "reasonable" */
142 /*     interpretations used for exceptional cases. */
143 
144 /* $ Examples */
145 
146 /*     A typical use for this routine might be to replace all */
147 /*     occurrences of one word in a string with another word. */
148 /*     For example, the following code fragment replaces every */
149 /*     occurrence of the word 'AND' with the word 'OR' in the */
150 /*     character string LINE. */
151 
152 /*        LEFT = WDINDX ( LINE, 'AND' ) */
153 
154 /*        DO WHILE ( LEFT .NE. 0 ) */
155 /*           CALL REPSUB ( LINE, LEFT, LEFT+2, 'OR', LINE ) */
156 /*           LEFT = WDINDX ( LINE, 'AND' ) */
157 /*        END DO */
158 
159 /*     This routine can also be used to insert substring between */
160 /*     two characters.  Consider the string: */
161 
162 /*         IN   = 'The defendant,, was found innocent.' */
163 
164 /*     to insert ' Emelda Marcos' between the first and second commas */
165 /*     determine the location of the pair ',,' */
166 
167 /*        RIGHT = POS ( IN, ',,', 1 ) */
168 /*        LEFT  = RIGHT + 1 */
169 
170 /*     then */
171 
172 /*        CALL REPSUB ( IN, LEFT, RIGHT, ' Emelda Marcos', OUT ) */
173 
174 /*     The output (OUT) will have the value: */
175 
176 /*        'The defendant, Emelda Marcos, was found innocent.' */
177 
178 /* $ Restrictions */
179 
180 /*     The memory used by STRING and OUT must be disjoint. The memory */
181 /*     used by IN and OUT must be identical or disjoint. */
182 
183 /* $ Literature_References */
184 
185 /*     None. */
186 
187 /* $ Author_and_Institution */
188 
189 /*     W.L. Taber     (JPL) */
190 /*     I.M. Underwood (JPL) */
191 
192 /* $ Version */
193 
194 /* -    SPICELIB Version 1.0.0, 27-APR-1996 (WLT) */
195 
196 
197 /* -& */
198 
199 /*     SPICELIB functions */
200 
201 
202 /*     Local variables */
203 
204 
205 /*     Get the lengths of all the strings involved in this transaction. */
206 
207     inlen = i_len(in, in_len);
208     strlen = i_len(string, string_len);
209     outlen = i_len(out, out_len);
210 /* Computing MIN */
211     i__1 = inlen + 1, i__2 = max(1,*left);
212     myleft = min(i__1,i__2);
213 /* Computing MIN */
214     i__1 = inlen, i__2 = max(0,*right);
215     myrght = min(i__1,i__2);
216 
217 /*     Reject bad inputs. */
218 
219     if (myleft < 1) {
220 	myleft = 1;
221     } else if (myrght > inlen) {
222 	myrght = inlen;
223     } else if (*right < *left - 1) {
224 	return 0;
225     }
226 
227 /*     Consider three separate sections: */
228 
229 /*        1) The front of the original string. */
230 
231 /*        2) The replacement string. */
232 
233 /*        3) The end of the original string. */
234 
235 /*     Determine how much of each section to use in the output string. */
236 /*     REMAIN is the number of characters that will fit in the output */
237 /*     string. */
238 
239     remain = outlen;
240 /* Computing MIN */
241     i__1 = remain, i__2 = myleft - 1;
242     use[0] = min(i__1,i__2);
243     remain -= use[0];
244     use[1] = min(remain,strlen);
245     remain -= use[1];
246 /* Computing MIN */
247     i__1 = remain, i__2 = inlen - *right;
248     use[2] = min(i__1,i__2);
249 
250 /*     Move the third section first. It gets moved back to front */
251 /*     or front to back, depending on whether the replacement string */
252 /*     is longer than the original substring. The main thing is to */
253 /*     avoid overwriting characters that have yet to be moved. */
254 
255     end = sumai_(use, &c__3);
256     if (myleft + strlen > *right) {
257 	next = end;
258 	for (i__ = use[2]; i__ >= 1; --i__) {
259 	    i__1 = *right + i__ - 1;
260 	    s_copy(out + (next - 1), in + i__1, (ftnlen)1, *right + i__ -
261 		    i__1);
262 	    --next;
263 	}
264     } else {
265 	next = myleft + strlen;
266 	i__1 = use[2];
267 	for (i__ = 1; i__ <= i__1; ++i__) {
268 	    i__2 = *right + i__ - 1;
269 	    s_copy(out + (next - 1), in + i__2, (ftnlen)1, *right + i__ -
270 		    i__2);
271 	    ++next;
272 	}
273     }
274 
275 /*     The first two sections can be moved directly to the front of */
276 /*     the output string. */
277 
278     next = 1;
279     i__1 = use[0];
280     for (i__ = 1; i__ <= i__1; ++i__) {
281 	*(unsigned char *)&out[next - 1] = *(unsigned char *)&in[i__ - 1];
282 	++next;
283     }
284     i__1 = use[1];
285     for (i__ = 1; i__ <= i__1; ++i__) {
286 	*(unsigned char *)&out[next - 1] = *(unsigned char *)&string[i__ - 1];
287 	++next;
288     }
289 
290 /*     Pad with blanks, if the output string was not filled. */
291 
292     if (end < outlen) {
293 	i__1 = end;
294 	s_copy(out + i__1, " ", out_len - i__1, (ftnlen)1);
295     }
296     return 0;
297 } /* zzrepsub_ */
298 
299