Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
svutil.h
Go to the documentation of this file.
1 
2 // File: svutil.h
3 // Description: ScrollView Utilities
4 // Author: Joern Wanke
5 // Created: Thu Nov 29 2007
6 //
7 // (C) Copyright 2007, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 //
20 // SVUtil contains the SVSync, SVSemaphore, SVMutex and SVNetwork
21 // classes, which are used for thread/process creation & synchronization
22 // and network connection.
23 
24 #ifndef TESSERACT_VIEWER_SVUTIL_H__
25 #define TESSERACT_VIEWER_SVUTIL_H__
26 
27 #ifdef _WIN32
28 #ifndef __GNUC__
29 #include <windows.h>
30 #define snprintf _snprintf
31 #if (_MSC_VER <= 1400)
32 #define vsnprintf _vsnprintf
33 #endif
34 #pragma warning(disable:4786)
35 #else
36 #include "platform.h"
37 #include <windows.h>
38 #endif
39 #else
40 #include <pthread.h>
41 #include <semaphore.h>
42 #endif
43 
44 #include <string>
45 
46 #ifndef MAX
47 #define MAX(a, b) ((a > b) ? a : b)
48 #endif
49 
50 #ifndef MIN
51 #define MIN(a, b) ((a < b) ? a : b)
52 #endif
53 
55 class SVSync {
56  public:
58  static void StartThread(void *(*func)(void*), void* arg);
60  static void ExitThread();
62  static void StartProcess(const char* executable, const char* args);
63 };
64 
67 class SVSemaphore {
68  public:
70  SVSemaphore();
72  void Signal();
74  void Wait();
75  private:
76 #ifdef _WIN32
77  HANDLE semaphore_;
78 #else
79  sem_t semaphore_;
80 #endif
81 };
82 
85 class SVMutex {
86  public:
88  SVMutex();
90  void Lock();
92  void Unlock();
93  private:
94 #ifdef _WIN32
95  HANDLE mutex_;
96 #else
97  pthread_mutex_t mutex_;
98 #endif
99 };
100 
105 class SVNetwork {
106  public:
108  SVNetwork(const char* hostname, int port);
109 
111  ~SVNetwork();
112 
114  void Send(const char* msg);
115 
118  char* Receive();
119 
121  void Close();
122 
124  void Flush();
125 
126  private:
128  SVMutex* mutex_send_;
130  int stream_;
132  char* msg_buffer_in_;
133 
135  std::string msg_buffer_out_;
136 
137  bool has_content; // Win32 (strtok)
139  char* buffer_ptr_; // Unix (strtok_r)
140 };
141 
142 #endif // TESSERACT_VIEWER_SVUTIL_H__