]> git.tdb.fi Git - ext/subsurface.git/commitdiff
Add helper function for doing depth unit calculations
authorLinus Torvalds <torvalds@linux-foundation.org>
Wed, 21 Sep 2011 19:12:54 +0000 (12:12 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 21 Sep 2011 19:12:54 +0000 (12:12 -0700)
.. and use it for printing too.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
dive.c
dive.h
print.c
profile.c

diff --git a/dive.c b/dive.c
index 1b906ee43bd9a7e86db41d08cc5d3f288732a012..41bbabd698868bf43c90555acc38d13a83b41a9c 100644 (file)
--- a/dive.c
+++ b/dive.c
@@ -5,6 +5,31 @@
 
 #include "dive.h"
 
+double get_depth_units(unsigned int mm, int *frac, const char **units)
+{
+       int decimals;
+       double d;
+       const char *unit;
+
+       switch (output_units.length) {
+       case METERS:
+               d = mm / 1000.0;
+               unit = "m";
+               decimals = d < 20;
+               break;
+       case FEET:
+               d = mm_to_feet(mm);
+               unit = "ft";
+               decimals = 0;
+               break;
+       }
+       if (frac)
+               *frac = decimals;
+       if (units)
+               *units = unit;
+       return d;
+}
+
 struct dive *alloc_dive(void)
 {
        const int initial_samples = 5;
diff --git a/dive.h b/dive.h
index 85fdabf5d5f2d8ecda15f6c279022467693c649a..fc458d04d4292df0a36bf99428c84b66c169aad2 100644 (file)
--- a/dive.h
+++ b/dive.h
@@ -86,6 +86,8 @@ typedef struct {
        pressure_t start, end;
 } cylinder_t;
 
+extern double get_depth_units(unsigned int mm, int *frac, const char **units);
+
 static inline double mm_to_feet(int mm)
 {
        return mm * 0.00328084;
diff --git a/print.c b/print.c
index 8d3bc6150cc6514d1d6d8f5d40f37dcfeeb89c56..704ecaf66b76721ad2276f81ce8271c996921928 100644 (file)
--- a/print.c
+++ b/print.c
@@ -26,7 +26,9 @@ static void set_font(PangoLayout *layout, PangoFontDescription *font, double siz
  */
 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
 {
-       int len, width, height, maxwidth, maxheight;
+       double depth;
+       const char *unit;
+       int len, decimals, width, height, maxwidth, maxheight;
        PangoLayout *layout;
        struct tm *tm;
        char buffer[1024], divenr[20];
@@ -64,11 +66,12 @@ static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, P
         * with the depth/duration information. Need to mask that or
         * create a box or something.
         */
+       depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
        snprintf(buffer, sizeof(buffer),
-               "Max depth: %d ft\n"
+               "Max depth: %.*f %s\n"
                "Duration: %d:%02d\n"
                "%s",
-               to_feet(dive->maxdepth),
+               decimals, depth, unit,
                dive->duration.seconds / 60,
                dive->duration.seconds % 60,
                dive->buddy ? :"");
index 3f3dd42bc108f1c0b7e048a1521388e92e1a7c2d..1b7db1d904909bbb440dfe4e184e0579132fafa1 100644 (file)
--- a/profile.c
+++ b/profile.c
@@ -158,22 +158,12 @@ static void plot_text(struct graphics_context *gc, const text_render_options_t *
 
 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
 {
-       int sec = entry->sec;
-       depth_t depth = { entry->val };
-       const char *fmt;
+       int sec = entry->sec, decimals;
        double d;
 
-       switch (output_units.length) {
-       case METERS:
-               d = depth.mm / 1000.0;
-               fmt = "%.1f";
-               break;
-       case FEET:
-               d = to_feet(depth);
-               fmt = "%.0f";
-               break;
-       }
-       plot_text(gc, tro, sec, depth.mm, fmt, d);
+       d = get_depth_units(entry->val, &decimals, NULL);
+
+       plot_text(gc, tro, sec, entry->val, "%.*f", decimals, d);
 }
 
 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)