From 318ba8836f1e86f23a876788f37406de7c52774c Mon Sep 17 00:00:00 2001
From: "boris@boressoft.ru" <������� ��������@DANCE-SERVER-2.(none)>
Date: Wed, 14 Mar 2012 19:12:28 +0400
Subject: [PATCH] Add WinCE support.

* Added two new files: errno.hpp and errno.cpp. They are required to use errno functionality on WM.
* zmq.cpp, msg.h: removed inclusion of errno.h because it is included in zmq.h that is also included by .cpp.
* windows.hpp: process.h is included only for desktop builds.
* thread.cpp: on CE CreateThread is used instead of __beginthreadex
* socket_base.cpp, clock.cpp: on CE include cmnintrin.h instead on intrin.h
* signaler.cpp: on Windows should use special macro around event name (for unicode builds)
* err.hpp: make it include errno.hpp (my file) instead on errno.h when building for CE
* err.cpp: use FormatMessage when building for CE (because CE does not have ANSI API functions)
* zmq.h: do not include errno.h whe building for CE
* libzmq.vcproj: add tro new files
---
 builds/msvc/errno.cpp            | 32 ++++++++++++++++++
 builds/msvc/errno.hpp            | 56 ++++++++++++++++++++++++++++++++
 builds/msvc/libzmq/libzmq.vcproj |  8 +++++
 include/zmq.h                    |  2 ++
 src/clock.cpp                    |  4 +++
 src/err.cpp                      |  6 ++++
 src/err.hpp                      |  4 +++
 src/msg.cpp                      |  1 -
 src/signaler.cpp                 |  2 +-
 src/socket_base.cpp              |  4 +++
 src/thread.cpp                   |  9 +++++
 src/windows.hpp                  |  2 ++
 src/zmq.cpp                      |  1 -
 13 files changed, 128 insertions(+), 3 deletions(-)
 create mode 100644 builds/msvc/errno.cpp
 create mode 100644 builds/msvc/errno.hpp

diff --git a/builds/msvc/errno.cpp b/builds/msvc/errno.cpp
new file mode 100644
index 00000000..bf386371
--- /dev/null
+++ b/builds/msvc/errno.cpp
@@ -0,0 +1,32 @@
+#if defined WINCE
+
+//#include "..\..\include\zmq.h"
+#include "..\..\src\err.hpp"
+
+int errno;
+int _doserrno;
+int _sys_nerr;
+
+char* error_desc_buff = NULL;
+
+char* strerror(int errno)
+{
+	if (NULL != error_desc_buff)
+	{
+		LocalFree(error_desc_buff);
+		error_desc_buff = NULL;
+	}
+
+	FormatMessage(
+		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+		NULL,
+		errno,
+		0,
+		(LPTSTR)&error_desc_buff,
+		1024,
+		NULL
+	);
+	return error_desc_buff;
+}
+
+#endif
\ No newline at end of file
diff --git a/builds/msvc/errno.hpp b/builds/msvc/errno.hpp
new file mode 100644
index 00000000..f40ee0fc
--- /dev/null
+++ b/builds/msvc/errno.hpp
@@ -0,0 +1,56 @@
+#ifndef ERRNO_H
+#define ERRNO_H 1
+
+//#define EPERM           1
+//#define ENOENT          2
+//#define ESRCH           3
+#define EINTR           4
+//#define EIO             5
+//#define ENXIO           6
+//#define E2BIG           7
+//#define ENOEXEC         8
+#define EBADF           9
+//#define ECHILD          10
+#define EAGAIN          11
+//#define ENOMEM          12
+//#define EACCES          13
+#define EFAULT          14
+//#define EOSERR          15 // rk
+//#define EBUSY           16
+//#define EEXIST          17
+//#define EXDEV           18
+//#define ENODEV          19
+//#define ENOTDIR         20
+//#define EISDIR          21
+#define EINVAL          22
+//#define ENFILE          23
+#define EMFILE          24
+//#define ENOTTY          25
+//#define EFBIG           27
+//#define ENOSPC          28
+//#define ESPIPE          29
+//#define EROFS           30
+//#define EMLINK          31
+//#define EPIPE           32
+//#define EDOM            33
+//#define ERANGE          34
+//#define EDEADLK         36
+//#define ENOSYS          37
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int errno;
+extern int _doserrno;
+extern int _sys_nerr;
+
+char* strerror(int errno);
+
+#define sys_nerr _sys_nerr
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif
\ No newline at end of file
diff --git a/builds/msvc/libzmq/libzmq.vcproj b/builds/msvc/libzmq/libzmq.vcproj
index bb94551f..464e0529 100644
--- a/builds/msvc/libzmq/libzmq.vcproj
+++ b/builds/msvc/libzmq/libzmq.vcproj
@@ -282,6 +282,10 @@
 				RelativePath="..\..\..\src\err.cpp"
 				>
 			</File>
+			<File
+				RelativePath="..\errno.cpp"
+				>
+			</File>
 			<File
 				RelativePath="..\..\..\src\fq.cpp"
 				>
@@ -528,6 +532,10 @@
 				RelativePath="..\..\..\src\err.hpp"
 				>
 			</File>
+			<File
+				RelativePath="..\errno.hpp"
+				>
+			</File>
 			<File
 				RelativePath="..\..\..\src\fd.hpp"
 				>
diff --git a/include/zmq.h b/include/zmq.h
index 423db88b..cc5db4ee 100644
--- a/include/zmq.h
+++ b/include/zmq.h
@@ -27,7 +27,9 @@
 extern "C" {
 #endif
 
+#if !defined WINCE
 #include <errno.h>
+#endif
 #include <stddef.h>
 #include <stdio.h>
 #if defined _WIN32
diff --git a/src/clock.cpp b/src/clock.cpp
index 9429f71b..1646397c 100644
--- a/src/clock.cpp
+++ b/src/clock.cpp
@@ -27,8 +27,12 @@
 #include <stddef.h>
 
 #if defined _MSC_VER
+#if defined WINCE
+#include <cmnintrin.h>
+#else
 #include <intrin.h>
 #endif
+#endif
 
 #if !defined ZMQ_HAVE_WINDOWS
 #include <sys/time.h>
diff --git a/src/err.cpp b/src/err.cpp
index 028d752e..8a25dbc7 100644
--- a/src/err.cpp
+++ b/src/err.cpp
@@ -202,9 +202,15 @@ const char *zmq::wsa_error_no (int no_)
 void zmq::win_error (char *buffer_, size_t buffer_size_)
 {
     DWORD errcode = GetLastError ();
+#if defined WINCE
+    DWORD rc = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
+        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
+        SUBLANG_DEFAULT), (LPWSTR)buffer_, buffer_size_ / sizeof(wchar_t), NULL );
+#else
     DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
         FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
         SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL );
+#endif
     zmq_assert (rc);
 }
 
diff --git a/src/err.hpp b/src/err.hpp
index 53a65697..cdae4e9d 100644
--- a/src/err.hpp
+++ b/src/err.hpp
@@ -26,7 +26,11 @@
 #include "../include/zmq.h"
 
 #include <assert.h>
+#if defined WINCE
+#include "..\builds\msvc\errno.hpp"
+#else
 #include <errno.h>
+#endif
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
diff --git a/src/msg.cpp b/src/msg.cpp
index 1afe85f5..ba4dcd37 100644
--- a/src/msg.cpp
+++ b/src/msg.cpp
@@ -23,7 +23,6 @@
 #include "../include/zmq.h"
 
 #include <string.h>
-#include <errno.h>
 #include <stdlib.h>
 #include <new>
 
diff --git a/src/signaler.cpp b/src/signaler.cpp
index 29895c28..571e7d93 100644
--- a/src/signaler.cpp
+++ b/src/signaler.cpp
@@ -238,7 +238,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
     //  two instances of the library don't accidentally create signaler
     //  crossing the process boundary.
     //  We'll use named event object to implement the critical section.
-    HANDLE sync = CreateEvent (NULL, FALSE, TRUE, "zmq-signaler-port-sync");
+    HANDLE sync = CreateEvent (NULL, FALSE, TRUE, TEXT("zmq-signaler-port-sync"));
     win_assert (sync != NULL);
 
     //  Enter the critical section.
diff --git a/src/socket_base.cpp b/src/socket_base.cpp
index 4dab68c4..8f8f5d64 100644
--- a/src/socket_base.cpp
+++ b/src/socket_base.cpp
@@ -29,8 +29,12 @@
 #if defined ZMQ_HAVE_WINDOWS
 #include "windows.hpp"
 #if defined _MSC_VER
+#if defined WINCE
+#include <cmnintrin.h>
+#else
 #include <intrin.h>
 #endif
+#endif
 #else
 #include <unistd.h>
 #endif
diff --git a/src/thread.cpp b/src/thread.cpp
index 00628e5c..cc1964a4 100644
--- a/src/thread.cpp
+++ b/src/thread.cpp
@@ -27,7 +27,11 @@
 
 extern "C"
 {
+#if defined _WIN32_WCE
+	static DWORD thread_routine (LPVOID arg_)
+#else
     static unsigned int __stdcall thread_routine (void *arg_)
+#endif
     {
         zmq::thread_t *self = (zmq::thread_t*) arg_;
         self->tfn (self->arg);
@@ -39,8 +43,13 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
 {
     tfn = tfn_;
     arg =arg_;
+#if defined WINCE
+    descriptor = (HANDLE) CreateThread (NULL, 0,
+        &::thread_routine, this, 0 , NULL);
+#else
     descriptor = (HANDLE) _beginthreadex (NULL, 0,
         &::thread_routine, this, 0 , NULL);
+#endif
     win_assert (descriptor != NULL);    
 }
 
diff --git a/src/windows.hpp b/src/windows.hpp
index f5f946cc..4a3124eb 100644
--- a/src/windows.hpp
+++ b/src/windows.hpp
@@ -170,7 +170,9 @@
 
 #include <ws2tcpip.h>
 #include <ipexport.h>
+#if !defined WINCE
 #include <process.h>
+#endif
 
 //  In MinGW environment AI_NUMERICSERV is not defined.
 #ifndef AI_NUMERICSERV
diff --git a/src/zmq.cpp b/src/zmq.cpp
index 627d7b10..ed241819 100644
--- a/src/zmq.cpp
+++ b/src/zmq.cpp
@@ -65,7 +65,6 @@ struct iovec {
 
 
 #include <string.h>
-#include <errno.h>
 #include <stdlib.h>
 #include <new>