Common.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /***************************************************************************
  2. * Copyright © 2007 TWAIN Working Group:
  3. * Adobe Systems Incorporated, AnyDoc Software Inc., Eastman Kodak Company,
  4. * Fujitsu Computer Products of America, JFL Peripheral Solutions Inc.,
  5. * Ricoh Corporation, and Xerox Corporation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of the TWAIN Working Group nor the
  16. * names of its contributors may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY TWAIN Working Group ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL TWAIN Working Group BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ***************************************************************************/
  31. /**
  32. * @file Common.h
  33. * Common defines and typedefs used by the DS, App, and scanner
  34. * @author TWAIN Working Group
  35. * @date April 2007
  36. */
  37. #ifndef __COMMON_H__
  38. #define __COMMON_H__
  39. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  40. #define TWNDS_OS_WIN
  41. #ifndef WIN32_LEAN_AND_MEAN
  42. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  43. #endif
  44. #include <windows.h>
  45. #endif
  46. #if defined(__APPLE__)
  47. #define TWNDS_OS_APPLE
  48. #endif
  49. #include "twain.h"
  50. #ifdef TWH_CMP_GNU
  51. #if !(defined(TWNDS_OS_WIN) || defined(TWNDS_OS_APPLE))
  52. #define TWNDS_OS_LINUX
  53. #endif
  54. #include <wchar.h>
  55. #include <stdarg.h>
  56. #endif
  57. /**
  58. * These headers are available on all platforms...
  59. */
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <limits.h>
  63. #include <stdio.h>
  64. #include <assert.h>
  65. /**
  66. * First off, figure out what compiler we're running and on which
  67. * platform we think we're running it. We assume that you're building
  68. * on the same platform you intend to run, so if you are cross compiling
  69. * you will likely have a bit of work to do here...
  70. */
  71. /**
  72. * Compilers we support...
  73. */
  74. #define TWNDS_CMP_VISUALCPP 0x1001 // Preferably 2005+
  75. #define TWNDS_CMP_GNUGPP 0x1002 // Preferably v4.x+
  76. #define TWNDS_CMP_XCODE 0x1003 // Xcode
  77. /**
  78. * If the user defines TWNDS_CMP in their make file or project,
  79. * then we'll assume they want to take responsibility for picking
  80. * how we'll build the system. At this point it seems like the
  81. * compiler definition is used to select which native library calls
  82. * we're dealing with, while the os definition is more about
  83. * where we'll expect to find stuff on the running system, like
  84. * directories...
  85. */
  86. #ifndef TWNDS_CMP
  87. // GNU g++
  88. #if defined(TWH_CMP_GNU) || defined(TWH_CMP_XCODE)
  89. #define TWNDS_CMP TWNDS_CMP_GNUGPP
  90. #define TWNDS_CMP_VERSION __GNUC__
  91. #define kTWAIN_DSM_DIR "/usr/local/lib/"
  92. // Visual Studio C++
  93. #elif defined(TWH_CMP_MSC)
  94. #define TWNDS_CMP TWNDS_CMP_VISUALCPP
  95. #define TWNDS_CMP_VERSION _MSC_VER
  96. // Not neccessary it is in Windows path
  97. #define kTWAIN_DSM_DIR ""
  98. // Xcode
  99. #elif defined (TWH_CMP_XCODE)
  100. #define TWNDS_CMP TWNDS_CMP_XCODE
  101. #define TWNDS_CMP_VERSION
  102. // ruh-roh...
  103. #else
  104. #error Sorry, we don't recognize this system...
  105. #endif
  106. #endif
  107. /**
  108. * @def LOADLIBRARY(lib)
  109. * Call system loadibrary function. OS abstraction macro that tries to load a library.
  110. * @param lib path and name of library
  111. *
  112. * @def LOADFUNCTION(lib, func)
  113. * Call system GetProcAddress function. OS abstraction macro that tries to locate the addess of a funtion name.
  114. * @param lib path and name of library
  115. * @param func name of the funtion
  116. *
  117. * @def UNLOADLIBRARY(lib)
  118. * Call system FreeLibrary function. OS abstraction macro that tries to release the library.
  119. * @param lib library modual to unload
  120. *
  121. * @def UNLINK
  122. * OS abstraction macro that calls system _unlink function.
  123. *
  124. * @def READ
  125. * OS abstraction macro that calls system _read function.
  126. *
  127. * @def CLOSE
  128. * OS abstraction macro that calls system _close function.
  129. *
  130. * @def SNPRINTF
  131. * OS abstraction macro that calls system _snprintf function.
  132. *
  133. */
  134. #if (TWNDS_CMP == TWNDS_CMP_VISUALCPP)
  135. //#include "stdafx.h"
  136. #define DllExport __declspec( dllexport )
  137. #define LOADLIBRARY(lib) LoadLibraryA(lib)
  138. #define LOADFUNCTION(lib, func) GetProcAddress(lib, func)
  139. #define UNLOADLIBRARY(lib) FreeLibrary(lib)
  140. #define UNLINK _unlink
  141. #define READ _read
  142. #define CLOSE _close
  143. #define FILE_EXISTS(FILE_NAME) ((0xFFFFFFFF==GetFileAttributes(FILE_NAME))?FALSE:TRUE)
  144. #define PATH_SEPERATOR '\\'
  145. #ifndef PATH_MAX
  146. #define PATH_MAX _MAX_PATH
  147. #endif
  148. #if (TWNDS_CMP_VERSION >= 1400)
  149. #define SNPRINTF _snprintf_s
  150. #define SSCANF sscanf_s
  151. #define FOPEN(pf,name,mode) (void)fopen_s(&pf,name,mode)
  152. #define _OPEN(pf,name,mode,share,perm) (void)_sopen_s(&pf,name,mode,share,perm)
  153. #else
  154. #define SSCANF sscanf
  155. #define SNPRINTF _snprintf
  156. #define FOPEN(pf,name,mode) pf=fopen(name,mode)
  157. #define _OPEN(pf,name,mode,share,perm) pf = _open(name,mode,share)
  158. #endif
  159. #define MAX(a, b) max(a,b)
  160. #define MIN(a, b) min(a,b)
  161. #elif (TWNDS_CMP == TWNDS_CMP_GNUGPP)
  162. #define DllExport
  163. #define LOADLIBRARY(lib) dlopen(lib, RTLD_NOW)
  164. #define LOADFUNCTION(lib, func) dlsym(lib, func)
  165. #define UNLOADLIBRARY(lib) dlclose(lib)
  166. #define UNLINK unlink
  167. #define kTWAIN_DSM_DLL_NAME "libtwaindsm.so"
  168. #define READ read
  169. #define CLOSE close
  170. #define PATH_SEPERATOR '/'
  171. #define SNPRINTF snprintf
  172. #define SSCANF sscanf
  173. typedef void * HMODULE;
  174. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  175. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  176. #define FILE_EXISTS(FILE_NAME) ((0 == access(FILE_NAME, R_OK))?TRUE:FALSE)
  177. #define FOPEN(pf,name,mode) (pf=fopen(name,mode))
  178. #if !defined(TRUE)
  179. #define FALSE 0
  180. #define TRUE 1
  181. #endif
  182. #include <inttypes.h>
  183. typedef uint16_t WORD;
  184. typedef uint32_t DWORD;
  185. #else
  186. #error Sorry, we don't recognize this system...
  187. #endif
  188. /**
  189. * We want to use secure string functions whenever possible, if g++
  190. * every includes a set I think it would be excellent to switch over
  191. * to it, but at least with Windows using them we stand a better
  192. * chance of finding boo-boos...
  193. */
  194. #if (TWNDS_CMP == TWNDS_CMP_VISUALCPP) && (TWNDS_CMP_VERSION >= 1400)
  195. #define SSTRCPY(d,z,s) strncpy_s(d,z,s,_TRUNCATE)
  196. #define SSTRCAT(d,z,s) strncat_s(d,z,s,_TRUNCATE)
  197. #define SSTRNCPY(d,z,s,m) strncpy_s(d,z,s,m)
  198. #define SGETENV(d,z,n) ::GetEnvironmentVariable(n,d,z)
  199. inline int SSNPRINTF(char *d, size_t z, size_t c, const char *f,...)
  200. {
  201. int result;
  202. va_list valist;
  203. va_start(valist,f);
  204. result = _vsnprintf_s(d,z,c,f,valist);
  205. va_end(valist);
  206. return result;
  207. }
  208. /**
  209. * These functions are insecure, but everybody has them, so we
  210. * don't need an else/error section like we use everywhere else...
  211. */
  212. #else
  213. #define SSTRCPY(d,z,s) strcpy(d,s)
  214. #define SSTRCAT(d,z,s) strcat(d,s)
  215. #define SSTRNCPY(d,z,s,m) strncpy(d,s,m)
  216. #define SGETENV(d,z,n) strcpy(d,getenv(n)?getenv(n):"")
  217. inline int SSNPRINTF(char *d, size_t, size_t c, const char *f,...)
  218. {
  219. int result;
  220. va_list valist;
  221. va_start(valist,f);
  222. #if (TWNDS_CMP == TWNDS_CMP_VISUALCPP)
  223. result = _vsnprintf(d,c,f,valist);
  224. #elif (TWNDS_CMP == TWNDS_CMP_GNUGPP)
  225. result = vsnprintf(d,c,f,valist);
  226. #else
  227. #error Sorry, we don't recognize this system...
  228. #endif
  229. va_end(valist);
  230. return result;
  231. }
  232. #endif
  233. /**
  234. * Determine the number of bytes needed for one line.
  235. */
  236. #define BYTES_PERLINE(width, bpp) ((((int)(width)*(bpp))+7)/8)
  237. /**
  238. * Determine the number of bytes needed rouned up to 4 byte alignment.
  239. */
  240. #define BYTES_PERLINE_ALIGN4(width, bpp) (((((int)(width)*(bpp))+31)/32)*4)
  241. #endif // __COMMON_H__