cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parse_init.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 /*ParseInit bring an initialization file into input stream before parse */
4 #include "cddefines.h"
5 #include "input.h"
6 #include "trace.h"
7 #include "parser.h"
8 
9 void ParseInit(Parser &p)
10 {
11  char *ipEndL;
12  string chName;
13  long int ip,
14  k;
15  FILE *ioInitFile; /* will use this as pointer to ini file */
16 
17  DEBUG_ENTRY( "ParseInit()" );
18 
19  /*bring an initialization file into input stream before parsing */
20 
21  /* check whether single quote on line, this was used in c90 */
22  if( p.nMatch( "\'" ) )
23  {
24  fprintf( ioQQQ,
25  " ParseInit found a single quote on this line. This was used for file names in C90, but double quotes are used now.\n");
26  fprintf( ioQQQ, " The single quote has been ignored.\n");
27  }
28 
29  if( p.nMatch( "\"" ) )
30  {
31  /*
32  * if a quote occurs on the line then get the ini file name
33  * this will also set the name in chCard and OrgCard to spaces
34  * so later keywords do not key off it
35  */
36  if (p.GetQuote( chName ) )
37  p.StringError();
38  }
39  else
40  {
41  /* no quote appeared, so this is the default name, cloudy.ini */
42  chName = "cloudy.ini";
43  }
44 
45  /* at this point we have init file name, now make full name
46  * this can be a local file, or on the path if the key path appears */
47 
48  /* option to get cloudy.ini from a path */
49  if( p.nMatch("PATH") )
50  {
51  ioInitFile = open_data( chName.c_str(), "r" );
52  }
53  else
54  {
55  /* just use file name, and try to open file in current directory first */
56  ioInitFile = open_data( chName.c_str(), "r", AS_LOCAL_DATA );
57  }
58 
59  /* at this point the init file is open, now bring it into the command stack */
60  input.nSaveIni = 1;
61  ip = NKRD + 1 - input.nSaveIni;
62  while( (read_whole_line( input.chCardSav[ip-1],(int)sizeof(input.chCardSav[ip-1]),ioInitFile)!=NULL ) )
63  {
64  /* add extra space to be trailing space, needed for commands that end with space */
65  ipEndL = strrchr( input.chCardSav[ip-1] , '\n' );
66  /* make sure that we found the newline */
67  if(ipEndL == NULL )
68  {
69  fprintf(ioQQQ," ParseInit read in a init file line that did not end with a newline\n");
70  fprintf(ioQQQ," line was the following=>%s<=\n",input.chCardSav[ip-1]);
72  }
73  /* >>chng 01 oct 22, add cast */
74  /* find offset to end of line, the cr */
75  k = (long)(ipEndL - input.chCardSav[ip-1]);
76  /* replace cr with space */
77  input.chCardSav[ip-1][k] = ' ';
78  /* add extra space */
79  input.chCardSav[ip-1][k+1] = ' ';
80  /* finally null terminate the line */
81  input.chCardSav[ip-1][k+2] = '\0';
82  /* line starting with space is one way to end input stream */
83  if( input.chCardSav[ip-1][0]==' ' ) break;
84  /* totally ignore these lines
85  * >>chng 06 sep 04 use routine to check for comments */
86  if( lgInputComment(input.chCardSav[ip-1]) /*input.chCardSav[ip-1][0]=='#' || input.chCardSav[ip-1][0]=='*' ||
87  input.chCardSav[ip-1][0]=='%' || input.chCardSav[ip-1][0]=='/'*/ )
88  continue;
89 
90  /* print input lines if trace specified */
91  if( trace.lgTrace )
92  {
93  fprintf( ioQQQ,"initt=%s=\n",input.chCardSav[ip-1] );
94  }
95 
96  input.nSaveIni += 1;
97  ip = NKRD + 1 - input.nSaveIni;
98  if( ip <= input.nSave )
99  {
100  fprintf( ioQQQ,
101  " Too many ini lines. Total of all input and ini lines cannot exceed NKRD, presently%4i\n",
102  NKRD );
104  }
105  }
106  fclose(ioInitFile);
107  /* last one with real data is NKRD+1-nSaveIni */
108  input.nSaveIni -= 1;
109  return;
110 }
long int nSave
Definition: input.h:62
bool nMatch(const char *chKey) const
Definition: parser.h:140
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:751
const int NKRD
Definition: input.h:12
t_input input
Definition: input.cpp:12
long int nSaveIni
Definition: input.h:62
int GetQuote(string &chLabel)
Definition: parser.cpp:184
FILE * ioQQQ
Definition: cddefines.cpp:7
Definition: parser.h:42
NORETURN void StringError() const
Definition: parser.cpp:174
t_trace trace
Definition: trace.cpp:5
bool lgTrace
Definition: trace.h:12
#define EXIT_FAILURE
Definition: cddefines.h:168
#define cdEXIT(FAIL)
Definition: cddefines.h:484
void ParseInit(Parser &p)
Definition: parse_init.cpp:9
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:729
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1217
bool lgInputComment(const char *chLine)
Definition: input.cpp:31
char * read_whole_line(char *chLine, int nChar, FILE *ioIN)
Definition: service.cpp:72
char chCardSav[NKRD][INPUT_LINE_LENGTH]
Definition: input.h:48