⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]All functions to manipulate files
//[cf]
//[of]:imports
//[c]
import "base/types"
import "base/memory-allocator"
import "text/array-of-strings"
import "libc/stdio"
import "private/io"
//[c]
public import "file/file-defines"
//[cf]
//[c]
//[of]:filename
//[of]:constants
//[c]
//public equ path separator = path_separator
//[cf]
//[c]
//[of]:converting
//[of]:convert full name (filename)
//[c]
/*public equ convert full name (filename: string) =
	convert_full_name (filename)*/
//[cf]
//[of]:full name (filename, full name)
//[c]Platform dependent - See file-defines
//[cf]
//[of]:resolve full name (directory, filename, full name)
//[c]Platform dependent - See file-defines
//[cf]
//[c]
//[of]:convert filename to uri (filename)
//[c]Platform dependent - See file-defines
//[cf]
//[of]:convert uri to filename (uri)
//[c]Platform dependent - See file-defines
//[cf]
//[cf]
//[of]:accessing
//[of]:file size (filename)
//[c]
public equ file size (filename: [] char) = 
	file_size (filename)
//[cf]
//[of]:file last modification (filename)
//[c]
public equ file last modification (filename: [] char) =
	file_last_modification (filename)
//[cf]
//[cf]
//[of]:testing
//[of]:file exists (filename)
//[c]
public equ file exists (filename: [] char) =
	file_exists (filename) <> 0
//[cf]
//[of]:is file read only (filename)
//[c]
public equ is file read only (filename: [] char) =
	file_read_only (filename) <> 0
//[cf]
//[of]:is same file (fn1, fn2)
//[c]Platform dependent - See file-defines
//[cf]
//[cf]
//[cf]
//[of]:directory
//[of]:create directory (dirname)
//[c]Platform dependent - see file-defines
//[cf]
//[of]:append root dir (string buffer, name)
//[c]Platform dependent - see file-defines
//[cf]
//[of]:append home dir (string buffer, name)
//[c]Platform dependent - see file-defines
//[cf]
//[cf]
//[of]:file buffer
//[of]:constants
//[c]
public enum fb error

	fb ok
	fb file not found
	fb seek error
	fb not enough memory

end
//[cf]
//[of]:definition
//[c]
public struct file buffer

	base: [] byte
	size: size

end
//[cf]
//[c]
//[of]:initialize - release
//[of]:initialize (fb, filename, is text)
//[c]
public func initialize (buffer: file buffer, filename: string, is text: bool)

	base (buffer) = nil

	// Get file size
	def file size = file_size (filename)
	if file size == 0xFFFFFFFF
		return fb seek error
	end
	def buffer size = file size
	if is text
		++buffer size
	end
	
	// open the file
	def f = fopen (filename, "rb")
	if is nil (f)
		return fb file not found
	end

	// allocate memory
	def base = allocate memory (buffer size):[] byte
	if is nil (base) 
		return fb not enough memory
	end
		
	// read the file
	def res = fread (base, 1, file size, f)

	// close the file
	fclose (f)

	// append a zero if text
	if is text
		base[file size] = 0:byte
	end
	
	// setup object status and return
	base (buffer) = base
	size (buffer) = file size
	return fb ok

end
//[cf]
//[of]:release (fb)
//[c]
public func release (buffer: file buffer)

	if not nil (base (buffer))
		free memory (base (buffer))
	end

end
//[cf]
//[cf]
//[of]:accessing
//[of]:base (fb)
//[cf]
//[of]:size (fb)
//[cf]
//[cf]
//[cf]
//[of]:filemask
//[of]:testing
//[of]:match wildcards (pattern, string)
//[c]Platform dependent - See file-defines
//[cf]
//[cf]
//[cf]
//[of]:list of filemasks
//[of]:definition
public struct list of filemasks : local array of strings
	// empty
end
//[cf]
//[c]
//[of]:adding - removing
//[of]:add all (m, string)
//[c]Add filemasks from a string containing a list of masks separated by semicolons
//[c]
public func add all (m: list of filemasks, list: string)

	def p = list

	repeat
		p = skip blanks (p)
		if is nul (p[])
			break
		end
		
		def q = p
		while not nul (q[]) && q[]<>$;
			++q
		end
		def next = q
		
		// remove trailing blanks
		while q:[]byte > p:[]byte && is blank (q[-1])
			q -= 1
		end
		
		def size = q - p
		if size > 0
			add substring (m, p, size)
		end
		p = next
		if not nul (p[])
			++ p
		end
	end

end
//[cf]
//[cf]
//[of]:testing
//[of]:match (m, string)
//[c]
public func match (m: list of filemasks, name: string)
	each (m) ? mask
		if match wildcards (mask, name)
			return true
		end
	end
	return false
end
//[cf]
//[cf]
//[cf]
//[of]:io
//[of]:fputs stderr (s)
public equ fputs stderr (s: [] char) = 
	fputs_stderr (s)
//[cf]
//[cf]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -