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-2022 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 /* lgIsCommentSeq - is the string pointer s pointing to a comment sequence?
28  * lgColumn0 indicates whether we are in column 0 or not; the set of valid
29  * comment characters depends on this
30  * if lgReportVisible is true, visible comments will be reported, otherwise not */
31 bool lgIsCommentSeq( const char *s, bool lgColumn0, bool lgReportVisible )
32 {
33  DEBUG_ENTRY( "lgIsCommentSeq()" );
34 
35  if( strncmp( s, "##", 2 ) == 0 )
36  return true;
37  else if( strncmp( s, "//", 2 ) == 0 || s[0] == '%' )
38  {
39  input.lgDeprecatedComment = true;
40  return true;
41  }
42  else if( lgColumn0 && s[0] == '*' )
43  {
44  input.lgDeprecatedComment = true;
45  return true;
46  }
47  else if( !lgColumn0 && s[0] == ';' )
48  {
49  input.lgDeprecatedComment = true;
50  return true;
51  }
52  else if( lgReportVisible && s[0] == '#' )
53  return true;
54  else
55  return false;
56 }
57 
58 /* lgIsExpungedCommentSeq - does the string s start with an old-style comment? */
59 bool lgIsExpungedCommentSeq( const char *s )
60 {
61  DEBUG_ENTRY( "lgIsExpungedCommentSeq()" );
62 
63  if( strncmp( s, "C ", 2 ) == 0 )
64  return true;
65  else
66  return false;
67 }
68 
69 /* lgInputComment - parse comment - check if argument is comment string,
70  * either upper or lower case -
71  * returns true if line is a comment, false if not */
72 bool lgInputComment( const char *chLine )
73 {
74  DEBUG_ENTRY( "lgInputComment()" );
75 
76  /* should not call this routine with null line */
77  if( chLine[0] == 0 )
78  TotalInsanity();
79 
80  return lgIsCommentSeq( chLine, true, true );
81 }
82 
83 /* lgInputEOF - is this line an EOF marker? */
84 bool lgInputEOF( const char *chLine )
85 {
86  if( chLine[0] == '\n' || chLine[0] == '\r' || chLine[0] == '\0' || chLine[0] == ' ' )
87  return true;
88  else if( strncmp( chLine, "***", 3 ) == 0 )
89  return true;
90  else
91  return false;
92 }
93 
94 /* StripComment- strips comment part off the command line s
95  * if lgStripVisible is false, visible comments are retained
96  * hidden comments are always stripped
97  * this routine also removes underscores, brackets, and EOL characters */
98 void StripComment( string& s, bool lgStripVisible )
99 {
100  DEBUG_ENTRY( "StripComment()" );
101 
102  for( size_t p=0; p < s.length(); )
103  {
104  bool lgColumn0 = ( p == 0 );
105  if( s[p] == '\"' )
106  {
107  string buf;
108  p = GetString( s, p, buf );
109  }
110  else if( lgIsCommentSeq(&s[p],lgColumn0,lgStripVisible) )
111  {
112  s.erase(p);
113  break;
114  }
115  else if( lgIsCommentSeq(&s[p],lgColumn0,true) )
116  {
117  break;
118  }
119  else if( s[p] == '_' )
120  {
121  s[p++] = ' ';
122  input.lgUnderscoreFound = true;
123  }
124  else if( s[p] == '[' || s[p] == ']' )
125  {
126  s[p++] = ' ';
127  input.lgBracketFound = true;
128  }
129  else
130  {
131  ++p;
132  }
133  }
134  // now erase eol character. this should be a separate loop since it would
135  // otherwise be skipped if GetString() doesn't find the second quote...
136  for( size_t p=0; p < s.length(); ++p )
137  {
138  if( s[p] == '\n' || s[p] == '\r' )
139  {
140  s.erase(p);
141  break;
142  }
143  }
144 }
145 
146 /* GetString: retrieve a string between double quotes
147  * s : string to be parsed
148  * s[p] : place in string where to start parsing, should point to first set of double quotes
149  * buf : buffer that will hold the string between double quotes
150  * return value : pointer just beyond second set of double quotes, or string::npos in case of failure
151  * (second set of double quotes wasn't found) */
152 size_t GetString( const string& s, size_t p, string& buf )
153 {
154  DEBUG_ENTRY( "GetString()" );
155 
156  ASSERT( s[p] == '\"' );
157 
158  buf.clear();
159  for( ++p; p < s.length(); )
160  {
161  if( s[p] == '\\' )
162  buf.push_back( GetEscape(s,p) );
163  else if( s[p] == '\"' )
164  return ++p;
165  else
166  buf.push_back( s[p++] );
167  }
168  // no second set of double quotes was found
169  buf.clear();
170  return string::npos;
171 }
172 
173 char GetEscape( const string& s, size_t& p )
174 {
175  DEBUG_ENTRY( "GetEscape()" );
176 
177  // This routine is the placeholder for treating character escape
178  // sequences. For the moment we treat none. The routine assumes that
179  // an esacpe sequence will always generate a single character. This is
180  // true for all escape sequences except unicode sequences...
181  // on exit, p will point one character beyond the escape sequence.
182  return s[p++];
183 }
184 
185 /*input_init initial input_readarray array for storing line images at start of calculation */
186 void t_input::init(void)
187 {
188  DEBUG_ENTRY( "t_input::init()" );
189 
190  /* this sub must be called before calling READAR to get line images
191  * it simply sets the pointer to set up reading the images
192  * */
193  /* this is usual case, read from the start of array, the commands */
194  nRead = -1;
195 
196  return;
197 }
198 
199 void t_input::echo( FILE *ipOUT )
200 {
201  for( long i=0; i <= nSave; ++i )
202  if( InclLevel[i] == 0 && lgVisible[i] )
203  fprintf( ipOUT, "%s\n", chCardSav[i] );
204 }
205 
208 void t_input::readarray(char *chCardStripped,
209  char *chCardComment,
210  bool *lgEOF)
211 {
212  DEBUG_ENTRY( "t_input::readarray()" );
213 
214  /* usual case, reading commands from start of array
215  * nRead points to one plus the array element with the next line, it is
216  * one on the first call, which references line[0] */
217  ++nRead;
218 
219  /* nSave points to the last line array element that was saved,
220  * so it is one less than the number of lines read. the last element
221  * containing a line image is [input.nSave]. There is a -1 for
222  * nRead to bring it onto the same c counting scale as nSave */
223  if( nRead > nSave )
224  {
225  *lgEOF = true;
226  }
227  else
228  {
229  /* get the line image */
230  strcpy( chCardStripped, chCardStrip[nRead] );
231  strcpy( chCardComment, chCardSav[nRead] );
232  *lgEOF = false;
233  }
234 
235  /* if any "trace" appeared on a command line, then this flag was set
236  * so print the input command before it is parsed */
237  if( trace.lgTrace )
238  {
239  fprintf( ioQQQ, "t_input::readarray returns=%s=\n", chCardComment );
240  }
241 
242  return;
243 }
244 
246 void input_readvector(const char* chFile,
247  vector<double> &vec,
248  bool* lgError)
249 {
250  DEBUG_ENTRY( "input_readvector()" );
251 
252  vec.clear();
253 
254  fstream ioDATA;
255  open_data( ioDATA, chFile, mode_r, AS_LOCAL_ONLY );
256 
257  while( true )
258  {
259  double tmpValue;
260  ioDATA >> tmpValue;
261  if( !ioDATA.fail() )
262  vec.push_back(tmpValue);
263  else
264  break;
265  }
266  *lgError = !ioDATA.eof();
267 }
268 
270 void input_readvector(const char* chFile,
271  double vector[],
272  long n,
273  bool* lgEOF)
274 {
275  DEBUG_ENTRY( "input_readvector()" );
276 
277  fstream ioDATA;
278  open_data( ioDATA, chFile, mode_r, AS_LOCAL_ONLY );
279 
280  for( long i=0; i < n; ++i )
281  ioDATA >> vector[i];
282 
283  *lgEOF = !ioDATA.good();
284  return;
285 }
long int nSave
Definition: input.h:102
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:765
NORETURN void TotalInsanity(void)
Definition: service.cpp:971
t_input input
Definition: input.cpp:12
long int nRead
Definition: input.h:105
void zero()
Definition: input.cpp:14
bool lgInputEOF(const char *chLine)
Definition: input.cpp:84
size_t GetString(const string &s, size_t p, string &buf)
Definition: input.cpp:152
FILE * ioQQQ
Definition: cddefines.cpp:7
char chTitle[INPUT_LINE_LENGTH]
Definition: input.h:80
char chCardStrip[NKRD][INPUT_LINE_LENGTH]
Definition: input.h:77
bool lgDeprecatedComment
Definition: input.h:119
bool lgIsCommentSeq(const char *s, bool lgColumn0, bool lgReportVisible)
Definition: input.cpp:31
void StripComment(string &s, bool lgStripVisible)
Definition: input.cpp:98
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:246
bool lgVisible[NKRD]
Definition: input.h:96
void echo(FILE *ipOUT)
Definition: input.cpp:199
const int INPUT_LINE_LENGTH
Definition: cddefines.h:301
bool lgUnderscoreFound
Definition: input.h:112
bool lgBracketFound
Definition: input.h:116
bool lgIsExpungedCommentSeq(const char *s)
Definition: input.cpp:59
#define ASSERT(exp)
Definition: cddefines.h:613
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:723
void readarray(char *chCardStripped, char *chCardComment, bool *lgEOF)
Definition: input.cpp:208
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1121
bool lgInputComment(const char *chLine)
Definition: input.cpp:72
char GetEscape(const string &s, size_t &p)
Definition: input.cpp:173
char chCardSav[NKRD][INPUT_LINE_LENGTH]
Definition: input.h:74
Definition: input.h:62
int InclLevel[NKRD]
Definition: input.h:91
void init(void)
Definition: input.cpp:186