--------------------------------------
 
-- 
 
--------------------------------------
 
on uuencode(str)
 
  ret=""
 
  n = str.Length mod 3
 
  
 
  if (n <> 0) then
 
    repeat with i = 1 to 3 - n
 
      put numtochar(0) after str
 
    end repeat
 
  end if
 
  
 
  j = str.Length
 
  i = 1
 
  
 
  repeat while true
 
    if i>j then exit repeat
 
    
 
    put numtochar(chartonum(str.char[1+  i-1]) / 4 + 32) after ret
 
    put numtochar(chartonum(str.char[1+  i-1]) mod 4 * 16 + chartonum(str.char[1+ i]) / 16 + 32) after ret
 
    put numtochar(chartonum(str.char[1+  i  ]) mod 16 * 4 + chartonum(str.char[1+ i + 1]) / 64 + 32) after ret
 
    put numtochar(chartonum(str.char[1+  i+1]) mod 64 + 32) after ret
 
    
 
    i = i + 3
 
  end repeat
 
  return ret 
 
end 
 
 
 
--------------------------------------
 
-- 
 
--------------------------------------
 
on uudecode(str)
 
  
 
  --if str starts "," then delete char 1 of str  
 
  --if the last char of str = "`" then delete the last char of str
 
  --if the last char of str = numtochar(10) then delete the last char of str
 
  
 
  str = str_replace("`"," ",str)
 
  
 
  ret=""
 
  j = str.Length
 
  i = 1
 
  
 
  repeat while true
 
    if i>j then exit repeat
 
    
 
    put numtochar( ( chartonum(str.char[1+ i-1]) - 32)   * 4   + (chartonum( str.char[1+ i]) - 32) / 16 ) after ret
 
    put numtochar( ((chartonum(str.char[1+ i  ]) mod 16) * 16) + (chartonum( str.char[1+ i+1]) - 32) / 4 ) after ret
 
    put numtochar( ((chartonum(str.char[1+ i+1]) mod 4)  * 64) + (chartonum( str.char[1+ i+2]) - 32) ) after ret
 
    
 
    i = i + 4
 
  end repeat
 
  return ret 
 
end
 
 
 
--------------------------------------
 
-- TO DO
 
--------------------------------------
 
on uuencode_file(str)
 
  -- "begin %s %s\n" <mode>, <decode_pathname>
 
  
 
  -- "end\n"
 
end
 
 
 
--------------------------------------
 
-- 
 
--------------------------------------
 
on uudecode_file(str)
 
  if str starts "begin " then delete line 1 of str
 
  if str starts "," then delete char 1 of str
 
  
 
  repeat while true
 
    if [numtochar(10),numtochar(13)].getPos(str.char[str.length]) then
 
      delete the last char of str
 
      
 
    else if str.char[str.length-1..str.length]=numtochar(10)&"`" then
 
      delete char str.length-2 to str.length of str
 
      
 
    else if str.char[str.length-2..str.length]="end" then
 
      delete char str.length-2 to str.length of str
 
    else
 
      exit repeat
 
    end if
 
  end repeat
 
  
 
  -- if the last char of str = "`" then delete the last char of str
 
  -- if the last char of str = numtochar(10) then delete the last char of str
 
  
 
  ret = ""
 
  
 
  cnt = str.line.count
 
  repeat with k = 1 to cnt
 
    
 
    tLine = str.line[k]
 
    if tLine.char[1]=numtochar(10) then delete char 1 of tLine
 
    if k=cnt then len = chartonum(tLine.char[1])-32
 
    delete char 1 of tLine -- usually "M"
 
    
 
    tLine = str_replace("`"," ",tLine)
 
    
 
    chunk=""
 
    j = tLine.length
 
    i = 1
 
    
 
    repeat while true
 
      if i>j then exit repeat
 
      
 
      put numtochar( (chartonum(tLine.char[1+ i-1]) - 32) * 4 + (chartonum( tLine.char[1+ i]) - 32) / 16 ) after chunk
 
      put numtochar( (chartonum(tLine.char[1+ i  ]) mod 16 * 16) + (chartonum( tLine.char[1+ i+1]) - 32) / 4 ) after chunk
 
      put numtochar( (chartonum(tLine.char[1+ i+1]) mod 4 * 64) + (chartonum( tLine.char[1+ i+2]) - 32) ) after chunk
 
      
 
      i = i + 4
 
    end repeat
 
    
 
    if k=cnt then delete char len+1 to chunk.length of chunk
 
    
 
    put chunk after ret
 
  end repeat
 
  
 
  return ret 
 
end
 
 
 
----------------------------------------
 
-- replace in string
 
----------------------------------------
 
on str_replace (stringToFind, stringToInsert, input)
 
  output = ""
 
  findLen = stringToFind.length - 1
 
  repeat while true
 
    currOffset = offset(stringToFind, input)
 
    if currOffset=0 then exit repeat
 
    put input.char [1..currOffset] after output
 
    delete the last char of output
 
    put stringToInsert after output
 
    delete input.char [1.. (currOffset + findLen)]
 
  end repeat
 
  put input after output
 
  return output
 
end