tesseract
3.03
|
00001 00002 // File: svutil.h 00003 // Description: ScrollView Utilities 00004 // Author: Joern Wanke 00005 // Created: Thu Nov 29 2007 00006 // 00007 // (C) Copyright 2007, Google Inc. 00008 // Licensed under the Apache License, Version 2.0 (the "License"); 00009 // you may not use this file except in compliance with the License. 00010 // You may obtain a copy of the License at 00011 // http://www.apache.org/licenses/LICENSE-2.0 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // 00019 // 00020 // SVUtil contains the SVSync, SVSemaphore, SVMutex and SVNetwork 00021 // classes, which are used for thread/process creation & synchronization 00022 // and network connection. 00023 00024 #ifndef TESSERACT_VIEWER_SVUTIL_H__ 00025 #define TESSERACT_VIEWER_SVUTIL_H__ 00026 00027 #ifdef _WIN32 00028 #ifndef __GNUC__ 00029 #include <windows.h> 00030 #define snprintf _snprintf 00031 #if (_MSC_VER <= 1400) 00032 #define vsnprintf _vsnprintf 00033 #endif 00034 #pragma warning(disable:4786) 00035 #else 00036 #include "platform.h" 00037 #include <windows.h> 00038 #endif 00039 #else 00040 #include <pthread.h> 00041 #include <semaphore.h> 00042 #endif 00043 00044 #include <string> 00045 00046 #ifndef MAX 00047 #define MAX(a, b) ((a > b) ? a : b) 00048 #endif 00049 00050 #ifndef MIN 00051 #define MIN(a, b) ((a < b) ? a : b) 00052 #endif 00053 00055 class SVSync { 00056 public: 00058 static void StartThread(void *(*func)(void*), void* arg); 00060 static void ExitThread(); 00062 static void StartProcess(const char* executable, const char* args); 00063 }; 00064 00067 class SVSemaphore { 00068 public: 00070 SVSemaphore(); 00072 void Signal(); 00074 void Wait(); 00075 private: 00076 #ifdef _WIN32 00077 HANDLE semaphore_; 00078 #elif defined(__APPLE__) 00079 sem_t *semaphore_; 00080 #else 00081 sem_t semaphore_; 00082 #endif 00083 }; 00084 00087 class SVMutex { 00088 public: 00090 SVMutex(); 00092 void Lock(); 00094 void Unlock(); 00095 private: 00096 #ifdef _WIN32 00097 HANDLE mutex_; 00098 #else 00099 pthread_mutex_t mutex_; 00100 #endif 00101 }; 00102 00107 class SVNetwork { 00108 public: 00110 SVNetwork(const char* hostname, int port); 00111 00113 ~SVNetwork(); 00114 00116 void Send(const char* msg); 00117 00120 char* Receive(); 00121 00123 void Close(); 00124 00126 void Flush(); 00127 00128 private: 00130 SVMutex* mutex_send_; 00132 int stream_; 00134 char* msg_buffer_in_; 00135 00137 std::string msg_buffer_out_; 00138 00139 bool has_content; // Win32 (strtok) 00141 char* buffer_ptr_; // Unix (strtok_r) 00142 }; 00143 00144 #endif // TESSERACT_VIEWER_SVUTIL_H__