]> git.mxchange.org Git - simgear.git/blob - simgear/ephemeris/stardata.cxx
Clamp pitch values rather than just dumping an error message.
[simgear.git] / simgear / ephemeris / stardata.cxx
1 // stardata.cxx -- manage star data
2 //
3 // Written by Curtis Olson, started March 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/misc/sgstream.hxx>
27
28 #include "stardata.hxx"
29
30 #if defined (_MSC_VER)
31   SG_USING_STD(getline);
32 #endif
33
34 // Constructor
35 SGStarData::SGStarData() :
36     nstars(0)
37 {
38 }
39
40 SGStarData::SGStarData( SGPath path ) :
41     nstars(0)
42 {
43     data_path = SGPath( path );
44     load();
45 }
46
47
48 // Destructor
49 SGStarData::~SGStarData() {
50 }
51
52
53 bool SGStarData::load() {
54
55     // -dw- avoid local data > 32k error by dynamic allocation of the
56     // array, problem for some compilers
57     stars = new sgdVec3[SG_MAX_STARS];
58
59      // build the full path name to the stars data base file
60     data_path.append( "stars" );
61     SG_LOG( SG_ASTRO, SG_INFO, "  Loading stars from " << data_path.str() );
62
63     sg_gzifstream in( data_path.str() );
64     if ( ! in.is_open() ) {
65         SG_LOG( SG_ASTRO, SG_ALERT, "Cannot open star file: "
66                 << data_path.str() );
67         exit(-1);
68     }
69
70     double ra, dec, mag;
71     char c;
72     string name;
73
74     nstars = 0;
75
76     // read in each line of the file
77     while ( ! in.eof() && nstars < SG_MAX_STARS ) {
78         in >> skipcomment;
79
80         getline( in, name, ',' );
81         // cout << "  data = " << name << endl;
82
83         // read name and first comma
84         while ( in.get(c) ) {
85             if ( (c != ' ') && (c != ',') ) {
86                 // push back on the stream
87                 in.putback(c);
88                 break;
89             }
90         }
91
92         in >> ra;
93
94         // read past optional comma
95         while ( in.get(c) ) {
96             if ( (c != ' ') && (c != ',') ) {
97                 // push back on the stream
98                 in.putback(c);
99                 break;
100             }
101         }
102
103         in >> dec;
104
105         // read past optional comma
106         while ( in.get(c) ) {
107             if ( (c != ' ') && (c != ',') ) {
108                 // push back on the stream
109                 in.putback(c);
110                 break;
111             }
112         }
113
114         in >> mag;
115
116         // cout << " star data = " << ra << " " << dec << " " << mag << endl;
117
118         sgdSetVec3( stars[nstars], ra, dec, mag );
119
120         ++nstars;
121     }
122
123     SG_LOG( SG_ASTRO, SG_INFO, "  Loaded " << nstars << " stars" );
124
125     return true;
126 }