Commit 10dbb7c9 by Francois Gygi

Add testlock.C and testreassign.C


git-svn-id: http://qboxcode.org/svn/qb/trunk@1919 cba15fb0-1239-40c8-b417-11db7ca47a34
parent fcd36ea9
//
// testlock.C
//
// test the creation and removal of a lock file using the C library
//
#include <stdio.h> // fopen, fclose, fprintf
#include <sys/stat.h> // stat()
#include <unistd.h> // 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 );
}
//
// 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 <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment