home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / postroad.zip / adr2asc.cmd < prev    next >
OS/2 REXX Batch file  |  1997-10-23  |  11KB  |  295 lines

  1. /*  ADR2ASC.CMD -- Convert Post Road Mailer address book to ASCII file.   */
  2. /*                     Parameters: address_book_file                      */
  3. /*  Output files have same filename as ADR, with ASC and GRP extensions.  */
  4. /*   (c) Copyright 1995, InnoVal System Solutions, Inc., Tom Springall    */
  5. /*        (c) Copyright 1996, modifications, InnoVal, Kari Jackson        */
  6. /*    InnoVal Systems Solutions, Inc., and the authors cannot be held     */
  7. /*   responsible for damage that might occur while using this program.    */
  8. /*                         Use at your own risk.                          */
  9.  
  10. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  11. signal on syntax name ERR5
  12. call SysLoadFuncs
  13. signal on syntax name Syntax
  14.  
  15.  CRLF = '0D'x||'0A'x
  16.  
  17.  SAVE_FILECARDS        = X2C('0700') ; SAVE_GROUPS           = X2C('0800')
  18.  SAVE_GNAME            = X2C('2000') ; SAVE_ADDRESSES        = X2C('2200')
  19.  SAVE_GADDRESS         = X2C('2400') ; SAVE_BOOK_NAME        = X2C('3000')
  20.  SAVE_FIRST            = X2C('3500') ; SAVE_CNAME            = X2C('4000')
  21.  SAVE_COMPANY          = X2C('4500') ; SAVE_EMAIL1           = X2C('5000')
  22.  SAVE_EMAIL2           = X2C('7500') ; SAVE_EMAIL3           = X2C('7600')
  23.  SAVE_EMAIL4           = X2C('7700') ; SAVE_EMAIL5           = X2C('7800')
  24.  SAVE_PHONE1           = X2C('8000') ; SAVE_PHONE2           = X2C('8500')
  25.  SAVE_FAX              = X2C('9000') ; SAVE_ADDRESS          = X2C('9500')
  26.  SAVE_NICKNAME1        = X2C('0001') ; SAVE_NICKNAME2        = X2C('0101')
  27.  SAVE_NICKNAME3        = X2C('0201') ; SAVE_NICKNAME4        = X2C('0301')
  28.  SAVE_NICKNAME5        = X2C('0401')
  29.  
  30.  Arg filespec
  31.  If filespec = '' | Pos('?',filespec) <> 0 then Signal Help
  32.  
  33.  If Pos('.', filespec) = 0 then filespec = filespec || '.ADR'
  34.  If Stream(filespec,'C','QUERY EXISTS') = '' then Signal Err1
  35.  
  36.  Parse var filespec file '.' ext
  37.  If ext <> 'ADR' then Signal Err2
  38.  
  39.  filename = SubStr(file, LastPos('\',file) + 1)
  40.  outfile = filename || '.ASC'
  41.  groupfile = filename || '.GRP'
  42.  
  43.  outexists=stream(outfile,'c','query exists')
  44.  groupexists=stream(groupfile,'c','query exists')
  45.  If outexists<>'' | groupexists<>'' then do
  46.     say ""
  47.     say "Output file exists....Type 1 to replace, 2 to append all"
  48.     say "entries, 3 to append unique entries only, or 4 to exit."
  49.     do forever
  50.        key=sysgetkey()
  51.        if key=4 then exit 1
  52.        if key=1 | key=2 | key=3 then leave
  53.        say ""
  54.        say "Please type 1, 2, 3, or 4."
  55.     end
  56.     if key=1 then do
  57.        if outexists<>'' then call sysfiledelete outfile
  58.        if groupexists<>'' then call sysfiledelete groupfile
  59.        outexists=''
  60.        groupexists=''
  61.     end
  62.     if key=2 then append='all'
  63.     if key=3 then append='unique'
  64.  end
  65.  
  66.  If SAVE_BOOK_NAME <> GetCodeLen() then Signal Err3
  67.  valu = CharIn(filespec,,len)
  68.  if outexists='' then do
  69.     If LineOut(outfile, valu '(Post Road Mailer address book' filespec')')<>0 then Signal Err4
  70.     Call LineOut outfile, 'The fields are:'
  71.     Call LineOut outfile, 'Last Name<tab>First Name<tab>Organization<tab>Email 1<tab>Nickname 1<tab>....Email 5<tab>Nickname 5<tab>Telephone 1<tab>Telephone 2<tab>Fax<tab>Notes 1<tab>Notes 2<tab>Notes 3<tab>....Notes X'
  72.     call lineout outfile,''
  73.  end
  74.  if groupexists='' then do
  75.     If LineOut(groupfile, valu '(Post Road Mailer address groups' filespec')')<>0 then Signal Err4
  76.     Call lineOut groupfile, 'The fields are:'
  77.     Call LineOut groupfile, 'Group Name<tab>Address 1<tab>Address 2<tab>Address 3<tab>....Address X'
  78.     call lineout groupfile,''
  79.  end
  80.  
  81.  typ = '' ; cards = 0 ; groups = 0 ; data. = '' ; carddata. = '' ; groupdata. = ''
  82.  
  83.  if append='unique' then do
  84.     do 4
  85.        if groupexists<>'' then call linein groupfile
  86.        if outexists<>'' then call linein outfile
  87.     end
  88.     i=0
  89.     do while lines(outfile)
  90.        lin=linein(outfile)
  91.        parse upper var lin . '09'x . '09'x . '09'x Unique1 '09'x . '09'x Unique2 '09'x . '09'x Unique3 '09'x . '09'x Unique4 '09'x . '09'x Unique5 '09'x .
  92.        do j=1 to 5
  93.           check=value(Unique||j)
  94.           if check<>'' then do
  95.              i=i+1
  96.              carddata.i=check
  97.           end
  98.        end
  99.     end
  100.     carddata.0=i
  101.     i=0
  102.     do while lines(groupfile)
  103.        lin=linein(groupfile)
  104.        i=i+1
  105.        parse upper var lin groupdata.i '09'x .
  106.     end
  107.     groupdata.0=i
  108.  end
  109.  
  110.  Do While Chars(filespec) > 0
  111.  
  112.    /* read in code value and length of field */
  113.    code = GetCodeLen()                  /* len is also returned */
  114.    /* read in value of field */
  115.    valu = CharIn(filespec,,len)
  116.  
  117.    Select                               /* handle various code values */
  118.      When code = SAVE_FILECARDS then nop
  119.      When code = SAVE_GROUPS then nop
  120.  
  121.      When code = SAVE_GNAME then do     /* beginning of a new group */
  122.        Call ProcessPrior typ            /* write prior group or page */
  123.        typ = 'GROUP'
  124.        groups = groups + 1
  125.        data. = ''
  126.        addresses = 0                    /* my count of addresses in group */
  127.        data.GROUP_NAME = valu
  128.        end
  129.  
  130.      When code = SAVE_ADDRESSES then nop
  131.  
  132.      When code = SAVE_GADDRESS then do   /* a group address */
  133.        addresses = addresses + 1
  134.        data.GADDRESS.addresses = valu
  135.        end
  136.  
  137.      When code = SAVE_CNAME then do      /* beginning of a new address page */
  138.        Call ProcessPrior typ             /* write prior page or group */
  139.        typ = 'CARD'
  140.        cards = cards + 1
  141.        data. = ''
  142.        data.LAST_NAME = valu             /* last name */
  143.        end
  144.  
  145.      When code = SAVE_FIRST then data.FIRST_NAME = valu
  146.      When code = SAVE_COMPANY then data.ORGANIZATION = valu
  147.      When code = SAVE_EMAIL1 then data.EMAIL1 = valu
  148.      When code = SAVE_EMAIL2 then data.EMAIL2 = valu
  149.      When code = SAVE_EMAIL3 then data.EMAIL3 = valu
  150.      When code = SAVE_EMAIL4 then data.EMAIL4 = valu
  151.      When code = SAVE_EMAIL5 then data.EMAIL5 = valu
  152.      When code = SAVE_PHONE1 then data.PHONE1 = valu
  153.      When code = SAVE_PHONE2 then data.PHONE2 = valu
  154.      When code = SAVE_FAX then data.FAX = valu
  155.      When code = SAVE_ADDRESS then data.COMMENTS = valu
  156.      When code = SAVE_NICKNAME1 then data.NICKNAME1 = valu
  157.      When code = SAVE_NICKNAME2 then data.NICKNAME2 = valu
  158.      When code = SAVE_NICKNAME3 then data.NICKNAME3 = valu
  159.      When code = SAVE_NICKNAME4 then data.NICKNAME4 = valu
  160.      When code = SAVE_NICKNAME5 then data.NICKNAME5 = valu
  161.  
  162.      Otherwise Nop
  163.  
  164.      End /* of Select */
  165.  
  166.    end /* of Do While */
  167.  
  168.  Call ProcessPrior typ            /* write last page or group */
  169.  
  170.  Call CharOut filespec            /* close files */
  171.  Call LineOut outfile
  172.  Call LineOut groupfile
  173.  
  174.  Say ''
  175.  Say 'The ASCII version of' filespec
  176.  Say 'is now in' outfile
  177.  Say cards 'entries were processed.'
  178.  Say ''
  179.  if groups>0 then do
  180.    Say 'The address groups in' filespec
  181.    Say 'are now in' groupfile
  182.    Say groups 'entries were processed.'
  183.    Say ''
  184.  end
  185.  
  186.  Exit                            /* all done. */
  187.  
  188. /*----------------------------------------------------------------------*/
  189. HELP:
  190.  Say ''
  191.  Say 'ADR2ASC.CMD converts a Post Road Mailer address book to a text file.'
  192.  Say ''
  193.  Say 'Syntax is:  ADR2ASC <path>filename.ADR'
  194.  Say ''
  195.  Say 'The ASCII files will be created in the current directory; one with'
  196.  Say 'an .ASC extension and the other (the address groups) with a .GRP'
  197.  Say 'extension.'
  198.  Say ''
  199.  Exit
  200. /*---------------------------------------------------------------------------*/
  201. GETCODELEN: Procedure Expose filespec len
  202.  
  203.    /* get 1st 2 words of each record: code, length */
  204.    w1w2 = CharIn(filespec,,8)
  205.  
  206.    /* save length of what follows (in bytes) */
  207.    len = C2D(Intel(Substr(w1w2,5,2)))
  208.  
  209.    /* return code for field type */
  210.    Return Left(w1w2,2)
  211.  
  212. /*---------------------------------------------------------------------------*/
  213. INTEL:  Procedure    /* Swaps bytes in an Intel 2 or 4 byte number. */
  214.  Parse arg x
  215.  
  216.  If Length(x) = 2 then
  217.    Return Right(x,1) || Left(x,1)
  218.  
  219.  Else
  220.    Return Intel(Right(x,2)) || Intel(Left(x,2))
  221.  
  222. /*---------------------------------------------------------------------------*/
  223. PROCESSPRIOR:
  224.  /* Writes field for prior record to the output file. */
  225.  Arg typ
  226.  
  227.  Select
  228.    When typ = 'CARD' then do            /* address page */
  229.  
  230.      if append='unique' then do m=1 to 5
  231.         check=translate(value('data.EMAIL'||m))
  232.         if check<>'' then do n=1 to carddata.0
  233.            if check=carddata.n then return
  234.         end
  235.      end
  236.  
  237.      holding=''
  238.      Do While data.COMMENTS <> ''
  239.        Parse var data.COMMENTS left (crlf) data.COMMENTS
  240.        if holding='' then holding=left
  241.        else holding=holding||'09'x||left
  242.      end
  243.      data.COMMENTS=holding
  244.      if 0<>LineOut(outfile, data.LAST_NAME||'09'x||data.FIRST_NAME||'09'x||data.ORGANIZATION||'09'x||data.EMAIL1||'09'x||data.NICKNAME1||'09'x||data.EMAIL2||'09'x||data.NICKNAME2||'09'x||data.EMAIL3||'09'x||data.NICKNAME3||'09'x||data.EMAIL4||'09'x||data.NICKNAME4||'09'x||data.EMAIL5||'09'x||data.NICKNAME5||'09'x||data.PHONE1||'09'x||data.PHONE2||'09'x||data.FAX||'09'x||data.COMMENTS) then signal err4
  245.    end /* of When */
  246.  
  247.    When typ = 'GROUP' then do        /* group of addresses */
  248.  
  249.      if append='unique' then do
  250.         check=translate(data.GROUP_NAME)
  251.         do q=1 to groupdata.0
  252.            if check=groupdata.q then return
  253.         end
  254.      end
  255.  
  256.      Do i = 1 to addresses
  257.        data.GROUP_NAME=data.GROUP_NAME||'09'x||data.GADDRESS.i
  258.      end
  259.  
  260.      if 0<>LineOut(groupfile, data.GROUP_NAME) then signal err4
  261.  
  262.    end /* of When */
  263.  
  264.    Otherwise Nop
  265.  
  266.  End /* of Select */
  267.  
  268.  Return
  269.  
  270. /*---------------------------------------------------------------------------*/
  271. ERR1:
  272.  Say 'File' filespec 'not found.'
  273.  Exit 1
  274. /*---------------------------------------------------------------------------*/
  275. ERR2:
  276.  Say 'File' filespec 'does not have an address book (.ADR) extension.'
  277.  Exit 1
  278. /*---------------------------------------------------------------------------*/
  279. ERR3:
  280.  Say 'File' filespec 'does not appear to be an address book.'
  281.  Call CharOut filespec
  282.  Exit 1
  283. /*---------------------------------------------------------------------------*/
  284. ERR4:
  285.  Say 'Error writing to file'
  286.  Call CharOut filespec
  287.  Call LineOut outfile
  288.  Call LineOut groupfile
  289.  Exit 1
  290. /*---------------------------------------------------------------------------*/
  291. ERR5:
  292.  say 'Unable to load the REXXUtil functions.  Either the REXXUTIL.DLL file'
  293.  say 'is not on the LIBPATH or REXX support is not installed on this system.'
  294.  exit 1
  295.