/
FileIO.prg
144 lines (127 loc) · 4.5 KB
/
FileIO.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Copyright (c) XSharp B.V. All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
//
// File and Disk IO Functions
USING System
USING System.Collections
USING System.IO
USING System.Linq
USING System.Runtime.InteropServices
USING System.Security
USING System.Runtime
USING System.Runtime.ConstrainedExecution
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/frename/*" />
FUNCTION FRename( cOldFile AS STRING , cNewFile AS STRING) AS LOGIC
LOCAL renamed := FALSE AS LOGIC
TRY
IF String.IsNullOrEmpty(cOldFile)
BadFileParam(__FUNCTION__, nameof(cOldFile), 1)
RETURN FALSE
ENDIF
IF String.IsNullOrEmpty(cNewFile)
BadFileParam(__FUNCTION__, nameof(cNewFile), 2)
RETURN FALSE
ENDIF
XSharp.IO.File.ClearErrorState()
IF System.IO.File.Exists(cOldFile)
System.IO.File.Move(cOldFile, cNewFile)
renamed := TRUE
ELSE
THROW FileNotFoundException { cOldFile }
ENDIF
CATCH e AS Exception
XSharp.IO.File.SetErrorState(e)
END TRY
RETURN renamed
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ferase/*" />
FUNCTION FErase(cFileName AS STRING) AS LOGIC
LOCAL isDeleted := FALSE AS LOGIC
TRY
IF String.IsNullOrEmpty(cFileName)
BadFileParam(__FUNCTION__, nameof(cFileName), 1)
RETURN FALSE
ENDIF
XSharp.IO.File.ClearErrorState()
IF System.IO.File.Exists(cFileName)
System.IO.File.Delete(cFileName)
isDeleted := TRUE
ELSE
THROW FileNotFoundException { cFileName }
ENDIF
CATCH e AS Exception
XSharp.IO.File.SetErrorState(e)
END TRY
RETURN isDeleted
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fcopy/*" />
FUNCTION FCopy(cSourceFile AS STRING,cTargetFile AS STRING) AS LOGIC
RETURN FCopy(cSourceFile, cTargetFile, TRUE)
/// <inheritdoc cref="FCopy" />
/// <param name="lOverWrite">Should the target file be overwritten.</param>
FUNCTION FCopy(cSourceFile AS STRING,cTargetFile AS STRING, lOverWrite AS LOGIC) AS LOGIC
LOCAL IsCopied := FALSE AS LOGIC
TRY
XSharp.IO.File.ClearErrorState()
IF String.IsNullOrEmpty(cSourceFile)
BadFileParam(__FUNCTION__, nameof(cSourceFile), 1)
RETURN FALSE
ENDIF
IF String.IsNullOrEmpty(cTargetFile)
BadFileParam(__FUNCTION__, nameof(cTargetFile), 2)
RETURN FALSE
ENDIF
System.IO.File.Copy(cSourceFile,cTargetFile,lOverWrite)
IsCopied := TRUE
CATCH e AS Exception
XSharp.IO.File.SetErrorState(e)
END TRY
RETURN IsCopied
/// <summary><include file="VoFunctionDocs.xml" path="Runtimefunctions/splitpath/summary" /></summary>
/// <returns><include file="VoFunctionDocs.xml" path="Runtimefunctions/splitpath/returns" /></returns>
/// <remarks><include file="VoFunctionDocs.xml" path="Runtimefunctions/splitpath/remarks" /></remarks>
/// <param name="cPath">The path name to break.</param>
/// <param name="cDrive">The drive letter followed by a colon. </param>
/// <param name="cDir">The directories, including the trailing slash. Forward slashes and backslashes both may be present in <cPath>. Forward slashes (/) are converted to backslashes (\). </param>
/// <param name="cName">The file name, without the extension. </param>
/// <param name="cExt">The extension, including the leading period. </param>
FUNCTION _SplitPath(cPath AS STRING, cDrive OUT STRING,cDir OUT STRING,cName OUT STRING,cExt OUT STRING) AS VOID
LOCAL nPos AS LONG
LOCAL cSeparators AS STRING
LOCAL lDotAfterDirSep := FALSE AS LOGIC
cDrive := ""
cDir := ""
cName := ""
cExt := ""
IF String.IsNullOrEmpty(cPath)
RETURN
ENDIF
cSeparators := "\/"
IF cSeparators:IndexOf(Path.DirectorySeparatorChar) == -1
cSeparators += Path.DirectorySeparatorChar:ToString()
END IF
IF cPath:Length >= 2 .and. cPath[1] == Path.VolumeSeparatorChar // What VO does
cDrive := cPath:Substring(0, 2)
cPath := cPath:Substring(2)
ENDIF
nPos := -1
FOREACH cSep AS Char IN cSeparators
nPos := Math.Max(nPos, cPath:LastIndexOf(cSep))
NEXT
IF cPath:LastIndexOf('.') > nPos
lDotAfterDirSep := TRUE
END IF
IF nPos != -1
cDir := cPath:Substring(0, nPos+1)
cName := cPath:Substring(nPos + 1)
ELSE
cName := cPath
ENDIF
IF lDotAfterDirSep
nPos := cName:LastIndexOf('.')
IF nPos != -1 // should be always true
cExt := cName:Substring(nPos)
cName := cName:Substring(0, nPos)
END IF
ENDIF
RETURN