8

MacでフォルダのFSEventsウォッチャーを作成する必要があります。私はC++に慣れており、Objective-CではなくC++コードでFSEvents通知を取得する方法があります。開始するためのサンプルコードと、含める必要のあるライブラリはありますか..?

私はすでにこのページにいます。 http://developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html

しかし、Objective Cしかないようですが、CPPバージョンを入手できますか

4

1 に答える 1

16

はい、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;
}

詳細はchにあります。MarkDalrympleによる「AdvancedMacOSXProgramming」の16(kqueuesおよびFSEvents)。追加情報は、kqueuesの*BSDドキュメントにあります。

または、FSEventsからこのAPIを使用します(ほとんどがCベースです)。

FSEventStreamRef FSEventStreamCreate (CFAllocatorRef allocator,
                                  FSEventStreamCallback callback,
                                  FSEventStreamContext *context,
                                  CFArrayRef pathsToWatch,
                                  FSEventStreamEventId sinceWhen,
                                  CFTimeInterval latency,
                                  FSEventStreamCreateFlags flags);

pure-Cコールバックを使用してFSEventsイベントストリームを作成します。

次に、このイベントストリームをrunloopにアタッチします。

void FSEventStreamScheduleWithRunLoop (FSEventStreamRef streamRef,
                                   CFRunLoopRef runLoop,
                                   CFStringRef runLoopMode);

はい、ここではおそらくObj-Cの行を使用してRunLoopハンドルを取得する必要があります。-getCFRunLoopを使用してNSRunLoopからCFRunLoopを取得します

CFRunLoop* loopRef = [[NSRunLoop currentRunLoop] getCFRunLoop];

または純粋なC呼び出しを使用します

CFRunLoop* loopRef =  CFRunLoopGetCurrent();

でイベントストリームを開始します

Boolean FSEventStreamStart (FSEventStreamRef streamRef);

でイベントストリームを停止します

void FSEventStreamStop (FSEventStreamRef streamRef);

次に、次のようにしてrunloopからスケジュールを解除します。

void FSEventStreamUnscheduleFromRunLoop (FSEventStreamRef streamRef,
                                     CFRunLoopRef runLoop,
                                     CFStringRef runLoopMode);

ストリームを無効にします(クリーンアップ):

void FSEventStreamInvalidate (FSEventStreamRef streamRef);

これで始められることを願っています。

于 2012-07-19T08:56:28.097 に答える