[Gpredict-svn] SF.net SVN: gpredict:[793] trunk
Real time satellite tracking and orbit prediction
Status: Beta
Brought to you by:
csete
From: <aa...@us...> - 2011-03-26 01:10:27
|
Revision: 793 http://gpredict.svn.sourceforge.net/gpredict/?rev=793&view=rev Author: aa1vs Date: 2011-03-26 01:10:20 +0000 (Sat, 26 Mar 2011) Log Message: ----------- Add natural comparison by Martin Pool Modified Paths: -------------- trunk/AUTHORS trunk/ChangeLog trunk/src/Makefile.am trunk/src/about.c trunk/src/gpredict-utils.c trunk/src/gpredict-utils.h trunk/src/gtk-rig-ctrl.c trunk/src/gtk-rot-ctrl.c trunk/src/gtk-sat-selector.c trunk/src/gtk-single-sat.c trunk/src/menubar.c trunk/src/mod-cfg.c Added Paths: ----------- trunk/src/strnatcmp.c trunk/src/strnatcmp.h Modified: trunk/AUTHORS =================================================================== --- trunk/AUTHORS 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/AUTHORS 2011-03-26 01:10:20 UTC (rev 793) @@ -13,6 +13,8 @@ - Maidenhead locator functions are based on work done by Stephane Fillod, Nate Bargmann, Dave Hines, Mirko Caserta, and S. R. Sampson. - Paul Schulz, various patches. +- Martin Pool, for his natural string compare routines + see http://sourcefrog.net/projects/natsort/ Imagery: Most of the maps originate from NASA Visible Earth, http://visibleearth.nasa.gov/ Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/ChangeLog 2011-03-26 01:10:20 UTC (rev 793) @@ -1,3 +1,20 @@ +2011-03-25 Charles Suprin <hamaa1vs at gmail.com> + + * AUTHORS + * src/about.c + * src/Makefile.am + * src/menubar.c + * src/mod-cfg.c + * src/gpredict-utils.c + * src/gpredict-utils.h + * src/gtk-rig-ctrl.c + * src/gtk-rot-ctrl.c + * src/gtk-sat-selector.c + * src/gtk-single-sat.c + * src/strnatcmp.c + * src/strnatcmp.h + Added "natural comparison" by Martin Pool from patch 3237220 by Patrick Strasser. + 2011-03-24 Charles Suprin <hamaa1vs at gmail.com> * src/predict-tools.c Modified: trunk/src/Makefile.am =================================================================== --- trunk/src/Makefile.am 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/Makefile.am 2011-03-26 01:10:20 UTC (rev 793) @@ -112,7 +112,8 @@ time-tools.c time-tools.h \ tle-tools.c tle-tools.h \ tle-update.c tle-update.h \ - sat-debugger.c sat-debugger.h + sat-debugger.c sat-debugger.h \ + strnatcmp.c strnatcmp.h Modified: trunk/src/about.c =================================================================== --- trunk/src/about.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/about.c 2011-03-26 01:10:20 UTC (rev 793) @@ -54,6 +54,7 @@ "Mirko Caserta (locator.c)", "S. R. Sampson (locator.c)", "Paul Schulz (various patches)", + "Martin Pool (natural sorting)", "", "Imagery:", "Most of the maps originate from NASA Visible Earth", Modified: trunk/src/gpredict-utils.c =================================================================== --- trunk/src/gpredict-utils.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/gpredict-utils.c 2011-03-26 01:10:20 UTC (rev 793) @@ -28,6 +28,9 @@ /** \brief Various utility functions. * */ + +#define _GNU_SOURCE +#include <ctype.h> #include <gtk/gtk.h> #include <glib/gi18n.h> #include <glib/gstdio.h> @@ -37,9 +40,11 @@ #include "compat.h" #include "sat-log.h" #include "gpredict-utils.h" +#include "strnatcmp.h" + static void set_combo_tooltip (GtkWidget *combo, gpointer text); @@ -477,3 +482,19 @@ return col; } +int gpredict_strcmp (const char *s1, const char *s2) { +#if 0 + + gchar *a,*b; + int retval; + a=g_ascii_strup(s1,-1); + b=g_ascii_strup(s2,-1); + + retval=strverscmp(a,b); + g_free(a); + g_free(b); + return retval; +#else + return strnatcasecmp(s1,s2); +#endif +} Modified: trunk/src/gpredict-utils.h =================================================================== --- trunk/src/gpredict-utils.h 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/gpredict-utils.h 2011-03-26 01:10:20 UTC (rev 793) @@ -62,5 +62,5 @@ void gdk2rgb (const GdkColor *color, guint *rgb); void gdk2rgba (const GdkColor *color, guint16 alpha, guint *rgba); gchar *rgba2html (guint rgba); - +int gpredict_strcmp (const char *s1, const char *s2); #endif Modified: trunk/src/gtk-rig-ctrl.c =================================================================== --- trunk/src/gtk-rig-ctrl.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/gtk-rig-ctrl.c 2011-03-26 01:10:20 UTC (rev 793) @@ -2755,11 +2755,11 @@ /*simple function to sort the list of satellites in the combo box.*/ static gint sat_name_compare (sat_t* a,sat_t*b){ - return (g_ascii_strcasecmp(a->nickname,b->nickname)); + return (gpredict_strcmp(a->nickname,b->nickname)); } static gint rig_name_compare (const gchar* a,const gchar *b){ - return (g_ascii_strcasecmp(a,b)); + return (gpredict_strcmp(a,b)); } static inline gboolean check_set_response (gchar *buffback,gboolean retcode,const gchar *function){ Modified: trunk/src/gtk-rot-ctrl.c =================================================================== --- trunk/src/gtk-rot-ctrl.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/gtk-rot-ctrl.c 2011-03-26 01:10:20 UTC (rev 793) @@ -42,6 +42,7 @@ #include "compat.h" #include "sat-log.h" #include "predict-tools.h" +#include "gpredict-utils.h" #include "gtk-polar-plot.h" #include "gtk-rot-knob.h" #include "gtk-rot-ctrl.h" @@ -1444,14 +1445,14 @@ *simple function to sort the list of satellites in the combo box. */ static gint sat_name_compare (sat_t* a,sat_t*b){ - return (g_ascii_strcasecmp(a->nickname,b->nickname)); + return (gpredict_strcmp(a->nickname,b->nickname)); } /** \brief Compare Rotator Names. */ static gint rot_name_compare (const gchar* a,const gchar *b){ - return (g_ascii_strcasecmp(a,b)); + return (gpredict_strcmp(a,b)); } Modified: trunk/src/gtk-sat-selector.c =================================================================== --- trunk/src/gtk-sat-selector.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/gtk-sat-selector.c 2011-03-26 01:10:20 UTC (rev 793) @@ -32,6 +32,7 @@ /*needed _gnu_source to have strcasestr defined*/ #define _GNU_SOURCE + #include <string.h> #include <gtk/gtk.h> #include <glib/gi18n.h> @@ -44,9 +45,9 @@ #include "compat.h" #include "sat-cfg.h" #include "gtk-sat-selector.h" +#include "gpredict-utils.h" - static void gtk_sat_selector_class_init (GtkSatSelectorClass *class); static void gtk_sat_selector_init (GtkSatSelector *selector); static void gtk_sat_selector_destroy (GtkObject *object); @@ -614,7 +615,7 @@ gtk_tree_model_get(model, a, GTK_SAT_SELECTOR_COL_NAME, &sat1, -1); gtk_tree_model_get(model, b, GTK_SAT_SELECTOR_COL_NAME, &sat2, -1); - ret = g_ascii_strcasecmp (sat1, sat2); + ret = strverscmp (sat1, sat2); g_free (sat1); g_free (sat2); @@ -900,7 +901,7 @@ cat_a = load_cat_file_cat (a); cat_b = load_cat_file_cat (b); - temp = g_ascii_strcasecmp (cat_a,cat_b); + temp = gpredict_strcmp (cat_a,cat_b); g_free (cat_a); g_free (cat_b); return (temp); Modified: trunk/src/gtk-single-sat.c =================================================================== --- trunk/src/gtk-single-sat.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/gtk-single-sat.c 2011-03-26 01:10:20 UTC (rev 793) @@ -51,7 +51,6 @@ #include "sat-pass-dialogs.h" - /** \brief Column titles indexed with column symb. refs. */ const gchar *SINGLE_SAT_FIELD_TITLE[SINGLE_SAT_FIELD_NUMBER] = { N_("Azimuth"), @@ -1146,5 +1145,5 @@ } static gint sat_name_compare (sat_t *a,sat_t *b) { - return g_ascii_strcasecmp(a->nickname,b->nickname); + return gpredict_strcmp(a->nickname,b->nickname); } Modified: trunk/src/menubar.c =================================================================== --- trunk/src/menubar.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/menubar.c 2011-03-26 01:10:20 UTC (rev 793) @@ -48,6 +48,7 @@ #include "gtk-sat-module.h" #include "gtk-sat-module-popup.h" #include "gpredict-help.h" +#include "gpredict-utils.h" #include "tle-update.h" #include "compat.h" #include "menubar.h" @@ -1083,7 +1084,7 @@ gtk_tree_model_get(model, a, 0, &sat1, -1); gtk_tree_model_get(model, b, 0, &sat2, -1); - ret = g_ascii_strcasecmp (sat1, sat2); + ret = gpredict_strcmp (sat1, sat2); g_free (sat1); g_free (sat2); Modified: trunk/src/mod-cfg.c =================================================================== --- trunk/src/mod-cfg.c 2011-03-26 00:55:01 UTC (rev 792) +++ trunk/src/mod-cfg.c 2011-03-26 01:10:20 UTC (rev 793) @@ -39,7 +39,6 @@ #include "sat-pref-modules.h" #include "qth-editor.h" #include "mod-cfg.h" - #include "gtk-sat-selector.h" @@ -1164,7 +1163,7 @@ gtk_tree_model_get(model, a, GTK_SAT_SELECTOR_COL_NAME, &sat1, -1); gtk_tree_model_get(model, b, GTK_SAT_SELECTOR_COL_NAME, &sat2, -1); - ret = g_ascii_strcasecmp (sat1, sat2); + ret = gpredict_strcmp (sat1, sat2); g_free (sat1); g_free (sat2); @@ -1259,5 +1258,5 @@ static gint qth_name_compare (const gchar* a,const gchar *b){ - return (g_ascii_strcasecmp(a,b)); + return (gpredict_strcmp(a,b)); } Added: trunk/src/strnatcmp.c =================================================================== --- trunk/src/strnatcmp.c (rev 0) +++ trunk/src/strnatcmp.c 2011-03-26 01:10:20 UTC (rev 793) @@ -0,0 +1,178 @@ +/* -*- mode: c; c-file-style: "k&r" -*- + + strnatcmp.c -- Perform 'natural order' comparisons of strings in C. + Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + + +/* partial change history: + * + * 2004-10-10 mbp: Lift out character type dependencies into macros. + * + * Eric Sosman pointed out that ctype functions take a parameter whose + * value must be that of an unsigned int, even on platforms that have + * negative chars in their default char type. + */ + +#include <ctype.h> +#include <string.h> +#include <assert.h> +#include <stdio.h> + +#include "strnatcmp.h" + + +/* These are defined as macros to make it easier to adapt this code to + * different characters types or comparison functions. */ +static inline int +nat_isdigit(nat_char a) +{ + return isdigit((unsigned char) a); +} + + +static inline int +nat_isspace(nat_char a) +{ + return isspace((unsigned char) a); +} + + +static inline nat_char +nat_toupper(nat_char a) +{ + return toupper((unsigned char) a); +} + + + +static int +compare_right(nat_char const *a, nat_char const *b) +{ + int bias = 0; + + /* The longest run of digits wins. That aside, the greatest + value wins, but we can't know that it will until we've scanned + both numbers to know that they have the same magnitude, so we + remember it in BIAS. */ + for (;; a++, b++) { + if (!nat_isdigit(*a) && !nat_isdigit(*b)) + return bias; + else if (!nat_isdigit(*a)) + return -1; + else if (!nat_isdigit(*b)) + return +1; + else if (*a < *b) { + if (!bias) + bias = -1; + } else if (*a > *b) { + if (!bias) + bias = +1; + } else if (!*a && !*b) + return bias; + } + + return 0; +} + + +static int +compare_left(nat_char const *a, nat_char const *b) +{ + /* Compare two left-aligned numbers: the first to have a + different value wins. */ + for (;; a++, b++) { + if (!nat_isdigit(*a) && !nat_isdigit(*b)) + return 0; + else if (!nat_isdigit(*a)) + return -1; + else if (!nat_isdigit(*b)) + return +1; + else if (*a < *b) + return -1; + else if (*a > *b) + return +1; + } + + return 0; +} + + +static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case) +{ + int ai, bi; + nat_char ca, cb; + int fractional, result; + + assert(a && b); + ai = bi = 0; + while (1) { + ca = a[ai]; cb = b[bi]; + + /* skip over leading spaces or zeros */ + while (nat_isspace(ca)) + ca = a[++ai]; + + while (nat_isspace(cb)) + cb = b[++bi]; + + /* process run of digits */ + if (nat_isdigit(ca) && nat_isdigit(cb)) { + fractional = (ca == '0' || cb == '0'); + + if (fractional) { + if ((result = compare_left(a+ai, b+bi)) != 0) + return result; + } else { + if ((result = compare_right(a+ai, b+bi)) != 0) + return result; + } + } + + if (!ca && !cb) { + /* The strings compare the same. Perhaps the caller + will want to call strcmp to break the tie. */ + return 0; + } + + if (fold_case) { + ca = nat_toupper(ca); + cb = nat_toupper(cb); + } + + if (ca < cb) + return -1; + else if (ca > cb) + return +1; + + ++ai; ++bi; + } +} + + + +int strnatcmp(nat_char const *a, nat_char const *b) { + return strnatcmp0(a, b, 0); +} + + +/* Compare, recognizing numeric string and ignoring case. */ +int strnatcasecmp(nat_char const *a, nat_char const *b) { + return strnatcmp0(a, b, 1); +} Added: trunk/src/strnatcmp.h =================================================================== --- trunk/src/strnatcmp.h (rev 0) +++ trunk/src/strnatcmp.h 2011-03-26 01:10:20 UTC (rev 793) @@ -0,0 +1,31 @@ +/* -*- mode: c; c-file-style: "k&r" -*- + + strnatcmp.c -- Perform 'natural order' comparisons of strings in C. + Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + + +/* CUSTOMIZATION SECTION + * + * You can change this typedef, but must then also change the inline + * functions in strnatcmp.c */ +typedef char nat_char; + +int strnatcmp(nat_char const *a, nat_char const *b); +int strnatcasecmp(nat_char const *a, nat_char const *b); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |