1 /* This software was developed by Bruce Hendrickson and Robert Leland *
2 * at Sandia National Laboratories under US Department of Energy *
3 * contract DE-AC04-76DP00789 and is copyrighted by Sandia Corporation. */
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include "defs.h"
9 #include "params.h"
10
11 static char line[LINE_LENGTH]; /* space to hold values */
12 static int offset = 0; /* offset into line for next data */
13 static int break_pnt = LINE_LENGTH; /* place in sequence to pause */
14 static int save_pnt; /* place in sequence to save */
15
16 static void flush_line();
17
read_val(infile,end_flag)18 double read_val(infile, end_flag)
19 FILE *infile; /* file to read value from */
20 int *end_flag; /* 0 => OK, 1 => EOL, -1 => EOF */
21 {
22 double val; /* return value */
23 char *ptr; /* ptr to next string to read */
24 char *ptr2; /* ptr to next string to read */
25 int length; /* length of line to read */
26 int length_left; /* length of line still around */
27 int white_seen; /* have I detected white space yet? */
28 int done; /* checking for end of scan */
29 int i; /* loop counter */
30 double strtod();
31
32 *end_flag = 0;
33
34 if (offset == 0 || offset >= break_pnt) {
35 if (offset >= break_pnt) { /* Copy rest of line back to beginning. */
36 length_left = LINE_LENGTH - save_pnt - 1;
37 ptr2 = line;
38 ptr = &line[save_pnt];
39 for (i=length_left; i; i--) *ptr2++ = *ptr++;
40 length = save_pnt + 1;
41 }
42 else {
43 length = LINE_LENGTH;
44 length_left = 0;
45 }
46
47 line[LINE_LENGTH - 1] = ' ';
48 line[LINE_LENGTH - 2] = ' ';
49 /* Now read next line, or next segment of current one. */
50 ptr2 = fgets(&line[length_left], length, infile);
51
52 if (ptr2 == (char *) NULL) { /* We've hit end of file. */
53 *end_flag = -1;
54 return((double) 0.0);
55 }
56
57 if (line[LINE_LENGTH - 1] == '\0' && line[LINE_LENGTH - 2] != '\0' &&
58 line[LINE_LENGTH - 2] != '\n' && line[LINE_LENGTH - 2] != '\f') {
59 /* Line too long. Find last safe place in line. */
60 break_pnt = LINE_LENGTH - 1;
61 save_pnt = break_pnt;
62 white_seen = FALSE;
63 done = FALSE;
64 while (!done) {
65 --break_pnt;
66 if (line[break_pnt] != '\0') {
67 if (isspace(line[break_pnt])) {
68 if (!white_seen) {
69 save_pnt = break_pnt + 1;
70 white_seen = TRUE;
71 }
72 }
73 else if (white_seen) {
74 done= TRUE;
75 }
76 }
77 }
78 }
79 else {
80 break_pnt = LINE_LENGTH;
81 }
82
83 offset = 0;
84 }
85
86 while (isspace(line[offset]) && offset < LINE_LENGTH) offset++;
87 if (line[offset] == '%' || line[offset] == '#') {
88 *end_flag = 1;
89 if (break_pnt < LINE_LENGTH) {
90 flush_line(infile);
91 }
92 return((double) 0.0);
93 }
94
95 ptr = &(line[offset]);
96 val = strtod(ptr, &ptr2);
97
98 if (ptr2 == ptr) { /* End of input line. */
99 offset = 0;
100 *end_flag = 1;
101 return((double) 0.0);
102 }
103 else {
104 offset = (int) (ptr2 - line) / sizeof(char);
105 }
106
107 return(val);
108 }
109
110
read_int(infile,end_flag)111 int read_int(infile, end_flag)
112 FILE *infile; /* file to read value from */
113 int *end_flag; /* 0 => OK, 1 => EOL, -1 => EOF */
114 {
115 int val; /* return value */
116 char *ptr; /* ptr to next string to read */
117 char *ptr2; /* ptr to next string to read */
118 int length; /* length of line to read */
119 int length_left; /* length of line still around */
120 int white_seen; /* have I detected white space yet? */
121 int done; /* checking for end of scan */
122 int i; /* loop counter */
123 long strtol();
124
125 *end_flag = 0;
126
127 if (offset == 0 || offset >= break_pnt) {
128 if (offset >= break_pnt) { /* Copy rest of line back to beginning. */
129 length_left = LINE_LENGTH - save_pnt - 1;
130 ptr2 = line;
131 ptr = &line[save_pnt];
132 for (i=length_left; i; i--) *ptr2++ = *ptr++;
133 length = save_pnt + 1;
134 }
135 else {
136 length = LINE_LENGTH;
137 length_left = 0;
138 }
139
140 line[LINE_LENGTH - 1] = ' ';
141 line[LINE_LENGTH - 2] = ' ';
142 /* Now read next line, or next segment of current one. */
143 ptr2 = fgets(&line[length_left], length, infile);
144
145 if (ptr2 == (char *) NULL) { /* We've hit end of file. */
146 *end_flag = -1;
147 return(0);
148 }
149
150 if (line[LINE_LENGTH - 1] == '\0' && line[LINE_LENGTH - 2] != '\0' &&
151 line[LINE_LENGTH - 2] != '\n' && line[LINE_LENGTH - 2] != '\f') {
152 /* Line too long. Find last safe place in line. */
153 break_pnt = LINE_LENGTH - 1;
154 save_pnt = break_pnt;
155 white_seen = FALSE;
156 done = FALSE;
157 while (!done) {
158 --break_pnt;
159 if (line[break_pnt] != '\0') {
160 if (isspace(line[break_pnt])) {
161 if (!white_seen) {
162 save_pnt = break_pnt + 1;
163 white_seen = TRUE;
164 }
165 }
166 else if (white_seen) {
167 done= TRUE;
168 }
169 }
170 }
171 }
172 else {
173 break_pnt = LINE_LENGTH;
174 }
175
176 offset = 0;
177 }
178
179 while (isspace(line[offset]) && offset < LINE_LENGTH) offset++;
180 if (line[offset] == '%' || line[offset] == '#') {
181 *end_flag = 1;
182 if (break_pnt < LINE_LENGTH) {
183 flush_line(infile);
184 }
185 return(0);
186 }
187
188 ptr = &(line[offset]);
189 val = (int) strtol(ptr, &ptr2, 10);
190
191 if (ptr2 == ptr) { /* End of input line. */
192 offset = 0;
193 *end_flag = 1;
194 return(0);
195 }
196 else {
197 offset = (int) (ptr2 - line) / sizeof(char);
198 }
199
200 return(val);
201 }
202
203
flush_line(infile)204 static void flush_line(infile)
205 FILE *infile; /* file to read value from */
206 {
207 char c; /* character being read */
208
209 c = getc(infile);
210 while (c != '\n' && c != '\f')
211 c = getc(infile);
212 }
213