home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #3 / amigamamagazinepolishissue1998.iso / bazy / fiasco_2.1 / arexx / importcolumn.frx < prev    next >
Text File  |  1997-10-06  |  7KB  |  311 lines

  1. /* ImportColumn.frx
  2.  * Import an ascii file that is structured with columns
  3.  * (seperated by spaces)
  4.  * You have to create the fields in the database before using this
  5.  * script!
  6.  * Copyright © 1997 Nils Bandener
  7.  * $VER: ImportColumn.frx 6.3 (6.10.97)
  8.  */
  9.  
  10. scriptname = "ImportColumn"
  11.  
  12. Parse Arg File
  13. Options Results
  14.  
  15. /*
  16.  *  If not called from Fiasco, try to address the active
  17.  *  Fiasco project
  18.  */
  19.  
  20. if ~abbrev(address(), "FIASCO.") then
  21. do
  22.     ports = show("Ports")
  23.  
  24.     do i = 1 to words(ports)
  25.  
  26.         if abbrev(word(ports, i), "FIASCO.") then
  27.         do
  28.             P = word(ports, i)
  29.  
  30.             Address Value P
  31.  
  32.             GetAttr Project Name Active ARexx
  33.  
  34.             Address Value Result
  35.  
  36.             break
  37.         end
  38.     end
  39. end
  40.  
  41. fiasco_port = address()
  42.  
  43. Signal on Syntax
  44. Signal on Halt
  45. Signal on Break_C
  46. Signal on Failure
  47.  
  48. LockGUI
  49.  
  50. if File = "" then
  51. do
  52.     RequestFile 'Var FILE'
  53.  
  54.     if rc ~= 0 then call bail_out
  55. end
  56.  
  57. if Open("g", File, "read") then
  58. do
  59.     SetStatus '"Scanning file..."'
  60.  
  61.     fieldcnt = 0
  62.  
  63.     do while ~eof("g")
  64.  
  65.         ln = readln("g")
  66.  
  67.         if ln ~= "" then
  68.         do
  69.             /* Ignore empty lines */
  70.  
  71.             if fieldcnt = 0 then
  72.             do
  73.                 /* The first line is completely parsed */
  74.  
  75.                 i = 1
  76.                 done = 0
  77.  
  78.                 do while i <= length(ln) & ~done
  79.  
  80.                     /* Search for a non-space character */
  81.  
  82.                     txpos = verify(ln, " ", ,i)
  83.  
  84.                     if txpos ~= 0 then
  85.                     do
  86.                         /* Search for a space */
  87.  
  88.                         sppos = verify(ln, " ", "Match", txpos)
  89.  
  90.                         if sppos = 0 then
  91.                         do
  92.                             endpos = length(ln)
  93.  
  94.                             done = 1
  95.                         end
  96.                         else
  97.                         do
  98.                             endpos = verify(ln, " ", ,sppos) - 1
  99.  
  100.                             if endpos = 0 then
  101.                             do
  102.                                 endpos = length(ln)
  103.  
  104.                                 done = 1
  105.                             end
  106.                         end
  107.  
  108.                         fieldcnt = fieldcnt + 1
  109.  
  110.                         fields.fieldcnt.f_start = txpos
  111.                         fields.fieldcnt.f_end = endpos
  112.                         fields.fieldcnt.f_id = trim(substr(ln, txpos, endpos - txpos + 1))
  113.  
  114.                         i = endpos + 1
  115.                     end
  116.                     else done = 1
  117.                 end
  118.             end
  119.             else
  120.             do
  121.                 /* The following lines are used to verify the
  122.                  * structure data created using the first line
  123.                  */
  124.  
  125.                 i = 1
  126.  
  127.                 do while i <= fieldcnt
  128.  
  129.                     ni = i + 1
  130.  
  131.                     sppos = fields.i.f_start - 1
  132.  
  133.                     if sppos > 0 then
  134.                     do
  135.                         if substr(ln, sppos, 1) ~= " " then
  136.                         do
  137.                             /* No space at this place,
  138.                              * this place is not a field
  139.                              * border
  140.                              */
  141.  
  142.                             if i = 1 then
  143.                             do
  144.                                 /* Special case for first field
  145.                                  */
  146.  
  147.                                 fields.i.f_start = 1
  148.                             end
  149.                             else
  150.                             do
  151.                                 /* Make one field
  152.                                  * out of two fields
  153.                                  */
  154.  
  155.                                 pi = i -1
  156.  
  157.                                 fields.pi.f_end = fields.i.f_end
  158.                                 fields.pi.f_id = fields.pi.f_id || " " || fields.i.f_id
  159.  
  160.                                 /* Delete field at i
  161.                                  */
  162.  
  163.                                 do k = i + 1 to fieldcnt
  164.  
  165.                                     pk = k - 1
  166.  
  167.                                     fields.pk.f_start = fields.k.f_start
  168.                                     fields.pk.f_end = fields.k.f_end
  169.                                     fields.pk.f_id = fields.k.f_id
  170.                                 end
  171.  
  172.                                 fieldcnt = fieldcnt - 1
  173.  
  174.                                 ni = i
  175.  
  176.                             end /* if i ~= 1 */
  177.  
  178.                         end /* if substr ~= " " */
  179.  
  180.                     end /* if sppos > 0 */
  181.  
  182.                     i = ni
  183.  
  184.                 end /* while i <= fieldcnt */
  185.  
  186.             end /* if fieldcnt ~= 0 */
  187.  
  188.         end /* if ln ~= "" */
  189.  
  190.     end /* do while ~eof() */
  191.  
  192.     goon = 1
  193.  
  194.     do while goon
  195.  
  196.         infostr = "Results from the file scan:*n"
  197.  
  198.         do i = 1 to fieldcnt
  199.  
  200.             infostr = infostr || "Field " || i || ": " || fields.i.f_id || ", Start: " || fields.i.f_start || ", End: " || fields.i.f_end || "*n"
  201.  
  202.         end
  203.  
  204.         infostr = infostr || "*nMake sure that you have created all required fields*nbefore you start importing"
  205.  
  206.         RequestChoice 'Body "' || infostr || '" Gadgets "Start Import|Change Field ID|Cancel" Title "ImportColumn"'
  207.  
  208.         if result = 2 then
  209.         do
  210.             RequestNumber 'Text "Please specify the number of the field:"' Var num
  211.             if rc = 0 then
  212.             do
  213.                 if num > 0 & num <= fieldcnt then
  214.                 do
  215.                     RequestString '"' || fields.num.f_id || '" Text "New ID:"'
  216.  
  217.                     if rc = 0 then
  218.                     do
  219.                         fields.num.f_id = result
  220.                     end
  221.                 end
  222.             end
  223.         end
  224.         else goon = 0
  225.     end
  226.  
  227.     if result = 1 then
  228.     do
  229.  
  230.         SetStatus '"Reading..."'
  231.  
  232.         call seek("g", 0, "Begin")
  233.  
  234.         ln = readln("g")    /* Ignore first line */
  235.  
  236.         do while ~eof("g")
  237.  
  238.             ln = readln("g")
  239.  
  240.             if ln ~= "" then do
  241.  
  242.                 AddRecord
  243.  
  244.                 do i = 1 to fieldcnt
  245.  
  246.                     data = trim(substr(ln, fields.i.f_start, fields.i.f_end - fields.i.f_start + 1))
  247.  
  248.                     SetField '"' || fields.i.f_id || '" "' || data '"'
  249.                 end
  250.             end
  251.         end
  252.     end
  253.  
  254.     call close("g")
  255. end
  256. else
  257. do
  258.     RequestChoice '"Could not open ' || File || '" "Cancel"  Title "ImportColumn.rexx"'
  259. end
  260.  
  261.  
  262.  
  263. bail_out:
  264.  
  265. Address Value fiasco_port
  266.  
  267. UnlockGUI
  268. ResetStatus
  269.  
  270. exit
  271.  
  272. syntax:
  273. failure:
  274.  
  275. if show("Ports", fiasco_port) then
  276. do
  277.     Address Value fiasco_port
  278.  
  279.     RequestChoice '"Error ' || rc || ' in line ' || sigl || ':*n' || errortext(rc) || '" "Cancel" Title "' || scriptname || '"'
  280. end
  281. else
  282. do
  283.     say "Error" rc "in line" sigl ":" errortext(rc)
  284.     say "Enter to continue"
  285.     pull dummy
  286. end
  287.  
  288. call bail_out
  289.  
  290. halt:
  291. break_c:
  292.  
  293. if show("Ports", fiasco_port) then
  294. do
  295.     Address Value fiasco_port
  296.  
  297.     RequestChoice '"Script Abort Requested" "Abort Script" Title "' || scriptname || '"'
  298.  
  299.     if result = 0 then return
  300. end
  301. else
  302. do
  303.     say "*** Break"
  304.     say "Enter to continue"
  305.     pull dummy
  306. end
  307.  
  308. call bail_out
  309.  
  310.  
  311.