Skip to content

Commit

Permalink
Added static and const keywords in bunches of places
Browse files Browse the repository at this point in the history
This helps in self-documenting the interfaces, keeping the
global namespace clean, and providing optimization hints
to the compiler.
  • Loading branch information
dfandrich committed Aug 18, 2012
1 parent 4cc0312 commit df88775
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 88 deletions.
31 changes: 15 additions & 16 deletions correlate.c
Expand Up @@ -36,20 +36,21 @@
#include "unixtime.h"

/* Internal functions used to make it work. */
void Round(struct GPSPoint* First, struct GPSPoint* Result,
struct CorrelateOptions* Options, time_t PhotoTime);
void Interpolate(struct GPSPoint* First, struct GPSPoint* Result,
struct CorrelateOptions* Options, time_t PhotoTime);
static void Round(const struct GPSPoint* First, struct GPSPoint* Result,
time_t PhotoTime);
static void Interpolate(const struct GPSPoint* First, struct GPSPoint* Result,
time_t PhotoTime);

/* This function returns a GPSPoint with the point selected for the
* file. This allows us to do funky stuff like not actually write
* the files - ie, just correlate and keep into memory... */

struct GPSPoint* CorrelatePhoto(char* Filename,
struct GPSPoint* CorrelatePhoto(const char* Filename,
struct CorrelateOptions* Options)
{
/* Read out the timestamp from the EXIF data. */
char* TimeTemp; int IncludesGPS = 0;
char* TimeTemp;
int IncludesGPS = 0;
TimeTemp = ReadExifDate(Filename, &IncludesGPS);
if (!TimeTemp)
{
Expand Down Expand Up @@ -144,7 +145,7 @@ struct GPSPoint* CorrelatePhoto(char* Filename,
/* Time to run through the list, and see if our PhotoTime
* is in between two points. Alternately, it might be
* exactly on a point... even better... */
struct GPSPoint* Search;
const struct GPSPoint* Search;
struct GPSPoint* Actual = malloc(sizeof(struct GPSPoint));

Options->Result = CORR_NOMATCH; /* For convenience later */
Expand Down Expand Up @@ -232,14 +233,12 @@ struct GPSPoint* CorrelatePhoto(char* Filename,
if (Options->NoInterpolate)
{
/* No interpolation. Round. */
Round(Search, Actual,
Options, PhotoTime);
Round(Search, Actual, PhotoTime);
Options->Result = CORR_ROUND;
break;
} else {
/* Interpolate away! */
Interpolate(Search, Actual,
Options, PhotoTime);
Interpolate(Search, Actual, PhotoTime);
Options->Result = CORR_INTERPOLATED;
break;
}
Expand Down Expand Up @@ -279,12 +278,12 @@ struct GPSPoint* CorrelatePhoto(char* Filename,
return NULL;
};

void Round(struct GPSPoint* First, struct GPSPoint* Result,
struct CorrelateOptions* Options, time_t PhotoTime)
void Round(const struct GPSPoint* First, struct GPSPoint* Result,
time_t PhotoTime)
{
/* Round the point between the two points - ie, it will end
* up being one or the other point. */
struct GPSPoint* CopyFrom = NULL;
const struct GPSPoint* CopyFrom = NULL;

/* Determine the difference between the two points.
* We're using the scale function used by interpolate.
Expand Down Expand Up @@ -312,8 +311,8 @@ void Round(struct GPSPoint* First, struct GPSPoint* Result,

}

void Interpolate(struct GPSPoint* First, struct GPSPoint* Result,
struct CorrelateOptions* Options, time_t PhotoTime)
void Interpolate(const struct GPSPoint* First, struct GPSPoint* Result,
time_t PhotoTime)
{
/* Interpolate between the two points. The first point
* is First, the other First->Next. Results into Result. */
Expand Down
2 changes: 1 addition & 1 deletion correlate.h
Expand Up @@ -77,6 +77,6 @@ struct CorrelateOptions {
#define CORR_GPSDATAEXISTS 8


struct GPSPoint* CorrelatePhoto(char* Filename,
struct GPSPoint* CorrelatePhoto(const char* Filename,
struct CorrelateOptions* Options);

13 changes: 7 additions & 6 deletions exif-gps.cpp
Expand Up @@ -83,7 +83,7 @@ int main(int argc, char* argv[])
};
*/

char* ReadExifDate(char* File, int* IncludesGPS)
char* ReadExifDate(const char* File, int* IncludesGPS)
{
// Open and read the file.
Exiv2::ExifData ExifRead;
Expand Down Expand Up @@ -143,7 +143,7 @@ char* ReadExifDate(char* File, int* IncludesGPS)
return Copy; // Its up to the caller to free this.
};

char* ReadExifData(char* File, double* Lat, double* Long, double* Elev, int* IncludesGPS)
char* ReadExifData(const char* File, double* Lat, double* Long, double* Elev, int* IncludesGPS)
{
// This function varies in that it reads
// much more data than the last, specifically
Expand Down Expand Up @@ -282,7 +282,7 @@ char* ReadExifData(char* File, double* Lat, double* Long, double* Elev, int* Inc

// This function is for the --fix-datestamp option.
// DateStamp and TimeStamp should be 12-char strings.
char* ReadGPSTimestamp(char* File, char* DateStamp, char* TimeStamp, int* IncludesGPS)
char* ReadGPSTimestamp(const char* File, char* DateStamp, char* TimeStamp, int* IncludesGPS)
{
// This function varies in that it reads
// much more data than the last, specifically
Expand Down Expand Up @@ -436,7 +436,8 @@ void ConvertToRational(double Number,

}

int WriteGPSData(char* File, struct GPSPoint* Point, char* Datum, int NoChangeMtime, int DegMinSecs)
int WriteGPSData(const char* File, const struct GPSPoint* Point,
const char* Datum, int NoChangeMtime, int DegMinSecs)
{
// Write the GPS data to the file...

Expand Down Expand Up @@ -651,7 +652,7 @@ int WriteGPSData(char* File, struct GPSPoint* Point, char* Datum, int NoChangeMt

};

int WriteFixedDatestamp(char* File, time_t Time)
int WriteFixedDatestamp(const char* File, time_t Time)
{
// Write the GPS data to the file...

Expand Down Expand Up @@ -728,7 +729,7 @@ int WriteFixedDatestamp(char* File, time_t Time)
return 1;
}

int RemoveGPSExif(char* File, int NoChangeMtime)
int RemoveGPSExif(const char* File, int NoChangeMtime)
{
struct stat statbuf;
struct stat statbuf2;
Expand Down
13 changes: 7 additions & 6 deletions exif-gps.h
Expand Up @@ -31,12 +31,13 @@
extern "C" {
#endif

char* ReadExifDate(char* File, int* IncludesGPS);
char* ReadExifData(char* File, double* Lat, double* Long, double* Elevation, int* IncludesGPS);
char* ReadGPSTimestamp(char* File, char* DateStamp, char* TimeStamp, int* IncludesGPS);
int WriteGPSData(char* File, struct GPSPoint* Point, char* Datum, int NoChangeMtime, int DegMinSecs);
int WriteFixedDatestamp(char* File, time_t TimeStamp);
int RemoveGPSExif(char* File, int NoChangeMtime);
char* ReadExifDate(const char* File, int* IncludesGPS);
char* ReadExifData(const char* File, double* Lat, double* Long, double* Elevation, int* IncludesGPS);
char* ReadGPSTimestamp(const char* File, char* DateStamp, char* TimeStamp, int* IncludesGPS);
int WriteGPSData(const char* File, const struct GPSPoint* Point,
const char* Datum, int NoChangeMtime, int DegMinSecs);
int WriteFixedDatestamp(const char* File, time_t TimeStamp);
int RemoveGPSExif(const char* File, int NoChangeMtime);

#ifdef __cplusplus
}
Expand Down
43 changes: 21 additions & 22 deletions gpx-read.c
Expand Up @@ -35,26 +35,26 @@
#include "unixtime.h"
#include "gpsstructure.h"

struct GPSPoint* FirstPoint;
struct GPSPoint* LastPoint;
static struct GPSPoint* FirstPoint;
static struct GPSPoint* LastPoint;

void ExtractTrackPoints(xmlNodePtr Start)
static void ExtractTrackPoints(xmlNodePtr Start)
{
/* The pointer passed to us should be the start
* of a heap of trkpt's. So walk though them,
* extracting what we need. */
xmlNodePtr Current = NULL;
xmlNodePtr CCurrent = NULL;
xmlAttrPtr Properties = NULL;
char* Lat;
char* Long;
char* Elev;
char* Time;
const char* Lat;
const char* Long;
const char* Elev;
const char* Time;

for (Current = Start; Current; Current = Current->next)
{
if ((Current->type == XML_ELEMENT_NODE) &&
(strcmp((char *)Current->name, "trkpt") == 0))
(strcmp((const char *)Current->name, "trkpt") == 0))
{
/* This is indeed a trackpoint. Extract! */

Expand All @@ -72,13 +72,13 @@ void ExtractTrackPoints(xmlNodePtr Start)
Properties;
Properties = Properties->next)
{
if (strcmp((char *)Properties->name, "lat") == 0)
if (strcmp((const char *)Properties->name, "lat") == 0)
{
Lat = (char *)Properties->children->content;
Lat = (const char *)Properties->children->content;
}
if (strcmp((char *)Properties->name, "lon") == 0)
if (strcmp((const char *)Properties->name, "lon") == 0)
{
Long = (char *)Properties->children->content;
Long = (const char *)Properties->children->content;
}
}

Expand All @@ -90,15 +90,15 @@ void ExtractTrackPoints(xmlNodePtr Start)
CCurrent;
CCurrent = CCurrent->next)
{
if (strcmp((char *)CCurrent->name, "ele") == 0)
if (strcmp((const char *)CCurrent->name, "ele") == 0)
{
if (CCurrent->children)
Elev = (char *)CCurrent->children->content;
Elev = (const char *)CCurrent->children->content;
}
if (strcmp((char *)CCurrent->name, "time") == 0)
if (strcmp((const char *)CCurrent->name, "time") == 0)
{
if (CCurrent->children)
Time = (char *)CCurrent->children->content;
Time = (const char *)CCurrent->children->content;
}
}

Expand Down Expand Up @@ -153,15 +153,15 @@ void ExtractTrackPoints(xmlNodePtr Start)
/* Return control to the recursive function... */
}

void FindTrackSeg(xmlNodePtr Start)
static void FindTrackSeg(xmlNodePtr Start)
{
/* Go recursive till we find a <trgseg> tag. */
xmlNodePtr Current = NULL;

for (Current = Start; Current; Current = Current->next)
{
if ((Current->type == XML_ELEMENT_NODE) &&
(strcmp((char *)Current->name, "trkseg") == 0))
(strcmp((const char *)Current->name, "trkseg") == 0))
{
/* Found it... the children should
* all be trkpt's. */
Expand All @@ -181,7 +181,7 @@ void FindTrackSeg(xmlNodePtr Start)
}


struct GPSPoint* ReadGPX(char* File)
struct GPSPoint* ReadGPX(const char* File)
{
/* Init the libxml library. Also checks version. */
LIBXML_TEST_VERSION
Expand Down Expand Up @@ -209,7 +209,7 @@ struct GPSPoint* ReadGPX(char* File)

/* Check that this is indeed a GPX - the root node
* should be "gpx". */
if (strcmp((char *)GPXRoot->name, "gpx") == 0)
if (strcmp((const char *)GPXRoot->name, "gpx") == 0)
{
/* Ok, it is a GPX file. */
} else {
Expand Down Expand Up @@ -246,7 +246,7 @@ struct GPSPoint* ReadGPX(char* File)
FirstPoint = NULL;
LastPoint = NULL;

char* OldLocale = setlocale(LC_NUMERIC, NULL);
const char* OldLocale = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "C");

FindTrackSeg(GPXRoot);
Expand Down Expand Up @@ -275,4 +275,3 @@ void FreePointList(struct GPSPoint* List)
CurrentFree = NextFree;
}
};

2 changes: 1 addition & 1 deletion gpx-read.h
Expand Up @@ -25,5 +25,5 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

struct GPSPoint* ReadGPX(char* File);
struct GPSPoint* ReadGPX(const char* File);
void FreePointList(struct GPSPoint* List);

0 comments on commit df88775

Please sign in to comment.