Function File_Services(@Service, @Params)
/************************************************************************************************
    This program is proprietary and is not to be used by or disclosed to others, nor is it to
    be copied without written permission from SRP Computer Solutions, Inc.

    Name        : File_Services

    Description : Service module input / output operations.
    
    Notes       : For more information visit https://docs.microsoft.com/en-us/windows/win32/api/fileapi/
                  For a list of Windows error messages visit:
                        https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
                        
    Parameters  :
        Service         [in] -- Name of the service being requested
        Params      [in/out] -- Service specific parameters
        Response       [out] -- Response to be sent back to the requesting procedure

    Metadata    :
    
    History     :   (Date, Initials, Notes)
        09/04/20    djs    Original programmer
        09/21/20    djs    Added CopyFile and GetFileSize services.
        
************************************************************************************************/
#pragma precomp SRP_PreCompiler

$insert SERVICE_SETUP
$insert LOGICAL
$insert FILE_SERVICES_EQUATES

Declare function   SRPWinAPI_CreateFile, SRPWinAPI_GetFileSize, SRPWinAPI_CopyFile, Direxists, DirList, SRP_Datetime, Dir
Declare subroutine SRPWinAPI_CloseHandle

GoToService else
	Error_Services('Set', Service : ' is not a valid service request within the ' : ServiceModule : ' services module.')
end

Return Response or ""

//-----------------------------------------------------------------------------
// Service Parameter Options
//-----------------------------------------------------------------------------
Options DESIRED_ACCESS       = GENERIC_READ$, GENERIC_WRITE$, GENERIC_EXECUTE$, GENERIC_ALL$
Options SHARE_MODE           = FILE_SHARE_NONE$, FILE_SHARE_DELETE$, FILE_SHARE_READ$, FILE_SHARE_WRITE$
Options CREATION_DISPOSITION = CREATE_ALWAYS$, CREATE_NEW$, OPEN_ALWAYS$, OPEN_EXISTING$, TRUNCATE_EXISTING$
Options FILE_ATTRIBUTES      = FILE_ATTRIBUTE_ARCHIVE$, FILE_ATTRIBUTE_ENCRYPTED$, FILE_ATTRIBUTE_HIDDEN$, FILE_ATTRIBUTE_NORMAL$, FILE_ATTRIBUTE_OFFLINE$, FILE_ATTRIBUTE_READONLY$ FILE_ATTRIBUTE_SYSTEM$, FILE_ATTRIBUTE_TEMPORARY$

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service LockFile(FilePath, DesiredAccess = DESIRED_ACCESS, ShareMode = SHARE_MODE, CreationDisposition = CREATION_DISPOSITION, FileAttributes = FILE_ATTRIBUTES)
   
    If FileAttributes  EQ '' then FileAttribute = FILE_ATTRIBUTE_NORMAL$
    SecurityAttributes = 0 ; // Pointer to security structure - not utilized by this service module at this time
    TemplateFile       = 0 ; // Handle to a template file     - not utilized by this service module at this time    
    FileHandle         = SRPWinAPI_CreateFile(FilePath, DesiredAccess, ShareMode, SecurityAttributes, CreationDisposition, FileAttributes, TemplateFile)
    Response           = FileHandle
    
End Service


Service UnlockFile(FileHandle)
    
    SRPWinAPI_CloseHandle(FileHandle)
*    Sleepery(30000)
    
End Service


Service GetFileSize(FileHandle)
    
    UnusedPointer = ''
    Response      = SRPWinAPI_GetFileSize(FileHandle, UnusedPointer)
    
end service


Service CopyFile(SourceFilepath, DestFilepath)
    
    SourceFilepath := \00\
    DestFilepath   := \00\
    Response        = SRPWinAPI_CopyFile(GetPointer(SourceFilepath), GetPointer(DestFilepath), 0)
    
end service

Service CheckFileExtension(FilePath, ExpectedExtension)
    If ExpectedExtension NE '' then
        If ExpectedExtension[1,1] NE '.' then
            ExpectedExtension = '.' : ExpectedExtension
        end
        ExtensionLen = Len(ExpectedExtension)
        If FilePath NE '' then
            LastCharPos  = Len(FilePath)
            CompareValue = FilePath[LastCharPos - (ExtensionLen - 1), ExtensionLen]
            If CompareValue EQ ExpectedExtension then
                Response = True$
            end else
                Response = False$
            end
        end else
            Error_Services('Add', 'Error in FILE_SERVICES -> CheckFileExtension, File path not provided.')
        end
    end else
        Error_Services('Add', 'Error in FILE_SERVICES -> CheckFileExtension, Comparison extension not provided.')
    end
end service

Service GetDirFileCount(Dir)
	Response = 0
	If dir NE '' then
		If DirExists(Dir) then
			InitDir Dir:'\*.*'
			FileList = DirList()
			Response = DCount(FileList, @FM)
		end else
			Error_Services('Add', 'Error in File_Services -> GetDirFileCount: Directory does not exist.')
		end
	end else
		Error_Services('Add', 'Error in File_Services -> GetDirFileCount: No directory specified.')
	end
end service

Service GetDirOldestFileDate(Dir)
	Response = ''
	If dir NE '' then
		If DirExists(Dir) then
			InitDir Dir:'\*.*'
			FileList = DirList()
			OldestFileDate = ''
			for each File in FileList using @FM
				If File NE '' then
					FilePath = Dir : '\' : File
					FileInfo = Dir(FilePath)
					FileDate = FileInfo<2>
					FileTime = FileInfo<3>
					FileDTM = SRP_Datetime('Combine', FileDate, FileTime)
					If FileDTM LT OldestFileDate OR OldestFileDate EQ '' then OldestFileDate = FileDTM
				end
			Next File
			Response = OldestFileDate
		end else
			Error_Services('Add', 'Error in File_Services -> GetDirOldestFileDate: Directory does not exist.')
		end
	end else
		Error_Services('Add', 'Error in File_Services -> GetDirOldestFileDate: No directory specified.')
	end
end service




