home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 July / macformat-039.iso / DATABASE / SHARED.DIR / 01002.ls next >
Encoding:
Text File  |  1996-03-14  |  7.3 KB  |  291 lines

  1. on retrieverec record, fileName, index
  2.   global gPath, gBit, gHDpath
  3.   set myfile to FileIO(mnew, "read", gHDpath & fileName)
  4.   if not objectp(myfile) then
  5.     alert("Problem opening database")
  6.     return 0
  7.   end if
  8.   if item record of field index <> EMPTY then
  9.     set tmp to integer(item record of field index)
  10.   else
  11.     alert("Error loading record: " & record & " from the database")
  12.   end if
  13.   set err to myfile(mSetPosition, tmp)
  14.   if err then
  15.     alert("Problem retrieving record " & record, " using fileio")
  16.   else
  17.     set tmp to myfile(mReadToken, "@", EMPTY)
  18.     myfile(mdispose)
  19.     return tmp
  20.   end if
  21. end
  22.  
  23. on retrieve record, fieldnum, fileName, index
  24.   global gPath, gBit, gHDpath
  25.   set myfile to FileIO(mnew, "read", gHDpath & fileName)
  26.   if not objectp(myfile) then
  27.     alert("Problem opening database")
  28.     return 0
  29.   end if
  30.   set tmp to integer(item record of field index)
  31.   set err to myfile(mSetPosition, tmp)
  32.   if err then
  33.     alert("problem retrieving record")
  34.   else
  35.     repeat with i = 1 to fieldnum
  36.       set tmp to myfile(mReadLine)
  37.     end repeat
  38.     set tmp to char 1 to the number of chars in tmp - 1 of tmp
  39.     myfile(mdispose)
  40.     return tmp
  41.   end if
  42. end
  43.  
  44. on search str, indextmp
  45.   set tmp to Bsearch(str, indextmp)
  46.   if tmp then
  47.     return item 2 to the number of items in line tmp of field indextmp of line tmp of field indextmp
  48.   else
  49.     return 0
  50.   end if
  51. end
  52.  
  53. on LogicalAnd listA, listB
  54.   set tmplist to EMPTY
  55.   if (listA = EMPTY) or (listA = 0) then
  56.     return EMPTY
  57.   end if
  58.   if (listB = EMPTY) or (listB = 0) then
  59.     return EMPTY
  60.   end if
  61.   set numA to the number of items in listA
  62.   set numB to the number of items in listB
  63.   set posA to 1
  64.   set posB to 1
  65.   repeat while (posA <= numA) and (posB <= numB)
  66.     if integer(item posA of listA) = integer(item posB of listB) then
  67.       put item posA of listA & "," after tmplist
  68.       set posA to posA + 1
  69.       set posB to posB + 1
  70.       next repeat
  71.     end if
  72.     if integer(item posA of listA) < integer(item posB of listB) then
  73.       set posA to posA + 1
  74.       next repeat
  75.     end if
  76.     set posB to posB + 1
  77.   end repeat
  78.   set tmplist to item 1 to the number of items in tmplist - 1 of tmplist
  79.   return tmplist
  80. end
  81.  
  82. on LogicalOr listA, listB
  83.   set tmplist to EMPTY
  84.   set numA to the number of items in listA
  85.   set numB to the number of items in listB
  86.   set posA to 1
  87.   set posB to 1
  88.   if (listA <> EMPTY) and (listB <> EMPTY) then
  89.     repeat while (posA <= numA) and (posB <= numB)
  90.       if integer(item posA of listA) = integer(item posB of listB) then
  91.         put item posA of listA & "," after tmplist
  92.         set posA to posA + 1
  93.         set posB to posB + 1
  94.         next repeat
  95.       end if
  96.       if integer(item posA of listA) < integer(item posB of listB) then
  97.         put item posA of listA & "," after tmplist
  98.         set posA to posA + 1
  99.         next repeat
  100.       end if
  101.       put item posB of listB & "," after tmplist
  102.       set posB to posB + 1
  103.     end repeat
  104.   end if
  105.   if (posA <= numA) and (listA <> EMPTY) then
  106.     put item posA to numA of listA & "," after tmplist
  107.   else
  108.     if posB <= numB then
  109.       put item posB to numB of listB & "," after tmplist
  110.     end if
  111.   end if
  112.   set tmplist to item 1 to the number of items in tmplist - 1 of tmplist
  113.   return tmplist
  114. end
  115.  
  116. on Bsearch str, txt
  117.   set txt to the number of cast txt
  118.   set start to 1
  119.   set end to the number of lines in field txt
  120.   set middle to (start + end) / 2
  121.   repeat while str <> item 1 of line middle of field txt
  122.     if str = item 1 of line end of field txt then
  123.       return end
  124.     end if
  125.     if start = middle then
  126.       return 0
  127.     end if
  128.     if str < item 1 of line middle of field txt then
  129.       set end to middle
  130.     else
  131.       set start to middle
  132.     end if
  133.     set middle to (start + end) / 2
  134.   end repeat
  135.   if str = item 1 of line middle of field txt then
  136.     return middle
  137.   else
  138.     return 0
  139.   end if
  140. end
  141.  
  142. on iBsearch str, txt
  143.   set start to 1
  144.   set end to the number of items in field txt
  145.   set middle to (start + end) / 2
  146.   repeat while str <> integer(item middle of txt)
  147.     put start && middle && end
  148.     if str = integer(item end of txt) then
  149.       return end
  150.     end if
  151.     if start = integer(middle) then
  152.       return 0
  153.     end if
  154.     if str < integer(item middle of txt) then
  155.       set end to middle
  156.     else
  157.       set start to middle
  158.     end if
  159.     set middle to (start + end) / 2
  160.   end repeat
  161.   if str = integer(item middle of txt) then
  162.     put "found " && str && " in list"
  163.     return middle
  164.   else
  165.     put "didn't find " && str
  166.     return 0
  167.   end if
  168. end
  169.  
  170. on sortlines txt
  171.   set num to the number of lines in field txt
  172.   set tmp2 to the number of cast txt
  173.   repeat with i = 1 to num
  174.     set flag to 0
  175.     repeat with j = 2 to num
  176.       if item 1 of line j of field tmp2 < item 1 of line j - 1 of field tmp2 then
  177.         set flag to 1
  178.         set tmp to line j - 1 of field tmp2
  179.         put line j of field txt into line j - 1 of field tmp2
  180.         put tmp into line j of field tmp2
  181.       end if
  182.     end repeat
  183.     if flag = 0 then
  184.       exit repeat
  185.     end if
  186.   end repeat
  187. end
  188.  
  189. on sortitems txt
  190.   if txt = EMPTY then
  191.     return EMPTY
  192.   end if
  193.   set num to the number of items in txt
  194.   repeat with i = 1 to num
  195.     set flag to 0
  196.     repeat with j = 2 to num
  197.       if item j of txt < item j - 1 of txt then
  198.         set flag to 1
  199.         set tmp to item j - 1 of txt
  200.         put item j of txt into item j - 1 of txt
  201.         put tmp into item j of txt
  202.       end if
  203.     end repeat
  204.     if flag = 0 then
  205.       exit repeat
  206.     end if
  207.   end repeat
  208.   return txt
  209. end
  210.  
  211. on merge listA, listB
  212.   if listA = EMPTY then
  213.     return listB
  214.   end if
  215.   if listB = EMPTY then
  216.     return listA
  217.   end if
  218.   set listNew to EMPTY
  219.   set posA to 1
  220.   set posB to 1
  221.   set posNew to 1
  222.   set numA to the number of items in listA
  223.   set numB to the number of items in listB
  224.   repeat while (posA <= numA) and (posB <= numB)
  225.     if integer(item posA of listA) = integer(item posB of listB) then
  226.       put item posA of listA & "," after listNew
  227.       set posA to posA + 1
  228.       set posB to posB + 1
  229.     else
  230.       if integer(item posA of listA) < integer(item posB of listB) then
  231.         put item posA of listA & "," after listNew
  232.         set posA to posA + 1
  233.       else
  234.         put item posB of listB & "," after listNew
  235.         set posB to posB + 1
  236.       end if
  237.     end if
  238.     set posNew to posNew + 1
  239.   end repeat
  240.   if posA <= numA then
  241.     repeat while posA <= numA
  242.       put item posA of listA & "," after listNew
  243.       set posA to posA + 1
  244.     end repeat
  245.   else
  246.     repeat while posB <= numB
  247.       put item posB of listB & "," after listNew
  248.       set posB to posB + 1
  249.     end repeat
  250.   end if
  251.   return listNew
  252. end
  253.  
  254. on siBsearch str, txt
  255.   set start to 1
  256.   set end to the number of items in field txt
  257.   set middle to (start + end) / 2
  258.   repeat while str <> item middle of txt
  259.     put start && middle && end
  260.     if str = item end of txt then
  261.       return end
  262.     end if
  263.     if start = middle then
  264.       return 0
  265.     end if
  266.     if str < item middle of txt then
  267.       set end to middle
  268.     else
  269.       set start to middle
  270.     end if
  271.     set middle to (start + end) / 2
  272.   end repeat
  273.   if str = item middle of txt then
  274.     put "found " && str && " in list"
  275.     return middle
  276.   else
  277.     put "didn't find " && str
  278.     return 0
  279.   end if
  280. end
  281.  
  282. on find str, txt
  283.   repeat with i = 1 to the number of items in field txt
  284.     if str = item i of field txt then
  285.       put i
  286.       exit repeat
  287.     end if
  288.   end repeat
  289.   return -1
  290. end
  291.