[Gpredict-svn] SF.net SVN: gpredict:[905] trunk
Real time satellite tracking and orbit prediction
Status: Beta
Brought to you by:
csete
From: <aa...@us...> - 2011-09-23 00:37:34
|
Revision: 905 http://gpredict.svn.sourceforge.net/gpredict/?rev=905&view=rev Author: aa1vs Date: 2011-09-23 00:37:27 +0000 (Fri, 23 Sep 2011) Log Message: ----------- Unifies saving glib key file routine into one place instead of eight. Modified Paths: -------------- trunk/ChangeLog trunk/src/first-time.c trunk/src/gpredict-utils.c trunk/src/gpredict-utils.h trunk/src/mod-cfg.c trunk/src/qth-data.c trunk/src/radio-conf.c trunk/src/rotor-conf.c trunk/src/sat-cfg.c trunk/src/tle-update.c Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/ChangeLog 2011-09-23 00:37:27 UTC (rev 905) @@ -5,6 +5,17 @@ * src/gtk-polar-view-popup.c Saves polarview showtrack information from one session to another. + * ChangeLog + * src/first-time.c + * src/gpredict-utils.c + * src/gpredict-utils.h + * src/mod-cfg.c + * src/qth-data.c + * src/radio-conf.c + * src/rotor-conf.c + * src/sat-cfg.c + * src/tle-update.c + Unifies saving glib key file routine into one place instead of eight. 2011-09-21 Charles Suprin <hamaa1vs at gmail.com> Modified: trunk/src/first-time.c =================================================================== --- trunk/src/first-time.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/first-time.c 2011-09-23 00:37:27 UTC (rev 905) @@ -453,10 +453,7 @@ gchar *satfilename,*targetfilename; gchar *datadir; gchar **satellites; - gchar *cfgstr; GKeyFile *satfile,*target; - gsize length; - gsize written; gsize num; GError *err = NULL; guint i; @@ -464,7 +461,6 @@ //gdouble cfgver; gchar *name, *nickname, *website, *tle1, *tle2, *cfgver; - GIOChannel *cfgfile; GDir *srcdir; gchar *srcdirname; @@ -521,52 +517,14 @@ g_key_file_set_string (target, "Satellite", "TLE1", tle1); g_key_file_set_string (target, "Satellite", "TLE2", tle2); - /* convert configuration data struct to charachter string */ - cfgstr = g_key_file_to_data (target, &length, NULL); /* this function never reports error */ - - /* create and open a file for writing */ - cfgfile = g_io_channel_new_file (targetfilename, "w", &err); - - if (err != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create satellite file (%s)."), - __FUNCTION__, err->message); - g_clear_error (&err); + if (gpredict_save_key_file ( target, targetfilename)) { *error |= FTC_ERROR_STEP_05; + } else { + newsats++; } - else { - g_io_channel_write_chars (cfgfile, - cfgstr, - length, - &written, - &err); - - g_io_channel_shutdown (cfgfile, TRUE, NULL); - g_io_channel_unref (cfgfile); - - if (err != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Error writing satellite data (%s)."), - __FUNCTION__, err->message); - g_clear_error (&err); - *error |= FTC_ERROR_STEP_05; - } - else if (length != written) { - sat_log_log (SAT_LOG_LEVEL_WARN, - _("%s: Wrote only %d out of %d chars for satellite data."), - __FUNCTION__, written, length); - } - else { - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: Satellite data written for %s."), - __FUNCTION__, satellites[i]); - newsats++; - } - } - + g_key_file_free (target); - g_free (cfgstr); g_free (cfgver); g_free (name); g_free (nickname); Modified: trunk/src/gpredict-utils.c =================================================================== --- trunk/src/gpredict-utils.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/gpredict-utils.c 2011-09-23 00:37:27 UTC (rev 905) @@ -458,6 +458,11 @@ return col; } +/** \brief String comparison function + * \param s1 first string + * \param s2 second string + */ + int gpredict_strcmp (const char *s1, const char *s2) { #if 0 @@ -475,6 +480,16 @@ #endif } + +/** \brief substring finding function + * \param s1 the larger string + * \param s2 the substring that we are searching for. + * \return pointer to the substring location + * + * this is a substitute for strcasestr which is a gnu c extension and not available everywhere. + */ + + char * gpredict_strcasestr(const char *s1, const char *s2) { size_t s1_len = strlen(s1); @@ -489,3 +504,83 @@ return NULL; } + +/** \brief Save a GKeyFile structure to a file + * \param cfgdata is a pointer to the GKeyFile. + * \param filename is a pointer the filename string. + * \return 1 on error and zero on success. + * This might one day be in glib but for now it is not a standard function. + * Variants of this were throughout the code and it is now consilidated here. + */ + +gboolean gpredict_save_key_file (GKeyFile *cfgdata, const char *filename) { + gchar *datastream; /* cfgdata string */ + gsize length; /* length of the data stream */ + gsize written; /* number of bytes actually written */ + gboolean err = 0; /* the error value */ + GIOChannel *cfgfile; /* file */ + GError *error = NULL; /* Error handler */ + + /* ok, go on and convert the data */ + datastream = g_key_file_to_data (cfgdata, &length, &error); + + if (error != NULL) { + sat_log_log (SAT_LOG_LEVEL_ERROR, + _("%s: Could not create config data (%s)."), + __FUNCTION__, error->message); + + g_clear_error (&error); + + err = 1; + } else { + cfgfile = g_io_channel_new_file (filename, "w", &error); + + if (error != NULL) { + sat_log_log (SAT_LOG_LEVEL_ERROR, + _("%s: Could not create config file (%s)."), + __FUNCTION__, error->message); + + g_clear_error (&error); + + err = 1; + } + else { + g_io_channel_write_chars (cfgfile, + datastream, + length, + &written, + &error); + + g_io_channel_shutdown (cfgfile, TRUE, NULL); + g_io_channel_unref (cfgfile); + + if (error != NULL) { + sat_log_log (SAT_LOG_LEVEL_ERROR, + _("%s: Error writing config data (%s)."), + __FUNCTION__, error->message); + + g_clear_error (&error); + + err = 1; + } + else if (length != written) { + sat_log_log (SAT_LOG_LEVEL_WARN, + _("%s: Wrote only %d out of %d chars."), + __FUNCTION__, written, length); + + err = 1; + } + else { + sat_log_log (SAT_LOG_LEVEL_MSG, + _("%s: Configuration saved for %s."), + __FUNCTION__, filename); + + err = 0; + } + } + } + g_free (datastream); + + return err; + +} Modified: trunk/src/gpredict-utils.h =================================================================== --- trunk/src/gpredict-utils.h 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/gpredict-utils.h 2011-09-23 00:37:27 UTC (rev 905) @@ -64,4 +64,5 @@ gchar *rgba2html (guint rgba); int gpredict_strcmp (const char *s1, const char *s2); char *gpredict_strcasestr(const char *s1, const char *s2); +gboolean gpredict_save_key_file (GKeyFile *cfgdata, const char *filename); #endif Modified: trunk/src/mod-cfg.c =================================================================== --- trunk/src/mod-cfg.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/mod-cfg.c 2011-09-23 00:37:27 UTC (rev 905) @@ -310,13 +310,8 @@ */ mod_cfg_status_t mod_cfg_save (gchar *modname, GKeyFile *cfgdata) { - GError *error = NULL; /* Error handler */ - gchar *datastream; /* cfgdata string */ - GIOChannel *cfgfile; /* .mod file */ gchar *filename; /* file name */ gchar *confdir; - gsize length; /* length of the data stream */ - gsize written; /* number of bytes actually written */ gboolean err; @@ -335,71 +330,14 @@ return MOD_CFG_ERROR; } - /* ok, go on and convert the data */ - datastream = g_key_file_to_data (cfgdata, &length, &error); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create config data (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - return MOD_CFG_ERROR; - } - + /* create file and write data stream */ confdir = get_modules_dir (); filename = g_strconcat (confdir, G_DIR_SEPARATOR_S, modname, ".mod", NULL); g_free (confdir); - cfgfile = g_io_channel_new_file (filename, "w", &error); + err = gpredict_save_key_file (cfgdata, filename); - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create config file (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - err = 1; - } - else { - g_io_channel_write_chars (cfgfile, - datastream, - length, - &written, - &error); - - g_io_channel_shutdown (cfgfile, TRUE, NULL); - g_io_channel_unref (cfgfile); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Error writing config data (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - err = 1; - } - else if (length != written) { - sat_log_log (SAT_LOG_LEVEL_WARN, - _("%s: Wrote only %d out of %d chars."), - __FUNCTION__, written, length); - - err = 1; - } - else { - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: Configuration saved for module %s."), - __FUNCTION__, modname); - - err = 0; - } - } - - g_free (datastream); g_free (filename); if (err) Modified: trunk/src/qth-data.c =================================================================== --- trunk/src/qth-data.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/qth-data.c 2011-09-23 00:37:27 UTC (rev 905) @@ -40,6 +40,7 @@ #include "orbit-tools.h" #include "time-tools.h" #include "locator.h" +#include "gpredict-utils.h" #ifdef HAS_LIBGPS # include <gps.h> #endif @@ -279,12 +280,7 @@ gint qth_data_save (const gchar *filename, qth_t *qth) { - GError *error = NULL; gchar *buff; - GIOChannel *cfgfile; - gsize length; - gsize written; - gchar *cfgstr; gint ok = 1; qth->data = g_key_file_new (); @@ -369,69 +365,8 @@ /* saving code */ - /* convert configuration data struct to charachter string */ - cfgstr = g_key_file_to_data (qth->data, &length, &error); + ok = !(gpredict_save_key_file (qth->data, filename)); - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create QTH data (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - ok = 0; - } - else { - - cfgfile = g_io_channel_new_file (filename, "w", &error); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create QTH file %s\n%s."), - __FUNCTION__, filename, error->message); - - g_clear_error (&error); - - ok = 0; - } - else { - g_io_channel_write_chars (cfgfile, - cfgstr, - length, - &written, - &error); - - g_io_channel_shutdown (cfgfile, TRUE, NULL); - g_io_channel_unref (cfgfile); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Error writing QTH data (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - ok = 0; - } - else if (length != written) { - sat_log_log (SAT_LOG_LEVEL_WARN, - _("%s: Wrote only %d out of %d chars."), - __FUNCTION__, written, length); - - ok = 0; - } - else { - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: QTH data saved."), - __FUNCTION__); - - ok = 1; - } - } - - g_free (cfgstr); - } - return ok; } Modified: trunk/src/radio-conf.c =================================================================== --- trunk/src/radio-conf.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/radio-conf.c 2011-09-23 00:37:27 UTC (rev 905) @@ -31,6 +31,7 @@ #include <glib/gi18n.h> #include "sat-log.h" #include "compat.h" +#include "gpredict-utils.h" #include "radio-conf.h" @@ -216,8 +217,6 @@ GKeyFile *cfg = NULL; gchar *confdir; gchar *fname; - gchar *data; - gsize len; if (conf->name == NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, @@ -240,22 +239,14 @@ g_key_file_set_integer (cfg, GROUP, KEY_VFO_UP, conf->vfoUp); g_key_file_set_integer (cfg, GROUP, KEY_VFO_DOWN, conf->vfoDown); } - - /* 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, ".rig", NULL); g_free (confdir); - - g_file_set_contents (fname, data, len, NULL); + + gpredict_save_key_file (cfg, fname); g_free (fname); - g_free (data); g_key_file_free (cfg); - - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: Saved radio configuration %s"), - __FUNCTION__, conf->name); } Modified: trunk/src/rotor-conf.c =================================================================== --- trunk/src/rotor-conf.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/rotor-conf.c 2011-09-23 00:37:27 UTC (rev 905) @@ -33,6 +33,7 @@ #include "compat.h" #include "rotor-conf.h" +#include "gpredict-utils.h" #define GROUP "Rotator" #define KEY_HOST "Host" @@ -172,8 +173,6 @@ GKeyFile *cfg = NULL; gchar *confdir; gchar *fname; - gchar *data; - gsize len; if (conf->name == NULL) return; @@ -189,17 +188,16 @@ 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); - + /* build filename */ 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); - + /* save information */ + gpredict_save_key_file (cfg, fname); + + /* cleanup */ g_free (fname); - g_free (data); g_key_file_free (cfg); } Modified: trunk/src/sat-cfg.c =================================================================== --- trunk/src/sat-cfg.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/sat-cfg.c 2011-09-23 00:37:27 UTC (rev 905) @@ -45,8 +45,8 @@ #include "sat-pass-dialogs.h" #include "compat.h" #include "sat-cfg.h" +#include "gpredict-utils.h" - #define LIST_COLUMNS_DEFAULTS (SAT_LIST_FLAG_NAME |\ SAT_LIST_FLAG_AZ |\ SAT_LIST_FLAG_EL |\ @@ -311,82 +311,17 @@ */ guint sat_cfg_save () { - gsize length; - gsize written; - GError *error = NULL; - gchar *cfgstr; gchar *keyfile; gchar *confdir; - GIOChannel *cfgfile; guint err = 0; + + confdir = get_user_conf_dir (); + keyfile = g_strconcat (confdir, G_DIR_SEPARATOR_S, "gpredict.cfg", NULL); + + err = gpredict_save_key_file( config , keyfile); - /* convert configuration data struct to charachter string */ - cfgstr = g_key_file_to_data (config, &length, &error); + g_free (confdir); - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create config data (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - err = 1; - } - else { - /* create and open a file for writing */ - confdir = get_user_conf_dir (); - keyfile = g_strconcat (confdir, G_DIR_SEPARATOR_S, "gpredict.cfg", NULL); - g_free (confdir); - cfgfile = g_io_channel_new_file (keyfile, "w", &error); - g_free (keyfile); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create config file (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - err = 1; - } - else { - g_io_channel_write_chars (cfgfile, - cfgstr, - length, - &written, - &error); - - g_io_channel_shutdown (cfgfile, TRUE, NULL); - g_io_channel_unref (cfgfile); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Error writing config data (%s)."), - __FUNCTION__, error->message); - - g_clear_error (&error); - - err = 1; - } - else if (length != written) { - sat_log_log (SAT_LOG_LEVEL_WARN, - _("%s: Wrote only %d out of %d chars."), - __FUNCTION__, written, length); - - err = 1; - } - else { - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: Configuration saved."), - __FUNCTION__); - - err = 0; - } - } - - g_free (cfgstr); - } - return err; } Modified: trunk/src/tle-update.c =================================================================== --- trunk/src/tle-update.c 2011-09-23 00:33:36 UTC (rev 904) +++ trunk/src/tle-update.c 2011-09-23 00:37:27 UTC (rev 905) @@ -39,8 +39,8 @@ #include "sat-cfg.h" #include "compat.h" #include "tle-update.h" +#include "gpredict-utils.h" - /* Flag indicating whether TLE update is in progress. This should avoid multiple attempts to update TLE, e.g. user starts update from menubar while automatic @@ -165,7 +165,6 @@ /* scan directory for tle files */ while ((fnam = g_dir_read_name (cache_dir)) != NULL) { - /* check that we got a TLE file */ if (is_tle_file(dir, fnam)) { @@ -188,6 +187,8 @@ /* now, do read the fresh data */ num = read_fresh_tle (dir, fnam, data); + } else { + num = 0; } if (num < 1) { @@ -405,7 +406,6 @@ GKeyFile *satdata; GIOChannel *satfile; gchar *cfgstr, *cfgfile; - gsize length, written; GError *err = NULL; (void) key; /* avoid unused parameter compiler warning */ @@ -423,45 +423,13 @@ g_key_file_set_string (satdata, "Satellite", "TLE2", ntle->line2); g_key_file_set_integer (satdata, "Satellite", "STATUS", ntle->status); - /* convert data to text */ - cfgstr = g_key_file_to_data (satdata, &length, NULL); - /* create an I/O channel and store data */ cfgfile = sat_file_name_from_catnum (ntle->catnum); - satfile = g_io_channel_new_file (cfgfile, "w", &err); - - if (err != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create satellite file (%s)."), - __FUNCTION__, err->message); - g_clear_error (&err); + if (!gpredict_save_key_file (satdata, cfgfile)){ + *num += 1; } - else { - g_io_channel_write_chars (satfile, cfgstr, length, &written, &err); - g_io_channel_shutdown (satfile, TRUE, NULL); - g_io_channel_unref (satfile); - if (err != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Error writing satellite data for %d (%s)."), - __FUNCTION__, ntle->catnum, err->message); - g_clear_error (&err); - } - else if (length != written) { - sat_log_log (SAT_LOG_LEVEL_WARN, - _("%s: Wrote only %d out of %d chars for satellite data %d."), - __FUNCTION__, written, length, ntle->catnum); - } - else { - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: Data for new sat %d successfully added."), - __FUNCTION__, ntle->catnum); - *num += 1; - } - } - /* clean up memory */ - g_free (cfgstr); g_free (cfgfile); g_key_file_free (satdata); @@ -1129,9 +1097,6 @@ GError *error = NULL; GKeyFile *satdata; gchar *tlestr1, *tlestr2, *rawtle, *satname, *satnickname; - gchar *cfgstr; - GIOChannel *cfgfile; - gsize length, written; gboolean updateddata; @@ -1272,46 +1237,12 @@ } if (updateddata ==TRUE) { - /* convert configuration data struct to charachter string */ - cfgstr = g_key_file_to_data (satdata, &length, NULL); /* this function never reports error */ - - /* create and open a file for writing */ - cfgfile = g_io_channel_new_file (path, "w", &error); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Could not create satellite data file (%s)."), - __FUNCTION__, error->message); - g_clear_error (&error); + if (gpredict_save_key_file(satdata, path)) { skipped++; + } else { + updated++; } - else { - g_io_channel_write_chars (cfgfile, cfgstr, length, &written, &error); - - g_io_channel_shutdown (cfgfile, TRUE, NULL); - g_io_channel_unref (cfgfile); - - if (error != NULL) { - sat_log_log (SAT_LOG_LEVEL_ERROR, - _("%s: Error writing satellite data (%s)."), - __FUNCTION__, error->message); - g_clear_error (&error); - skipped++; - } - else if (length != written) { - sat_log_log (SAT_LOG_LEVEL_WARN, - _("%s: Wrote only %d out of %d chars for satellite data."), - __FUNCTION__, written, length); - skipped++; - } - else { - sat_log_log (SAT_LOG_LEVEL_MSG, - _("%s: Satellite data written for %d."), - __FUNCTION__, catnr); - updated++; - } - } - g_free (cfgstr); + } else { skipped++; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |