Zeu*_*eus 8 c c++ macos fsevents
我需要为Mac中的文件夹创建FSEvents观察器.我对C++感到满意,有没有办法用C++代码而不是Objective-C来获取FSEvents通知.是否有一些示例代码开始和我需要包含的任何库..?
我已经在这个页面上了. http://developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html
但似乎只有Objective C,我可以有它的CPP版本
Vik*_*pov 15
是的,它可以用C.你应该寻找内核队列.
这是一个观察目录的小样本:
#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;
}
Run Code Online (Sandbox Code Playgroud)
细节可以在ch中找到.Mark Dalrymple的"高级Mac OSX编程"16(kqueues和FSEvents).其他信息可以在*BSD文档中找到kqueues.
或者使用FSEvents中的这个API(它主要是基于C的).
FSEventStreamRef FSEventStreamCreate (CFAllocatorRef allocator,
FSEventStreamCallback callback,
FSEventStreamContext *context,
CFArrayRef pathsToWatch,
FSEventStreamEventId sinceWhen,
CFTimeInterval latency,
FSEventStreamCreateFlags flags);
Run Code Online (Sandbox Code Playgroud)
使用pure-C回调创建FSEvents事件流.
然后使用.将此事件流附加到您的runloop
void FSEventStreamScheduleWithRunLoop (FSEventStreamRef streamRef,
CFRunLoopRef runLoop,
CFStringRef runLoopMode);
Run Code Online (Sandbox Code Playgroud)
是的,在这里你可能应该使用一行Obj-C来获取RunLoop句柄:使用-getCFRunLoop从NSRunLoop获取CFRunLoop
CFRunLoop* loopRef = [[NSRunLoop currentRunLoop] getCFRunLoop];
Run Code Online (Sandbox Code Playgroud)
或使用纯C调用
CFRunLoop* loopRef = CFRunLoopGetCurrent();
Run Code Online (Sandbox Code Playgroud)
使用启动事件流
Boolean FSEventStreamStart (FSEventStreamRef streamRef);
Run Code Online (Sandbox Code Playgroud)
使用停止事件流
void FSEventStreamStop (FSEventStreamRef streamRef);
Run Code Online (Sandbox Code Playgroud)
然后使用以下方法从runloop中取消计划:
void FSEventStreamUnscheduleFromRunLoop (FSEventStreamRef streamRef,
CFRunLoopRef runLoop,
CFStringRef runLoopMode);
Run Code Online (Sandbox Code Playgroud)
使流无效(清理):
void FSEventStreamInvalidate (FSEventStreamRef streamRef);
Run Code Online (Sandbox Code Playgroud)
希望这会让你开始.
| 归档时间: |
|
| 查看次数: |
4044 次 |
| 最近记录: |