[Gpsbabel-code] misc cleanups applied
Brought to you by:
robertl
From: Robert L. <rob...@us...> - 2003-12-21 20:29:40
|
This is a grab-bag of stuff (the very kind I ask you guys not to send me :-) that I've collected over recent weeks. It ranges from cosmetic to trivial to hygenic. Nothing very interesting... A) it replaces the wretched endianness code in some of our older binary modules with our smaller, somewhat less wretched endianness functions. B) Clarity improvements to the free loops. Applied. RJL Index: duplicate.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/duplicate.c,v retrieving revision 1.11 diff -u -p -r1.11 duplicate.c --- duplicate.c 23 Oct 2003 06:10:57 -0000 1.11 +++ duplicate.c 21 Dec 2003 20:22:45 -0000 @@ -109,7 +109,7 @@ get_crc32(void * data, int datalen) static btree_node * addnode (btree_node * tree, btree_node * newnode, btree_node **oldnode) { - btree_node * tmp, * last; + btree_node * tmp, * last = NULL; if ( *oldnode ) {*oldnode = NULL;} Index: garmin_tables.h =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/garmin_tables.h,v retrieving revision 1.4 diff -u -p -r1.4 garmin_tables.h --- garmin_tables.h 20 Oct 2003 16:34:02 -0000 1.4 +++ garmin_tables.h 21 Dec 2003 20:22:45 -0000 @@ -35,7 +35,6 @@ extern int mps_find_icon_number_from_desc(const char *desc, garmin_formats_e garmin_format); - /* MapSource 4.13 */ static icon_mapping_t icon_table[] = { /* mps pcx desc */ Index: main.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/main.c,v retrieving revision 1.27 diff -u -p -r1.27 main.c --- main.c 29 Oct 2003 03:37:52 -0000 1.27 +++ main.c 21 Dec 2003 20:22:47 -0000 @@ -218,7 +218,6 @@ main(int argc, char *argv[]) usage(argv[0]); exit(0); } - if (ovecs == NULL) waypt_disp_all(waypt_disp); Index: mapsend.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/mapsend.c,v retrieving revision 1.31 diff -u -p -r1.31 mapsend.c --- mapsend.c 23 Oct 2003 06:10:57 -0000 1.31 +++ mapsend.c 21 Dec 2003 20:22:47 -0000 @@ -29,8 +29,8 @@ static FILE *mapsend_file_in; static FILE *mapsend_file_out; static void *mkshort_handle; -static int endianness_tested; -static int i_am_little_endian; static int route_wp_count; static int mapsend_infile_version; @@ -39,21 +39,6 @@ static int trk_version = 30; #define MYNAME "mapsend" static void -test_endianness(void) -{ - union { - long l; - unsigned char uc[sizeof (long)]; - } u; - - u.l = 1; - i_am_little_endian = u.uc[0]; - - endianness_tested = 1; - -} - -static void mapsend_rd_init(const char *fname) { mapsend_file_in = fopen(fname, "rb"); @@ -69,103 +54,45 @@ mapsend_rd_deinit(void) } static -size_t +void my_fread8(void *ptr, FILE *stream) { unsigned char cbuf[8]; - unsigned char *cptr = ptr; size_t rv; - if (!endianness_tested) { - test_endianness(); - } - - if (i_am_little_endian) { - rv = fread(ptr, 8, 1, stream); - } else { - rv = fread(cbuf, 8, 1, stream); - cptr[0] = cbuf[7]; - cptr[1] = cbuf[6]; - cptr[2] = cbuf[5]; - cptr[3] = cbuf[4]; - cptr[4] = cbuf[3]; - cptr[5] = cbuf[2]; - cptr[6] = cbuf[1]; - cptr[7] = cbuf[0]; - } - return rv; + rv = fread(cbuf, 8, 1, stream); + le_read64(ptr, cbuf); + } static -size_t +void my_fwrite8(void *ptr, FILE *stream) { unsigned char cbuf[8]; - unsigned char *cptr = ptr; - if (!endianness_tested) { - test_endianness(); - } - - if (i_am_little_endian) { - return fwrite(ptr, 8, 1, stream); - } else { - cbuf[0] = cptr[7]; - cbuf[1] = cptr[6]; - cbuf[2] = cptr[5]; - cbuf[3] = cptr[4]; - cbuf[4] = cptr[3]; - cbuf[5] = cptr[2]; - cbuf[6] = cptr[1]; - cbuf[7] = cptr[0]; - return fwrite(cbuf, 8, 1, stream); - } + le_read64(cbuf, ptr); + fwrite(cbuf, 8, 1, stream); } static -size_t +void my_fread4(void *ptr, FILE *stream) { + unsigned int *iptr = ptr; unsigned char cbuf[4]; - unsigned char *cptr = ptr; size_t rv; - - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - rv = fread(ptr, 4, 1, stream); - } else { - rv = fread(cbuf, 4, 1, stream); - cptr[0] = cbuf[3]; - cptr[1] = cbuf[2]; - cptr[2] = cbuf[1]; - cptr[3] = cbuf[0]; - } - return rv; + rv = fread(cbuf, 4, 1, stream); + *iptr = le_read32(cbuf); } static size_t my_fwrite4(int *ptr, FILE *stream) { - unsigned char cbuf[4]; - unsigned char *cptr = (unsigned char *) ptr; - - if (!endianness_tested) { - test_endianness(); - } - - if (i_am_little_endian) { - return fwrite(ptr, 4, 1, stream); - } else { - cbuf[0] = cptr[3]; - cbuf[1] = cptr[2]; - cbuf[2] = cptr[1]; - cbuf[3] = cptr[0]; - return fwrite(cbuf, 4, 1, stream); - } + int i = le_read32(ptr); + return fwrite(&i, 4, 1, stream); } static void @@ -528,7 +455,7 @@ void mapsend_track_hdr(const route_head * we write mapsend v3.0 tracks as mapsend v2.0 tracks get * tremendously out of whack time/date wise. */ - char *verstring; + char *verstring = "30"; queue *elem, *tmp; char *tname; unsigned char c; Index: mapsource.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/mapsource.c,v retrieving revision 1.29 diff -u -p -r1.29 mapsource.c --- mapsource.c 3 Nov 2003 14:35:55 -0000 1.29 +++ mapsource.c 21 Dec 2003 20:22:47 -0000 @@ -1499,7 +1499,7 @@ mps_write(void) char recType; int reclen; int reclen2; - int tocopy; + unsigned int tocopy; unsigned char copybuf[8192]; Index: navicache.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/navicache.c,v retrieving revision 1.5 diff -u -p -r1.5 navicache.c --- navicache.c 23 Oct 2003 06:10:57 -0000 1.5 +++ navicache.c 21 Dec 2003 20:22:47 -0000 @@ -205,11 +205,6 @@ nav_wr_deinit(void) fclose(ofd); } -static void -nav_waypt_pr(const waypoint *waypointp) -{ -} - void nav_write(void) { Index: polygon.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/polygon.c,v retrieving revision 1.7 diff -u -p -r1.7 polygon.c --- polygon.c 16 Nov 2003 20:10:38 -0000 1.7 +++ polygon.c 21 Dec 2003 20:22:47 -0000 @@ -24,7 +24,6 @@ extern queue waypt_head; -static double pos_dist; static char *polyfileopt = NULL; static char *exclopt = NULL; Index: psitrex.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/psitrex.c,v retrieving revision 1.4 diff -u -p -r1.4 psitrex.c --- psitrex.c 3 Nov 2003 14:35:24 -0000 1.4 +++ psitrex.c 21 Dec 2003 20:22:47 -0000 @@ -503,6 +503,8 @@ psit_routehdr_w(FILE *psit_file, const r fprintf(psit_file, "Route: %s\n", rname); + + xfree(rname); } } @@ -657,7 +659,7 @@ psit_trackhdr_w(FILE *psit_file, const r fprintf (psit_file, "Track: %s\n", tname); - + xfree(tname); } } psit_track_state = 1; Index: psp.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/psp.c,v retrieving revision 1.21 diff -u -p -r1.21 psp.c --- psp.c 16 Nov 2003 20:10:38 -0000 1.21 +++ psp.c 21 Dec 2003 20:22:47 -0000 @@ -23,10 +23,6 @@ #include "defs.h" #include <ctype.h> -#ifndef M_PI -# define M_PI 3.14159265358979323846 -#endif - #define MYNAME "PSP" #define MAXPSPSTRINGSIZE 256 @@ -36,24 +32,6 @@ static FILE *psp_file_in; static FILE *psp_file_out; static FILE *mkshort_handle; -static int i_am_little_endian; -static int endianness_tested; - -static void -test_endianness(void) -{ - union { - long l; - unsigned char uc[sizeof (long)]; - } u; - - u.l = 1; - i_am_little_endian = u.uc[0]; - - endianness_tested = 1; - -} - static int psp_fread(void *buff, size_t size, size_t members, FILE * fp) { @@ -74,23 +52,9 @@ psp_fread_double(FILE *fp) unsigned char buf[8]; unsigned char sbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - - psp_fread(buf, 1, 8, psp_file_in); - if (i_am_little_endian) { - return *(double *) buf; - } - sbuf[0] = buf[7]; - sbuf[1] = buf[6]; - sbuf[2] = buf[5]; - sbuf[3] = buf[4]; - sbuf[4] = buf[3]; - sbuf[5] = buf[2]; - sbuf[6] = buf[1]; - sbuf[7] = buf[0]; - return *(double *)sbuf; + psp_fread(buf, 1, 8, fp); + le_read64(sbuf, buf); + return *(double *) sbuf; } static void @@ -99,46 +63,9 @@ psp_fwrite_double(double x, FILE *fp) unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - fwrite(&x, 8, 1, fp); - } else { - cbuf[0] = cptr[7]; - cbuf[1] = cptr[6]; - cbuf[2] = cptr[5]; - cbuf[3] = cptr[4]; - cbuf[4] = cptr[3]; - cbuf[5] = cptr[2]; - cbuf[6] = cptr[1]; - cbuf[7] = cptr[0]; - fwrite(cbuf, 8, 1, fp); - } - + le_read64(cbuf, cptr); + fwrite(cbuf, 8, 1, fp); } -#if 0 -static void -psp_fwrite_word(unsigned int x, FILE *fp) -{ - char *cptr = &x; - char *cbuf[4]; - - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - fwrite(&x, 4, 1, fp); - } else { - cbuf[0] = cptr[3]; - cbuf[1] = cptr[2]; - cbuf[2] = cptr[1]; - cbuf[3] = cptr[0]; - fwrite(cbuf, 4, 1, fp); - } -} -#endif - /* Implement the grid in ascii art... This makes a bit of sense if you stand on a point over the north pole and look down on the earth. Index: route.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/route.c,v retrieving revision 1.9 diff -u -p -r1.9 route.c --- route.c 1 Apr 2003 03:21:21 -0000 1.9 +++ route.c 21 Dec 2003 20:22:47 -0000 @@ -109,19 +109,14 @@ void route_flush(queue *head) { queue *elem, *tmp; - route_head *last = NULL; - + queue *q; + QUEUE_FOR_EACH(head, elem, tmp) { - if ( last ) { - route_free(last); - } - last = (route_head *)elem; + q = dequeue(elem); + route_free((route_head *) q); } - if ( last ) { - route_free(last); - } - QUEUE_INIT(head); } + void route_flush_all() { Index: tpg.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/tpg.c,v retrieving revision 1.12 diff -u -p -r1.12 tpg.c --- tpg.c 23 Oct 2003 06:10:57 -0000 1.12 +++ tpg.c 21 Dec 2003 20:22:47 -0000 @@ -32,25 +32,8 @@ static FILE *tpg_file_in; static FILE *tpg_file_out; static void *mkshort_handle; -static int i_am_little_endian; -static int endianness_tested; static unsigned int waypt_out_count; -static void -test_endianness(void) -{ - union { - long l; - unsigned char uc[sizeof (long)]; - } u; - - u.l = 1; - i_am_little_endian = u.uc[0]; - - endianness_tested = 1; - -} - static int tpg_fread(void *buff, size_t size, size_t members, FILE * fp) { @@ -71,22 +54,8 @@ tpg_fread_double(FILE *fp) unsigned char buf[8]; unsigned char sbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - tpg_fread(buf, 1, 8, fp); - if (i_am_little_endian) { - return *(double *) buf; - } - sbuf[0] = buf[7]; - sbuf[1] = buf[6]; - sbuf[2] = buf[5]; - sbuf[3] = buf[4]; - sbuf[4] = buf[3]; - sbuf[5] = buf[2]; - sbuf[6] = buf[1]; - sbuf[7] = buf[0]; + le_read64(sbuf, buf); return *(double *)sbuf; } @@ -96,23 +65,8 @@ tpg_fwrite_double(double x, FILE *fp) unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - fwrite(&x, 8, 1, fp); - } else { - cbuf[0] = cptr[7]; - cbuf[1] = cptr[6]; - cbuf[2] = cptr[5]; - cbuf[3] = cptr[4]; - cbuf[4] = cptr[3]; - cbuf[5] = cptr[2]; - cbuf[6] = cptr[1]; - cbuf[7] = cptr[0]; - fwrite(cbuf, 8, 1, fp); - } - + le_read64(cbuf, cptr); + fwrite(cbuf, 8, 1, fp); } static int Index: waypt.c =================================================================== RCS file: /cvsroot/gpsbabel/gpsbabel/waypt.c,v retrieving revision 1.17 diff -u -p -r1.17 waypt.c --- waypt.c 3 Nov 2003 14:35:35 -0000 1.17 +++ waypt.c 21 Dec 2003 20:22:47 -0000 @@ -53,6 +53,7 @@ waypt_dupe(waypoint *wpt) return tmp; } + void waypt_add(waypoint *wpt) { @@ -190,21 +191,14 @@ void waypt_flush( queue *head ) { queue *elem, *tmp; - waypoint *last = NULL; - + + queue *q; QUEUE_FOR_EACH(head, elem, tmp) { - if ( last ) { - waypt_free(last); - } - last = (waypoint *)elem; - } - - if ( last ) { - waypt_free(last); + q = dequeue(elem); + waypt_free((waypoint *) q); } - - QUEUE_INIT(head); } + void waypt_flush_all() { |