cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
input.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2017 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 
15 {
16  DEBUG_ENTRY( "t_input::zero()" );
17  /* some titles and line images */
18  const int INPUT_LINE_LENGTH_EMPTY=75;
19  ASSERT(INPUT_LINE_LENGTH_EMPTY <= INPUT_LINE_LENGTH);
20  for( long i=0; i<INPUT_LINE_LENGTH_EMPTY-1; ++i)
21  {
22  chTitle[i] = ' ';
23  }
24  chTitle[INPUT_LINE_LENGTH_EMPTY-1] = '\0';
25 }
26 
27 /*lgInputComment - parse comment - check if argument is comment string,
28  * either upper or lower case -
29  * returns true if line is a comment, false if not
30  * a comment is any line starting with "C ", *, %, //, or # */
31 bool lgInputComment( const char *chLine )
32 {
33  bool lgReturn;
34 
35  DEBUG_ENTRY( "lgInputComment()" );
36 
37  /* should not call this routine with null line */
38  if( chLine[0] == 0 )
39  TotalInsanity();
40 
41  /* first case - the special characters that can start a line */
42  if( chLine[0] == '#' || chLine[0] == '*' || chLine[0] == '%' || chLine[0] == ' ' )
43  {
44  lgReturn = true;
45  }
46  else if( strncmp(chLine,"//", 2 ) == 0 )
47  {
48  lgReturn = true;
49  }
50  /* second case is line that starts with c */
51  else if( chLine[0] == 'C' || chLine[0] == 'c' )
52  {
53  /* line starts with C, could be a command or a comment,
54  * if a comment then line is "C ", but there could be a newline '\n')
55  * or carriage return '\r' in the [1] position
56  * '\r' is carriage return, happens on cygwin gcc */
57  if( chLine[1] == '\n' || chLine[1] == ' ' || chLine[1] == '\r' )
58  {
59  lgReturn = true;
60  }
61  else
62  {
63  lgReturn = false;
64  }
65  }
66  else
67  {
68  lgReturn = false;
69  }
70  /*fprintf(ioQQQ,"DEBUG %c \n", TorF(lgReturn ) );*/
71 
72  return lgReturn;
73 }
74 
75 /*input_init initial input_readarray array for storing line images at start of calculation */
76 void t_input::init(void)
77 {
78 
79  DEBUG_ENTRY( "t_input::init()" );
80 
81  /* this sub must be called before calling READAR to get line images
82  * it simply sets the pointer to set up reading the images
83  * */
84  if( iReadWay > 0 )
85  {
86  /* this is usual case, read from the start of array, the commands */
87  nRead = -1;
88  }
89  else if( iReadWay < 0 )
90  {
91  /* this is special case where we read from end of array, the ini file */
92  /* save the current counter so we can reset it when done */
93  nReadSv = nRead;
94 
95  /* and set current counter to the bottom of the stack */
96  nRead = NKRD;
97  }
98 
99  return;
100 }
101 
102 void t_input::echo( FILE *ipOUT)
103 {
104  char chCard[INPUT_LINE_LENGTH];
105 
106  /* start the file with the input commands */
107  init();
108 
109  bool lgEOF = false;
110  while( !lgEOF )
111  {
112  readarray(chCard,&lgEOF);
113  if( !lgEOF )
114  {
115  char chCAPS[INPUT_LINE_LENGTH];
116  strcpy( chCAPS , chCard );
117  caps( chCAPS );
118  /* keyword HIDE means to hide the command - do not print it */
119  if( !nMatch( "HIDE" , chCAPS ) )
120  fprintf( ipOUT, "%s\n", chCard );
121  }
122  }
123 
124 }
125 /*input_readarray read input commands from array where images are stored *
126  * returns chCard, which will have <=80 characters before eol */
127 void t_input::readarray(char *chCard,
128  bool *lgEOF)
129 {
130  long int last;
131 
132  DEBUG_ENTRY( "t_input::readarray()" );
133 
134  if( iReadWay > 0 )
135  {
136  /* usual case, reading commands from start of array
137  * nRead points to one plus the array element with the next line, it is
138  * one on the first call, which references line[0] */
139  ++nRead;
140 
141  /* nSave points to the last line array element that was saved,
142  * so it is one less than the number of lines read. the last element
143  * containing a line image is [input.nSave]. There is a -1 for
144  * nRead to bring it onto the same c counting scale as nSave */
145  if( nRead > nSave )
146  {
147  *lgEOF = true;
148  }
149  else
150  {
151  /* get the line image */
152  strcpy( chCard, chCardSav[nRead] );
153 
154  *lgEOF = false;
155  }
156  }
157  else
158  {
159  /* this is special case of reading cloudy.ini file,
160  * nRead was set to 1+last image in input_init, so first time
161  * we get here it is very large. decrement counter from end of file */
162  nRead -= 1;
163 
164  /* last one with real data is NKRD+1-nSaveIni */
165  last = NKRD - nSaveIni;
166 
167  /* this read is eof eof */
168  if( nRead < last )
169  {
170  /* reset counter so we read in the proper direction */
171  iReadWay = 1;
172  /* pointer to next line to read. this is on the scale where nRead-1
173  * is the actual array element */
174  nRead = nReadSv+1;
175  }
176 
177  /* check if we hit eof while reading in forward direction */
178  if( iReadWay == 1 && nRead > nSave )
179  {
180  *lgEOF = true;
181  }
182  else
183  {
184  strcpy( chCard, chCardSav[nRead] );
185 
186  /* did not hit eof */
187  *lgEOF = false;
188  }
189  }
190 
191  /* if any "trace" appeared on a command line, then this flag was set
192  * so print the input command before it is parsed */
193  if( trace.lgTrace )
194  {
195  fprintf( ioQQQ, "t_input::readarray returns=%s=\n",chCard );
196  }
197 
198  return;
199 }
200 
202 void input_readvector(const char* chFile,
203  vector<double> &vec,
204  bool* lgError)
205 {
206  DEBUG_ENTRY( "input_readvector()" );
207 
208  fstream ioDATA;
209  open_data( ioDATA, chFile, mode_r, AS_LOCAL_ONLY );
210 
211  string line;
212  getline( ioDATA,line );
213  if( line.empty() )
214  {
215  fprintf( ioQQQ, " PROBLEM Empty line found reading file %s.\n Sorry.\n\n", chFile );
216  cdEXIT( EXIT_FAILURE );
217  }
218 
219  istringstream iss( line );
220  do
221  {
222  double tmpValue;
223  iss >> skipws >> tmpValue;
224  if( !iss.fail() )
225  vec.push_back(tmpValue);
226  }
227  while( !iss.eof() );
228 
229  *lgError = iss.bad() || ioDATA.bad();
230  return;
231 }
232 
234 void input_readvector(const char* chFile,
235  double vector[],
236  long n,
237  bool* lgEOF)
238 {
239  DEBUG_ENTRY( "input_readvector()" );
240 
241  fstream ioDATA;
242  open_data( ioDATA, chFile, mode_r, AS_LOCAL_ONLY );
243 
244  for( long i=0; i < n; ++i )
245  ioDATA >> vector[i];
246 
247  *lgEOF = !ioDATA.good();
248  return;
249 }
long int nSave
Definition: input.h:62
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:751
const int NKRD
Definition: input.h:12
NORETURN void TotalInsanity(void)
Definition: service.cpp:1067
t_input input
Definition: input.cpp:12
long int nSaveIni
Definition: input.h:62
long nMatch(const char *chKey, const char *chCard)
Definition: service.cpp:537
long int nRead
Definition: input.h:62
void zero()
Definition: input.cpp:14
FILE * ioQQQ
Definition: cddefines.cpp:7
char chTitle[INPUT_LINE_LENGTH]
Definition: input.h:48
t_trace trace
Definition: trace.cpp:5
const ios_base::openmode mode_r
Definition: cpu.h:267
bool lgTrace
Definition: trace.h:12
void input_readvector(const char *chFile, vector< double > &vec, bool *lgError)
Definition: input.cpp:202
#define EXIT_FAILURE
Definition: cddefines.h:168
void echo(FILE *ipOUT)
Definition: input.cpp:102
const int INPUT_LINE_LENGTH
Definition: cddefines.h:301
#define cdEXIT(FAIL)
Definition: cddefines.h:484
void readarray(char *chCard, bool *lgEOF)
Definition: input.cpp:127
#define ASSERT(exp)
Definition: cddefines.h:617
long int iReadWay
Definition: input.h:62
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:729
long int nReadSv
Definition: input.h:62
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1217
bool lgInputComment(const char *chLine)
Definition: input.cpp:31
void caps(char *chCard)
Definition: service.cpp:304
char chCardSav[NKRD][INPUT_LINE_LENGTH]
Definition: input.h:48
Definition: input.h:34
void init(void)
Definition: input.cpp:76