Main Page | Data Structures | Directories | File List | Data Fields | Globals

rddf.c

Go to the documentation of this file.
00001 #include <config.h>
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <float.h>
00006 
00007 #include <rddf/rddf.h>
00008 
00009 // Provided by rddf_parser.c
00010 int rddf_parse(FILE *file, gpsroute_t *route);
00011 
00013 
00018 static size_t line_count(FILE *f) {
00019     char buf[1024];
00020     size_t lines = 0;
00021     long pos;
00022 
00023     pos = ftell(f);
00024 
00025     while(!feof(f)) {
00026         fgets(buf, sizeof(buf), f);
00027         if(buf[0] != '\n' && !feof(f))
00028             lines++;
00029     }
00030 
00031     fseek(f, pos, SEEK_SET);
00032 
00033     return lines;
00034 }
00035 
00037 
00040 gpsroute_t *rddf_import(FILE *file) {
00041     int result;
00042     gpsroute_t *route;
00043 
00044     route = rddf_alloc(line_count(file));
00045 
00046     if(route == NULL) {
00047         fclose(file);
00048         return NULL;
00049     }
00050 
00051     result = rddf_parse(file, route);
00052 
00053     if(result != 0) {
00054         fprintf(stderr, "Error importing RDDF file!\n");
00055         return NULL;
00056     } else {
00057         return route;
00058     }
00059 }
00060 
00062 
00065 void rddf_export(FILE *file, gpsroute_t *route) {
00066     size_t i;
00067 
00068     for(i = 0; i < route->size; i++) {
00069         fprintf(file, "%d,%.*g,%.*g,%.*g,%.*g\n",
00070                 i+1,
00071                 DBL_DIG, route->points[i].lat,
00072                 DBL_DIG, route->points[i].lon,
00073                 DBL_DIG, route->points[i].radius,
00074                 DBL_DIG, route->points[i].speed);
00075     }
00076 }
00077 
00079 
00082 gpsroute_t *rddf_alloc(size_t sz) {
00083     gpsroute_t *route;
00084 
00085     route = (gpsroute_t *) malloc(sizeof(gpsroute_t));
00086     if(route == NULL)
00087         return NULL;
00088 
00089     route->size = sz;
00090     route->points = (gpspoint_t *) malloc(sizeof(gpspoint_t) * (sz == 0 ? 1 : sz));
00091 
00092     if(route->points == NULL) {
00093         free(route);
00094         return NULL;
00095     }
00096 
00097     return route;
00098 }
00099 
00101 
00105 gpsroute_t *rddf_extend(gpsroute_t *route, size_t sz) {
00106     gpspoint_t *newpoints;
00107 
00108     newpoints = (gpspoint_t *) realloc(route->points, sizeof(gpspoint_t) * (sz + 1));
00109     if(newpoints == NULL)
00110         return NULL;
00111 
00112     route->points = newpoints;
00113     return route;
00114 }
00115 
00117 
00119 void rddf_free(gpsroute_t *route) {
00120     free(route->points);
00121     free(route);
00122 }

Generated on Thu Sep 6 13:13:10 2007 for driver by  doxygen 1.3.9.1