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

📄 file-defines.zc

📁 实现树形结构
💻 ZC
字号:
//[c]file-defines - OS specific definitions
//[c]
//[of]:license
//[c]    Code Browser - a folding text editor for programmers
//[c]    Copyright (C) 2003  Marc Kerbiquet
//[c]
//[c]    This program is free software; you can redistribute it and/or modify
//[c]    it under the terms of the GNU General Public License as published by
//[c]    the Free Software Foundation; either version 2 of the License, or
//[c]    (at your option) any later version.
//[c]
//[c]    This program is distributed in the hope that it will be useful,
//[c]    but WITHOUT ANY WARRANTY; without even the implied warranty of
//[c]    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//[c]    GNU General Public License for more details.
//[c]
//[c]    You should have received a copy of the GNU General Public License
//[c]    along with this program; if not, write to the Free Software
//[c]    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//[cf]
//[of]:imports
//[c]
import "base/types"
import "text/string"
import "text/string-buffer"
import "net/uri"

import "linux/unistd"
import "linux/stat"
import "linux/dirent"
import "config"

public [name="c"] import func getenv (string): string
//[cf]
//[c]
//[of]:path separator
//[c]
public equ path separator  = $/
//[cf]
//[c]
//[of]:convert full name (filename)
//[c]
public equ convert full name (filename: string)

	def p = filename
	repeat
		def c = p[]
		if is nul (c)
			break
		end

		if c == $\
			p[] = path separator
		end
		
		++p
	end

end
//[cf]
//[of]:is same file (fn1, fn2)
//[c]
public func is same file (fn1: string, fn2: string)

	return is equal (fn1, fn2)

end
//[cf]
//[of]:full name (filename, full name)
//[c]
public func full name (filename: string, full name: string buffer)

	def t := temp string buffer

	if filename [0] == $~ && 
		(filename [1] == nul char || filename [1] == $/)

		append no dot (t, getenv ("HOME"))
		filename += 1

	// already a fully qualified name ?
	elsif filename [0] <> $/

		def buf : [1024] char
		if not nil (getcwd (buf, 1024))
			append no dot (t, buf)
			t << path separator
		end

	end

	t << filename
	append no dot (full name, as string (t))
	release (t)

end
//[cf]
//[of]:resolve full name (directory, filename, full name)
//[c]
public func resolve full name (directory: string,  filename: string, full name: string buffer)

	def t := temp string buffer

	// already a fully qualified name ?
	if filename [0] <> $/
		t << directory << path separator
	end

	t << filename
	append no dot (full name, as string (t))
	release (t)

end
//[cf]
//[c]
//[of]:create directory (filename)
//[c]Creates a new directory
//[c]
//[c]Returns true if success
//[c]
public func create directory (path: string)

	equ mask (u: int, g: int, o: int) = ( (u<<6) | (g<<3) | o ) : mode_t

	def res = mkdir (path, mask (7, 5, 5))
	return res == 0

end
//[cf]
//[c]
//[of]:append home dir (string buffer, name)
//[c]
public func append home dir (s: string buffer, name: string)
	s << getenv ("HOME") << path separator << "." << name
end
//[cf]
//[of]:append root dir (string buffer, name)
//[c]
public func append root dir (s: string buffer, name: string)
	s << datadir << path separator << name
end
//[cf]
//[c]
//[of]:convert filename to uri (return uri, filename)
//[c]Converts a filename into an URL
//[c]
public func convert filename to uri (filename: string, return m: uri)

	initialize (m)

	def p = filename
	def s = size (filename)
	def path := temp string buffer

	repeat
		def c = p++[]
		if is nul (c)
			break
		end

		if c == $\
			path << $/
		else
			path << c
		end
		
	end

	set scheme (m, "file")
	set path (m, as string (path))
	
	release (path)

end
//[cf]
//[of]:convert uri to filename (return string buffer, uri)
//[c]Converts an uri to a filename
//[c]
public func convert uri to filename (uri: uri, return m: string buffer)

	initialize (m)

	m << path (uri)

end
//[cf]
//[c]
//[c]private:
//[of]:append no dot (string buffer, path)
//[c]Appends the path to s without any '/.' sequence
//[c]
private func append no dot (s: string buffer, path: string)

	def p = path
	repeat
		def c = p++ []
		if is nul (c)
			break
		elsif c == $/ && p[] == $. && (p[1] == $/ || is nul (p[1]))
			++p
		else
			s << c
		end
	end

end
//[cf]
//[c]
//[of]:directory
//[of]:definition
public struct directory
	private dirname: string
	private dir: dir
	private dirent: dirent
end
//[cf]
//[c]
//[of]:initialize - release
//[of]:directory (dirname)
//[c]
public func directory (dirname: string, return m: directory)
	initialize (m, dirname)
end
//[cf]
//[of]:initialize (m, dirname)
public func initialize (m: directory, dirname: string)
	dirname (m) = new string (dirname)
	dir (m) = nil
end
//[cf]
//[of]:release (m)
public func release (m: directory)
	delete (dirname (m))
end
//[cf]
//[cf]
//[of]:enumerating
//[of]:each file (m)
//[c]
public equ each file (m: directory)
	def file found = first (m)
	while file found
		if check file (m)
			yield (d_name (dirent (m)))
		end
		file found = next (m)
	end
	close (m)
end
//[cf]
//[of]:each directory (m)
//[c]
public equ each directory (m: directory)
	def file found = first (m)
	while file found
		if check directory (m)
			yield (d_name (dirent (m)))
		end
		file found = next (m)
	end
	close (m)
end
//[cf]
//[cf]
//[of]:private
//[of]:first (m)
private func first (m: directory)
	def d = opendir (dirname (m))
	dir (m) = d
	if is nil (d)
		return false
	end
	return next (m)
end
//[cf]
//[of]:next (m)
private func next (m: directory)
	def d = dir (m)
	def dirent = readdir (d)
	if is nil (dirent)
		return false
	end
	dirent (m) = dirent
	return true
end
//[cf]
//[of]:close (m)
private func close (m: directory)
	def d = dir (m)
	if  not nil (d)
		closedir (d)
	end
end
//[cf]
//[c]
//[of]:check file (m)
private func check file (m: directory)

	def d = dirent (m)
	def n = d_name (d)
	if n[0] == $. && (n[1] == nul char || (n[1] == $. && n[2] == nul char))
		return false
	end

	return d_type (d) <> DT_DIR:byte

end	
//[c]
//[cf]
//[of]:check directory (m)
private func check directory (m: directory)

	def d = dirent (m)
	def n = d_name (d)
	if n[0] == $. && (n[1] == nul char || (n[1] == $. && n[2] == nul char))
		return false
	end

	return d_type (d) == DT_DIR:byte

end	
//[c]
//[cf]
//[cf]
//[cf]
//[of]:filemask
//[of]:testing
//[of]:match wildcards (pattern, string)
//[c]Compares a string with a pattern having wildcards (*)
//[c]
public func match wildcards (pattern: string, value: string)

	def stacked p = nil : string
	def stacked q = nil : string

	def p = pattern
	def q = value
	
	repeat
		if p[] == $*
			p += 1
			stacked p = p
			stacked q = q
		end
		
		if q[] == nul char
			return p[] == nul char
		end
		
		if p[] == q[]
			p += 1
			q += 1
		else
			if is nil (stacked p)
				// no previous wild card: no match
				break
			end
			
			// assimilate char and restart
			stacked q += 1
			p = stacked p
			q = stacked q
		end
	end

	return false

end
//[cf]
//[cf]
//[cf]

⌨️ 快捷键说明

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