<% 

'====================================================================================
'this two functions read_file and write_file provide wrapping
'for read and write file system objects for Microsoft Windows OS.
'
'if you migrate to a different platform, (Linux for example),
'you may replace this file with wrap file appropriate for a target
'platform without changing text of project which calls read_file and
'write_file;

'both function read_file and write_file 
'perform mapping to the current-context-folder.
'therefore, full path will look: current-context-folder\wichFN_not_mapped

function read_file( whichFN_not_mapped )
'====================================================================================
  dim mapped, map
  dim FullName
  dim fstemp, filetemp  
  dim excep
  
  on error resume next

  read_file = ""
  map ="."

  if whichFN_not_mapped = "" then 
     err.raise 200,, "Empty path in ""read_file"" procedure" 
  else

       '-----------------------------------------------------------------
       '       mapping 
       '-----------------------------------------------------------------
       mapped = Server.Mappath(map) 	
       'response.write "responding mapped=" & mapped & "<br>"
       FullName = mapped & "\" & whichFN_not_mapped
       '-----------------------------------------------------------------
       
       'response.write "not responding complex=" & FullName & "<br>"
       Set fstemp   = server.CreateObject("Scripting.FileSystemObject")
       if err.number > 0 then
          excep = "When creating file system object for file" & ve & _
                  FullName & ve & _
		  err.number & err.description 
          err.raise 6666,, excep
       else
          Set filetemp = fstemp.OpenTextFile(FullName)
          if err.number > 0  then
	    excep = "When opening text file" & ve & _
                  FullName & ve & _
		  err.number & err.description 
            err.raise 6666,, excep
          else
	     read_file = filetemp.ReadAll
             if err.number > 0 then 
		excep = "When reading the file" & ve & _
                  FullName & ve & _
		  err.number & err.description 
	        err.raise 6666,, excep
             else
                filetemp.Close
                set filetemp=nothing
		set fstemp=nothing
		exit function
             end if
          end if
       end if
   end if   

   set filetemp=nothing
   set fstemp=nothing
	
end function





'====================================================================================
'Maps unmapped file to a current directory ".".
'Then writes text file "text_body" into this file.
'If no success, raises exception 6666.
'
function write_file( whichFN_not_mapped , text_body )
'====================================================================================
dim mapped, map, whichFN
write_file = ""
dim fstemp, filetemp  

on error resume next
   
if whichFN_not_mapped = "" then 
   excep = "Cannot write to a file because it is empty."
   err.raise 6666,, excep
   exit function
end if


Set fstemp   = server.CreateObject("Scripting.FileSystemObject")
if err.number > 0 then
    excep = "When creating FileSystemObject for file" & ve & _
             whichFN_not_mapped & ve & _
             err.number & err.description 
    err.raise 6666,, excep
    exit function
end if

      map ="."
      '-----------------------------------------------------------------
      '       mapping 
      '-----------------------------------------------------------------
      mapped = Server.Mappath(map) 	
      whichFN =  mapped & "\" & whichFN_not_mapped
      'response.write "in write, full name=" & whichfn & "<br>"
      '-----------------------------------------------------------------
    
      Set filetemp = fstemp.CreateTextFile(whichFN, true)
      if err.number > 0 then
            excep = "When creating text file " & ve & _
                  whichFN & ve & _
		  err.number & err.description 
            err.raise 6666,, excep
      else
         filetemp.WriteLine( text_body )
         if err.number > 0 then 
	    excep = "When writing line to file" & ve & _
                  whichFN & ve & _
	          err.number & err.description 
            err.raise 6666,, excep
      	 else
            filetemp.Close
            if err.number > 0 then 
               excep = "When closing text file" & ve & _
                  whichFN & ve & _
		  err.number & err.description 
               err.raise 6666,, excep
            end if
         end if
      end if


'maybe redundant:
'   set filetemp=nothing
'   set fstemp=nothing

end function


'=======================================================
'returns list of files in the folder
'referenced by full path as 
'appropriate for this operating system;
'
'INPUT: folderspec - full path
'OUTPUT: returns: ShowFileList - list of files surrounded
'					   "with" vbcrlf
'-------------------------------------------------------
Function ShowFileList(folderspec)
    Dim fs, f, fl, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    'p_lsl " folder: "  &  f
    Set fc = f.Files
    'p_sl "count:"  & cstr(fc.count)
    s = vbcrlf
    For Each fl in fc
        s = s & vbcrlf & fl.name
    Next
    s = s & vbcrlf
    ShowFileList = s
    'MsgBox s  'permission denied? when can it work?
End Function

'=====================================
'the same as ShowFileList, but path is
'relative to web page;
'-------------------------------------
Function vir_ShowFileList(folderspec)
  dim mapped
  'mapped = Server.Mappath(".") & "\" & folderspec
  mapped  = Server.Mappath(folderspec)
  vir_ShowFileList = ShowFileList(mapped)
end function

%>