c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

json.c (4590B)


      1 struct Coordinates {
      2     double x0;
      3     double x1;
      4     double y0;
      5     double y1;
      6 };
      7 
      8 struct Json {
      9     struct Coordinates* coords;
     10 };
     11 
     12 char* loadFileToString(char* filename) {
     13     FILE* fp = fopen(filename, "r");
     14     if(!fp) {
     15         printf("Unable to open %s", filename);
     16         return NULL;
     17     }
     18     char *line = NULL;
     19     size_t len = 0;
     20     ssize_t read;
     21     read = getline(&line, &len, fp);
     22     fclose(fp);
     23     return line;
     24 }
     25 
     26 int isFloatNumeric(char ch) {
     27     if( ((ch > '9') || (ch < '0')) && ch != '.' && ch != '-') {
     28         return 0;
     29     }
     30     return 1;
     31 }
     32 
     33 // idxStart must be first character of the number
     34 double loadNextNumber(char* jsonStr, int idxStart) {
     35     char chars[100];
     36     int idx = 0;
     37     while(isFloatNumeric(jsonStr[idx+idxStart])) {
     38         chars[idx] = jsonStr[idx+idxStart];
     39         idx += 1;
     40     }
     41     chars[idx+1] = '\0';
     42     double num = atof(chars);
     43     return num;
     44 }
     45 
     46 int getCoordinateCount(char* jsonData) {
     47     int elements = strlen(jsonData);
     48     int count = 0;
     49     for(int i = 0; i < elements; ++i) {
     50         if(jsonData[i] == 'x' && jsonData[i+1] == '1') {
     51             count += 1;
     52         }
     53     }
     54     return count;
     55 }
     56 struct Coordinates* parseCoordinates(char* jsonData, int* sz) {
     57 
     58     int elements = strlen(jsonData);
     59 
     60     int lfN = 0;
     61     int onX = 1;
     62     char next = 'x';
     63 
     64     int coordinateCount = getCoordinateCount(jsonData);
     65     sz[0] = coordinateCount;
     66     struct Coordinates* coordinates = (struct Coordinates*)malloc(coordinateCount * sizeof(struct Coordinates));
     67 
     68     int cNum = 0;
     69     struct Coordinates* current = &coordinates[cNum];
     70 
     71     for(int i = 0; i < elements; ++i) {
     72         if(jsonData[i] != next) {
     73             continue;
     74         } else {
     75             if(next == 'x' ) {
     76                 if(lfN == 0) {
     77                     next = '0';
     78                 } else {
     79                     next = '1';
     80                 }
     81                 continue;
     82             }
     83 
     84 
     85             if(next == 'y') {
     86                 if(lfN == 0) {
     87                     next = '0';
     88                     lfN = 1;
     89                 } else {
     90                     next = '1';
     91                     lfN = 0;
     92                 }
     93                 continue;
     94             }
     95             if(next == '0' || next == '1') {
     96                 double num = loadNextNumber(jsonData,i+5);
     97 
     98                 if(next == '0' && onX == 1) {
     99                     current->x0 = num;
    100                 }
    101                 if(next == '1' && onX == 1) {
    102                     current->x1 = num;
    103                 }
    104                 if(next == '0' && onX == 0) {
    105                     current->y0 = num;
    106                 }
    107                 if(next == '1' && onX == 0) {
    108                     current->y1 = num;
    109                     cNum += 1;
    110                     if(cNum < coordinateCount) {
    111                         current = &coordinates[cNum];
    112                     }
    113                 }
    114 
    115                 if(onX == 0) {
    116                     next = 'x';
    117                     onX = 1;
    118                 } else {
    119                     next = 'y';
    120                     onX = 0;
    121                 }
    122                 continue;
    123             }
    124         }
    125     }
    126     return coordinates;
    127 }
    128 
    129 double square(double x) {
    130     return x*x;
    131 }
    132 
    133 double radiansFromDegrees (double deg) {
    134     return 0.01745329251994329577 * deg;
    135 }
    136 
    137 double* computeHaversines(struct Coordinates* coords, int size, double radius) {
    138     double* res = malloc(sizeof(double)*size);
    139 
    140     for(int i = 0; i < size; ++i) {
    141         struct Coordinates* current = &coords[i];
    142 
    143         double lat1 = current->y0;
    144         double lat2 = current->y1;
    145         double lon1 = current->x0;
    146         double lon2 = current->x1;
    147 
    148         double dLat = radiansFromDegrees(lat2 - lat1);
    149         double dLon = radiansFromDegrees(lon2 - lon1);
    150         lat1 = radiansFromDegrees(lat1);
    151         lat2 = radiansFromDegrees(lat2);
    152 
    153         double a = square(sin(dLat/2.0)) + cos(lat1)*cos(lat2)*square(sin(dLon/2));
    154         double c = 2.0*asin(sqrt(a));
    155         res[i] = radius * c;
    156     }
    157     return res;
    158 }
    159 
    160 struct Coordinates* loadJson(char* filename, int* size) {
    161 
    162     char* line = NULL;
    163     TIME_BLOCK_EXEC("Load file TS", line = loadFileToString(filename));
    164 
    165     if(line == NULL) {
    166         return NULL;
    167     }
    168 
    169     int sz = 0;
    170 
    171     uint64_t startParse = rdtsc();
    172     struct Coordinates* res = NULL;
    173 
    174     TIME_BLOCK_EXEC("Parse File", res = parseCoordinates(line, &sz));
    175 
    176     size[0] = sz;
    177 
    178     TIME_BLOCK_EXEC("Free line", free(line));
    179 
    180     double* havs = NULL;
    181     TIME_BLOCK_EXEC("Compute havs", havs = computeHaversines(res,sz, 6372.8));
    182 
    183     TIME_BLOCK_EXEC("Free havs", free(havs));
    184 
    185     return res;
    186 }