FSEvents C ++示例 [英] FSEvents C++ Example
问题描述
我需要创建FSEvents观察家于Mac的文件夹。我很舒服与C ++和是否有一种方式来获得在C ++中code,而不是Objective-C的FSEvents通知。有一些例如code开始与任何库,我需要包括..?
I need to create FSEvents watcher for a Folder in Mac. I'm comfortable with C++ and is there a way to get FSEvents notifications in C++ code, rather than Objective-C. Is there some example code to start with and any libraries i need to include ..?
我已经在此页面上。
<一href=\"http://developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html\">http://developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html
但是,似乎只有目标C,我可以有它的CPP版
But there seems to be only Objective C, can i have CPP version of it
推荐答案
是的,这是可能的C.你应该寻找内核队列。
Yes, it is possible in C. You should look for Kernel Queues.
下面是一个小样本看目录:
Here's a small sample to watch the directory:
#include <errno.h> // for errno
#include <fcntl.h> // for O_RDONLY
#include <stdio.h> // for fprintf()
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h> // for strerror()
#include <sys/event.h> // for kqueue() etc.
#include <unistd.h> // for close()
int main (int argc, const char *argv[])
{
int kq = kqueue ();
// dir name is in argv[1], NO checks for errors here
int dirfd = open (argv[1], O_RDONLY);
struct kevent direvent;
EV_SET (&direvent, dirfd, EVFILT_VNODE, EV_ADD | EV_CLEAR | EV_ENABLE,
NOTE_WRITE, 0, (void *)dirname);
kevent(kq, &direvent, 1, NULL, 0, NULL);
// Register interest in SIGINT with the queue. The user data
// is NULL, which is how we'll differentiate between
// a directory-modification event and a SIGINT-received event.
struct kevent sigevent;
EV_SET (&sigevent, SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, NULL);
// kqueue event handling happens after the legacy API, so make
// sure it doesn eat the signal before the kqueue can see it.
signal (SIGINT, SIG_IGN);
// Register the signal event.
kevent(kq, &sigevent, 1, NULL, 0, NULL);
while (1) {
// camp on kevent() until something interesting happens
struct kevent change;
if (kevent(kq, NULL, 0, &change, 1, NULL) == -1) { exit(1); }
// The signal event has NULL in the user data. Check for that first.
if (change.udata == NULL) {
break;
} else {
// udata is non-null, so it's the name of the directory
printf ("%s\n", (char*)change.udata);
}
}
close (kq);
return 0;
}
详细信息可以在章找到。 16(kqueues和FSEvents)先进的Mac OSX编程由马克达尔林普尔的。附加信息可以为kqueues * BSD文档中找到。
The details can be found in ch. 16 (kqueues and FSEvents) of "Advanced Mac OSX Programming" by Mark Dalrymple. The additional info may be found in *BSD documentation for kqueues.
或者使用从这个FSEvents API(这主要是基于C语言)。
Or use this API from FSEvents (it's mostly C-based).
FSEventStreamRef FSEventStreamCreate (CFAllocatorRef allocator,
FSEventStreamCallback callback,
FSEventStreamContext *context,
CFArrayRef pathsToWatch,
FSEventStreamEventId sinceWhen,
CFTimeInterval latency,
FSEventStreamCreateFlags flags);
打造纯-C回调FSEvents事件流。
to create the FSEvents event stream with pure-C callback.
那么这个事件流使用连接到您runloop的
Then attach this event stream to your runloop using the
void FSEventStreamScheduleWithRunLoop (FSEventStreamRef streamRef,
CFRunLoopRef runLoop,
CFStringRef runLoopMode);
是的,在这里你应该使用线路的OBJ-C来获得RunLoop手柄:使用-getCFRunLoop得到一个NSRunLoop的CFRunLoop
Yes, here you probably should use a line of Obj-C to get the RunLoop handle: get the CFRunLoop from an NSRunLoop by using -getCFRunLoop
CFRunLoop* loopRef = [[NSRunLoop currentRunLoop] getCFRunLoop];
或使用纯C呼叫
CFRunLoop* loopRef = CFRunLoopGetCurrent();
开始的事件流
Boolean FSEventStreamStart (FSEventStreamRef streamRef);
与停止事件流
void FSEventStreamStop (FSEventStreamRef streamRef);
然后取消调度它与这个runloop:
And then unschedule it from the runloop with this:
void FSEventStreamUnscheduleFromRunLoop (FSEventStreamRef streamRef,
CFRunLoopRef runLoop,
CFStringRef runLoopMode);
无效流(清理):
Invalidate the stream (cleanup):
void FSEventStreamInvalidate (FSEventStreamRef streamRef);
希望这将让你开始。
Hope this will get you started.
这篇关于FSEvents C ++示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!