Content-type: text/html
/* include: */ #include <config.h> #include <CBufferCallback.h> /* to get: */ class CBufferCallback { public: // The constructor and destructor are just place holders for now. CBufferCallback() {} virtual ~CBufferCallback() {} int operator==(const CBufferCallback& rhs) const { return 1; } int operator!=(const CBufferCallback& rhs) const { return 0; } // Class functions: virtual void operator()(unsigned int nType, const void* pBuffer) = 0; }; /* In your code somewhere: */ void* gpTCLApplication; /* Compile sort of like this */ c++ -o yourprogram yourprogram.c -I$SpecTclHome/include -L$SpecTclHome/lib \ -lAnalysis -ltclPlus -lException -lBufferAnalysis
The CBufferCallback class is an abstract base class that you must create a derived class from in order to create callbacks for the C++ buffer analysis framework. See EXAMPLES for an example of how this is supposed to work. Be sure, as well, to see the man page for CBufferProcessor(3). If you are more comfortable programming to a C interface, check out the man page for BufferAnalysis(3).
Much of the error handling require for production software is omitted for this example for the sake of brevity.
The example registers a callback object on all buffer types from 0 through MAXBUFTYPE. It counts the number of buffers of each type and, and the end of the data file, prints out the histogram of buffer types with zeroes supressed.
#include <config.h> #include <CBufferCallback.h> #include <CBufferProcessor.h> #include <buftypes.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <Iostream.h> #ifdef HAVE_STD_NAMESPACE using namespace std; #endif class HistogramCallback : public CBufferCallback { private: int histogram[MAXBUFTYPE]; public: void clear() { memset(histogram, 0, sizeof(histogram)); } void dump(ostream& output) { for(int i=0; i < MAXBUFTYPE; i++) { if(histogram[i] != 0) { output << histogram[i] << " instances of buffer type: " << i << endl; } } } virtual void operator()(unsigned int ntype, const void* pBuffer) { histogram[ntype]++; } }; int main(int argc, char** argv) { unsigned short buffer[4096]; int fd = open(argv[1], O_RDONLY); int nread; CBufferProcessor processor; HistogramCallback callback; for(int i=0; i < MAXBUFTYPE; i++) { processor.addCallback(i, callback); } callback.clear(); while((nread = read(fd, buffer, sizeof(buffer))) > 0) { processor(buffer); } cout << "File : " << argv[1] << " run " << processor.runNumber() << " " << processor.Title() << endl; cout << "Run started: " << processor.runStartTime(); cout << " Run ended: " << processor.runEndTime() << endl; cout << "Buffer type distribution: ; callback.dump(cout); } void* gpTCLApplication;