branch with improvemnts

Fork of M2XStreamClient by AT&T M2X Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LocationParseFunctions.h Source File

LocationParseFunctions.h

00001 #ifndef LocationParseFunctions_h
00002 #define LocationParseFunctions_h
00003 
00004 // Data structures and functions used to parse locations
00005 
00006 #define LOCATION_BUF_LEN 20
00007 
00008 typedef struct {
00009   uint16_t state;
00010   char name_str[LOCATION_BUF_LEN + 1];
00011   double latitude;
00012   double longitude;
00013   double elevation;
00014   char timestamp_str[LOCATION_BUF_LEN + 1];
00015   int index;
00016 
00017   location_read_callback callback;
00018   void* context;
00019 } location_parsing_context_state;
00020 
00021 #define WAITING_NAME 0x1
00022 #define WAITING_LATITUDE 0x2
00023 #define WAITING_LONGITUDE 0x4
00024 #define WAITING_ELEVATION 0x8
00025 #define WAITING_TIMESTAMP 0x10
00026 
00027 #define GOT_NAME 0x20
00028 #define GOT_LATITUDE 0x40
00029 #define GOT_LONGITUDE 0x80
00030 #define GOT_ELEVATION 0x100
00031 #define GOT_TIMESTAMP 0x200
00032 
00033 #define GOT_LOCATION (GOT_NAME | GOT_LATITUDE | GOT_LONGITUDE | GOT_ELEVATION | GOT_TIMESTAMP)
00034 #define TEST_GOT_LOCATION(state_) (((state_) & GOT_LOCATION) == GOT_LOCATION)
00035 
00036 #define TEST_IS_NAME(state_) (((state_) & (WAITING_NAME | GOT_NAME)) == WAITING_NAME)
00037 #define TEST_IS_LATITUDE(state_) (((state_) & (WAITING_LATITUDE | GOT_LATITUDE)) \
00038                                   == WAITING_LATITUDE)
00039 #define TEST_IS_LONGITUDE(state_) (((state_) & (WAITING_LONGITUDE | GOT_LONGITUDE)) \
00040                                    == WAITING_LONGITUDE)
00041 #define TEST_IS_ELEVATION(state_) (((state_) & (WAITING_ELEVATION | GOT_ELEVATION)) \
00042                                    == WAITING_ELEVATION)
00043 #define TEST_IS_TIMESTAMP(state_) (((state_) & (WAITING_TIMESTAMP | GOT_TIMESTAMP)) \
00044                                    == WAITING_TIMESTAMP)
00045 
00046 static void on_location_key_found(jsonlite_callback_context* context,
00047                                   jsonlite_token* token) {
00048   location_parsing_context_state* state =
00049       (location_parsing_context_state*) context->client_state;
00050   if (strncmp((const char*) token->start, "waypoints", 9) == 0) {
00051     // only parses those locations in waypoints, skip the outer one
00052     state->state = 0;
00053   } else if (strncmp((const char*) token->start, "name", 4) == 0) {
00054     state->state |= WAITING_NAME;
00055   } else if (strncmp((const char*) token->start, "latitude", 8) == 0) {
00056     state->state |= WAITING_LATITUDE;
00057   } else if (strncmp((const char*) token->start, "longitude", 9) == 0) {
00058     state->state |= WAITING_LONGITUDE;
00059   } else if (strncmp((const char*) token->start, "elevation", 9) == 0) {
00060     state->state |= WAITING_ELEVATION;
00061   } else if (strncmp((const char*) token->start, "timestamp", 9) == 0) {
00062     state->state |= WAITING_TIMESTAMP;
00063   }
00064 }
00065 
00066 static void on_location_string_found(jsonlite_callback_context* context,
00067                                      jsonlite_token* token) {
00068   location_parsing_context_state* state =
00069       (location_parsing_context_state*) context->client_state;
00070 
00071   if (TEST_IS_NAME(state->state)) {
00072     strncpy(state->name_str, (const char*) token->start,
00073             MIN(token->end - token->start, LOCATION_BUF_LEN));
00074     state->name_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0';
00075     state->state |= GOT_NAME;
00076   } else if (TEST_IS_LATITUDE(state->state)) {
00077     state->latitude = atof((const char*) token->start);
00078     state->state |= GOT_LATITUDE;
00079   } else if (TEST_IS_LONGITUDE(state->state)) {
00080     state->longitude = atof((const char*) token->start);
00081     state->state |= GOT_LONGITUDE;
00082   } else if (TEST_IS_ELEVATION(state->state)) {
00083     state->elevation = atof((const char*) token->start);
00084     state->state |= GOT_ELEVATION;
00085   } else if (TEST_IS_TIMESTAMP(state->state)) {
00086     strncpy(state->timestamp_str, (const char*) token->start,
00087             MIN(token->end - token->start, LOCATION_BUF_LEN));
00088     state->timestamp_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0';
00089     state->state |= GOT_TIMESTAMP;
00090   }
00091 
00092   if (TEST_GOT_LOCATION(state->state)) {
00093     state->callback(state->name_str, state->latitude, state->longitude,
00094                     state->elevation, state->timestamp_str, state->index++,
00095                     state->context);
00096     state->state = 0;
00097   }
00098 }
00099 
00100 #endif  /* LocationParseFunctions_h */