On 31 Jul 2008, at 18:29, Niklas B. wrote:
platform-native functions (for example COM on windows)?
C:>irb
require ‘win32/dir’
=> true
Dir::PROFILE
=> “C:\Documents and Settings\gthiesfeld”
Thanks that’ll help alot! Is something similiar available on linux as
well?
Dunno about linux, but here’s something that will work on mac os x.
The way mac os x tracks things like disk is a combination of a domain
and a folder type.
Domains are thinks like user domain, local domain etc…, folder types
are things like ‘applications folder’, ‘library folder’ and so on.
So (in C) the user’s library folder (/Users/fred/Library in my case)
is identified by kUserDomain, kDomainLibraryFolderType and the top
level one (/Library) is identified by
kLocalDomain, kDomainLibraryFolderType
The following code uses rubyinline to call some C code. This doesn’t
seem to work if you paste it into irb, but as a standalone ruby file
runs fine
require ‘rubygems’
gem ‘RubyInline’
require ‘inline’
class Folder
module Domains
USER = -32763
SYSTEM_DISK = -32768
SYSTEM = -32766
LOCAL = -32765
end
module Types
def self.int_for_code code
code.unpack(‘N’).first
end
TOP = int_for_code 'dtop' #more of these constants in Folders.h
DOCUMENTS = int_for_code 'docs'
end
inline do |builder|
builder.include “<CoreServices/CoreServices.h>”
builder.add_compile_flags “-framework CoreServices”
builder.c_singleton "
static char find(int domain, unsigned long folder_type){
char c_path[PATH_MAX];
FSRef folder;
if(noErr == FSFindFolder(domain, folder_type, true, &folder)){
if(noErr ==FSRefMakePath(&folder, (UInt8)c_path, PATH_MAX))
return c_path;
}
/* decide what you want to do in case of failure */
return “err”;
}
"
end
end
puts Folder::find(Folder::Domains::USER, Folder::Types::DOCUMENTS)
#should output the user’s documents folder
puts Folder::find(Folder::Domains::USER, Folder::Types::TOP) #should
output the top level of the user domain, ie the home folder