diff --git a/util/qbdriver/testlock.C b/util/qbdriver/testlock.C new file mode 100644 index 0000000..921d1cc --- /dev/null +++ b/util/qbdriver/testlock.C @@ -0,0 +1,41 @@ +// +// testlock.C +// +// test the creation and removal of a lock file using the C library +// + +#include // fopen, fclose, fprintf +#include // stat() +#include // fsync() +int main() +{ + // create a lock file "lock.dat" + FILE *lockfile = fopen("lock.dat","w"); + fprintf(lockfile,"1"); + fclose(lockfile); + fsync(fileno(lockfile)); + + // test for the presence of the lock file + struct stat statbuf; + int status; + do + { + // stat returns 0 if the file exists + status = stat("lock.dat",&statbuf); + usleep(100000); + } + while ( status != 0 ); + + // remove the lock file + remove("lock.dat"); + usleep(100000); + + // test for the absence of the file + do + { + // stat returns 0 if the file exists + status = stat("lock.dat",&statbuf); + usleep(100000); + } + while ( status == 0 ); +} diff --git a/util/qbdriver/testreassign.C b/util/qbdriver/testreassign.C new file mode 100644 index 0000000..d2d1b14 --- /dev/null +++ b/util/qbdriver/testreassign.C @@ -0,0 +1,50 @@ +// +// testreassign.C +// +// test the functionality of: +// - reassign the streambuf of std::cout to an ostringstream +// - write and sync using C library functions +// +// This program tests the functionality needed in UserInterface when +// operating in server mode. In that case, the std::cout stream is redirected +// to an ostringstream. The contents of the ostringstream are written at the +// end using C library functions, which allow for the use of the fsync() call. +// + +#include +#include +#include +#include +int main() +{ + // write something to std::cout before reassign + std::cout << "initial write on std::cout" << std::endl; + + // streambuf pointers + std::streambuf *qbout_buf; + std::streambuf *cout_buf; + + // save copy of cout streambuf + cout_buf = std::cout.rdbuf(); + // create an ostringstream + std::ostringstream os; + qbout_buf = os.rdbuf(); + // redirect std::cout to os + std::cout.rdbuf(qbout_buf); + + // the following output should go to the ostringstream + std::cout << " output written to cout after redirect" << std::endl; + std::cout.flush(); + + // write contents of os to file "out.txt" + FILE *qboutfile = fopen("out.txt","w"); + fprintf(qboutfile,"%s",os.str().c_str()); + fclose(qboutfile); + fsync(fileno(qboutfile)); + + // restore cout streambuf + std::cout.rdbuf(cout_buf); + + // write more output to std::cout + std::cout << "more output on std::cout" << std::endl; +}