cloudy trunk
Loading...
Searching...
No Matches
input.cpp
Go to the documentation of this file.
1/* This file is part of Cloudy and is copyright (C)1978-2013 by Gary J. Ferland and
2 * others. For conditions of distribution and use see copyright notice in license.txt */
3/* input_readarray read input commands from array where images are stored *
4 * returns chCard, which will have <=80 characters before eol *
5 * line image is up and low case */
6/*input_init initial input_readarray array for storing line images at start of calculation */
7/*lgInputComment - parse comment - check if argument is comment string */
8#include "cddefines.h"
9#include "trace.h"
10#include "input.h"
11
13
14/*lgInputComment - parse comment - check if argument is comment string,
15 * either upper or lower case -
16 * returns true if line is a comment, false if not
17 * a comment is any line starting with "C ", *, %, //, or # */
18bool lgInputComment( const char *chLine )
19{
20 bool lgReturn;
21
22 DEBUG_ENTRY( "lgInputComment()" );
23
24 /* should not call this routine with null line */
25 if( chLine[0] == 0 )
27
28 /* first case - the special characters that can start a line */
29 if( chLine[0] == '#' || chLine[0] == '*' || chLine[0] == '%' || chLine[0] == ' ' )
30 {
31 lgReturn = true;
32 }
33 else if( strncmp(chLine,"//", 2 ) == 0 )
34 {
35 lgReturn = true;
36 }
37 /* second case is line that starts with c */
38 else if( chLine[0] == 'C' || chLine[0] == 'c' )
39 {
40 /* line starts with C, could be a command or a comment,
41 * if a comment then line is "C ", but there could be a newline '\n')
42 * or carriage return '\r' in the [1] position
43 * '\r' is carriage return, happens on cygwin gcc */
44 if( chLine[1] == '\n' || chLine[1] == ' ' || chLine[1] == '\r' )
45 {
46 lgReturn = true;
47 }
48 else
49 {
50 lgReturn = false;
51 }
52 }
53 else
54 {
55 lgReturn = false;
56 }
57 /*fprintf(ioQQQ,"DEBUG %c \n", TorF(lgReturn ) );*/
58
59 return lgReturn;
60}
61
62/*input_init initial input_readarray array for storing line images at start of calculation */
63void t_input::init(void)
64{
65
66 DEBUG_ENTRY( "t_input::init()" );
67
68 /* this sub must be called before calling READAR to get line images
69 * it simply sets the pointer to set up reading the images
70 * */
71 if( iReadWay > 0 )
72 {
73 /* this is usual case, read from the start of array, the commands */
74 nRead = -1;
75 }
76 else if( iReadWay < 0 )
77 {
78 /* this is special case where we read from end of array, the ini file */
79 /* save the current counter so we can reset it when done */
80 nReadSv = nRead;
81
82 /* and set current counter to the bottom of the stack */
83 nRead = NKRD;
84 }
85
86 return;
87}
88
89void t_input::echo( FILE *ipOUT)
90{
91 char chCard[INPUT_LINE_LENGTH];
92
93 /* start the file with the input commands */
94 init();
95
96 bool lgEOF = false;
97 while( !lgEOF )
98 {
99 readarray(chCard,&lgEOF);
100 if( !lgEOF )
101 {
102 char chCAPS[INPUT_LINE_LENGTH];
103 strcpy( chCAPS , chCard );
104 caps( chCAPS );
105 /* keyword HIDE means to hide the command - do not print it */
106 if( !nMatch( "HIDE" , chCAPS ) )
107 fprintf( ipOUT, "%s\n", chCard );
108 }
109 }
110
111}
112/*input_readarray read input commands from array where images are stored *
113 * returns chCard, which will have <=80 characters before eol */
114void t_input::readarray(char *chCard,
115 bool *lgEOF)
116{
117 long int last;
118
119 DEBUG_ENTRY( "t_input::readarray()" );
120
121 if( iReadWay > 0 )
122 {
123 /* usual case, reading commands from start of array
124 * nRead points to one plus the array element with the next line, it is
125 * one on the first call, which references line[0] */
126 ++nRead;
127
128 /* nSave points to the last line array element that was saved,
129 * so it is one less than the number of lines read. the last element
130 * containing a line image is [input.nSave]. There is a -1 for
131 * nRead to bring it onto the same c counting scale as nSave */
132 if( nRead > nSave )
133 {
134 *lgEOF = true;
135 }
136 else
137 {
138 /* get the line image */
139 strcpy( chCard, chCardSav[nRead] );
140
141 *lgEOF = false;
142 }
143 }
144 else
145 {
146 /* this is special case of reading cloudy.ini file,
147 * nRead was set to 1+last image in input_init, so first time
148 * we get here it is very large. decrement counter from end of file */
149 nRead -= 1;
150
151 /* last one with real data is NKRD+1-nSaveIni */
152 last = NKRD - nSaveIni;
153
154 /* this read is eof eof */
155 if( nRead < last )
156 {
157 /* reset counter so we read in the proper direction */
158 iReadWay = 1;
159 /* pointer to next line to read. this is on the scale where nRead-1
160 * is the actual array element */
161 nRead = nReadSv+1;
162 }
163
164 /* check if we hit eof while reading in forward direction */
165 if( iReadWay == 1 && nRead > nSave )
166 {
167 *lgEOF = true;
168 }
169 else
170 {
171 strcpy( chCard, chCardSav[nRead] );
172
173 /* did not hit eof */
174 *lgEOF = false;
175 }
176 }
177
178 /* if any "trace" appeared on a command line, then this flag was set
179 * so print the input command before it is parsed */
180 if( trace.lgTrace )
181 {
182 fprintf( ioQQQ, "t_input::readarray returns=%s=\n",chCard );
183 }
184
185 return;
186}
187
189void input_readvector(const char* chFile,
190 double vector[],
191 long n,
192 bool* lgEOF)
193{
194 DEBUG_ENTRY( "input_readvector()" );
195
196 fstream ioDATA;
197 open_data( ioDATA, chFile, mode_r, AS_LOCAL_ONLY );
198
199 for( long i=0; i < n; ++i )
200 ioDATA >> vector[i];
201
202 *lgEOF = !ioDATA.good();
203 return;
204}
FILE * ioQQQ
Definition cddefines.cpp:7
long nMatch(const char *chKey, const char *chCard)
Definition service.cpp:451
const int INPUT_LINE_LENGTH
Definition cddefines.h:254
void caps(char *chCard)
Definition service.cpp:280
NORETURN void TotalInsanity(void)
Definition service.cpp:886
#define DEBUG_ENTRY(funcname)
Definition cddefines.h:684
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition cpu.cpp:625
const ios_base::openmode mode_r
Definition cpu.h:212
@ AS_LOCAL_ONLY
Definition cpu.h:208
void input_readvector(const char *chFile, double vector[], long n, bool *lgEOF)
Definition input.cpp:189
t_input input
Definition input.cpp:12
bool lgInputComment(const char *chLine)
Definition input.cpp:18
#define NKRD
Definition input.h:10
long int nRead
Definition input.h:49
void echo(FILE *ipOUT)
Definition input.cpp:89
long int iReadWay
Definition input.h:56
long int nSaveIni
Definition input.h:52
long int nReadSv
Definition input.h:59
long int nSave
Definition input.h:46
char chCardSav[NKRD][INPUT_LINE_LENGTH]
Definition input.h:32
void readarray(char *chCard, bool *lgEOF)
Definition input.cpp:114
void init(void)
Definition input.cpp:63
t_trace trace
Definition trace.cpp:5