File: rotor-conf.c

package info (click to toggle)
gpredict 1.1-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,280 kB
  • ctags: 2,873
  • sloc: ansic: 34,903; sh: 10,151; makefile: 417
file content (205 lines) | stat: -rw-r--r-- 6,660 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
    Gpredict: Real-time satellite tracking and orbit prediction program

    Copyright (C)  2001-2009  Alexandru Csete.

    Authors: Alexandru Csete <oz9aec@gmail.com>

    Comments, questions and bugreports should be submitted via
    http://sourceforge.net/projects/gpredict/
    More details can be found at the project home page:

            http://gpredict.oz9aec.net/
 
    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, visit http://www.fsf.org/
 
*/

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "sat-log.h"
#include "compat.h"

#include "rotor-conf.h"

#define GROUP           "Rotator"
#define KEY_HOST        "Host"
#define KEY_PORT        "Port"
#define KEY_AZTYPE      "AzType"
#define KEY_MINAZ       "MinAz"
#define KEY_MAXAZ       "MaxAz"
#define KEY_MINEL       "MinEl"
#define KEY_MAXEL       "MaxEl"


/** \brief Read rotator configuration.
 * \param conf Pointer to a rotor_conf_t structure where the data will be
 *             stored.
 * 
 * This function reads a rotoator configuration from a .rot file into conf.
 * conf->name must contain the file name of the configuration (no path, just
 * file name and without the .rot extension).
 */
gboolean rotor_conf_read (rotor_conf_t *conf)
{
    GKeyFile *cfg = NULL;
    gchar    *confdir;
    gchar    *fname;
    GError   *error = NULL;
    
    
    if (conf->name == NULL) {
        sat_log_log (SAT_LOG_LEVEL_ERROR,
                     _("%s: NULL configuration name!"),
                       __FUNCTION__);
        return FALSE;
    }
    
    confdir = get_hwconf_dir();
    fname = g_strconcat (confdir, G_DIR_SEPARATOR_S,
                         conf->name, ".rot", NULL);
    g_free (confdir);
    
    /* open .grc file */
    cfg = g_key_file_new ();
    g_key_file_load_from_file(cfg, fname, 0, NULL);
    
    if (cfg == NULL) {
        sat_log_log (SAT_LOG_LEVEL_ERROR,
                     _("%s: Could not load file %s\n"),
                     __FUNCTION__, fname);
        g_free (fname);
        
        return FALSE;
    }
    
    g_free (fname);
    
    /* read parameters */
    conf->host = g_key_file_get_string (cfg, GROUP, KEY_HOST, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_ERROR,
                     _("%s: Error reading rotor conf from %s (%s)."),
                       __FUNCTION__, conf->name, error->message);
        g_clear_error (&error);
        g_key_file_free (cfg);
        return FALSE;
    }
    
    conf->port = g_key_file_get_integer (cfg, GROUP, KEY_PORT, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_ERROR,
                     _("%s: Error reading rotor conf from %s (%s)."),
                       __FUNCTION__, conf->name, error->message);
        g_clear_error (&error);
        g_key_file_free (cfg);
        return FALSE;
    }
    
    conf->aztype = g_key_file_get_integer (cfg, GROUP, KEY_AZTYPE, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_WARN,
                     _("%s: Az type not defined for %s. Assuming 0..360\302\260"),
                       __FUNCTION__, conf->name);
        g_clear_error (&error);
        
        conf->aztype = ROT_AZ_TYPE_360;
    }
    
    conf->minaz = g_key_file_get_double (cfg, GROUP, KEY_MINAZ, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_WARN,
                     _("%s: MinAz not defined for %s. Assuming 0\302\260."),
                       __FUNCTION__, conf->name);
        g_clear_error (&error);
        conf->minaz = 0.0;
    }
    
    conf->maxaz = g_key_file_get_double (cfg, GROUP, KEY_MAXAZ, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_WARN,
                     _("%s: MaxAz not defined for %s. Assuming 360\302\260."),
                       __FUNCTION__, conf->name);
        g_clear_error (&error);
        conf->maxaz = 360.0;
    }
    
    conf->minel = g_key_file_get_double (cfg, GROUP, KEY_MINEL, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_WARN,
                     _("%s: MinEl not defined for %s. Assuming 0\302\260."),
                       __FUNCTION__, conf->name);
        g_clear_error (&error);
        conf->minel = 0.0;
    }
    
    conf->maxel = g_key_file_get_double (cfg, GROUP, KEY_MAXEL, &error);
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_WARN,
                     _("%s: MaxEl not defined for %s. Assuming 90\302\260."),
                       __FUNCTION__, conf->name);
        g_clear_error (&error);
        conf->maxel = 90.0;
    }
    
    g_key_file_free (cfg);
    
    return TRUE;
}


/** \brief Save rotator configuration.
 * \param conf Pointer to the rotator configuration.
 * 
 * This function saves the rotator configuration stored in conf to a
 * .rig file. conf->name must contain the file name of the configuration
 * (no path, just file name and without the .rot extension).
 */
void rotor_conf_save (rotor_conf_t *conf)
{
    GKeyFile *cfg = NULL;
    gchar    *confdir;
    gchar    *fname;
    gchar    *data;
    gsize     len;
    
    if (conf->name == NULL)
        return;
    
    /* create a config structure */
    cfg = g_key_file_new();
    
    g_key_file_set_string  (cfg, GROUP, KEY_HOST, conf->host);
    g_key_file_set_integer (cfg, GROUP, KEY_PORT, conf->port);
    g_key_file_set_integer (cfg, GROUP, KEY_AZTYPE, conf->aztype);
    g_key_file_set_double  (cfg, GROUP, KEY_MINAZ, conf->minaz);
    g_key_file_set_double  (cfg, GROUP, KEY_MAXAZ, conf->maxaz);
    g_key_file_set_double  (cfg, GROUP, KEY_MINEL, conf->minel);
    g_key_file_set_double  (cfg, GROUP, KEY_MAXEL, conf->maxel);
    
    /* convert to text sdata */
    data = g_key_file_to_data (cfg, &len, NULL);
    
    confdir = get_hwconf_dir();
    fname = g_strconcat (confdir, G_DIR_SEPARATOR_S,
                         conf->name, ".rot", NULL);
    g_free (confdir);
        
    g_file_set_contents (fname, data, len, NULL);
    
    g_free (fname);
    g_free (data);
    g_key_file_free (cfg);
}