|
@@ -0,0 +1,219 @@
|
|
1
|
+#include "cmGlobal.h"
|
|
2
|
+#include "cmRpt.h"
|
|
3
|
+#include "cmErr.h"
|
|
4
|
+#include "cmCtx.h"
|
|
5
|
+#include "cmMem.h"
|
|
6
|
+#include "cmMallocDebug.h"
|
|
7
|
+#include "cmLinkedHeap.h"
|
|
8
|
+#include "cmStrStream.h"
|
|
9
|
+#include "cmText.h"
|
|
10
|
+
|
|
11
|
+typedef struct cmSsBlk_str
|
|
12
|
+{
|
|
13
|
+ char* blk;
|
|
14
|
+ unsigned i;
|
|
15
|
+ struct cmSsBlk_str* link;
|
|
16
|
+} cmSsBlk_t;
|
|
17
|
+
|
|
18
|
+typedef struct
|
|
19
|
+{
|
|
20
|
+ cmErr_t err;
|
|
21
|
+ cmLHeapH_t lhH;
|
|
22
|
+ unsigned blkByteCnt;
|
|
23
|
+ cmSsBlk_t* blp;
|
|
24
|
+ cmSsBlk_t* elp;
|
|
25
|
+} cmOss_t;
|
|
26
|
+
|
|
27
|
+cmOss_t* _cmOssHandleToPtr( cmStrStreamH_t h )
|
|
28
|
+{
|
|
29
|
+ cmOss_t* p = (cmOss_t*)h.h;
|
|
30
|
+ assert(p != NULL );
|
|
31
|
+ return p;
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+cmSsRC_t _cmOssDestroy( cmOss_t* p )
|
|
35
|
+{
|
|
36
|
+ cmSsRC_t rc = kOkSsRC;
|
|
37
|
+ cmLHeapDestroy(&p->lhH);
|
|
38
|
+ cmMemFree(p);
|
|
39
|
+ return rc;
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+cmSsRC_t cmOStrStreamCreate( cmCtx_t* ctx, cmStrStreamH_t* hp, unsigned dfltBlkByteCnt )
|
|
43
|
+{
|
|
44
|
+ cmSsRC_t rc;
|
|
45
|
+ if((rc = cmOStrStreamDestroy(hp)) != kOkSsRC )
|
|
46
|
+ return rc;
|
|
47
|
+
|
|
48
|
+ cmOss_t* p = cmMemAllocZ(cmOss_t,1);
|
|
49
|
+
|
|
50
|
+ p->blkByteCnt = dfltBlkByteCnt==0 ? 4096 : dfltBlkByteCnt;
|
|
51
|
+
|
|
52
|
+ cmErrSetup(&p->err,&ctx->rpt,"OStrStream");
|
|
53
|
+
|
|
54
|
+ if( cmLHeapIsValid(p->lhH = cmLHeapCreate(p->blkByteCnt+sizeof(cmSsBlk_t),ctx)) == false )
|
|
55
|
+ {
|
|
56
|
+ rc = cmErrMsg(&p->err,kLHeapMemFailSsRC,"Linked heap allocation failed.");
|
|
57
|
+ goto errLabel;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ hp->h = p;
|
|
61
|
+
|
|
62
|
+ errLabel:
|
|
63
|
+ if(rc != kOkSsRC )
|
|
64
|
+ _cmOssDestroy(p);
|
|
65
|
+
|
|
66
|
+ return rc;
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+cmSsRC_t cmOStrStreamDestroy(cmStrStreamH_t* hp )
|
|
70
|
+{
|
|
71
|
+ cmSsRC_t rc = kOkSsRC;
|
|
72
|
+
|
|
73
|
+ if( hp==NULL || cmOStrStreamIsValid(*hp)==false )
|
|
74
|
+ return rc;
|
|
75
|
+
|
|
76
|
+ cmOss_t* p = _cmOssHandleToPtr(*hp);
|
|
77
|
+
|
|
78
|
+ if((rc = _cmOssDestroy(p)) != kOkSsRC )
|
|
79
|
+ return rc;
|
|
80
|
+
|
|
81
|
+ hp->h = NULL;
|
|
82
|
+
|
|
83
|
+ return rc;
|
|
84
|
+}
|
|
85
|
+
|
|
86
|
+bool cmOStrStreamIsValid( cmStrStreamH_t h )
|
|
87
|
+{ return h.h != NULL; }
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+cmSsRC_t cmOStrStreamWrite( cmStrStreamH_t h, const void* vp, unsigned byteCnt )
|
|
91
|
+{
|
|
92
|
+ cmSsRC_t rc = kOkSsRC;
|
|
93
|
+ cmOss_t* p = _cmOssHandleToPtr(h);
|
|
94
|
+ char* cp = (char*)vp;
|
|
95
|
+ do
|
|
96
|
+ {
|
|
97
|
+ // if a blk exists
|
|
98
|
+ if( p->elp != NULL )
|
|
99
|
+ {
|
|
100
|
+ // copy as much of vp[] as possible into the current end block
|
|
101
|
+ unsigned n = cmMin(byteCnt, p->blkByteCnt - p->elp->i);
|
|
102
|
+ memcpy(p->elp->blk,cp,n);
|
|
103
|
+ byteCnt -= n;
|
|
104
|
+ p->elp->i += n;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ // if all of vp[] has been copied then we are done
|
|
108
|
+ if( byteCnt == 0 )
|
|
109
|
+ break;
|
|
110
|
+
|
|
111
|
+ assert( p->elp==NULL || p->elp->i == p->blkByteCnt );
|
|
112
|
+
|
|
113
|
+ // allocate a new block
|
|
114
|
+ cmSsBlk_t* nbp = (cmSsBlk_t*)cmLHeapAlloc(p->lhH,p->blkByteCnt+sizeof(cmSsBlk_t));
|
|
115
|
+ nbp->blk = (char*)(nbp + 1);
|
|
116
|
+ nbp->i = 0;
|
|
117
|
+ nbp->link = NULL;
|
|
118
|
+
|
|
119
|
+ // end the new blk onto the end of the list
|
|
120
|
+ if( p->elp == NULL )
|
|
121
|
+ p->blp = nbp;
|
|
122
|
+ else
|
|
123
|
+ p->elp->link = nbp;
|
|
124
|
+
|
|
125
|
+ p->elp = nbp;
|
|
126
|
+
|
|
127
|
+ }while(1);
|
|
128
|
+
|
|
129
|
+ return rc;
|
|
130
|
+}
|
|
131
|
+
|
|
132
|
+cmSsRC_t cmOStrStreamWriteStr( cmStrStreamH_t h, const cmChar_t* str )
|
|
133
|
+{
|
|
134
|
+ if( str == NULL )
|
|
135
|
+ return kOkSsRC;
|
|
136
|
+
|
|
137
|
+ return cmOStrStreamWrite(h,str,strlen(str));
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+cmSsRC_t cmOStrStreamVPrintf( cmStrStreamH_t h, const cmChar_t* fmt, va_list vl )
|
|
141
|
+{
|
|
142
|
+ cmChar_t* s = cmTsVPrintfP(NULL,fmt,vl);
|
|
143
|
+ cmSsRC_t rc = cmOStrStreamWriteStr(h,s);
|
|
144
|
+ cmMemFree(s);
|
|
145
|
+ return rc;
|
|
146
|
+}
|
|
147
|
+
|
|
148
|
+cmSsRC_t cmOStrStreamPrintf( cmStrStreamH_t h, const cmChar_t* fmt, ... )
|
|
149
|
+{
|
|
150
|
+ va_list vl;
|
|
151
|
+ va_start(vl,fmt);
|
|
152
|
+ cmSsRC_t rc = cmOStrStreamVPrintf(h,fmt,vl);
|
|
153
|
+ va_end(vl);
|
|
154
|
+ return rc;
|
|
155
|
+}
|
|
156
|
+
|
|
157
|
+unsigned cmOStrStreamByteCount( cmStrStreamH_t h )
|
|
158
|
+{
|
|
159
|
+ unsigned n = 0;
|
|
160
|
+ cmOss_t* p = _cmOssHandleToPtr(h);
|
|
161
|
+ cmSsBlk_t* bp = p->blp;
|
|
162
|
+
|
|
163
|
+ for(; bp!=NULL; bp=bp->link)
|
|
164
|
+ n += bp->i;
|
|
165
|
+
|
|
166
|
+ return n;
|
|
167
|
+}
|
|
168
|
+
|
|
169
|
+unsigned _cmOssCopyBuf( cmOss_t* p, char* buf, unsigned n )
|
|
170
|
+{
|
|
171
|
+ unsigned i = 0;
|
|
172
|
+ cmSsBlk_t* bp = p->blp;
|
|
173
|
+
|
|
174
|
+ for(; bp!=NULL; bp=bp->link)
|
|
175
|
+ {
|
|
176
|
+ assert( i + bp->i <= n );
|
|
177
|
+
|
|
178
|
+ memcpy(buf+i,bp->blk,bp->i);
|
|
179
|
+ i += bp->i;
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ return i;
|
|
183
|
+}
|
|
184
|
+
|
|
185
|
+void* cmOStrStreamAllocBuf( cmStrStreamH_t h )
|
|
186
|
+{
|
|
187
|
+ unsigned n = cmOStrStreamByteCount(h);
|
|
188
|
+ cmOss_t* p = _cmOssHandleToPtr(h);
|
|
189
|
+
|
|
190
|
+ if( n == 0 )
|
|
191
|
+ return NULL;
|
|
192
|
+
|
|
193
|
+ char* buf = cmMemAlloc(char,n);
|
|
194
|
+
|
|
195
|
+ unsigned i = _cmOssCopyBuf(p,buf,n);
|
|
196
|
+
|
|
197
|
+ assert(i==n);
|
|
198
|
+
|
|
199
|
+ return buf;
|
|
200
|
+}
|
|
201
|
+
|
|
202
|
+cmChar_t* cmOStrStreamAllocText( cmStrStreamH_t h )
|
|
203
|
+{
|
|
204
|
+ unsigned n = cmOStrStreamByteCount(h);
|
|
205
|
+ cmOss_t* p = _cmOssHandleToPtr(h);
|
|
206
|
+
|
|
207
|
+ if( n == 0 )
|
|
208
|
+ return NULL;
|
|
209
|
+
|
|
210
|
+ char* buf = cmMemAlloc(char,n+1);
|
|
211
|
+
|
|
212
|
+ unsigned i = _cmOssCopyBuf(p,buf,n);
|
|
213
|
+
|
|
214
|
+ assert(i==n);
|
|
215
|
+
|
|
216
|
+ buf[n] = 0;
|
|
217
|
+
|
|
218
|
+ return buf;
|
|
219
|
+}
|