Main Page | File List | Globals | Related Pages

dbio.c

Go to the documentation of this file.
00001 
00010 /* 
00011    Copyright (C) 1999-2004 Christian Claveleira (Christian.Claveleira@cru.fr)
00012 
00013    This program is free software; you can redistribute it and/or
00014    modify it under the terms of the GNU General Public License
00015    as published by the Free Software Foundation; either version 2
00016    of the License, or (at your option) any later version.
00017    
00018    This program is distributed in the hope that it will be useful,
00019    but WITHOUT ANY WARRANTY; without even the implied warranty of
00020    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021    GNU General Public License for more details.
00022    
00023    You should have received a copy of the GNU General Public License
00024    along with this program; if not, write to the Free Software
00025    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00026    
00027  */
00028 
00029 /*      $Id: dbio.c,v 1.10 2004/01/02 20:36:55 clavelei Exp clavelei $   */
00030 
00031 #ifndef lint
00032 // static char vcid[] = "$Id: dbio.c,v 1.10 2004/01/02 20:36:55 clavelei Exp clavelei $";
00033 #endif /* lint */
00034 
00035 #include <PalmOS.h>
00036 #include <PalmCompatibility.h>
00037 #include "gtalkRsc.h"
00038 #include "gpslib.h"
00039 #include "gtalk.h"
00040 #include "gpsutil.h"
00041 #include "log.h"
00042 #include "dbio.h"
00043 #include "gpsio.h"
00044 
00045 static Raw_D210_Rte_Link_Type rteLink = {
00046     {0, 0},
00047     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00048     {0}
00049 };
00050 
00051 
00065 Err
00066 SetRecord (VoidHand RecH, void *p, ULong l, DmOpenRef DB, UInt pos, UInt Category)
00067 {
00068     Ptr RecP;
00069     Err error;
00070 
00071     if (!RecH)
00072         return (FrmAlert (NoRoomID) - 1);
00073 
00074     if (MemHandleSize (RecH) != l)
00075         RecH = DmResizeRecord (DB, pos, l);                /* need to resize record ? */
00076     if (!RecH)
00077         return (FrmAlert (NoRoomID) - 1);
00078 
00079     RecP = MemHandleLock (RecH);
00080     if (p)
00081         error = DmWrite (RecP, 0, p, l);
00082     else
00083         error = DmSet (RecP, 0L, l, 0);
00084     ErrFatalDisplayIf (error, "Could not write to new record.");
00085     MemHandleUnlock (RecH);
00086     DmReleaseRecord (DB, pos, true);
00087     error = SetCategory (DB, pos, Category);
00088     SetBackupBit(DB);
00089     return (error);
00090 }                                                          // SetRecord
00091 
00092 
00102 Err
00103 SetCategory (DmOpenRef DB, UInt index, UInt Category)
00104 {
00105     UInt attributes;
00106     Err error;
00107 
00108     error = DmRecordInfo (DB, index, &attributes, NULL, NULL);
00109     if (error)
00110         return (error);
00111     //    ErrFatalDisplayIf (error, "Can't read record information");
00112     attributes &= ~dmRecAttrCategoryMask;
00113     attributes |= ((Category < 0xF ? Category : 0) | dmRecAttrDirty);
00114     error = DmSetRecordInfo (DB, index, &attributes, NULL);
00115     ErrFatalDisplayIf (error, "Can't set record information");
00116     return (0);
00117 }   /* SetCategory */
00118 
00119 
00120 
00121 #ifdef EVENTS
00122 
00123 
00132 Garmin_Packet *
00133 get_next_packet (UInt current)
00134 {
00135     VoidHand RecH = NULL;
00136     static UInt c;
00137     static Garmin_Packet packet;
00138     Garmin_Packet *r,
00139      *RecP;
00140     ULong l;
00141 
00142     if (current == 0)
00143         c = 0;
00144     RecH = DmQueryNextInCategory (LogDB, &c, dmAllCategories);
00145     c++;
00146     if (RecH)
00147     {
00148         RecP = MemHandleLock (RecH);
00149         l = MemHandleSize (RecH);
00150         if (l > sizeof (packet))
00151             l = sizeof (packet);
00152         (void) MemMove (&packet, RecP, l);
00153         MemHandleUnlock (RecH);
00154         Sent++;
00155         r = &packet;
00156     }
00157     else
00158         r = NULL;
00159     return (r);
00160 }                  /* get_next_packet */
00161 
00162 
00171 Err
00172 put_packet (Garmin_Packet * p, UInt current)
00173 {
00174     UInt pos = dmMaxRecordIndex;
00175     static VoidHand RecH = NULL;
00176 
00177     RecH = DmNewRecord (LogDB, &pos, p->length);
00178     if (SetRecord (RecH, p, p->length, LogDB, pos, dmUnfiledCategory))
00179         return (-1);
00180     New++;
00181     return (0);
00182 }              /* put_packet */
00183 #endif
00184 
00185 
00197 Err
00198 put_wpt (GarminObject * p, UInt total, UInt current, LocalFormatID format, unsigned char size)
00199 {
00200     UInt pos,
00201       differ;
00202     VoidHand RecH;
00203     Ptr RecP;
00204     Err error;
00205     Object o;
00206 
00207     switch (format)
00208     {
00209     case G100Type:
00210     case G102Type:
00211     case G103Type:
00212     case G104Type:
00213     case G107Type:
00214     case G108Type:
00215     case G109Type:
00216       InitObject(&o);
00217         o.header.size = size;
00218         MemMove (&(o.d), p, o.header.size);
00219         o.header.type = format;
00220         SortWpts (Alpha);                                  /* il faut que la base soit triée alphabétiquement */
00221         error = 0;
00222         // insertion dans la base de donnees :
00223         pos = DmFindSortPosition (WptDB, (VoidPtr) & o, NULL, (DmComparF *) & CompareWpt, 0);
00224         differ = 1;
00225         if (pos > 0)
00226         {                                                  /* duplicata ? */
00227             RecH = DmGetRecord (WptDB, pos - 1);           /* regardons precedent record */
00228             RecP = MemHandleLock (RecH);
00229             differ = CompareWpt (&o, RecP, 0, NULL, NULL, NULL);
00230             MemHandleUnlock (RecH);
00231             DmReleaseRecord (WptDB, pos - 1, false);
00232         }
00233         if (differ)
00234             goto new;
00235         else if (!OnlyNews)
00236         {
00237             if (!Prefs.allowduplicate)
00238             {                                              /* record de meme nom, on remplace l'ancien */
00239                 RecH = DmGetRecord (WptDB, pos - 1);
00240                 error = SetRecord (RecH, &o, SizeOfObject (&o), WptDB, pos - 1, Prefs.wptcategory);
00241                 if (!error)
00242                     Replaced++;
00243             }
00244             else
00245             {                                              /* creation nouveau record */
00246               new:RecH = DmNewRecord (WptDB, &pos, SizeOfObject (&o));
00247                 error = SetRecord (RecH, &o, SizeOfObject (&o), WptDB, pos, Prefs.wptcategory);
00248                 if (!error)
00249                     New++;
00250             }
00251         }
00252 
00253         break;
00254 
00255     default:
00256         FrmCustomAlert (UnsupportedID, "waypoint", "", "");
00257         error = -2;
00258     }                                                      /* switch */
00259 
00260     return (error);
00261 }                                                          // put_wpt
00262 
00263 
00275 Err
00276 put_rte (GarminObject * p, UInt total, UInt current, LocalFormatID format, unsigned char size)
00277 {
00278     Err error;
00279     static UInt pos;
00280     ULong l;
00281     static VoidHand RecH;
00282     Ptr RecP;
00283     //    struct RteDB *t = p;
00284     //    byte *cp = p;
00285     //    void *v;
00286     Object o;
00287 
00288     debug_in (GTALKDBG_CALLS,
00289               ("->put_rte(total=%d,current=%d,format=%d,size=%d", total, current, format, size));
00290 
00291     error = 0;
00292     InitObject(&o);
00293     o.header.size = size;
00294     MemMove (&(o.d), p, o.header.size);
00295     o.header.type = format;
00296 
00297     switch (format)
00298     {
00299     case G200Type:
00300     case G201Type:
00301     case G202Type:
00302     case CustomRteHdr:                                     /* utilise en interne pour format custom */
00303         pos = dmMaxRecordIndex;                            /* reception header,initialisation du record */
00304         RecH = DmNewRecord (RteDB, &pos, SizeOfObject (&o));
00305         error = SetRecord (RecH, &o, SizeOfObject (&o), RteDB, pos, Prefs.rtecategory);
00306         if (!error)
00307             New++;
00308         break;
00309 
00310     case G210Type:                                         /* route link */
00311 #ifndef SUPPORT_LINKS
00312         break;                                             /* we drop links */
00313 #endif
00314 
00315     case G100Type:                                         /* waypoints */
00316     case G102Type:
00317     case G103Type:
00318     case G104Type:
00319     case G107Type:
00320     case G108Type:
00321     case G109Type:
00322         if (RecH)
00323         {                                                  // reception waypoint
00324             l = MemHandleSize (RecH);
00325             RecH = DmResizeRecord (RteDB, pos, l + SizeOfObject (&o));
00326             RecH = DmGetRecord (RteDB, pos);
00327             if (RecH)
00328             {
00329                 RecP = MemHandleLock (RecH);
00330                 error = DmWrite (RecP, l, &o, SizeOfObject (&o));
00331                 MemHandleUnlock (RecH);
00332                 error = DmReleaseRecord (RteDB, pos, true);
00333             }
00334         }
00335         break;
00336 
00337     default:
00338         FrmCustomAlert (UnsupportedID, "route element", "", "");
00339         error = -2;
00340     }
00341     debug_out (GTALKDBG_CALLS, ("<-put_rte returns %d", error));
00342 
00343     return (error);
00344 }                                                          // put_rte
00345 
00346 
00357 static void
00358 MakeTrackHeader (Object * trkhdrp, LocalFormatID format, GarminObject * p, TrkPts * trkp)
00359 {
00360     D310_Trk_Hdr_Type *p310;
00361     DateTimeType dtp;
00362     char date[dateStringLength];
00363     char time[timeStringLength];
00364 
00365     InitObject(trkhdrp);
00366     //    trkhdrp->header.version = OBJECTVERSION;
00367     trkhdrp->header.type = CustomTrkHdr;
00368     trkhdrp->header.size = sizeof (Custom_Trk_Hdr_Type);
00369     trkhdrp->d.CustTrkHdr.dspl = trkhdrp->d.CustTrkHdr.color = 0;
00370     trkhdrp->d.CustTrkHdr.type = CompactTracks ? CompactTrkPt : CustomTrkPt;    /* format des trk pts a suivre */
00371     trkhdrp->d.CustTrkHdr.number = 0;
00372     trkhdrp->d.CustTrkHdr.latmin = LATMAX;
00373     trkhdrp->d.CustTrkHdr.latmax = LATMIN;
00374     trkhdrp->d.CustTrkHdr.lonmin = LONMAX;
00375     trkhdrp->d.CustTrkHdr.lonmax = LONMIN;
00376     if (format == G310Type)
00377     {
00378         //        trkhdrp->header.type = G301Type;
00379         p310 = (D310_Trk_Hdr_Type *) p;
00380         StrNCopySafe (trkhdrp->d.CustTrkHdr.name, p310->trk_ident,
00381                       sizeof (trkhdrp->d.CustTrkHdr.name));
00382         trkhdrp->d.CustTrkHdr.dspl = p310->dspl;
00383         trkhdrp->d.CustTrkHdr.color = p310->color;
00384     }
00385     else
00386     {
00387         if (CompactTracks)
00388             TimSecondsToDateTime (TimGetSeconds (), &dtp);
00389         else
00390             TimSecondsToDateTime (GarminTime2PalmSeconds (trkp->custom.time), &dtp);
00391         DateToAscii (dtp.month, dtp.day, dtp.year, dfDMYWithDashes, date);
00392         TimeToAscii (dtp.hour, dtp.minute, tfColon24h, time);
00393         StrNCopySafe (trkhdrp->d.CustTrkHdr.name, date, sizeof (trkhdrp->d.CustTrkHdr.name));
00394         StrNCat (trkhdrp->d.CustTrkHdr.name, " ", sizeof (trkhdrp->d.CustTrkHdr.name));
00395         StrNCat (trkhdrp->d.CustTrkHdr.name, time, sizeof (trkhdrp->d.CustTrkHdr.name));
00396     }
00397 }                                                          /* MakeTrackHeader */
00398 
00399 
00411 Err
00412 put_trk (GarminObject * p, UInt total, UInt current, LocalFormatID format, unsigned char size)
00413 {
00414     Err error;
00415     static UInt pos;
00416     static VoidHand RecH = NULL;
00417     Ptr RecP;
00418     UInt sizeoftrkpt = 0;
00419     static ULong l = 0;
00420     static Object trkhdr;
00421     TrkPts trk;
00422 
00423     debug (GTALKDBG_CALLS,
00424            ("<->put_trk(total=%d,current=%d,format=%d,size=%d)", total, current, (int) format,
00425             (int) size));
00426     if (format != G310Type)
00427     {
00428         sizeoftrkpt =
00429             AnyRawToAnyTrkPt (format, CompactTracks ? CompactTrkPt : CustomTrkPt, p, &trk);
00430         if (sizeoftrkpt == 0)
00431         {
00432             FrmCustomAlert (UnsupportedID, "track", "", "");
00433             return (GErrUnsupportedFmt);
00434         }
00435     }                                                      /* switch */
00436 
00437     if (current > total)
00438         return (GErrProtocolError);
00439 
00440     if ((current == 1) || (format == G310Type))
00441     {                                                      /* initialisation du record */
00442         if (current != 1)
00443             CleanUpTracks ();                              /* cleaning of previous track */
00444         MakeTrackHeader (&trkhdr, format, p, &trk);        /* init du header */
00445         RecH = NULL;
00446         if (format == G310Type)
00447             return (0);
00448     }
00449     if ((RecH == NULL) && (format != G310Type))
00450     {                                                      /* initialisation du record */
00451 
00452         l = SizeOfObject (&trkhdr) + (long) sizeoftrkpt *(long) total - current + 1;    /* espace qu'il faudrait */
00453         //debug (GTALKDBG_INFOS, ("put_trk : space needed : %ld bytes",l)); // $$
00454         if (l > MaxRecordSize)
00455             l = (((MaxRecordSize - SizeOfObject (&trkhdr)) / sizeoftrkpt)) * sizeoftrkpt + SizeOfObject (&trkhdr);      /* ce que l'on peut mettre */
00456         //debug (GTALKDBG_INFOS, ("put_trk : allocated %ld bytes record (%d b/trkpt)", l,sizeoftrkpt));  // $$
00457 
00458         pos = dmMaxRecordIndex;
00459         RecH = DmNewRecord (TrkDB, &pos, l);
00460         error = SetRecord (RecH, NULL, l, TrkDB, pos, Prefs.trkcategory);
00461         if (error)
00462             return (error);                                /* initialisation */
00463         l = SizeOfObject (&trkhdr);
00464         New++;
00465     }
00466 
00467     if (trk.custom.posn.lat > trkhdr.d.CustTrkHdr.latmax)
00468         trkhdr.d.CustTrkHdr.latmax = trk.custom.posn.lat;
00469     else if (trk.custom.posn.lat < trkhdr.d.CustTrkHdr.latmin)
00470         trkhdr.d.CustTrkHdr.latmin = trk.custom.posn.lat;
00471     if (trk.custom.posn.lon > trkhdr.d.CustTrkHdr.lonmax)
00472         trkhdr.d.CustTrkHdr.lonmax = trk.custom.posn.lon;
00473     else if (trk.custom.posn.lon < trkhdr.d.CustTrkHdr.lonmin)
00474         trkhdr.d.CustTrkHdr.lonmin = trk.custom.posn.lon;
00475     //debug (GTALKDBG_INFOS, ("<->put_trk(format=%d,size=%d) : lat=%ld, lon=%ld", format, (int) size,   trk.custom.posn.lat, trk.custom.posn.lon));
00476     if (RecH)
00477     {
00478         if (l >= MemHandleSize (RecH))
00479         {
00480             error = put_trk (p, total - current + 1, 1, format, size);  /* essayons un nouveau record */
00481             return (error);
00482         }
00483         RecH = DmGetRecord (TrkDB, pos);
00484         RecP = MemHandleLock (RecH);
00485         trkhdr.d.CustTrkHdr.number++;
00486         error = DmWrite (RecP, 0, &trkhdr, SizeOfObject (&trkhdr));     /* header */
00487         error = DmWrite (RecP, l, &trk, sizeoftrkpt);
00488         ErrFatalDisplayIf (error, "Can't write trackpoint");
00489         l += sizeoftrkpt;
00490         MemHandleUnlock (RecH);
00491         DmReleaseRecord (TrkDB, pos, true);
00492         return (GErrNoError);
00493     }
00494 
00495     else
00496         return (GErrNoHandle);
00497 }                                                          // put_trk
00498 
00499 
00500 static int active_trk_index = -1;
00501 #define ACTIVETRKNAME "ACTIVE TRACK"
00502 
00503 
00511 UInt
00512 init_active_trk ()
00513 {
00514     VoidHand RecH = NULL;
00515     Object *op,
00516       trkhdr;
00517     UInt t;
00518     Boolean gotit = 0;
00519 
00520     for (t = DmNumRecords (TrkDB); t > 0; t--)
00521     {
00522         RecH = DmQueryRecord (TrkDB, t - 1);
00523         if (RecH)
00524         {
00525             op = MemHandleLock (RecH);
00526             if (op->header.type == CustomTrkHdr)
00527             {
00528                 if (!StrCompare (op->d.CustTrkHdr.name, ACTIVETRKNAME))
00529                     gotit = 1;
00530             }
00531             MemHandleUnlock (RecH);
00532         }
00533         if (gotit)
00534             break;
00535     }                                                      // for
00536     if (!t)
00537     {
00538         RecH = DmNewRecord (TrkDB, &t, SizeOfObject (&trkhdr)); /* factorisable avec put_trk ? */
00539         InitObject(&trkhdr);
00540                    //   trkhdr.header.version = OBJECTVERSION;
00541         trkhdr.header.type = CustomTrkHdr;
00542         trkhdr.header.size = sizeof (Custom_Trk_Hdr_Type);
00543         trkhdr.d.CustTrkHdr.dspl = trkhdr.d.CustTrkHdr.color = 0;
00544         trkhdr.d.CustTrkHdr.type = CustomTrkPt;            /* format des trk pts a suivre */
00545         trkhdr.d.CustTrkHdr.number = 0;
00546         trkhdr.d.CustTrkHdr.latmin = LATMAX;
00547         trkhdr.d.CustTrkHdr.latmax = LATMIN;
00548         trkhdr.d.CustTrkHdr.lonmin = LONMAX;
00549         trkhdr.d.CustTrkHdr.lonmax = LONMIN;
00550         StrNCopySafe (trkhdr.d.CustTrkHdr.name, ACTIVETRKNAME, sizeof (trkhdr.d.CustTrkHdr.name));
00551         SetRecord (RecH, &trkhdr, SizeOfObject (&trkhdr), TrkDB, t, dmUnfiledCategory);
00552         active_trk_index = t;
00553     }
00554     else
00555         active_trk_index = t - 1;
00556     return (active_trk_index);
00557 }                                                          // init_active_trk
00558 
00559 
00568 void
00569 push_active_trkpt (Custom_Trk_Point_Type trkpt)
00570 {
00571     static VoidHand RecH = NULL;
00572     Ptr RecP;
00573     static Object trkhdr;
00574     ULong l;
00575     Err error;
00576 
00577     if (active_trk_index >= 0)
00578     {
00579         RecH = DmQueryRecord /*  DmGetRecord */ (TrkDB, active_trk_index);
00580         if (RecH)
00581         {
00582             RecP = MemHandleLock (RecH);
00583             MemMove (&trkhdr, RecP, SizeOfObject ((ObjectPtr) RecP));
00584             trkhdr.d.CustTrkHdr.number++;
00585             if (trkpt.posn.lat > trkhdr.d.CustTrkHdr.latmax)
00586                 trkhdr.d.CustTrkHdr.latmax = trkpt.posn.lat;
00587             else if (trkpt.posn.lat < trkhdr.d.CustTrkHdr.latmin)
00588                 trkhdr.d.CustTrkHdr.latmin = trkpt.posn.lat;
00589             if (trkpt.posn.lon > trkhdr.d.CustTrkHdr.lonmax)
00590                 trkhdr.d.CustTrkHdr.lonmax = trkpt.posn.lon;
00591             else if (trkpt.posn.lon < trkhdr.d.CustTrkHdr.lonmin)
00592                 trkhdr.d.CustTrkHdr.lonmin = trkpt.posn.lon;
00593             MemHandleUnlock (RecH);
00594         }
00595         l = SizeOfObject (&trkhdr) + trkhdr.d.CustTrkHdr.number * sizeof (Custom_Trk_Point_Type);
00596         RecH = DmResizeRecord (TrkDB, active_trk_index, l);
00597         if (RecH)
00598         {
00599             RecH = DmGetRecord (TrkDB, active_trk_index);
00600             RecP = MemHandleLock (RecH);
00601             error = DmWrite (RecP, 0, &trkhdr, SizeOfObject (&trkhdr)); /* header */
00602             error =
00603                 DmWrite (RecP, l - sizeof (Custom_Trk_Point_Type), &trkpt,
00604                          sizeof (Custom_Trk_Point_Type));
00605             ErrFatalDisplayIf (error, "Can't write trackpoint");
00606             MemHandleUnlock (RecH);
00607             DmReleaseRecord (TrkDB, active_trk_index, true);
00608         }
00609         else
00610             FrmCustomAlert (ErrorID, "Can't store new track point", " ", " ");
00611     }
00612 }                                                          /* push_active_trkpt */
00613 
00614 
00625 GarminObject *
00626 get_next_wpt (UInt current, LocalFormatID format, unsigned char *size)
00627 {
00628     static Object obj;
00629     Object *RecP, *p;
00630     GarminObject *r;
00631     VoidHand RecH = NULL;
00632     static UInt c;
00633 
00634     debug_in (GTALKDBG_CALLS, ("->get_next_wpt(current=%d,format=%d)", current, format));
00635     if (current == 0)
00636         c = 0;
00637 #ifdef SELECT
00638     if (Prefs.selectmode)
00639     {
00640         while ((RecH = DmQueryNextInCategory (WptDB, &c, Prefs.wptcategory)))
00641         {
00642             if (RecIsSecret (WptDB, c))
00643                 break;
00644             else
00645                 c++;
00646         }
00647     }
00648     else
00649 #endif
00650         RecH = DmQueryNextInCategory (WptDB, &c, Prefs.wptcategory);
00651     c++;
00652     if (RecH)
00653       {
00654         RecP = MemHandleLock (RecH);
00655         //      (void) memcpy (&buf, RecP, sizeof (Raw_D103_Wpt_Type));
00656         p=AnyToAnyWpt (RecP, &obj, format);
00657         MemHandleUnlock (RecH);
00658         if (!p){
00659           FrmCustomAlert (UnsupportedID, "requested waypoint", "", "");
00660           r=NULL;
00661         }
00662         else{
00663           Sent++;
00664           r = &obj.d.g;
00665           *size = obj.header.size;
00666         }
00667       }
00668     else
00669         r = NULL;
00670     debug_out (GTALKDBG_CALLS, ("<-get_next_wpt()=%ld, *size=%d", r, (int) *size));
00671     return (r);
00672 }                                                          // get_next_wpt
00673 
00674 
00687 GarminObject *
00688 get_next_trk (UInt current, LocalFormatID format, unsigned char *size, Boolean * eot)
00689 {
00690     static Raw_D300_Trk_Point_Type nt300;
00691     static Raw_D301_Trk_Point_Type nt301;
00692     static D310_Trk_Hdr_Type trkhdr;
00693     Custom_Trk_Point_Type *customp;
00694     Compact_Trk_Point_Type *compactp;
00695     GarminObject *r;
00696     //    struct TrkDB *RecP;
00697     Object *RecP;
00698     VoidHand RecH = NULL;
00699     static UInt count;
00700     static UInt c,
00701       t;
00702 
00703     debug_in (GTALKDBG_CALLS, ("->get_next_trk(current=%d,format=%d)", current, format));
00704     r = NULL;
00705     if ((current == 0) || (count == 0))
00706     {
00707         if (current == 0)
00708             c = 0;
00709         else
00710             c++;
00711 #ifdef SELECT
00712         if (Prefs.selectmode)
00713         {
00714             while ((RecH = DmQueryNextInCategory (TrkDB, &c, Prefs.trkcategory)))
00715             {
00716                 if (RecIsSecret (TrkDB, c))
00717                     break;
00718                 else
00719                     c++;
00720             }
00721         }
00722         else
00723 #endif
00724             RecH = DmQueryNextInCategory (TrkDB, &c, Prefs.trkcategory);
00725         if (!RecH)
00726             goto f;
00727         count = t = TrkptsInTrkDB (RecH);
00728         Sent++;
00729     }
00730 
00731     RecH = DmQueryRecord (TrkDB, c);
00732     if (RecH)
00733     {
00734         RecP = MemHandleLock (RecH);
00735         customp = (Custom_Trk_Point_Type *) ((void *) RecP + SizeOfObject (RecP));
00736         compactp = (Compact_Trk_Point_Type *) customp;
00737         customp += t - count;
00738         compactp += t - count;
00739         if (format == G300Type)
00740         {                                                  /* conversion en format network */
00741             if (RecP->d.CustTrkHdr.type == CustomTrkPt)
00742                 CustomToRawD300TrkPt (customp, &nt300);
00743             else
00744                 CompactToRawD300TrkPt (compactp, &nt300);
00745             r = (GarminObject *) & nt300;
00746             *size = sizeof (nt300);
00747         }
00748         else if (format == G301Type)
00749         {
00750             if (RecP->d.CustTrkHdr.type == CustomTrkPt)
00751                 CustomToRawD301TrkPt (customp, &nt301);
00752             else
00753                 CompactToRawD301TrkPt (compactp, &nt301);
00754             r = (GarminObject *) & nt301;
00755             *size = sizeof (nt301);
00756         }
00757         else if (format == G310Type)
00758         {
00759             CustomTrkHdrToD310 (RecP, &trkhdr);
00760             r = (GarminObject *) & trkhdr;
00761             *size = sizeof (trkhdr) - sizeof (trkhdr.trk_ident) + StrLen (trkhdr.trk_ident) + 1;
00762             count++;
00763         }
00764         MemHandleUnlock (RecH);
00765     }
00766     if (--count == 0)
00767         *eot = 1;
00768     else
00769         *eot = 0;
00770   f:
00771     debug_out (GTALKDBG_CALLS, ("<-get_next_trk()=%ld, *size=%d", r, (int) *size));
00772     return (r);
00773 }                                                          // get_next_trk
00774 
00775 
00788 GarminObject *
00789 get_next_rte (UInt current, LocalFormatID format, unsigned char *size, Boolean * header,
00790               Boolean * eor)
00791 {
00792     Object *objp,
00793      *RecP;
00794     static Object obj;
00795     GarminObject *gop = NULL;
00796     VoidHand RecH = NULL;
00797     static int count;
00798     static UInt c,
00799       t;
00800 
00801     debug_in (GTALKDBG_CALLS, ("->get_next_rte(current=%d,format=%d", current, current, format));
00802     *header = 0;
00803     if ((current == 0) || (count == 0))
00804     {
00805         if (current == 0)
00806             c = 0;
00807         else
00808             c++;
00809         *header = 1;
00810 #ifdef SELECT
00811         if (Prefs.selectmode)
00812         {
00813             while ((RecH = DmQueryNextInCategory (RteDB, &c, Prefs.rtecategory)))
00814             {
00815                 if (RecIsSecret (RteDB, c))
00816                     break;
00817                 else
00818                     c++;
00819             }
00820         }
00821         else
00822 #endif
00823             RecH = DmQueryNextInCategory (RteDB, &c, Prefs.rtecategory);
00824         if (RecH)
00825         {
00826             count = t = ObjectsInRec (RecH);
00827             Sent++;
00828         }
00829     }
00830 
00831     //    count--;
00832     RecH = DmQueryRecord (RteDB, c);
00833     if (RecH && count)
00834     {
00835         RecP = MemHandleLock (RecH);
00836         objp = (void *) RecP + OffsetOfObjectInRec (RecH, t - count);
00837         if (count-- == t)
00838             AnyToAnyRteHdr (objp, &obj, format);           /* header ? */
00839         else if ((format == G210Type) && (objp->header.type != G210Type))
00840         {                                                  /* route link */
00841             InitObject(&obj);
00842             obj.header.type = G210Type;                    /* s'il n'existe pas on en fabrique un */
00843             obj.header.size = sizeof (rteLink);
00844             //      obj.header.version = OBJECTVERSION;
00845             MemMove (&(obj.d), &rteLink, obj.header.size);
00846             count++;                                       /* compensation */
00847         }
00848         else
00849             AnyToAnyWpt (objp, &obj, format);
00850         if (count == 0)
00851             *eor = 1;                                      /* dernier elt de la route ? */
00852         *size = obj.header.size;
00853         MemHandleUnlock (RecH);
00854         gop = &obj.d.g;
00855     }
00856     debug_out (GTALKDBG_CALLS,
00857                ("<-get_next_rte returns size=%d, header=%d, eor=%d%d", *size, *header, *eor));
00858     return (gop);
00859 }                                                          // get_next_rte
00860 
00861 
00862 #ifdef FURUNO
00863 
00871 char *
00872 get_next_full_rte (UInt current)
00873 {
00874     byte *RecP;
00875     VoidHand RecH;
00876     UInt i,
00877       j,
00878       n = 0,
00879       NumWpts;
00880     static UInt c;
00881     Raw_D103_Wpt_Type *p;
00882     char *e = NULL;
00883     Object *objp;
00884     Custom_Wpt_Type custwp;
00885 
00886     if (current == 0)
00887         c = 0;
00888     else
00889         c++;
00890     RecH = DmQueryNextInCategory (RteDB, &c, Prefs.rtecategory);
00891     if (RecH)
00892     {
00893         RecP = MemHandleLock (RecH);
00894         NumWpts = WptsInRteDB (RecH);
00895         for (i = 0; i <= ((NumWpts - 1) / 8); i++)
00896         {
00897             n += StrPrintF (gbuf1 + n, "$GPRTE,%d,%d,C,0%d", 1 + (NumWpts - 1) / 8, i + 1,
00898                             ((D201_Rte_Hdr_Type *) RecP)->nmbr);
00899             for (j = i * 8; (j < (i + 1) * 8) && (j < NumWpts); j++)
00900             {
00901           objp=(void *) RecP + OffsetOfObjectInRec (RecH, j+1);
00902           PtrToCustomWpt (objp,&custwp);
00903 /*              p = (Raw_D103_Wpt_Type *) (RecP + sizeof (D201_Rte_Hdr_Type) + */
00904 /*                                         j * sizeof (Raw_D103_Wpt_Type)); */
00905                 StrNCopySafe (gbuf2, custwp.ident, sizeof (custwp.ident) + 1);
00906                 n += StrPrintF (gbuf1 + n, ", %s", gbuf2);
00907             }
00908             n += StrPrintF (gbuf1 + n, "\r\n");
00909         }
00910         MemHandleUnlock (RecH);
00911         e = gbuf1;
00912     }
00913     return (e);
00914 }                                                          /* get_next_full_rte */
00915 
00916 
00925 char *
00926 Furuno_get_next_wpt (UInt current)
00927 {
00928     GarminObject *nwp;
00929     //    D103_Wpt_Type wp;
00930     char name[7],
00931       comment[17],
00932      *r = NULL;
00933     //    static char buf[82];
00934     double latitude,
00935       longitude;
00936     unsigned char size;
00937     int n;
00938     Custom_Wpt_Type custwp;
00939 
00940     debug_in (GTALKDBG_CALLS, ("->Furuno_get_next_wpt(current=%d)", current));
00941     nwp = get_next_wpt (current, G103Type, &size);
00942     if (nwp != NULL)
00943     {
00944         //StrPrintF (buf, "$PFEC,GPxfr,CTL,E\r\n");
00945         //        return (NULL);
00946 
00947         //        D103_ntoh (nwp, &wp);
00948       G103ToCustom (&(nwp->d103),&custwp );
00949 
00950         StrNCopySafe (name, custwp.ident, sizeof (name));
00951         StrNCopySafe (comment, custwp.cmnt, sizeof (comment));
00952         SemiToDegre (&custwp.posn, &latitude, &longitude);
00953         //      translate (FromWGS84, &latitude, &longitude, DatumIndex);
00954         n = StrPrintF (gbuf1, "$PFEC,GPwpl,%s,", DegreToDM (latitude, 0, 1));
00955         n += StrPrintF (gbuf1 + n, "%s,%s,,@%c%s,A,,,,\r\n", DegreToDM (longitude, 1, 1), name,
00956                         (char) ('a' + 0x10 + (custwp.smbl % 9)), comment);
00957         r = gbuf1;
00958     }
00959     debug_out (GTALKDBG_CALLS, ("<-Furuno_get_next_wpt(r=%ld)", r));
00960     return (r);
00961 }                                                          /* Furuno_get_next_wpt */
00962 #endif
00963 
00964 
00973 void
00974 CleanUpTracks ()
00975 {
00976     UInt n,
00977       c,
00978       hdrsize,
00979       t;
00980     char *p,
00981      *pp;
00982     ULong newsize,
00983       size;
00984     Long l;
00985     VoidHand RecH = NULL;
00986     Object *op;
00987 
00988     c = 0;
00989     for (t = DmNumRecordsInCategory (TrkDB, Prefs.trkcategory); t > 0; t--)
00990     {
00991         RecH = DmQueryNextInCategory (TrkDB, &c, Prefs.trkcategory);
00992         if (RecH)
00993         {
00994             op = MemHandleLock (RecH);
00995             size =
00996                 op->d.CustTrkHdr.type ==
00997                 CompactTrkPt ? sizeof (Compact_Trk_Point_Type) : sizeof (Custom_Trk_Point_Type);
00998             p = (char *) op;
00999             hdrsize = SizeOfObject (op);
01000             for (pp = p + MemHandleSize (RecH) - 1; (pp > p) && !(*pp); pp--);
01001             l = pp - p;
01002             MemHandleUnlock (RecH);
01003             if (l > hdrsize)
01004                 n = (l - hdrsize) / size + 1;
01005             else
01006                 n = 0;
01007             newsize = hdrsize + n * size;
01008             if (newsize != MemHandleSize (RecH))
01009                 DmResizeRecord (TrkDB, c, newsize);
01010         }
01011         c++;
01012     }
01013 }                                                          // CleanUpTracks
01014 
01015 #ifdef BEAM
01016 
01026 Err
01027 BeamReceive (ExgSocketPtr sockp, DmOpenRef DB)
01028 {
01029     Err err;
01030     MemHandle dataH,
01031       RecH;
01032     UInt16 size,
01033       ChunkSize = 1024;
01034     UInt8 *dataP;
01035     UInt16 dataLen = 0,
01036       idx,
01037       len;
01038 
01039     size = ChunkSize;
01040     dataH = MemHandleNew (size);                           /* first chunk */
01041     if (dataH)
01042     {
01043         err = ExgAccept (sockp);
01044         if (!err)
01045         {
01046             dataP = MemHandleLock (dataH);
01047             do
01048             {
01049                 len = ExgReceive (sockp, dataP + dataLen, size - dataLen, &err);
01050                 if (len && !err)
01051                 {
01052                     dataLen += len;
01053                     if (dataLen >= size)
01054                     {
01055                         MemHandleUnlock (dataH);
01056                         err = MemHandleResize (dataH, size + ChunkSize);
01057                         dataP = MemHandleLock (dataH);
01058                         if (!err)
01059                             size += ChunkSize;
01060                     }
01061                 }
01062 
01063             } while (len && !err);
01064 
01065 
01066             if (!err && (dataLen > sizeof (RecordSeparator)))
01067             {
01068                 idx = dmMaxRecordIndex;
01069                 RecH = DmNewRecord (DB, &idx, dataLen - sizeof (RecordSeparator));
01070                 err = SetRecord (RecH, dataP + sizeof (RecordSeparator),
01071                                  dataLen - sizeof (RecordSeparator), DB, idx, dmUnfiledCategory);
01072 
01073             }
01074             if (err)
01075                 SndPlaySystemSound (sndError);             // $$
01076             MemHandleUnlock (dataH);
01077             MemHandleFree (dataH);
01078         }                                                  /* if (!err) */
01079     }
01080     ExgDisconnect (sockp, err);                            // closes transfer dialog
01081     return (err);
01082 }                                                          /* BeamReceive */
01083 
01084 #endif
01085 
01086 
01095 void InitObject (Object * op){
01096   MemSet(op,sizeof(Object),0);
01097   op->header.version = OBJECTVERSION;
01098 }

Generated on Sun Aug 29 11:00:40 2004 for GPilotS by doxygen 1.3.4