|
NCBI Home IEB Home C Toolkit docs C++ Toolkit source browser C Toolkit source browser (2) |
NCBI C Toolkit Cross ReferenceC/asnlib/asnbufo.c |
source navigation diff markup identifier search freetext search file search |
1 /* asnbufo.c
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name: asnbufo.c
27 *
28 * Author: Warren Gish
29 *
30 * Version Creation Date: 5/25/94
31 *
32 * $Revision: 6.0 $
33 *
34 * File Description:
35 * Routines to output a buffer in ASN.1
36 *
37 * Modifications:
38 * --------------------------------------------------------------------------
39 * Date Name Description of modification
40 * ------- ---------- -----------------------------------------------------
41 *
42 * $Log: asnbufo.c,v $
43 * Revision 6.0 1997/08/25 18:09:37 madden
44 * Revision changed to 6.0
45 *
46 * Revision 5.1 1996/12/03 21:43:48 vakatov
47 * Adopted for 32-bit MS-Windows DLLs
48 *
49 * Revision 5.0 1996/05/28 14:00:29 ostell
50 * Set to revision 5.0
51 *
52 * Revision 4.0 1995/07/26 13:47:38 ostell
53 * force revision to 4.0
54 *
55 * Revision 1.3 1995/05/15 18:38:28 ostell
56 * added Log line
57 *
58 *
59 *
60 * ==========================================================================
61 */
62 #include <ncbi.h>
63 #include "asnbuild.h"
64
65 /*****************************************************************************
66 *
67 * int AsnBufGetWordBreak(str, maxlen)
68 * return length (<= maxlen) of str to next white space
69 *
70 *****************************************************************************/
71 static size_t LIBCALL AsnBufGetWordBreak (CharPtr str, size_t stringlen, size_t maxlen)
72
73 {
74 register CharPtr tmp;
75 register size_t len;
76
77 if (stringlen <= maxlen)
78 return stringlen;
79
80 tmp = str + maxlen; /* point just PAST the end of region */
81 len = maxlen + 1;
82 while ((len) && (! IS_WHITESP(*tmp)))
83 {
84 len--; tmp--;
85 }
86 while ((len) && (IS_WHITESP(*tmp)))
87 {
88 len--; /* move past white space */
89 tmp--;
90 }
91 if (len < 1) /* never found any whitespace or only 1 space */
92 len = maxlen; /* have to break a word */
93
94 return len;
95 }
96
97 /*****************************************************************************
98 *
99 * void AsnPrintBuf(buf, buflen, aip)
100 *
101 *****************************************************************************/
102 static void LIBCALL AsnPrintBuf (CharPtr buf, size_t buflen, AsnIoPtr aip)
103 {
104 register size_t templen;
105 Boolean first = TRUE;
106 register CharPtr current, bufmax;
107 Boolean indent_state;
108
109 if (aip->type & ASNIO_CARRIER) /* pure iterator */
110 return;
111
112 indent_state = aip->first[aip->indent_level];
113
114 /* break it up into lines if necessary */
115 while (buflen) {
116 if (! first) { /* into multiple lines */
117 aip->first[aip->indent_level] = TRUE; /* no commas */
118 AsnPrintNewLine(aip);
119 aip->offset -= aip->linepos;
120 aip->linepos = 0;
121 }
122 first = FALSE;
123
124 templen = aip->linelength - aip->linepos;
125
126 if (buflen <= templen) /* it fits in remaining space */
127 templen = buflen;
128 else
129 templen = AsnBufGetWordBreak(buf, buflen, templen);
130
131 current = aip->linebuf + aip->linepos;
132 buflen -= templen;
133 aip->linepos += templen;
134 aip->offset += templen;
135 bufmax = buf + templen;
136 while (buf < bufmax) {
137 *current = *buf++;
138 if (*current++ == '\"') { /* must double quotes */
139 aip->linepos++;
140 aip->offset++;
141 *current++ = '\"';
142 }
143 }
144 }
145 aip->first[aip->indent_level] = indent_state; /* reset indent state */
146 return;
147 }
148
149 /*****************************************************************************
150 *
151 * void AsnPrintBufOctets(buf, buflen, aip)
152 *
153 *****************************************************************************/
154 static void LIBCALL AsnPrintBufOctets (register CharPtr buf, size_t buflen, AsnIoPtr aip)
155 {
156 register unsigned value, tval;
157 register int ctr = 0;
158 register CharPtr bufmax;
159 char obuf[100];
160
161 if (aip->type & ASNIO_CARRIER) /* pure iterator */
162 return;
163
164 AsnPrintChar('\'', aip);
165
166 /* break it up into lines if necessary */
167 bufmax = buf + buflen;
168 for (bufmax = buf + buflen; buf < bufmax; ++buf) {
169 value = *(unsigned char *)buf;
170 tval = value / 16;
171 if (tval < 10)
172 obuf[ctr] = (char)(tval + '0');
173 else
174 obuf[ctr] = (char)(tval + ('A' - 10));
175 ++ctr;
176 tval = value & 0x0f;
177 if (tval < 10)
178 obuf[ctr] = (char)(tval + '0');
179 else
180 obuf[ctr] = (char)(tval + ('A' - 10));
181 ++ctr;
182 if (ctr == DIM(obuf)) {
183 AsnPrintBuf(obuf, ctr, aip);
184 ctr = 0;
185 }
186 }
187 if (ctr > 0)
188 AsnPrintBuf(obuf, ctr, aip);
189
190 AsnPrintChar('\'', aip);
191 AsnPrintChar('H', aip);
192 return;
193 }
194
195 /*****************************************************************************
196 *
197 * void AsnTxtBufWrite(aip, atp, buf, buflen)
198 *
199 *****************************************************************************/
200 NLM_EXTERN Boolean LIBCALL AsnTxtBufWrite (AsnIoPtr aip, AsnTypePtr atp, CharPtr buf, size_t buflen)
201 {
202 Int2 isa;
203 AsnTypePtr atp2;
204 Boolean firstvalue;
205
206 if ((! aip->indent_level) && (aip->typestack[0].type == NULL))
207 firstvalue = TRUE; /* first call to this routine */
208 else
209 firstvalue = FALSE;
210
211 atp2 = AsnFindBaseType(atp);
212 isa = atp2->type->isa;
213 if (ISA_STRINGTYPE(isa))
214 isa = GENERALSTRING_TYPE;
215
216 switch (isa) {
217 case GENERALSTRING_TYPE:
218 case OCTETS_TYPE:
219 case STRSTORE_TYPE:
220 break;
221 default:
222 AsnIoErrorMsg(aip, 19, AsnErrGetTypeName(atp->name));
223 return FALSE;
224 }
225
226 if (! AsnTypeValidateOut(aip, atp, NULL))
227 return FALSE;
228
229 if (! aip->first[aip->indent_level])
230 AsnPrintNewLine(aip);
231 else
232 aip->first[aip->indent_level] = FALSE;
233
234 atp2 = atp;
235 if (firstvalue) { /* first item, need ::= */
236 while ((atp2->name == NULL) || (IS_LOWER(*atp2->name)))
237 atp2 = atp2->type; /* find a Type Reference */
238 }
239
240 if (atp2->name != NULL) {
241 AsnPrintString(atp2->name, aip); /* put the element name */
242 if (IS_LOWER(*atp2->name))
243 AsnPrintChar(' ', aip);
244 else
245 AsnPrintString(" ::= ", aip);
246 }
247
248 switch (isa) {
249 case GENERALSTRING_TYPE:
250 case STRSTORE_TYPE:
251 AsnPrintChar('\"', aip);
252 AsnPrintBuf(buf, buflen, aip);
253 AsnPrintChar('\"', aip);
254 break;
255 case OCTETS_TYPE:
256 AsnPrintBufOctets(buf, buflen, aip);
257 break;
258 default:
259 AsnIoErrorMsg(aip, 19, AsnErrGetTypeName(atp->name));
260 return FALSE;
261 }
262
263 if (aip->type_indent) { /* pop out of choice nests */
264 if (AsnFindBaseIsa(aip->typestack[aip->type_indent - 1].type) == CHOICE_TYPE) {
265 if (aip->type_indent >= 2)
266 isa = AsnFindBaseIsa(aip->typestack[aip->type_indent - 2].type);
267 else
268 isa = NULL_TYPE; /* just fake it */
269 AsnPrintIndent(FALSE, aip);
270 AsnTypeSetIndent(FALSE, aip, atp);
271 }
272 }
273 return TRUE;
274 }
275
276 /*****************************************************************************
277 *
278 * void AsnEnBinBuf(buf, buflen, aip)
279 *
280 *****************************************************************************/
281 static void LIBCALL AsnEnBinBuf (CharPtr buf, size_t buflen, AsnIoPtr aip, AsnTypePtr atp)
282 {
283 AsnTypePtr atp2;
284
285 atp2 = AsnFindBaseType(atp);
286 atp2 = atp2->type;
287 AsnEnBinTags(atp2, aip);
288
289 AsnEnBinLen((Uint4)buflen, aip);
290
291 AsnEnBinBytes(buf, (Uint4)buflen, aip);
292 return;
293 }
294
295 /*****************************************************************************
296 *
297 * Boolean AsnBinBufWrite(aip, atp, buf, buflen)
298 *
299 *****************************************************************************/
300 NLM_EXTERN Boolean LIBCALL AsnBinBufWrite (AsnIoPtr aip, AsnTypePtr atp, CharPtr buf, size_t buflen)
301 {
302 int isa;
303 AsnTypePtr atp2;
304 int next_type;
305
306 if (! AsnTypeValidateOut(aip, atp, NULL))
307 return FALSE;
308
309 atp2 = AsnFindBaseType(atp);
310 isa = atp2->type->isa;
311 if (ISA_STRINGTYPE(isa))
312 isa = GENERALSTRING_TYPE;
313
314 AsnEnBinTags(atp, aip); /* put in the tags */
315
316 switch (isa) {
317 case GENERALSTRING_TYPE:
318 case OCTETS_TYPE:
319 case STRSTORE_TYPE:
320 AsnEnBinBuf(buf, buflen, aip, atp);
321 break;
322 default:
323 AsnIoErrorMsg(aip, 19, AsnErrGetTypeName(atp->name));
324 return FALSE;
325 }
326
327 if (atp->tagclass != TAG_NONE)
328 AsnEnBinEndIndef(aip); /* put indefinite encoding */
329 next_type = aip->type_indent - 1; /* end any choices */
330 while ((next_type >= 0) &&
331 (AsnFindBaseIsa(aip->typestack[next_type].type) == CHOICE_TYPE)) {
332 if (aip->typestack[next_type].type->tagclass != TAG_NONE)
333 AsnEnBinEndIndef(aip);
334 next_type--;
335 }
336 if (AsnFindBaseIsa(aip->typestack[aip->type_indent - 1].type) == CHOICE_TYPE)
337 AsnTypeSetIndent(FALSE, aip, atp);
338 return TRUE;
339 }
340
341 /*****************************************************************************
342 *
343 * AsnBufWrite()
344 * generalized write buffer
345 *
346 *****************************************************************************/
347 NLM_EXTERN Boolean LIBCALL AsnBufWrite (AsnIoPtr aip, AsnTypePtr atp, CharPtr buf, size_t buflen)
348 {
349 Boolean retval = FALSE;
350
351 if (aip->aeop != NULL) /* exploring nodes */
352 AsnCheckExpOpt(aip, atp, NULL);
353
354 if (aip->type & ASNIO_TEXT)
355 retval = AsnTxtBufWrite(aip, atp, buf, buflen);
356 else if (aip->type & ASNIO_BIN)
357 retval = AsnBinBufWrite(aip, atp, buf, buflen);
358
359 if (aip->io_failure)
360 return FALSE;
361 return retval;
362 }
363 |
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more information. |