Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Takes CSV data as input and converts it to json
- * In this case we're using earthquake data
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- // define the maximum amount of data
- #define MAX 256
- // Kill the program and show an error message
- void die(const char *msg)
- {
- if(errno)
- perror(msg);
- else
- printf("%s\n", msg);
- exit(1);
- }
- int main(int argc, char *argv[])
- {
- // set up the variables for our expected data
- char src[10];
- char eqid[MAX];
- char version[MAX];
- char date[MAX];
- float latitude;
- float longitude;
- float magnitude;
- float depth;
- int nst;
- char region[MAX];
- int started = 0;
- // we'll convert the data into json wrapped inside a callback function
- printf("data_callback ({");
- printf("\"data\": [");
- // scan through the data we get and set our variables
- while(scanf(" %9[^,],%255[^,],%255[^,],\"%255[^\"]\",%f,%f,%f,%f,%d,%255[^\n]", src, eqid, version, date, &latitude, &longitude, &magnitude, &depth, &nst, region) == 10) {
- if(started)
- printf(",\n");
- else
- started = 1;
- // stay within latitude and longitude
- if((latitude < -90.0) || (latitude > 90.0)) {
- die("Latitude out of bounds");
- }
- else if((longitude < -180.0) || (longitude > 180.0)) {
- die("Longitude out of bounds");
- }
- printf("{\"src\": \"%s\", \"eqid\": \"%s\", \"version\": \"%s\", \"date\": \"%s\", \"latitude\": %f, \"longitude\": %f, \"magnitude\": %f, \"depth\": %f, \"nst\": %d, \"region\": %s}", src, eqid, version, date, latitude, longitude, magnitude, depth, nst, region);
- }
- // close the object and function
- printf("\n]})");
- return 0;
- }
Add Comment
Please, Sign In to add comment