home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / oplexamp / ORG2AGN.OPL < prev    next >
Text File  |  1992-08-27  |  5KB  |  228 lines

  1. rem !!!     Program to convert Organiser II format diary to Series 3 Agenda file  !!!
  2.  
  3. rem ! Top level procedure
  4. proc org2agn:
  5.     local dry$(130),agn$(130),off%(6),handle%,ret%,vb%
  6.  
  7.     dry$="\odb\*.odb"
  8.     agn$="\agn\.agn"
  9.     dInit "ORGII Diary Conversion V1.0"
  10.     dFile dry$,"OrgII diary file:",0 :rem selector
  11.     dFile agn$,"To Series 3 file:",49 :rem editor,query existing,allow null
  12.     vb%=1
  13.     dChoice vb%,"Verbose","No,Yes"
  14.     if dialog=0 :return :endif
  15.     agn$=parse$(agn$,"\agn\.agn",off%())
  16.     if off%(6)
  17.         print "Wild cards not handled"
  18.         get
  19.         return
  20.     endif
  21.     if (off%(5)-off%(4))=0
  22.         agn$=parse$(agn$,dry$,off%())
  23.     endif
  24.     print "Converting",dry$
  25.     print "        to",agn$;"..."
  26.     ret%=acreate%:(agn$)
  27.     if showErr%:(ret%)
  28.         return
  29.     endif
  30.     ret%=dOpen%:(dry$,addr(handle%))
  31.     if ret%
  32.         goto tidy1
  33.     endif
  34.     ret%=conv%:(handle%,vb%-1)
  35.     if     showErr%:(ret%)
  36.         goto tidy2
  37.     endif
  38. tidy2::
  39.     ioClose(handle%)
  40. tidy1::
  41.     busy "Closing...",1,4
  42.     close
  43.     busy off
  44.     if ret%
  45.         trap delete agn$
  46.     else
  47.         ret%=fixHead%:(agn$)
  48.         if ret%<0
  49.             print "Error fixing version in header"
  50.             showErr%:(ret%)
  51.         endif
  52.     endif
  53.     beep 5,200
  54.     print "Finished"
  55.     get
  56. endp
  57.  
  58. rem ! Fix version numbers in header from $110f to $100f at offset $11 and $15
  59. proc fixHead%:(name$)
  60.     local h%,ret%,off&,oneZero%,p%
  61.  
  62.     ret%=ioOpen(h%,name$,$0300)    :rem open for update and random access
  63.     if ret%<>0
  64.         return(ret%)
  65.     endif
  66.     off&=$11
  67.     ret%=ioSeek(h%,1,off&)
  68.     if ret%<0
  69.         goto closeIt
  70.     endif
  71.     oneZero%=$10
  72.     p%=addr(oneZero%)
  73.     ret%=ioWrite(h%,p%,1)
  74.     if ret%<0
  75.         goto closeIt
  76.     endif
  77.     off&=$15
  78.     ret%=ioSeek(h%,1,off&)
  79.     if ret%<0
  80.         goto closeIt
  81.     endif
  82.     ret%=ioWrite(h%,p%,1)
  83. closeIt::
  84.     ioclose(h%)
  85.     return ret%
  86. endp
  87.  
  88. rem ! Procedure to open a saved Organiser diary text file
  89. proc dOpen%:(name$,pHand%)
  90.     local ret%,mode%
  91.  
  92.     mode%= $0400 OR $0020   :rem open mode=$0000, text=$0020, share=$0400
  93.     ret%=ioOpen(#pHand%,name$,mode%)
  94.     if showErr%:(ret%)
  95.         return ret%
  96.     endif    
  97.     return(0)
  98. endp
  99.  
  100. proc conv%:(handle%,vb%)
  101.     local dy%,mt%,yr%,hh%,mm%,dur%,pre%,i$(64),txt$(255)
  102.     local address%,ret%,l%,i&,badDate%
  103.  
  104.     address%=addr(txt$)
  105.     while 1
  106.         ret%=ioread(handle%,address%+1,255)
  107.         if ret%=0    :rem TO OVERCOME BUG: ^Z at eof gives never-ending file
  108.                     :rem 0 length will never be generated by ORG diary
  109.             return(0)
  110.         endif
  111.         if ret%<0
  112.             if ret%<>-36 :rem if not eof
  113.                 showErr%:(ret%)
  114.             endif
  115.             break
  116.         endif
  117.         pokeb address%,ret% :rem poke leading byte count of string variable
  118.         yr%=stoui%:(left$(txt$,4))
  119.         mt%=stoui%:(mid$(txt$,5,2))
  120.         dy%=stoui%:(mid$(txt$,7,2))
  121.         hh%=stoui%:(mid$(txt$,9,2))
  122.         mm%=stoui%:(mid$(txt$,11,2))
  123.         dur%=stoui%:(mid$(txt$,13,2))*15
  124.         pre%=stoui%:(mid$(txt$,15,2))-1
  125.         i$=mid$(txt$,18,255)
  126.         badDate%=(yr%<1980) or (yr%>2049)
  127.         if badDate% or vb%
  128.             print dy%;"/";mt%;"/";yr%,hh%;":";mm%
  129.             print "  ";i$
  130.             gIPrint "Record "+num$(i&+1,8)
  131.             if badDate%
  132.                 beep 5,100
  133.                 print "WARNING: Record ignored",
  134.                 if yr%<1980
  135.                     print "before 1980"
  136.                 else
  137.                     print "after 2049"
  138.                 endif
  139.                 print "Press any key to continue..."
  140.                 get
  141.                 continue
  142.             else
  143.                 if key
  144.                     get
  145.                 endif
  146.             endif
  147.         endif
  148.         l%=len(i$)
  149.         if l%>64
  150.             print "Text length",l%,"exceeds 64 character maximum"
  151.             get
  152.         endif
  153.         ret%=aadd%:(dy%,mt%,yr%,hh%,mm%,dur%,pre%,i$)
  154.         i&=i&+1
  155.         if ret%<0
  156.             print "Error appending record",i&
  157.             showErr%:(ret%)
  158.         endif
  159.     endwh
  160. endp
  161.  
  162. rem ! Convert string to unsigned integer
  163. proc stoui%:(inS$)
  164.     local ax%,bx%,cx%,dx%,si%,di%
  165.     local s$(255)
  166.     local flags%
  167.  
  168.     s$=inS$+chr$(0)
  169.     cx%=10
  170.     si%=addr(s$)+1
  171.     ax%=$0500
  172.     flags%=os($8a,addr(ax%))
  173.     if (flags% and 1)
  174.         print "Error converting",ins$,"to unsigned int"
  175.         print "Flags=$";hex$(flags%)
  176.         showErr%:(ax% or $ff00)
  177.         raise ax%
  178.     else
  179.         return ax%
  180.     endif
  181. endp
  182.  
  183. proc showerr%:(err%)
  184.     if err%<0
  185.         print "Error",err%,err$(err%)
  186.         print "Press any key to continue..."
  187.         get
  188.         return err%
  189.     endif
  190.     return 0
  191. endp
  192.  
  193. rem ! Procedure to create an agenda dbf file
  194. proc acreate%:(name$)
  195.     trap delete name$
  196.     trap create name$,a,i1%,i2%,i3%,i4%,info$
  197.     return err
  198. endp
  199.  
  200. rem ! Conversion subroutine to add a timed agenda entry
  201. proc aadd%:(dy%,mt%,yr%,hh%,mm%,dur%,pre%,i$)
  202.     rem dy%,mt%,yr% 1-31,1-12,1980-2049
  203.     rem hh%,mm% 0-23,0-59
  204.     rem dur%<(1440-(hh%*60+mm%))
  205.     rem pre% must be such that ((hh%*60+mm%)-pre%)>=0
  206.     rem if pre%<0 then no alarm
  207.     rem len(i$)<=64
  208.  
  209.     local xd&,t%
  210.  
  211.     xd&=days(dy%,mt%,yr%)
  212.         pokeB addr(t%),peekB(addr(xd&))
  213.         pokeB addr(t%)+1,peekB(addr(xd&)+1)
  214.     a.i1%=t%
  215.     a.i2%=dur%*2
  216.     a.i3%=hh%*60+mm%
  217.     a.i4%=-1
  218.     if (pre%<0)
  219.     a.i2%=a.i2%+1
  220.     else
  221.     a.i4%=1439-((hh%*60+mm%)-pre%)
  222.     endif
  223.     a.info$=i$
  224.     trap append
  225.     return err
  226. endp
  227.  
  228.