cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cdgetlinelist.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 /*cdGetLineList routine to read in master list of emission line wavelengths and ids, for
4  * generating loc grids */
5 #include "cddefines.h"
6 #include "cddrive.h"
7 #include "lines.h"
8 #include "parser.h"
9 
10 /* return value is number of lines, -1 if file could not be opened */
11 long int cdGetLineList(
12  /* chFile is optional filename, if void then use BLRLineList,
13  * if not void then use file specified */
14  const char chFile[],
15  /* array of null term strings giving line labels */
16  vector<string>& chLabels,
17  /* a 1-d array of line wavelengths */
18  vector<realnum>& wl)
19 {
20  DEBUG_ENTRY( "cdGetLineList()" );
21 
22  /* first check that cdInit has been called, since we may have to write
23  * error output */
24  if( !lgcdInitCalled )
25  {
26  fprintf(stderr," cdInit must be called before cdGetLineList.\n");
28  }
29 
30  /* use default filename LineList_BLR.dat if void string, else use file specified */
31  const char* chFilename = ( strlen(chFile) == 0 ) ? "LineList_BLR.dat" : chFile;
32 
33  /* we will check local space first, then on path if not present */
34  FILE* ioData = open_data( chFilename, "r", AS_LOCAL_DATA_TRY );
35 
36  if( ioData == NULL )
37  {
38  /* did not find file, return -1 */
39  return -1;
40  }
41 
42  // make sure we are not leaking memory
43  ASSERT( chLabels.size() == 0 && wl.size() == 0 );
44 
45  Parser p;
46  char chLine[FILENAME_PATH_LENGTH_2];
47 
48  /* actually read and save the lines */
49  while( read_whole_line( chLine, (int)sizeof(chLine), ioData ) != NULL )
50  {
51  // strip eol
52  for( size_t i=0; ; ++i )
53  {
54  if( chLine[i] == '\n' || chLine[i] == '\r' )
55  chLine[i] = '\0';
56  if( chLine[i] == '\0' )
57  break;
58  }
59 
60  p.setline(chLine);
61  /* this checks for in-file EOF markers */
62  if( p.last() )
63  break;
64 
65  if( p.isComment() )
66  continue;
67 
68  LineID line = p.getLineID();
69  if( !p.lgReachedEnd() )
70  {
71  fprintf( ioQQQ, "cdGetLineList: found junk at end of input line in file %s:\n", chFile );
72  p.showLocation();
74  }
75  chLabels.push_back(line.chLabel);
76  wl.push_back(line.wave);
77  }
78 
79  fclose( ioData );
80 
81  /* return number of lines we found */
82  return chLabels.size();
83 }
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:765
realnum wave
Definition: lines.h:18
const int FILENAME_PATH_LENGTH_2
Definition: cddefines.h:296
void setline(const char *const card)
Definition: parser.h:82
long int cdGetLineList(const char chFile[], vector< string > &chLabels, vector< realnum > &wl)
Definition: lines.h:14
bool lgcdInitCalled
Definition: cdinit.cpp:27
FILE * ioQQQ
Definition: cddefines.cpp:7
string chLabel
Definition: lines.h:17
Definition: parser.h:43
#define EXIT_FAILURE
Definition: cddefines.h:168
#define cdEXIT(FAIL)
Definition: cddefines.h:482
#define ASSERT(exp)
Definition: cddefines.h:613
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:723
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1121
char * read_whole_line(char *chLine, int nChar, FILE *ioIN)
Definition: service.cpp:70