//[c]Utility function to manipulate files and filenames //[c] //[of]:license //[c] Code Browser - a folding text editor for programmers //[c] Copyright (C) 2003-07 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 import "base/types" import "text/string" import "text/string-buffer" import "file/file" import "libc/stdio" //[c] //[cf] //[c] //[of]:create directories (filename) //[c]Create all intermediary directories for the given filename //[c] //[c]NOTE: no result code returned //[c] public func create directories (filename: string) : void def path := temp string buffer append file path (path, filename) def parent = as string (path) if not empty (parent) && // hack for windows: detect drive (parent[1] <> $: || not nul (parent[2])) create directories (parent) create directory (parent) end release (path) end //[cf] //[of]:append file path (string buffer, filename) //[c]Gets the path of a filename //[c] public func append file path (stream: string buffer, filename: string) def occurence = last occurrence (filename, path separator) if not nil (occurence) append (stream, filename, occurence - filename) end end //[cf] //[of]:append base name (string buffer, filename) //[c]Gets the base name of a filename //[c] public func append base name (stream: string buffer, filename: string) def occurence = last occurrence (filename, path separator) if not nil (occurence) append (stream, occurence + 1) else append (stream, filename) end end //[cf] //[of]:append name (string buffer, filename) //[c]Gets the base name of a filename without extension //[c] public func append name (stream: string buffer, filename: string) def occurrence = last occurrence (filename, path separator) if not nil (occurrence) def p = occurrence + 1 def q = last occurrence (p, $.) if not nil (q) append (stream, p, q - p) else append (stream, p) end else append (stream, filename) end end //[cf] //[c] //[of]:resource filename (name) //[c]Returns the fullname of a filename next to the executable. //[c] public func resource filename (name: string, return s: string buffer) // try home first user resource filename (name, s) def f = fopen (as string (s), "r") if not nil (f) fclose (f) return end release (s) // use program dir program resource filename (name, s) end //[cf] //[of]:user resource filename (name) //[c]Returns the fullname of a user resource //[c] public func user resource filename (name: string, return s: string buffer) temp string buffer (s) append home dir (s, "code-browser") s << path separator << name end //[cf] //[of]:program resource filename (name) //[c]Returns the fullname of a program resource (next to the executable) //[c] public func program resource filename (name: string, return s: string buffer) temp string buffer (s) append root dir (s, "code-browser") s << path separator << name end //[cf] //[of]:user resource filename (path, name) //[c]Returns the fullname of a user resource //[c] public func user resource filename (path: string, name: string, return s: string buffer) temp string buffer (s) append home dir (s, "code-browser") s << path separator << path << path separator << name end //[cf] //[of]:program resource filename (path, name) //[c]Returns the fullname of a program resource (next to the executable) //[c] public func program resource filename (path: string, name: string, return s: string buffer) temp string buffer (s) append root dir (s, "code-browser") s << path separator << path << path separator << name end //[cf] //[c] //[of]:file exists (name) /*public func file exists (name: string) def f = fopen (filename, "r") if not nil (f) fclose (f) end return not nil (f) end*/ //[cf]