]> git.stg.codes - stg.git/blob - libs/ibpp/_dpb.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / libs / ibpp / _dpb.cpp
1 ///////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //      File    : $Id: _dpb.cpp,v 1.2 2009/03/19 20:00:27 faust Exp $\r
4 //      Subject : IBPP, internal DPB class implementation\r
5 //\r
6 ///////////////////////////////////////////////////////////////////////////////\r
7 //\r
8 //      (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)\r
9 //\r
10 //      The contents of this file are subject to the IBPP License (the "License");\r
11 //      you may not use this file except in compliance with the License.  You may\r
12 //      obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'\r
13 //      file which must have been distributed along with this file.\r
14 //\r
15 //      This software, distributed under the License, is distributed on an "AS IS"\r
16 //      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the\r
17 //      License for the specific language governing rights and limitations\r
18 //      under the License.\r
19 //\r
20 ///////////////////////////////////////////////////////////////////////////////\r
21 //\r
22 //      COMMENTS\r
23 //      * DPB == Database Parameter Block/Buffer, see Interbase 6.0 C-API\r
24 //      * Tabulations should be set every four characters when editing this file.\r
25 //\r
26 ///////////////////////////////////////////////////////////////////////////////\r
27 \r
28 #ifdef _MSC_VER\r
29 #pragma warning(disable: 4786 4996)\r
30 #ifndef _DEBUG\r
31 #pragma warning(disable: 4702)\r
32 #endif\r
33 #endif\r
34 \r
35 #include "_ibpp.h"\r
36 \r
37 #ifdef HAS_HDRSTOP\r
38 #pragma hdrstop\r
39 #endif\r
40 \r
41 #include <cstring>\r
42 \r
43 using namespace ibpp_internals;\r
44 \r
45 const int DPB::BUFFERINCR = 128;\r
46 \r
47 void DPB::Grow(int needed)\r
48 {\r
49         if (mBuffer == 0) ++needed;     // Initial alloc will require one more byte\r
50         if ((mSize + needed) > mAlloc)\r
51         {\r
52                 // We need to grow the buffer. We use increments of BUFFERINCR bytes.\r
53                 needed = (needed / BUFFERINCR + 1) * BUFFERINCR;\r
54                 char* newbuffer = new char[mAlloc + needed];\r
55                 if (mBuffer == 0)\r
56                 {\r
57                         // Initial allocation, initialize the version tag\r
58                         newbuffer[0] = isc_dpb_version1;\r
59                         mSize = 1;\r
60                 }\r
61                 else\r
62                 {\r
63                         // Move the old buffer content to the new one\r
64                         memcpy(newbuffer, mBuffer, mSize);\r
65                         delete [] mBuffer;\r
66                 }\r
67                 mBuffer = newbuffer;\r
68                 mAlloc += needed;\r
69         }\r
70 }\r
71 \r
72 void DPB::Insert(char type, const char* data)\r
73 {\r
74         int len = (int)strlen(data);\r
75         Grow(len + 2);\r
76     mBuffer[mSize++] = type;\r
77         mBuffer[mSize++] = char(len);\r
78     strncpy(&mBuffer[mSize], data, len);\r
79     mSize += len;\r
80 }\r
81 \r
82 void DPB::Insert(char type, int16_t data)\r
83 {\r
84         Grow(2 + 2);\r
85     mBuffer[mSize++] = type;\r
86         mBuffer[mSize++] = char(2);\r
87     *(int16_t*)&mBuffer[mSize] = int16_t((*gds.Call()->m_vax_integer)((char*)&data, 2));\r
88     mSize += 2;\r
89 }\r
90 \r
91 void DPB::Insert(char type, bool data)\r
92 {\r
93         Grow(2 + 1);\r
94     mBuffer[mSize++] = type;\r
95         mBuffer[mSize++] = char(1);\r
96     mBuffer[mSize++] = char(data ? 1 : 0);\r
97 }\r
98 \r
99 void DPB::Insert(char type, char data)\r
100 {\r
101         Grow(2 + 1);\r
102     mBuffer[mSize++] = type;\r
103         mBuffer[mSize++] = char(1);\r
104     mBuffer[mSize++] = data;\r
105 }\r
106 \r
107 void DPB::Reset()\r
108 {\r
109         if (mAlloc != 0)\r
110     {\r
111         delete [] mBuffer;\r
112         mBuffer = 0;\r
113         mSize = 0;\r
114                 mAlloc = 0;\r
115     }\r
116 }\r
117 \r
118 //\r
119 //      EOF\r
120 //\r