-
Notifications
You must be signed in to change notification settings - Fork 3
/
File.bas
142 lines (106 loc) · 4.91 KB
/
File.bas
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
'-----------------------------------------------------------------------------------------------------------------------
' File management routines
' Copyright (c) 2024 Samuel Gomes
'-----------------------------------------------------------------------------------------------------------------------
$INCLUDEONCE
'$INCLUDE:'File.bi'
'-----------------------------------------------------------------------------------------------------------------------
' TEST CODE
'-----------------------------------------------------------------------------------------------------------------------
'$DEBUG
'CONST SEARCH_URL = "https://api.modarchive.org/downloads.php?moduleid="
'File_SetDownloaderProperties 0, 0
'DIM buffer AS STRING: buffer = File_Load("https://modarchive.org/index.php?request=view_random")
'DIM bufPos AS LONG: bufPos = INSTR(buffer, SEARCH_URL)
'IF bufPos > 0 THEN
' PRINT MID$(buffer, bufPos, INSTR(bufPos, buffer, CHR$(34)) - bufPos)
'END IF
'DIM fname AS STRING: fname = "C:\Users\samue\.gitconfig"
'DIM attr AS _UNSIGNED LONG: attr = File_GetAttributes(fname)
'PRINT "File size:"; File_GetSize(fname)
'PRINT "Attributes :"; HEX$(attr)
'PRINT _IIF(attr AND FILE_ATTRIBUTE_DIRECTORY, "Is directory", "Not a directory")
'PRINT _IIF(attr AND FILE_ATTRIBUTE_REGULAR_FILE, "Is a regular file", "Not a regular file")
'PRINT _IIF(attr AND FILE_ATTRIBUTE_READONLY, "Is read-only", "Not read-only")
'PRINT _IIF(attr AND FILE_ATTRIBUTE_HIDDEN, "Is hidden", "Not hidden")
'PRINT _IIF(attr AND FILE_ATTRIBUTE_ARCHIVE, "Is archive", "Not archive")
'PRINT _IIF(attr AND FILE_ATTRIBUTE_SYSTEM, "Is system", "Not system")
'PRINT "Modified time:"; File_GetModifiedTime(fname)
'END
'-----------------------------------------------------------------------------------------------------------------------
' Load a file from a file or URL
FUNCTION File_Load$ (PathOrURL AS STRING)
IF LEN(Pathname_GetDriveOrScheme(PathOrURL)) > 2 THEN
File_Load = File_LoadFromURL(PathOrURL)
ELSE
File_Load = File_LoadFromDisk(PathOrURL)
END IF
END FUNCTION
' Loads a whole file from disk into memory
FUNCTION File_LoadFromDisk$ (path AS STRING)
IF _FILEEXISTS(path) THEN
File_LoadFromDisk = _READFILE$(path)
END IF
END FUNCTION
' Loads a whole file from a URL into memory
FUNCTION File_LoadFromURL$ (url AS STRING)
SHARED __File AS __FileType
' Set default properties if this is the first time
IF NOT __File.initialized THEN File_SetDownloaderProperties __FILE_UPDATES_PER_SECOND_DEFAULT, __FILE_TIMEOUT_DEFAULT
__File.percentCompleted = 0
DIM h AS LONG: h = _OPENCLIENT("HTTP:" + url)
IF h <> NULL THEN
DIM startTick AS _UNSIGNED _INTEGER64: startTick = Time_GetTicks ' record the current tick
DIM AS STRING content, buffer
WHILE NOT EOF(h)
GET h, , buffer
content = content + buffer
IF __File.updatesPerSecond > 0 THEN _LIMIT __File.updatesPerSecond
IF __File.timeoutTicks > 0 AND (Time_GetTicks - startTick) > __File.timeoutTicks THEN EXIT WHILE
__File.percentCompleted = (LEN(content) / LOF(h)) * 100!
WEND
CLOSE h
File_LoadFromURL = content
END IF
END FUNCTION
' Changes the default settings for the HTTP downloader
SUB File_SetDownloaderProperties (updatesPerSecond AS _UNSIGNED LONG, timeoutSeconds AS _UNSIGNED LONG)
SHARED __File AS __FileType
__File.updatesPerSecond = updatesPerSecond
__File.timeoutTicks = 1000 * timeoutSeconds ' convert to ticks
__File.initialized = _TRUE
END SUB
' Save a buffer to a file
FUNCTION File_Save%% (buffer AS STRING, fileName AS STRING, overwrite AS _BYTE)
IF _FILEEXISTS(fileName) AND NOT overwrite THEN EXIT FUNCTION
_WRITEFILE fileName, buffer
File_Save = _TRUE
END FUNCTION
' Sub version of the above
SUB File_Save (buffer AS STRING, fileName AS STRING, overwrite AS _BYTE)
IF _FILEEXISTS(fileName) AND NOT overwrite THEN EXIT SUB
_WRITEFILE fileName, buffer
END SUB
' Copies file src to dst. Src file must exist and dst file must not
FUNCTION File_Copy%% (fileSrc AS STRING, fileDst AS STRING, overwrite AS _BYTE)
File_Copy = File_Save(File_Load(fileSrc), fileDst, overwrite)
END FUNCTION
' Sub version of the above
SUB File_Copy (fileSrc AS STRING, fileDst AS STRING, overwrite AS _BYTE)
File_Save File_Load(fileSrc), fileDst, overwrite
END SUB
' Returns the size of a file
FUNCTION File_GetSize&& (pathName AS STRING)
File_GetSize = __File_GetSize(String_ToCStr(pathName))
END FUNCTION
' Returns the attributes of a file / directory
' See FILE_ATTRIBUTE_* CONSTs
FUNCTION File_GetAttributes~& (pathName AS STRING)
File_GetAttributes = __File_GetAttributes(String_ToCStr(pathName))
END FUNCTION
' Returns the creation time of a file
FUNCTION File_GetModifiedTime&& (pathName AS STRING)
File_GetModifiedTime = __File_GetModifiedTime(String_ToCStr(pathName))
END FUNCTION
'$INCLUDE:'StringOps.bas'
'$INCLUDE:'Pathname.bas'