File: star.c

package info (click to toggle)
spacechart 0.9.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,704 kB
  • ctags: 917
  • sloc: ansic: 7,718; sh: 330; makefile: 205; sed: 93
file content (353 lines) | stat: -rw-r--r-- 10,691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***********************************************************************
 * star.c : Implementation of star_t
 ***********************************************************************/

/***********************************************************************
 *  This file is part of SpaceChart.
 *  Copyright (C) 2000 Miguel Coca <e970095@zipi.fi.upm.es>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 ***********************************************************************/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <locale.h>
#include "../include/star.h"

#define CNAME_LENGTH    15
#define SNAME_LENGTH    16
#define NAME_LENGTH     40
#define SPECTRUM_LENGTH 15
#define MAX_COMPONENTS   6
#define MAX_LINE       256

struct st_star
{
        star_id_t id;
        char cat_name[CNAME_LENGTH];
        char short_name[SNAME_LENGTH];
        coords_3d_t coords;
        star_component_t *components[MAX_COMPONENTS];
};

struct st_star_component
{
        char component;
        char dm_name[CNAME_LENGTH];
        char spectrum[SPECTRUM_LENGTH];
        char bayer_name[NAME_LENGTH];
        char proper_name[NAME_LENGTH];
        spectrum_t spectrum_type;
        float luminosity;
};

/* Definition of Local Functions */

static void terminate_string( char* string, size_t length );
static void set_spectrum_type( star_component_t* comp );

/* Public Functions */

/* Star Functions. */

star_t* star_new( void )
{
        star_t* star;

        star = (star_t*) malloc( sizeof( struct st_star ) );
        /* If the malloc failed we pass the error to the caller */
        if( star )
        {
                star->id = 0;
                star->cat_name[0] = '\0';
                star->short_name[0] = '\0';
                star->coords.x = 0.0;
                star->coords.y = 0.0;
                star->coords.z = 0.0;
                star->components[0] = NULL;
        }
        return star;
}

int star_read( star_t* star, FILE* file )
{
        char line[MAX_LINE];
        int success;
        star_component_t* component;
        char* locale;

        /* We change the locale number formatting to the standard, since the
         * program may have changed it, confusing scanf. */
        locale = setlocale( LC_NUMERIC, "C" );
        if( ( success = (int) fgets( line, MAX_LINE-1, file ) ) )
        {
                success = ( sscanf( line, "%d,%9c,%15c,%lf,%lf,%lf\n",
                                    &star->id, star->cat_name, 
                                    star->short_name, &star->coords.x, 
                                    &star->coords.y, &star->coords.z ) == 6 );

                terminate_string( star->cat_name, CNAME_LENGTH );
                terminate_string( star->short_name, SNAME_LENGTH );

                while( success && fgets( line, MAX_LINE-1, file ) && 
                       line[0] != '%' )
                {
                        component = star_component_new();
                        success = ( sscanf( line, "%c,%14c,%39c,%39c,"
                                            "%14c,%f\n",
                                            &component->component, 
                                            component->dm_name, 
                                            component->proper_name,
                                            component->bayer_name,
                                            component->spectrum, 
                                            &component->luminosity ) == 6 );
                        if( !success )
                                star_component_destroy( component );
                        else
                        {
                                set_spectrum_type( component );
                                terminate_string( component->dm_name, 
                                                  CNAME_LENGTH );
                                terminate_string( component->proper_name,
                                                  NAME_LENGTH );
                                terminate_string( component->bayer_name,
                                                  NAME_LENGTH );
                                terminate_string( component->spectrum,
                                                  NAME_LENGTH );
                                star_add_component( star, component );
                        }
                }
        }
        setlocale( LC_NUMERIC, locale );

        return success;
}

inline void star_set_id( star_t* star, star_id_t id )
{
        star->id = id;
}

inline star_id_t star_get_id( star_t* star )
{
        return star->id;
}

inline void star_set_catalog_name( star_t* star, char* name )
{
        strncpy( star->cat_name, name, CNAME_LENGTH );
}

inline void star_get_catalog_name( star_t* star, char* name )
{
        strncpy( name, star->cat_name, CNAME_LENGTH );
}

inline void star_set_short_name( star_t* star, char* name )
{
        strncpy( star->short_name, name, SNAME_LENGTH );
}

inline void star_get_short_name( star_t* star, char* name )
{
        strncpy( name, star->short_name, SNAME_LENGTH );
}

inline void star_set_coords( star_t* star, coords_3d_t* coords )
{
        star->coords = *coords;
}

inline void star_get_coords( star_t* star, coords_3d_t* coords )
{
        *coords = star->coords;
}

inline int star_components( star_t* star )
{
        int i;
        for( i = 0; star->components[i]; i++ )
                ;
        return i;
}

void star_add_component( star_t* star, star_component_t* comp )
{
        int i;
        
        i = star_components( star );
        
        star->components[i] = comp;
        star->components[i+1] = NULL;
}

void star_foreach_component( star_t* star, 
                             void (*function)( star_component_t *component,
                                               void *data ),
                             void *data )
{
        int i;
        
        for( i = 0; star->components[i]; i++ )
                function( star->components[i], data );
}

inline void star_destroy( star_t* star )
{
        int i;
        for( i = 0; star->components[i]; i++ )
                star_component_destroy( star->components[i] );
        free( star );
}

/* Component Functions. */

star_component_t *star_component_new( void )
{
        star_component_t* comp;
        
        if( (comp = malloc( sizeof(star_component_t) )) )
        {
                comp->component = ' ';
                comp->dm_name[0] = '\0';
                comp->dm_name[CNAME_LENGTH-1] = '\0';
                comp->spectrum[0] = '\0';
                comp->spectrum[SPECTRUM_LENGTH-1] = '\0';
                comp->bayer_name[0] = '\0';
                comp->bayer_name[NAME_LENGTH-1] = '\0';
                comp->proper_name[0] = '\0';
                comp->proper_name[NAME_LENGTH-1] = '\0';
                comp->spectrum_type = SPECTRUM_M;
                comp->luminosity = 0.0;
        }
        
        return comp;
}

void star_component_set_component( star_component_t *comp, char name )
{
        comp->component = name;
}

char star_component_get_component( star_component_t *comp )
{
        return comp->component;
}

void star_component_set_proper_name( star_component_t* comp, char* name )
{
         strncpy( comp->proper_name, name, NAME_LENGTH );
}

void star_component_get_proper_name( star_component_t* comp, char* name )
{
        strncpy( name, comp->proper_name, NAME_LENGTH );
}

void star_component_set_bayer_name( star_component_t* comp, char* name )
{
        strncpy( comp->bayer_name, name, NAME_LENGTH );
}

void star_component_get_bayer_name( star_component_t* comp, char* name )
{
        strncpy( name, comp->bayer_name, NAME_LENGTH );
}

void star_component_set_dm_name( star_component_t* comp, char* name )
{
        strncpy( comp->dm_name, name, CNAME_LENGTH );
}

void star_component_get_dm_name( star_component_t* comp, char* name )
{
        strncpy( name, comp->dm_name, CNAME_LENGTH );
}

void star_component_set_spectrum( star_component_t* comp, char* spectrum )
{
        strncpy( comp->spectrum, spectrum, SPECTRUM_LENGTH );
}

void star_component_get_spectrum( star_component_t* comp, char* spectrum )
{
        strncpy( spectrum, comp->spectrum, SPECTRUM_LENGTH );
}

spectrum_t star_component_get_spectrum_type( star_component_t* comp )
{
        return comp->spectrum_type;
}

void star_component_set_luminosity( star_component_t* comp, float lum )
{
        comp->luminosity = lum;
}

float star_component_get_luminosity( star_component_t* comp )
{
        return comp->luminosity;
}

void star_component_destroy( star_component_t* comp )
{
        free( comp );
}

/* Local Functions */

void terminate_string( char* string, size_t length )
{
        register int i;
        
        string[length-1] = '\0';
        for( i = length-2; i && !isalnum( string[i] ); i-- )
                string[i] = '\0';
}

void set_spectrum_type( star_component_t* comp )
{
                if( comp->spectrum[0] == 'D' )
                comp->spectrum_type = SPECTRUM_WHITE_DWARF;
        else
        {
                switch( comp->spectrum[1] )
                {
                case 'O':
                        comp->spectrum_type = SPECTRUM_O;
                        break;
                case 'B':
                        comp->spectrum_type = SPECTRUM_B;
                        break;
                case 'A':
                        comp->spectrum_type = SPECTRUM_A;
                        break;
                case 'F':
                        comp->spectrum_type = SPECTRUM_F;
                        break;
                case 'G':
                        comp->spectrum_type = SPECTRUM_G;
                        break;
                case 'K':
                        comp->spectrum_type = SPECTRUM_K;
                        break;
                case 'M':
                default:
                        comp->spectrum_type = SPECTRUM_M;
                        break;
                }
        }
}