Cleanup: const-ify parameters to trivial accessor functions

Accessor-functions without noticeable logic, such as depth_to_bar()
can trivially be made "const-clean".

Moreover, let get_dive_location() return a "const char *". The
non-const version must have been an oversight, as the caller
must not free() or overwrite the string.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-08-17 00:36:51 +02:00 committed by Dirk Hohndel
parent 360f07e453
commit 44f34d8cd7
2 changed files with 35 additions and 35 deletions

View file

@ -3858,7 +3858,7 @@ static bool new_picture_for_dive(struct dive *d, const char *filename)
// only add pictures that have timestamps between 30 minutes before the dive and // only add pictures that have timestamps between 30 minutes before the dive and
// 30 minutes after the dive ends // 30 minutes after the dive ends
#define D30MIN (30 * 60) #define D30MIN (30 * 60)
bool dive_check_picture_time(struct dive *d, int shift_time, timestamp_t timestamp) bool dive_check_picture_time(const struct dive *d, int shift_time, timestamp_t timestamp)
{ {
offset_t offset; offset_t offset;
if (timestamp) { if (timestamp) {
@ -4025,7 +4025,7 @@ void delete_current_divecomputer(void)
/* helper function to make it easier to work with our structures /* helper function to make it easier to work with our structures
* we don't interpolate here, just use the value from the last sample up to that time */ * we don't interpolate here, just use the value from the last sample up to that time */
int get_depth_at_time(struct divecomputer *dc, unsigned int time) int get_depth_at_time(const struct divecomputer *dc, unsigned int time)
{ {
int depth = 0; int depth = 0;
if (dc && dc->sample) if (dc && dc->sample)
@ -4038,7 +4038,7 @@ int get_depth_at_time(struct divecomputer *dc, unsigned int time)
} }
//Calculate O2 in best mix //Calculate O2 in best mix
fraction_t best_o2(depth_t depth, struct dive *dive) fraction_t best_o2(depth_t depth, const struct dive *dive)
{ {
fraction_t fo2; fraction_t fo2;
@ -4050,7 +4050,7 @@ fraction_t best_o2(depth_t depth, struct dive *dive)
} }
//Calculate He in best mix. O2 is considered narcopic //Calculate He in best mix. O2 is considered narcopic
fraction_t best_he(depth_t depth, struct dive *dive) fraction_t best_he(depth_t depth, const struct dive *dive)
{ {
fraction_t fhe; fraction_t fhe;
int pnarcotic, ambient; int pnarcotic, ambient;
@ -4107,17 +4107,17 @@ int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity
return mbar; return mbar;
} }
int depth_to_mbar(int depth, struct dive *dive) int depth_to_mbar(int depth, const struct dive *dive)
{ {
return calculate_depth_to_mbar(depth, dive->surface_pressure, dive->salinity); return calculate_depth_to_mbar(depth, dive->surface_pressure, dive->salinity);
} }
double depth_to_bar(int depth, struct dive *dive) double depth_to_bar(int depth, const struct dive *dive)
{ {
return depth_to_mbar(depth, dive) / 1000.0; return depth_to_mbar(depth, dive) / 1000.0;
} }
double depth_to_atm(int depth, struct dive *dive) double depth_to_atm(int depth, const struct dive *dive)
{ {
return mbar_to_atm(depth_to_mbar(depth, dive)); return mbar_to_atm(depth_to_mbar(depth, dive));
} }
@ -4126,7 +4126,7 @@ double depth_to_atm(int depth, struct dive *dive)
* (that's the one that some dive computers like the Uemis Zurich * (that's the one that some dive computers like the Uemis Zurich
* provide - for the other models that do this libdivecomputer has to * provide - for the other models that do this libdivecomputer has to
* take care of this, but the Uemis we support natively */ * take care of this, but the Uemis we support natively */
int rel_mbar_to_depth(int mbar, struct dive *dive) int rel_mbar_to_depth(int mbar, const struct dive *dive)
{ {
int cm; int cm;
double specific_weight = 1.03 * 0.981; double specific_weight = 1.03 * 0.981;
@ -4137,7 +4137,7 @@ int rel_mbar_to_depth(int mbar, struct dive *dive)
return cm * 10; return cm * 10;
} }
int mbar_to_depth(int mbar, struct dive *dive) int mbar_to_depth(int mbar, const struct dive *dive)
{ {
pressure_t surface_pressure; pressure_t surface_pressure;
if (dive->surface_pressure.mbar) if (dive->surface_pressure.mbar)
@ -4148,7 +4148,7 @@ int mbar_to_depth(int mbar, struct dive *dive)
} }
/* MOD rounded to multiples of roundto mm */ /* MOD rounded to multiples of roundto mm */
depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, struct dive *dive, int roundto) depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive, int roundto)
{ {
depth_t rounded_depth; depth_t rounded_depth;
@ -4158,7 +4158,7 @@ depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, struct dive *dive, int
} }
/* Maximum narcotic depth rounded to multiples of roundto mm */ /* Maximum narcotic depth rounded to multiples of roundto mm */
depth_t gas_mnd(struct gasmix mix, depth_t end, struct dive *dive, int roundto) depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int roundto)
{ {
depth_t rounded_depth; depth_t rounded_depth;
pressure_t ppo2n2; pressure_t ppo2n2;
@ -4183,14 +4183,14 @@ struct dive *get_dive_from_table(int nr, struct dive_table *dt)
return dt->dives[nr]; return dt->dives[nr];
} }
struct dive_site *get_dive_site_for_dive(struct dive *dive) struct dive_site *get_dive_site_for_dive(const struct dive *dive)
{ {
if (dive) if (dive)
return get_dive_site_by_uuid(dive->dive_site_uuid); return get_dive_site_by_uuid(dive->dive_site_uuid);
return NULL; return NULL;
} }
const char *get_dive_country(struct dive *dive) const char *get_dive_country(const struct dive *dive)
{ {
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid); struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (ds) { if (ds) {
@ -4201,7 +4201,7 @@ const char *get_dive_country(struct dive *dive)
return NULL; return NULL;
} }
char *get_dive_location(struct dive *dive) const char *get_dive_location(const struct dive *dive)
{ {
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid); struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (ds && ds->name) if (ds && ds->name)
@ -4209,10 +4209,10 @@ char *get_dive_location(struct dive *dive)
return NULL; return NULL;
} }
unsigned int number_of_computers(struct dive *dive) unsigned int number_of_computers(const struct dive *dive)
{ {
unsigned int total_number = 0; unsigned int total_number = 0;
struct divecomputer *dc = &dive->dc; const struct divecomputer *dc = &dive->dc;
if (!dive) if (!dive)
return 1; return 1;
@ -4275,12 +4275,12 @@ int get_idx_by_uniq_id(int id)
return i; return i;
} }
bool dive_site_has_gps_location(struct dive_site *ds) bool dive_site_has_gps_location(const struct dive_site *ds)
{ {
return ds && (ds->latitude.udeg || ds->longitude.udeg); return ds && (ds->latitude.udeg || ds->longitude.udeg);
} }
int dive_has_gps_location(struct dive *dive) int dive_has_gps_location(const struct dive *dive)
{ {
if (!dive) if (!dive)
return false; return false;

View file

@ -373,7 +373,7 @@ struct picture {
for (struct picture *picture = (_divestruct).picture_list; picture; picture = picture->next) for (struct picture *picture = (_divestruct).picture_list; picture; picture = picture->next)
extern struct picture *alloc_picture(); extern struct picture *alloc_picture();
extern bool dive_check_picture_time(struct dive *d, int shift_time, timestamp_t timestamp); extern bool dive_check_picture_time(const struct dive *d, int shift_time, timestamp_t timestamp);
extern void dive_create_picture(struct dive *d, const char *filename, int shift_time, bool match_all); extern void dive_create_picture(struct dive *d, const char *filename, int shift_time, bool match_all);
extern void dive_add_picture(struct dive *d, struct picture *newpic); extern void dive_add_picture(struct dive *d, struct picture *newpic);
extern bool dive_remove_picture(struct dive *d, const char *filename); extern bool dive_remove_picture(struct dive *d, const char *filename);
@ -384,20 +384,20 @@ extern void picture_free(struct picture *picture);
extern bool has_gaschange_event(struct dive *dive, struct divecomputer *dc, int idx); extern bool has_gaschange_event(struct dive *dive, struct divecomputer *dc, int idx);
extern int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc); extern int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc);
extern int get_depth_at_time(struct divecomputer *dc, unsigned int time); extern int get_depth_at_time(const struct divecomputer *dc, unsigned int time);
extern fraction_t best_o2(depth_t depth, struct dive *dive); extern fraction_t best_o2(depth_t depth, const struct dive *dive);
extern fraction_t best_he(depth_t depth, struct dive *dive); extern fraction_t best_he(depth_t depth, const struct dive *dive);
extern int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null); extern int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null);
extern int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity); extern int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity);
extern int depth_to_mbar(int depth, struct dive *dive); extern int depth_to_mbar(int depth, const struct dive *dive);
extern double depth_to_bar(int depth, struct dive *dive); extern double depth_to_bar(int depth, const struct dive *dive);
extern double depth_to_atm(int depth, struct dive *dive); extern double depth_to_atm(int depth, const struct dive *dive);
extern int rel_mbar_to_depth(int mbar, struct dive *dive); extern int rel_mbar_to_depth(int mbar, const struct dive *dive);
extern int mbar_to_depth(int mbar, struct dive *dive); extern int mbar_to_depth(int mbar, const struct dive *dive);
extern depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, struct dive *dive, int roundto); extern depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive, int roundto);
extern depth_t gas_mnd(struct gasmix mix, depth_t end, struct dive *dive, int roundto); extern depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int roundto);
#define SURFACE_THRESHOLD 750 /* somewhat arbitrary: only below 75cm is it really diving */ #define SURFACE_THRESHOLD 750 /* somewhat arbitrary: only below 75cm is it really diving */
@ -442,10 +442,10 @@ extern unsigned int dc_number;
extern struct dive *get_dive(int nr); extern struct dive *get_dive(int nr);
extern struct dive *get_dive_from_table(int nr, struct dive_table *dt); extern struct dive *get_dive_from_table(int nr, struct dive_table *dt);
extern struct dive_site *get_dive_site_for_dive(struct dive *dive); extern struct dive_site *get_dive_site_for_dive(const struct dive *dive);
extern const char *get_dive_country(struct dive *dive); extern const char *get_dive_country(const struct dive *dive);
extern char *get_dive_location(struct dive *dive); extern const char *get_dive_location(const struct dive *dive);
extern unsigned int number_of_computers(struct dive *dive); extern unsigned int number_of_computers(const struct dive *dive);
extern struct divecomputer *get_dive_dc(struct dive *dive, int nr); extern struct divecomputer *get_dive_dc(struct dive *dive, int nr);
extern timestamp_t dive_endtime(const struct dive *dive); extern timestamp_t dive_endtime(const struct dive *dive);
@ -475,8 +475,8 @@ extern "C" {
extern struct dive *get_dive_by_uniq_id(int id); extern struct dive *get_dive_by_uniq_id(int id);
extern int get_idx_by_uniq_id(int id); extern int get_idx_by_uniq_id(int id);
extern bool dive_site_has_gps_location(struct dive_site *ds); extern bool dive_site_has_gps_location(const struct dive_site *ds);
extern int dive_has_gps_location(struct dive *dive); extern int dive_has_gps_location(const struct dive *dive);
extern int report_error(const char *fmt, ...); extern int report_error(const char *fmt, ...);
extern void set_error_cb(void(*cb)(char *)); // Callback takes ownership of passed string extern void set_error_cb(void(*cb)(char *)); // Callback takes ownership of passed string