text_thread.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:3k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef text_thread_h
- #define text_thread_h
- #include "mutex.h"
- /** structure that holds text and associated timestamp
- */
- struct text_data
- {
- std::string text;
- /** time in string form */
- std::string s_timestamp;
- text_data( const std::string &s ) : text( s )
- {
- s_timestamp.resize( 100 );
- # ifdef WIN32
- SYSTEMTIME st;
- GetSystemTime( &st );
- int l = _snprintf( (char *)s_timestamp.c_str(), s_timestamp.capacity(),
- "%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay,
- st.wHour, st.wMinute, st.wSecond ) + 1;
- # endif
- s_timestamp.resize( l );
- }
- text_data( const text_data &d ) : text( d.text ),
- s_timestamp( d.s_timestamp )
- {
- }
- const std::string &get_time() const { return s_timestamp; }
- const std::string &get_text() const { return text; }
- };
- /** hide all comport I/O complexity
- */
- class comport
- {
- std::string portname;
- /** open this with CreateFile */
- HANDLE port_h;
- /** mutex to lock shutdown procedure */
- mutex shutdown_mutex;
- /** allows background data loading */
- OVERLAPPED os;
- /** this var just keep the status of previous call of wait_comport_event()
- */
- DWORD dwEvtMask;
- /** internal buffer where get_data function places intermediate data */
- std::string internal_buffer;
-
- public:
- /** arg is com1 or com2. Do not connect immediately */
- comport( const std::string &comport_name );
- /** init only there */
- void init();
- void shutdown();
- ~comport();
- /** this is a blocking call which does not returns until some activity on
- port happens. We can do that because this thread have nothing to do
- if no data in port. It returns true if new data is awailable. It
- happens that it returns false if new event is coming but this is
- not a data event
- */
- bool wait_comport_event();
- /** fill given structures with data. If it happens that data from comport
- are invalid or end-of-line is not reached yet - return NULL.
- The structure is allocated inside this function and pointer is
- released - you are responsible to clean-up it
- */
- text_data *get_data();
- };
- /** thread class to work with closed captions grabber */
- class text_thread
- {
- comport *com_port;
- bool shutdown_now;
- /** mutex to lock shutdown procedure */
- mutex shutdown_mutex;
- public:
- /** should take the comport number from args. Do nothing else */
- text_thread( int argc, char **argv );
- ~text_thread()
- {
- shutdown();
- }
- static void text_thread_entry_func( void *_instance );
- void init()
- {
- com_port->init();
- }
- void shutdown()
- {
- wait_for_mutex wm( shutdown_mutex, 3 );
-
- if( com_port == NULL )
- return;
- com_port->shutdown();
- delete com_port;
- com_port = NULL;
- }
- void loop();
- };
- extern text_thread *textThread;
- #endif // text_thread_h