Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrocistringbuf.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Simple string buffer used to accumulate text of commands
5 * efficiently.
6 * Author: Frank Warmerdam, [email protected]
7 *
8 ******************************************************************************
9 * Copyright (c) 2002, Frank Warmerdam <[email protected]>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14 #include "ogr_oci.h"
15 #include "cpl_conv.h"
16
17 /************************************************************************/
18 /* OGROCIStringBuf() */
19 /************************************************************************/
20
21 OGROCIStringBuf::OGROCIStringBuf()
22 {
23 nBufSize = 25;
24 pszString = (char *)CPLMalloc(nBufSize);
25 nLen = 0;
26 pszString[0] = '\0';
27 }
28
29 /************************************************************************/
30 /* OGROCIStringBuf() */
31 /************************************************************************/
32
33 OGROCIStringBuf::~OGROCIStringBuf()
34
35 {
36 CPLFree(pszString);
37 }
38
39 /************************************************************************/
40 /* MakeRoomFor() */
41 /************************************************************************/
42
43 void OGROCIStringBuf::MakeRoomFor(int nCharacters)
44
45 {
46 UpdateEnd();
47
48 if (nLen + nCharacters > nBufSize - 2)
49 {
50 nBufSize = (int)((nLen + nCharacters) * 1.3);
51 pszString = (char *)CPLRealloc(pszString, nBufSize);
52 }
53 }
54
55 /************************************************************************/
56 /* Append() */
57 /************************************************************************/
58
59 void OGROCIStringBuf::Append(const char *pszNewText)
60
61 {
62 int nNewLen = static_cast<int>(strlen(pszNewText));
63
64 MakeRoomFor(nNewLen);
65 strcat(pszString + nLen, pszNewText);
66 nLen += nNewLen;
67 }
68
69 /************************************************************************/
70 /* Appendf() */
71 /************************************************************************/
72
73 void OGROCIStringBuf::Appendf(int nMax, const char *pszFormat, ...)
74
75 {
76 va_list args;
77 char szSimpleBuf[100];
78 char *pszBuffer;
79
80 if (nMax > (int)sizeof(szSimpleBuf) - 1)
81 pszBuffer = (char *)CPLMalloc(nMax + 1);
82 else
83 pszBuffer = szSimpleBuf;
84
85 va_start(args, pszFormat);
86 CPLvsnprintf(pszBuffer, nMax, pszFormat, args);
87 va_end(args);
88
89 Append(pszBuffer);
90 if (pszBuffer != szSimpleBuf)
91 CPLFree(pszBuffer);
92 }
93
94 /************************************************************************/
95 /* UpdateEnd() */
96 /************************************************************************/
97
98 void OGROCIStringBuf::UpdateEnd()
99
100 {
101 nLen += static_cast<int>(strlen(pszString + nLen));
102 }
103
104 /************************************************************************/
105 /* StealString() */
106 /************************************************************************/
107
108 char *OGROCIStringBuf::StealString()
109
110 {
111 char *pszStolenString = pszString;
112
113 nBufSize = 100;
114 pszString = (char *)CPLMalloc(nBufSize);
115 nLen = 0;
116
117 return pszStolenString;
118 }
119
120 /************************************************************************/
121 /* GetLast() */
122 /************************************************************************/
123
124 char OGROCIStringBuf::GetLast()
125
126 {
127 if (nLen != 0)
128 return pszString[nLen - 1];
129 else
130 return '\0';
131 }
132
133 /************************************************************************/
134 /* Clear() */
135 /************************************************************************/
136
137 void OGROCIStringBuf::Clear()
138
139 {
140 pszString[0] = '\0';
141 nLen = 0;
142 }