home *** CD-ROM | disk | FTP | other *** search
- /*
- GetTLE - URLsMemo.c - Handle the information in the GetTLE Memo
- Copyright ⌐2000 Andreas Schneider
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- #include <PalmOS.h>
- #include "GetTLERsc.h"
- #include "URLsMemo.h"
- #include "alerts.h"
- #include "debug.h"
-
- // Structures imposed by other programs
-
- typedef struct {
- char note; // null terminated
- } MemoDBRecordType, *MemoDBRecordPtr;
-
- #define MemoDBName "MemoDB"
- #define MemoDBType 'DATA'
-
- extern Char SiteUrls[MAX_SITES][MAX_SITE_URL_LEN+1]={0};
- extern Char SiteNames[MAX_SITES][MAX_SITE_NAME_LEN+1]={0};
- extern UInt16 NumSites=0;
-
- // array of URLs containing TLE data
- // followed by a description of that site
- // each entry must end with \n
- // last entry must be NULL
- static Char *default_sites[]=
- {"www.celestrak.com/NORAD/elements/stations.txt\n",
- "NORAD: Mir & ISS\n",
- "www.celestrak.com/NORAD/elements/visual.txt\n",
- "NORAD: visible satellites\n",
- "www.celestrak.com/NORAD/elements/tle-new.txt\n",
- "NORAD: new satellites\n",
- "oig1.gsfc.nasa.gov/files/visible.tle\n",
- "NASA: visible satellites\n",
- "tie.jpl.nasa.gov/pub/dransom/current.data/stsnew.txt\n",
- "Latest shuttle data\n",
- "home.t-online.de/home/R.Kracht/bright50.tle\n",
- "50+ brightest satellites\n",
- "www.wingar.demon.co.uk/satevo/tle/sts.tle\n",
- "SatEvo: Shuttle\n",
- "www.wingar.demon.co.uk/satevo/tle/mir.tle\n",
- "SatEvo: Mir\n",
- "www.wingar.demon.co.uk/satevo/tle/iss.tle\n",
- "SatEvo: ISS\n",
- "www.wingar.demon.co.uk/satevo/tle/select.tle\n",
- "SatEvo: selected satellites\n",
- "www.fc.net/~mikem/ftp/visual.tle\n",
- "Mike McCants' visible satellites\n",
- NULL};
-
- // store a site in the Sites array
- static void AddSite(Char *url,Char *name)
- {
- // reality check
- if (NumSites<MAX_SITES && StrLen(url)>0 && StrLen(name)>0)
- {
- // copy
- StrCopy(SiteUrls[NumSites],url);
- StrCopy(SiteNames[NumSites],name);
- // keep track
- NumSites++;
- LogMessage("Site: %s\n",url);
- LogMessage("Name: %s\n",name);
- }
- return;
- }
-
- // returns true if we found our GetTLE Memo
- // needs the Ref of the Memo application database
- static Boolean HandleURLsMemo(DmOpenRef MemoDB)
- {
- Char site_url[MAX_SITE_URL_LEN+1];
- Char site_name[MAX_SITE_NAME_LEN+1];
- UInt16 num_memos;
- UInt16 record_number=0; // start with record 0
- MemHandle record_handle=NULL;
- MemoDBRecordPtr record_ptr=NULL;
- Char *memo_ptr;
- Char *string_ptr=site_url;
- UInt16 len=0;
- UInt16 slashes=0; // count slashes in site names
- Boolean reading_url=true;
- Boolean found=false;
-
- // how many memos are there in the database
- num_memos=DmNumRecordsInCategory(MemoDB,dmAllCategories);
- do
- {
- record_handle=DmQueryNextInCategory(MemoDB,&record_number,dmAllCategories);
- if (record_handle)
- {
- // found another record (==memo)
- // lock it so we can look at it
- record_ptr=MemHandleLock(record_handle);
- // does it start with the right string
- if (StrStr(&(record_ptr->note),URLsMemoHeader)==&(record_ptr->note))
- {
- found=true;
- // found the right memo
- // Now we split the memo into lines, each representing
- // one site URL followed by a line containing a name.
- // Start after the GetTLE\n string. As the
- // memo is \0 terminated we know when to stop
- // first we reaqd the url
- reading_url=true;
- for (memo_ptr=&(record_ptr->note)+StrLen(URLsMemoHeader);*memo_ptr;memo_ptr++)
- {
- // Look at each character
- switch(*memo_ptr)
- {
- case '\n':
- // found one complete line
- // terminate the site string with \0
- *string_ptr='\0';
- // which string were we reading?
- if (reading_url)
- {
- // need to read the site name next
- site_name[0]='\0';
- string_ptr=site_name;
- reading_url=false;
- }
- else
- {
- // so now we've finished the site name
- // store the site's url and name
- AddSite(site_url,site_name);
- // get ready to read the next site url
- site_url[0]='\0';
- string_ptr=site_url;
- reading_url=true;
- }
- len=0;
- break;
- default:
- // everything else is part of the URL
- // up to a certain length of course
- if (len<MAX_SITE_URL_LEN)
- {
- if (reading_url==false && (*memo_ptr)=='/')
- {
- slashes++;
- }
- *string_ptr=*memo_ptr;
- string_ptr++;
- len++;
- }
- break;
- }
- }
- // end of memo, but the last site might not
- // have been \n terminated, so do the stuff for
- // the case '\n' now.
- *string_ptr='\0';
- AddSite(site_url,site_name);
- }
- // unlock the memo - we're finished with it
- MemHandleUnlock(record_handle);
- }
- record_number++; // continue search for Memo one record higher
- } while (record_handle!=NULL && !found);
- // warn if the memo looks like an old one...
- if (slashes>3) // some low number
- {
- MyStatusFunc("GetTLE memo looks outdated. If you have problems, delete it and retry.");
- }
- return found;
- }
-
- // read (and maybe create) the GetTLE memo
- extern void ReadURLsMemo(void)
- {
- DmOpenRef MemoDB=0;
- MemoDBRecordPtr record_ptr=NULL;
- MemHandle record_handle=NULL;
- Boolean found=false;
- UInt32 length;
- UInt32 offset;
- UInt16 index;
-
- LogMessage("Find Memo: %s",URLsMemoHeader);
- // open the database of the built in Memo application
- MemoDB = DmOpenDatabaseByTypeCreator(MemoDBType, 'memo', dmModeReadWrite);
- if (MemoDB)
- {
- // found it - as expected
- // now try to read URLs from the GetTLE memo
- found=HandleURLsMemo(MemoDB);
- if (!found)
- {
- // there is no GetTLE Memo yet - create one
- // and put the default sites into it
- // first find out how long the text is we are going to write
- length=StrLen(URLsMemoHeader); // includes \n
- for (index=0;default_sites[index]!=NULL;index++)
- {
- length+=StrLen(default_sites[index]);
- }
- length++; // the trailing \0
- // allocate memory
- record_handle=(MemHandle)DmNewHandle(MemoDB,length);
- if (record_handle!=NULL)
- {
- // lock the handle, so we can write to it
- record_ptr=MemHandleLock(record_handle);
- // first write the header
- offset=0;
- DmStrCopy(record_ptr,offset,URLsMemoHeader);
- offset+=StrLen(URLsMemoHeader);
- // then write the URLs
- for (index=0;default_sites[index]!=NULL;index++)
- {
- DmStrCopy(record_ptr,offset,default_sites[index]);
- offset+=StrLen(default_sites[index]);
- }
- // now we've got the data in the record.
- // first we release it
- MemPtrUnlock(record_ptr);
- // and then we attach it to the Memo database
- index=1000; // attach to end of database
- if (DmAttachRecord(MemoDB,&index,record_handle,0))
- {
- // record is in database - free used memory
- MemHandleFree(record_handle);
- }
- // now there is a GetTLE memo - use it
- HandleURLsMemo(MemoDB);
- }
- }
- DmCloseDatabase(MemoDB);
- }
- return;
- }