Skip to content

Commit

Permalink
initial options processing; output formats
Browse files Browse the repository at this point in the history
Something basic, to get things going.
  • Loading branch information
lindes committed Apr 10, 2012
1 parent c4dcb79 commit bbe9cf0
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ Hopefully, just type `make`.
It would be good if this tool had configuration for such things as:

* amount of accuracy required
* whether or not to wait for a non-cached result
* choosing (or even specifying) different output formats

If you'd like to help make any of these happen, feel free to fork the
github repo for this, and/or send me money to encourage my further
development. :)

## Past wish-list, now completed!

* whether or not to wait for a non-cached result (added 2012-04-10) -- `-r <results>`
* choosing (or even specifying) different output formats -- `-f <format>`

## Author:

David Lindes.
160 changes: 153 additions & 7 deletions get-location.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

#include <sysexits.h> // for EX_USAGE

///// Delegate interface for CoreLocation to talk to:
@interface MyLocationFinder : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
Expand Down Expand Up @@ -114,23 +116,167 @@ void defaultPrinter(CLLocation *loc)
printf("Location: %s\n", [[loc description] UTF8String]);
}

void _lispPrinter(CLLocation *loc, BOOL quote, BOOL comments)
{
char *quote1 = quote ? "\"" : "";
char *quote2 = quote ? "\\" : "";

printf("%s((%.8f %.8f %g) %s(%.1f %g) %s(%g %g) %s(%s\"%s%s\" %g))%s%s\n",
quote1,

loc.coordinate.latitude,
loc.coordinate.longitude,
loc.horizontalAccuracy,
comments ? " ; lat, lon, accuracy (meters)\n " : "",

loc.altitude,
loc.verticalAccuracy,
comments ? " ; altitude, accurace (both meters)\n " : "",

loc.speed,
loc.course,
comments ? " ; speed (k/h), course (degrees)\n " : "",

quote2,
[[loc.timestamp description] UTF8String],
quote2,
-[loc.timestamp timeIntervalSinceNow],
comments ? " ; gathered date, age at time printed" : "",

quote1);
}
void lispPrinter(CLLocation *loc) { _lispPrinter(loc, NO, NO); }
void lispPrinter_quoted(CLLocation *loc) { _lispPrinter(loc, YES, NO); }
void lispPrinter_commented(CLLocation *loc) { _lispPrinter(loc, NO, YES); }

void plistPrinter(CLLocation *loc)
{
printf("(:latitude %.8f :longitude %.8f :altitude %g"
" :horizontal-accuracy %g :vertical-accuracy %g"
" :speed %g :course %g :timestamp \"%s\" :age %g)\n",

loc.coordinate.latitude,
loc.coordinate.longitude,
loc.altitude,
loc.horizontalAccuracy,
loc.verticalAccuracy,
loc.speed,
loc.course,
[[loc.timestamp description] UTF8String],
-[loc.timestamp timeIntervalSinceNow]);
}

void jsonPrinter(CLLocation *loc)
{
printf("{ \"type\": \"Point\", \"coordinates\": [ %.8f, %.8f ] }\n",
loc.coordinate.latitude,
loc.coordinate.longitude);
}

struct {
char *name;
locationPrinter printer;
char *description;
} formats[] = {
{ "default", defaultPrinter, "the result of calling description on the CLLocation object [default]" },
{ "logging", defaultLogger, "as above, but use NSLog to report it [default with -l]" },
{ "json", jsonPrinter, "print a json object, as per http://www.geojson.org/" },
{ "lisp", lispPrinter, "print as a lisp S-expression (suitable for (read))" },
{ "quoted-lisp", lispPrinter_quoted, "as lisp, but quote it (suitable for (read-from-string ...))" },
{ "commented-lisp",
lispPrinter_commented,"as lisp, but with newlines and comments" },
{ "lisp-plist", plistPrinter, "a lisp properties list, suitable for (read)" }
};

#define FORMAT_COUNT (sizeof(formats) / sizeof(*formats))

void usage(const char *fmt, ...)
{
va_list args;
int i;

va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);

fprintf(stderr, "Usage:\n"
" get-location [-ldv] [-r <results>] [-f format]\n"
"\n"
" -l: use NSLog to log and print data (more data than just -f loging)\n"
" -v: more verbosity\n"
" -d: some debugging\n"
"\n"
" -r <results>: Wait for at least <results> results before finishing\n"
" (caveat: we do give up eventually, so use (0 <= results <= about 2)\n"
" (0 is the first, probably-cached result, 1 is the first subsequent, ...)\n"
"\n"
" -f format: use one of the following formats for printing:\n");

for(i = 0; i < FORMAT_COUNT; ++i)
fprintf(stderr, "%20s: %s\n", formats[i].name, formats[i].description);

exit(EX_USAGE);
}

///// main loop

int main(int argc, char *argv[])
{
int i;
BOOL logging = NO, verbose = NO, debug = NO; // TODO: make configurable, and probably handle differently
NSInteger hitsRequired = 1; // TODO: make configurable
// for loops and things:
int i, ch;

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
MyLocationFinder *finder;
// run-time flags:
BOOL logging = NO, verbose = NO, debug = NO; // TODO: make configurable, and probably handle differently
NSInteger hitsRequired = 0; // TODO: make configurable

locationPrinter printer = defaultPrinter;
locationPrinter newPrinter = NULL; // for option processing, and as a flag for if that was used

if(logging)
//// parse args

while((ch = getopt(argc, argv, "dlvf:r:")) != -1)
{
switch(ch)
{
case 'd': debug = YES; break;
case 'l': logging = YES; break;
case 'v': verbose = YES; break;

case 'r':
hitsRequired = atoi(optarg);
break;

case 'f':
for(i = 0; i < FORMAT_COUNT; ++i)
{
if(strcmp(optarg, formats[i].name) == 0)
{
newPrinter = formats[i].printer;
break;
}
}

if(newPrinter)
printer = newPrinter;
else
usage("Unknown output format: %s\n", optarg);

break;
default:
usage("Unknown option: %c\n", ch);
break;
}
}

if(logging && !newPrinter)
printer = defaultLogger;

//// go!

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
MyLocationFinder *finder;

if(![CLLocationManager locationServicesEnabled])
{
NSLog(@"Sorry, Location Services not available.");
Expand Down

0 comments on commit bbe9cf0

Please sign in to comment.