Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

642 lines
12 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File:
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #ifndef __JBDEF_H__
  13. #define __JBDEF_H__
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <ntddkbd.h>
  18. #include <ntddmou.h>
  19. #include <windows.h>
  20. #include <winbase.h>
  21. #include <winerror.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <tchar.h>
  25. #include <esent.h>
  26. #include "tlsdef.h"
  27. #include "tlsassrt.h"
  28. #ifdef JB_ASSERT
  29. #undef JB_ASSERT
  30. #endif
  31. #pragma warning(disable: 4018)
  32. #pragma warning(disable: 4512)
  33. #define JB_ASSERT TLSASSERT
  34. #define MAX_JB_ERRSTRING 512
  35. //
  36. //
  37. //
  38. #define INDEXNAME _TEXT("idx")
  39. #define SEPERATOR _TEXT("_")
  40. #define JBSTRING_NULL _TEXT("")
  41. #define JETBLUE_NULL _TEXT("")
  42. #define INDEX_SORT_ASCENDING _TEXT("+")
  43. #define INDEX_SORT_DESCENDING _TEXT("-")
  44. #define INDEX_END_COLNAME _TEXT("\0")
  45. #define JB_COLTYPE_TEXT JET_coltypLongBinary
  46. //#define JB_COLTYPE_TEXT JET_coltypLongText
  47. //------------------------------------------------------------------
  48. //
  49. // JetBlue does not support UNICODE string
  50. //
  51. //------------------------------------------------------------------
  52. #undef JET_BLUE_SUPPORT_UNICODE
  53. #ifdef JET_BLUE_SUPPORT_UNICODE
  54. typedef LPTSTR JB_STRING;
  55. #else
  56. typedef LPSTR JB_STRING;
  57. #endif
  58. //----------------------------------------------------------------
  59. #ifndef AllocateMemory
  60. #define AllocateMemory(size) \
  61. LocalAlloc(LPTR, size)
  62. #endif
  63. #ifndef FreeMemory
  64. #define FreeMemory(ptr) \
  65. if(ptr) \
  66. { \
  67. LocalFree(ptr); \
  68. ptr=NULL; \
  69. }
  70. #endif
  71. #ifndef ReallocateMemory
  72. #define ReallocateMemory(ptr, size) \
  73. LocalReAlloc(ptr, size, LMEM_ZEROINIT)
  74. #endif
  75. //
  76. // Private member function
  77. //
  78. #define CLASS_PRIVATE
  79. //
  80. // No define for NIL instance ID
  81. //
  82. #define JET_NIL_INSTANCE JET_sesidNil
  83. //
  84. // No define for NIL column id
  85. //
  86. #define JET_NIL_COLUMN JET_sesidNil
  87. //
  88. // No define for max table name length,
  89. // user2.doc says 64 ASCII
  90. //
  91. #define MAX_TABLENAME_LENGTH 32
  92. //
  93. // Jet Blue text only 255 BYTES
  94. //
  95. #define MAX_JETBLUE_TEXT_LENGTH LSERVER_MAX_STRING_SIZE
  96. //
  97. // Jet Blue Index, Column, ... name length
  98. //
  99. #define MAX_JETBLUE_NAME_LENGTH 64
  100. //
  101. // Jet Blue column code page must be 1200 or 1250
  102. //
  103. #define TLS_JETBLUE_COLUMN_CODE_PAGE 1252
  104. //
  105. // Jet Blue Text column language ID
  106. //
  107. #define TLS_JETBLUE_COLUMN_LANGID 0x409
  108. //
  109. // Jet Blue Column Country Code
  110. //
  111. #define TLS_JETBLUE_COLUMN_COUNTRY_CODE 1
  112. //
  113. // Max Jet Blue index key length - 127 fix columns
  114. //
  115. #define TLS_JETBLUE_MAX_INDEXKEY_LENGTH \
  116. (127 + 1) * MAX_JETBLUE_NAME_LENGTH
  117. //
  118. // Max. Jet Blue key length documented is 255 in user2.doc
  119. //
  120. //
  121. // Default table density
  122. //
  123. #define TLS_JETBLUE_DEFAULT_TABLE_DENSITY 20
  124. //
  125. // JetBlue max key size - user2.doc
  126. //
  127. #define TLS_JETBLUE_MAX_KEY_LENGTH 255
  128. //
  129. //
  130. #define TLS_TABLE_INDEX_DEFAULT_DENSITY 20
  131. ///////////////////////////////////////////////////////////////
  132. //
  133. // Various structure
  134. //
  135. ///////////////////////////////////////////////////////////////
  136. typedef struct __TLSJBTable {
  137. LPCTSTR pszTemplateTableName;
  138. unsigned long ulPages;
  139. unsigned long ulDensity;
  140. JET_GRBIT jbGrbit;
  141. } TLSJBTable, *PTLSJBTable;
  142. typedef struct __TLSJBColumn {
  143. TCHAR pszColumnName[MAX_JETBLUE_NAME_LENGTH];
  144. JET_COLTYP colType;
  145. unsigned long cbMaxLength; // max length of column
  146. JET_GRBIT jbGrbit;
  147. PVOID pbDefValue; // column default value
  148. int cbDefValue;
  149. unsigned short colCodePage;
  150. unsigned short wCountry;
  151. unsigned short langid;
  152. } TLSJBColumn, *PTLSJBColumn;
  153. typedef struct __TLSJBIndex {
  154. TCHAR pszIndexName[MAX_JETBLUE_NAME_LENGTH];
  155. LPTSTR pszIndexKey;
  156. unsigned long cbKey; // length of key
  157. JET_GRBIT jbGrbit;
  158. unsigned long ulDensity;
  159. } TLSJBIndex, *PTLSJBIndex;
  160. #ifdef __cplusplus
  161. extern "C" {
  162. #endif
  163. BOOL
  164. ConvertJBstrToWstr(
  165. JB_STRING in,
  166. LPTSTR* out
  167. );
  168. BOOL
  169. ConvertWstrToJBstr(
  170. LPCTSTR in,
  171. JB_STRING* out
  172. );
  173. void
  174. FreeJBstr(
  175. JB_STRING pstr
  176. );
  177. BOOL
  178. ConvertMWstrToMJBstr(
  179. LPCTSTR in,
  180. DWORD length,
  181. JB_STRING* out
  182. );
  183. BOOL
  184. ConvertMJBstrToMWstr(
  185. JB_STRING in,
  186. DWORD length,
  187. LPTSTR* out
  188. );
  189. #ifdef __cplusplus
  190. }
  191. #endif
  192. ///////////////////////////////////////////////////////////////
  193. //
  194. // Various template class
  195. //
  196. ///////////////////////////////////////////////////////////////
  197. template <class Type, int size> struct FixArray;
  198. template <class Type> struct Array;
  199. typedef FixArray<TCHAR, MAX_JETBLUE_TEXT_LENGTH> JetBlueText;
  200. typedef FixArray<char, MAX_JETBLUE_TEXT_LENGTH> JetBlueBinary;
  201. typedef Array<TCHAR> JetBlueLongText;
  202. typedef Array<char> JetBlueLongBinary;
  203. // ----------------------------------------------------------
  204. //
  205. // Variable length array template
  206. // Another choice is use vector<> in STL.
  207. //
  208. // ----------------------------------------------------------
  209. template<class Type>
  210. struct Array {
  211. public:
  212. Type* m_Data;
  213. int m_Size;
  214. private:
  215. BOOL m_UseExistingBuffer; // m_Data point to user
  216. // buffer
  217. BOOL
  218. init(const Type* v, int size) {
  219. if(m_UseExistingBuffer == FALSE)
  220. {
  221. if(size > m_Size)
  222. {
  223. FreeBuffer();
  224. m_Data = new Type[m_Size = size];
  225. if(m_Data == NULL)
  226. return FALSE;
  227. }
  228. }
  229. for(int i=0; i < min(size, m_Size); i++)
  230. m_Data[i] = v[i];
  231. return TRUE;
  232. }
  233. void
  234. FreeBuffer() {
  235. if(m_Data && m_UseExistingBuffer == FALSE)
  236. delete [] m_Data;
  237. m_UseExistingBuffer = FALSE;
  238. m_Data = NULL;
  239. m_Size = 0;
  240. }
  241. public:
  242. Array() : m_Data(NULL), m_Size(0), m_UseExistingBuffer(FALSE) {}
  243. Array(int size) {
  244. m_Data = new Type[m_Size = size];
  245. JB_ASSERT(m_Data != NULL);
  246. }
  247. Array(const Type* v, int count) {
  248. init(v, size);
  249. }
  250. ~Array() {
  251. FreeBuffer();
  252. }
  253. Array(const Array<Type>& v) {
  254. init(v.m_Data, v.size);
  255. }
  256. void
  257. UseBuffer( Type* v, int size ) {
  258. FreeBuffer();
  259. m_Data = v;
  260. m_Size = size;
  261. m_UseExistingBuffer = TRUE;
  262. }
  263. Array&
  264. operator=(const Array& v) {
  265. if(this == &v)
  266. return *this;
  267. init(v.GetBufferPtr(), v.GetArrayCount());
  268. return *this;
  269. }
  270. Array&
  271. operator+=(const Array& v) {
  272. int m_OldSize = m_Size;
  273. JB_ASSERT(Expand(v.GetArrayCount()));
  274. for(int i=m_OldSize; i< m_Size; i++)
  275. {
  276. m_Data[i] = v[i-m_OldSize];
  277. }
  278. return *this;
  279. }
  280. Type&
  281. operator[](int index) {
  282. JB_ASSERT(index < m_Size && m_Data != NULL);
  283. return m_Data[index];
  284. }
  285. operator Type*() const {
  286. return m_Data;
  287. }
  288. operator PVOID() const {
  289. return (PVOID) m_Data;
  290. }
  291. BOOL
  292. Expand(int size) {
  293. Type* ptr;
  294. ptr = new Type[m_Size+size];
  295. for(int i=0; i < m_Size; i++)
  296. ptr[i] = m_Data[i];
  297. delete [] m_Data;
  298. m_Data = ptr;
  299. m_Size += size;
  300. return TRUE;
  301. }
  302. PVOID
  303. GetBufferPtr() {
  304. return (PVOID) m_Data;
  305. }
  306. int
  307. GetBufferSize() {
  308. return m_Size * sizeof(m_Data[0]);
  309. }
  310. int
  311. GetArrayCount() {
  312. return m_Size;
  313. }
  314. virtual Type*
  315. operator=(const Type* v) {
  316. JB_ASSERT(FALSE);
  317. return *this;
  318. };
  319. virtual Type*
  320. operator+=(const Type* v) {
  321. JB_ASSERT(FALSE);
  322. return *this;
  323. };
  324. };
  325. inline TCHAR*
  326. Array<TCHAR>::operator=(const TCHAR* src) {
  327. init(src, _tcslen(src)+1);
  328. return m_Data;
  329. }
  330. inline TCHAR*
  331. Array<TCHAR>::operator+=(const TCHAR* src) {
  332. Expand(_tcslen(src)+1);
  333. _tcscat(m_Data, src);
  334. return m_Data;
  335. }
  336. /////////////////////////////////////////////////////////////////////////
  337. //
  338. // Fix Array template class
  339. //
  340. // Not much benefit from deriving from Array template
  341. /////////////////////////////////////////////////////////////////////////
  342. template< class Type, int size >
  343. struct FixArray : public Array<Type> {
  344. //
  345. // Currently only for STRING, use STL vector for
  346. // for general use.
  347. //
  348. Type m_Data[size+1]; // one more for string
  349. FixArray() : Array<Type>() {
  350. memset(m_Data, 0, sizeof(m_Data));
  351. Array<Type>::UseBuffer(m_Data, size);
  352. }
  353. FixArray(const Type* v) : Array<Type>() {
  354. Array<Type>::UseBuffer(m_Data, size);
  355. *this = v;
  356. }
  357. FixArray(const Type* v, int length) : Array<Type>() {
  358. Array<Type>::UseBuffer(m_Data, size);
  359. Array<Type>::Array<Type>(m_Data, length);
  360. }
  361. FixArray<Type, size>&
  362. operator=(const FixArray<Type, size>& v) {
  363. if(this == &v)
  364. return *this;
  365. *this = (Type *)v;
  366. return *this;
  367. }
  368. virtual Type*
  369. operator+=(const Type* v) {
  370. JB_ASSERT(FALSE);
  371. return m_Data;
  372. };
  373. virtual Type*
  374. operator=(const Type* v) {
  375. JB_ASSERT(FALSE);
  376. return m_Data;
  377. }
  378. };
  379. //-----------------------------------------------------------------
  380. inline TCHAR*
  381. JetBlueText::operator=(
  382. const TCHAR* v
  383. )
  384. /*
  385. */
  386. {
  387. int numCharsToCopy = min(_tcslen(v), size);
  388. _tcsncpy(
  389. m_Data,
  390. v,
  391. numCharsToCopy
  392. );
  393. m_Data[numCharsToCopy] = _TEXT('\0');
  394. return m_Data;
  395. }
  396. inline TCHAR*
  397. JetBlueText::operator+=(
  398. const TCHAR* v
  399. )
  400. /*
  401. */
  402. {
  403. int length=_tcslen(m_Data);
  404. _tcsncpy(
  405. m_Data+length,
  406. v,
  407. min(_tcslen(v), size-length)
  408. );
  409. return m_Data;
  410. }
  411. //
  412. /////////////////////////////////////////////////////////////////////
  413. //
  414. class JBError {
  415. public:
  416. JET_ERR m_JetErr;
  417. JBError() : m_JetErr(JET_errSuccess) {}
  418. JBError(const JET_ERR jet_error) : m_JetErr(jet_error) {}
  419. const JET_ERR
  420. GetLastJetError() const {
  421. return m_JetErr;
  422. }
  423. void
  424. SetLastJetError(JET_ERR jetError = JET_errSuccess) {
  425. m_JetErr = jetError;
  426. }
  427. BOOL
  428. IsSuccess() const {
  429. return m_JetErr >= JET_errSuccess;
  430. }
  431. BOOL
  432. GetJBErrString(
  433. const JET_ERR jbErr,
  434. LPTSTR* pszErrString
  435. )
  436. /*++
  437. --*/
  438. {
  439. BOOL bStatus=FALSE;
  440. JET_ERR err;
  441. CHAR szAnsiBuffer[MAX_JB_ERRSTRING+1];
  442. if(pszErrString == NULL)
  443. {
  444. SetLastError(ERROR_INVALID_PARAMETER);
  445. return bStatus;
  446. }
  447. memset(szAnsiBuffer, 0, sizeof(szAnsiBuffer));
  448. err = JetGetSystemParameter(
  449. JET_instanceNil,
  450. JET_sesidNil,
  451. JET_paramErrorToString,
  452. (ULONG_PTR *) &jbErr,
  453. szAnsiBuffer,
  454. MAX_JB_ERRSTRING
  455. );
  456. if(err == JET_errBufferTooSmall || err == JET_errSuccess)
  457. {
  458. // return partial error string.
  459. if(ConvertJBstrToWstr(szAnsiBuffer, pszErrString))
  460. {
  461. bStatus = TRUE;
  462. }
  463. }
  464. return bStatus;
  465. }
  466. void
  467. DebugOutput(
  468. LPTSTR format, ...
  469. ) const
  470. /*++
  471. ++*/
  472. {
  473. va_list marker;
  474. va_start(marker, format);
  475. #ifdef DEBUG_JETBLUE
  476. TCHAR buf[8096];
  477. DWORD dump;
  478. __try {
  479. memset(buf, 0, sizeof(buf));
  480. _vsntprintf(
  481. buf,
  482. sizeof(buf)/sizeof(buf[0]),
  483. format,
  484. marker
  485. );
  486. _tprintf(_TEXT("%s"), buf);
  487. // OutputDebugString(buf);
  488. }
  489. __except(EXCEPTION_EXECUTE_HANDLER) {
  490. }
  491. #endif
  492. va_end(marker);
  493. }
  494. };
  495. //------------------------------------------------------------
  496. #endif