VirtualBox

source: vbox/trunk/src/bldprogs/scmstream.cpp@ 76530

Last change on this file since 76530 was 76506, checked in by vboxsync, 6 years ago

scm: Header guard massaging for adding #pragma once. bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.4 KB
Line 
1/* $Id: scmstream.cpp 76506 2018-12-30 03:41:21Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager Stream Code.
4 */
5
6/*
7 * Copyright (C) 2010-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <iprt/assert.h>
23#include <iprt/ctype.h>
24#include <iprt/err.h>
25#include <iprt/file.h>
26#include <iprt/handle.h>
27#include <iprt/mem.h>
28#include <iprt/pipe.h>
29#include <iprt/string.h>
30
31#include "scmstream.h"
32
33
34/**
35 * Initializes the stream structure.
36 *
37 * @param pStream The stream structure.
38 * @param fWriteOrRead The value of the fWriteOrRead stream member.
39 */
40static void scmStreamInitInternal(PSCMSTREAM pStream, bool fWriteOrRead)
41{
42 pStream->pch = NULL;
43 pStream->off = 0;
44 pStream->cb = 0;
45 pStream->cbAllocated = 0;
46
47 pStream->paLines = NULL;
48 pStream->iLine = 0;
49 pStream->cLines = 0;
50 pStream->cLinesAllocated = 0;
51
52 pStream->fWriteOrRead = fWriteOrRead;
53 pStream->fFileMemory = false;
54 pStream->fFullyLineated = false;
55
56 pStream->rc = VINF_SUCCESS;
57}
58
59/**
60 * Initialize an input stream.
61 *
62 * @returns IPRT status code.
63 * @param pStream The stream to initialize.
64 * @param pszFilename The file to take the stream content from.
65 */
66int ScmStreamInitForReading(PSCMSTREAM pStream, const char *pszFilename)
67{
68 scmStreamInitInternal(pStream, false /*fWriteOrRead*/);
69
70 void *pvFile;
71 size_t cbFile;
72 int rc = pStream->rc = RTFileReadAll(pszFilename, &pvFile, &cbFile);
73 if (RT_SUCCESS(rc))
74 {
75 pStream->pch = (char *)pvFile;
76 pStream->cb = cbFile;
77 pStream->cbAllocated = cbFile;
78 pStream->fFileMemory = true;
79 }
80 return rc;
81}
82
83/**
84 * Initialize an output stream.
85 *
86 * @returns IPRT status code
87 * @param pStream The stream to initialize.
88 * @param pRelatedStream Pointer to a related stream. NULL is fine.
89 */
90int ScmStreamInitForWriting(PSCMSTREAM pStream, PCSCMSTREAM pRelatedStream)
91{
92 scmStreamInitInternal(pStream, true /*fWriteOrRead*/);
93
94 /* allocate stuff */
95 size_t cbEstimate = !pRelatedStream ? _64K
96 : pRelatedStream->cb > 0 ? pRelatedStream->cb + pRelatedStream->cb / 10 : 64;
97 cbEstimate = RT_ALIGN(cbEstimate, _4K);
98 pStream->pch = (char *)RTMemAlloc(cbEstimate);
99 if (pStream->pch)
100 {
101 size_t cLinesEstimate = pRelatedStream && pRelatedStream->fFullyLineated
102 ? pRelatedStream->cLines + pRelatedStream->cLines / 10
103 : cbEstimate / 24;
104 cLinesEstimate = RT_ALIGN(cLinesEstimate, 512);
105 if (cLinesEstimate == 0)
106 cLinesEstimate = 16;
107 pStream->paLines = (PSCMSTREAMLINE)RTMemAlloc(cLinesEstimate * sizeof(SCMSTREAMLINE));
108 if (pStream->paLines)
109 {
110 pStream->paLines[0].off = 0;
111 pStream->paLines[0].cch = 0;
112 pStream->paLines[0].enmEol = SCMEOL_NONE;
113 pStream->cbAllocated = cbEstimate;
114 pStream->cLinesAllocated = cLinesEstimate;
115 return VINF_SUCCESS;
116 }
117
118 RTMemFree(pStream->pch);
119 pStream->pch = NULL;
120 }
121 return pStream->rc = VERR_NO_MEMORY;
122}
123
124/**
125 * Frees the resources associated with the stream.
126 *
127 * Nothing is happens to whatever the stream was initialized from or dumped to.
128 *
129 * @param pStream The stream to delete.
130 */
131void ScmStreamDelete(PSCMSTREAM pStream)
132{
133 if (pStream->pch)
134 {
135 if (pStream->fFileMemory)
136 RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
137 else
138 RTMemFree(pStream->pch);
139 pStream->pch = NULL;
140 }
141 pStream->cbAllocated = 0;
142
143 if (pStream->paLines)
144 {
145 RTMemFree(pStream->paLines);
146 pStream->paLines = NULL;
147 }
148 pStream->cLinesAllocated = 0;
149}
150
151/**
152 * Get the stream status code.
153 *
154 * @returns IPRT status code.
155 * @param pStream The stream.
156 */
157int ScmStreamGetStatus(PCSCMSTREAM pStream)
158{
159 return pStream->rc;
160}
161
162/**
163 * Grows the buffer of a write stream.
164 *
165 * @returns IPRT status code.
166 * @param pStream The stream. Must be in write mode.
167 * @param cbAppending The minimum number of bytes to grow the buffer
168 * with.
169 */
170static int scmStreamGrowBuffer(PSCMSTREAM pStream, size_t cbAppending)
171{
172 size_t cbAllocated = pStream->cbAllocated;
173 cbAllocated += RT_MAX(0x1000 + cbAppending, cbAllocated);
174 cbAllocated = RT_ALIGN(cbAllocated, 0x1000);
175 void *pvNew;
176 if (!pStream->fFileMemory)
177 {
178 pvNew = RTMemRealloc(pStream->pch, cbAllocated);
179 if (!pvNew)
180 return pStream->rc = VERR_NO_MEMORY;
181 }
182 else
183 {
184 pvNew = RTMemDupEx(pStream->pch, pStream->off, cbAllocated - pStream->off);
185 if (!pvNew)
186 return pStream->rc = VERR_NO_MEMORY;
187 RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
188 pStream->fFileMemory = false;
189 }
190 pStream->pch = (char *)pvNew;
191 pStream->cbAllocated = cbAllocated;
192
193 return VINF_SUCCESS;
194}
195
196/**
197 * Grows the line array of a stream.
198 *
199 * @returns IPRT status code.
200 * @param pStream The stream.
201 * @param iMinLine Minimum line number.
202 */
203static int scmStreamGrowLines(PSCMSTREAM pStream, size_t iMinLine)
204{
205 size_t cLinesAllocated = pStream->cLinesAllocated;
206 cLinesAllocated += RT_MAX(512 + iMinLine, cLinesAllocated);
207 cLinesAllocated = RT_ALIGN(cLinesAllocated, 512);
208 void *pvNew = RTMemRealloc(pStream->paLines, cLinesAllocated * sizeof(SCMSTREAMLINE));
209 if (!pvNew)
210 return pStream->rc = VERR_NO_MEMORY;
211
212 pStream->paLines = (PSCMSTREAMLINE)pvNew;
213 pStream->cLinesAllocated = cLinesAllocated;
214 return VINF_SUCCESS;
215}
216
217/**
218 * Rewinds the stream and sets the mode to read.
219 *
220 * @param pStream The stream.
221 */
222void ScmStreamRewindForReading(PSCMSTREAM pStream)
223{
224 pStream->off = 0;
225 pStream->iLine = 0;
226 pStream->fWriteOrRead = false;
227 pStream->rc = VINF_SUCCESS;
228}
229
230/**
231 * Rewinds the stream and sets the mode to write.
232 *
233 * @param pStream The stream.
234 */
235void ScmStreamRewindForWriting(PSCMSTREAM pStream)
236{
237 pStream->off = 0;
238 pStream->iLine = 0;
239 pStream->cLines = 0;
240 pStream->fWriteOrRead = true;
241 pStream->fFullyLineated = true;
242 pStream->rc = VINF_SUCCESS;
243
244 /* Initialize the first line with a zero length so ScmStreamWrite won't misbehave. */
245 if (pStream->cLinesAllocated == 0)
246 scmStreamGrowLines(pStream, 1);
247 if (pStream->cLinesAllocated > 0)
248 {
249 pStream->paLines[0].off = 0;
250 pStream->paLines[0].cch = 0;
251 pStream->paLines[0].enmEol = SCMEOL_NONE;
252 }
253}
254
255/**
256 * Checks if it's a text stream.
257 *
258 * Not 100% proof.
259 *
260 * @returns true if it probably is a text file, false if not.
261 * @param pStream The stream. Write or read, doesn't matter.
262 */
263bool ScmStreamIsText(PSCMSTREAM pStream)
264{
265 if (RTStrEnd(pStream->pch, pStream->cb))
266 return false;
267 if (!pStream->cb)
268 return true;
269 return true;
270}
271
272/**
273 * Performs an integrity check of the stream.
274 *
275 * @returns IPRT status code.
276 * @param pStream The stream.
277 */
278int ScmStreamCheckItegrity(PSCMSTREAM pStream)
279{
280 /*
281 * Perform sanity checks.
282 */
283 size_t const cbFile = pStream->cb;
284 for (size_t iLine = 0; iLine < pStream->cLines; iLine++)
285 {
286 size_t offEol = pStream->paLines[iLine].off + pStream->paLines[iLine].cch;
287 AssertReturn(offEol + pStream->paLines[iLine].enmEol <= cbFile, VERR_INTERNAL_ERROR_2);
288 switch (pStream->paLines[iLine].enmEol)
289 {
290 case SCMEOL_LF:
291 AssertReturn(pStream->pch[offEol] == '\n', VERR_INTERNAL_ERROR_3);
292 break;
293 case SCMEOL_CRLF:
294 AssertReturn(pStream->pch[offEol] == '\r', VERR_INTERNAL_ERROR_3);
295 AssertReturn(pStream->pch[offEol + 1] == '\n', VERR_INTERNAL_ERROR_3);
296 break;
297 case SCMEOL_NONE:
298 AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_4);
299 break;
300 default:
301 AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_5);
302 }
303 }
304 return VINF_SUCCESS;
305}
306
307/**
308 * Writes the stream to a file.
309 *
310 * @returns IPRT status code
311 * @param pStream The stream.
312 * @param pszFilenameFmt The filename format string.
313 * @param ... Format arguments.
314 */
315int ScmStreamWriteToFile(PSCMSTREAM pStream, const char *pszFilenameFmt, ...)
316{
317 int rc;
318
319#ifdef RT_STRICT
320 /*
321 * Check that what we're going to write makes sense first.
322 */
323 rc = ScmStreamCheckItegrity(pStream);
324 if (RT_FAILURE(rc))
325 return rc;
326#endif
327
328 /*
329 * Do the actual writing.
330 */
331 RTFILE hFile;
332 va_list va;
333 va_start(va, pszFilenameFmt);
334 rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
335 if (RT_SUCCESS(rc))
336 {
337 rc = RTFileWrite(hFile, pStream->pch, pStream->cb, NULL);
338 RTFileClose(hFile);
339 }
340 va_end(va);
341 return rc;
342}
343
344/**
345 * Writes the stream to standard output.
346 *
347 * @returns IPRT status code
348 * @param pStream The stream.
349 */
350int ScmStreamWriteToStdOut(PSCMSTREAM pStream)
351{
352 int rc;
353
354#ifdef RT_STRICT
355 /*
356 * Check that what we're going to write makes sense first.
357 */
358 rc = ScmStreamCheckItegrity(pStream);
359 if (RT_FAILURE(rc))
360 return rc;
361#endif
362
363 /*
364 * Do the actual writing.
365 */
366 RTHANDLE h;
367 rc = RTHandleGetStandard(RTHANDLESTD_OUTPUT, &h);
368 if (RT_SUCCESS(rc))
369 {
370 switch (h.enmType)
371 {
372 case RTHANDLETYPE_FILE:
373 rc = RTFileWrite(h.u.hFile, pStream->pch, pStream->cb, NULL);
374 break;
375 case RTHANDLETYPE_PIPE:
376 rc = RTPipeWriteBlocking(h.u.hPipe, pStream->pch, pStream->cb, NULL);
377 break;
378 default:
379 rc = VERR_INVALID_HANDLE;
380 break;
381 }
382 }
383 return rc;
384}
385
386/**
387 * Worker for ScmStreamGetLine that builds the line number index while parsing
388 * the stream.
389 *
390 * @returns Same as SCMStreamGetLine.
391 * @param pStream The stream. Must be in read mode.
392 * @param pcchLine Where to return the line length.
393 * @param penmEol Where to return the kind of end of line marker.
394 */
395static const char *scmStreamGetLineInternal(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
396{
397 AssertReturn(!pStream->fWriteOrRead, NULL);
398 if (RT_FAILURE(pStream->rc))
399 return NULL;
400
401 size_t off = pStream->off;
402 size_t cb = pStream->cb;
403 if (RT_UNLIKELY(off >= cb))
404 {
405 pStream->fFullyLineated = true;
406 return NULL;
407 }
408
409 size_t iLine = pStream->iLine;
410 if (RT_UNLIKELY(iLine >= pStream->cLinesAllocated))
411 {
412 int rc = scmStreamGrowLines(pStream, iLine);
413 if (RT_FAILURE(rc))
414 return NULL;
415 }
416 pStream->paLines[iLine].off = off;
417
418 cb -= off;
419 const char *pchRet = &pStream->pch[off];
420 const char *pch = (const char *)memchr(pchRet, '\n', cb);
421 if (RT_LIKELY(pch))
422 {
423 cb = pch - pchRet;
424 pStream->off = off + cb + 1;
425 if ( cb < 1
426 || pch[-1] != '\r')
427 pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_LF;
428 else
429 {
430 pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_CRLF;
431 cb--;
432 }
433 }
434 else
435 {
436 pStream->off = off + cb;
437 pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_NONE;
438 }
439 *pcchLine = cb;
440 pStream->paLines[iLine].cch = cb;
441 pStream->cLines = pStream->iLine = ++iLine;
442
443 return pchRet;
444}
445
446/**
447 * Internal worker that delineates a stream.
448 *
449 * @returns IPRT status code.
450 * @param pStream The stream. Caller must check that it is in
451 * read mode.
452 */
453static int scmStreamLineate(PSCMSTREAM pStream)
454{
455 /* Save the stream position. */
456 size_t const offSaved = pStream->off;
457 size_t const iLineSaved = pStream->iLine;
458
459 /* Get each line. */
460 size_t cchLine;
461 SCMEOL enmEol;
462 while (scmStreamGetLineInternal(pStream, &cchLine, &enmEol))
463 /* nothing */;
464 Assert(RT_FAILURE(pStream->rc) || pStream->fFullyLineated);
465
466 /* Restore the position */
467 pStream->off = offSaved;
468 pStream->iLine = iLineSaved;
469
470 return pStream->rc;
471}
472
473/**
474 * Get the current stream position as an byte offset.
475 *
476 * @returns The current byte offset
477 * @param pStream The stream.
478 */
479size_t ScmStreamTell(PSCMSTREAM pStream)
480{
481 return pStream->off;
482}
483
484/**
485 * Get the current stream position as a line number.
486 *
487 * @returns The current line (0-based).
488 * @param pStream The stream.
489 */
490size_t ScmStreamTellLine(PSCMSTREAM pStream)
491{
492 return pStream->iLine;
493}
494
495
496/**
497 * Gets the stream offset of a given line.
498 *
499 * @returns The offset of the line, or the stream size if the line number is too
500 * high.
501 * @param pStream The stream. Must be in read mode.
502 * @param iLine The line we're asking about.
503 */
504size_t ScmStreamTellOffsetOfLine(PSCMSTREAM pStream, size_t iLine)
505{
506 AssertReturn(!pStream->fWriteOrRead, pStream->cb);
507 if (!pStream->fFullyLineated)
508 {
509 int rc = scmStreamLineate(pStream);
510 AssertRCReturn(rc, pStream->cb);
511 }
512 if (iLine >= pStream->cLines)
513 return pStream->cb;
514 return pStream->paLines[iLine].off;
515}
516
517
518/**
519 * Get the current stream size in bytes.
520 *
521 * @returns Count of bytes.
522 * @param pStream The stream.
523 */
524size_t ScmStreamSize(PSCMSTREAM pStream)
525{
526 return pStream->cb;
527}
528
529/**
530 * Gets the number of lines in the stream.
531 *
532 * @returns The number of lines.
533 * @param pStream The stream.
534 */
535size_t ScmStreamCountLines(PSCMSTREAM pStream)
536{
537 if (!pStream->fFullyLineated)
538 scmStreamLineate(pStream);
539 return pStream->cLines;
540}
541
542/**
543 * Seeks to a given byte offset in the stream.
544 *
545 * @returns IPRT status code.
546 * @retval VERR_SEEK if the new stream position is the middle of an EOL marker.
547 * This is a temporary restriction.
548 *
549 * @param pStream The stream. Must be in read mode.
550 * @param offAbsolute The offset to seek to. If this is beyond the
551 * end of the stream, the position is set to the
552 * end.
553 */
554int ScmStreamSeekAbsolute(PSCMSTREAM pStream, size_t offAbsolute)
555{
556 AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
557 if (RT_FAILURE(pStream->rc))
558 return pStream->rc;
559
560 /* Must be fully delineated. (lazy bird) */
561 if (RT_UNLIKELY(!pStream->fFullyLineated))
562 {
563 int rc = scmStreamLineate(pStream);
564 if (RT_FAILURE(rc))
565 return rc;
566 }
567
568 /* Ok, do the job. */
569 if (offAbsolute < pStream->cb)
570 {
571 /** @todo Should do a binary search here, but I'm too darn lazy tonight. */
572 pStream->off = ~(size_t)0;
573 for (size_t i = 0; i < pStream->cLines; i++)
574 {
575 if (offAbsolute < pStream->paLines[i].off + pStream->paLines[i].cch + pStream->paLines[i].enmEol)
576 {
577 pStream->off = offAbsolute;
578 pStream->iLine = i;
579 if (offAbsolute > pStream->paLines[i].off + pStream->paLines[i].cch)
580 return pStream->rc = VERR_SEEK;
581 break;
582 }
583 }
584 AssertReturn(pStream->off != ~(size_t)0, pStream->rc = VERR_INTERNAL_ERROR_3);
585 }
586 else
587 {
588 pStream->off = pStream->cb;
589 pStream->iLine = pStream->cLines;
590 }
591 return VINF_SUCCESS;
592}
593
594
595/**
596 * Seeks a number of bytes relative to the current stream position.
597 *
598 * @returns IPRT status code.
599 * @retval VERR_SEEK if the new stream position is the middle of an EOL marker.
600 * This is a temporary restriction.
601 *
602 * @param pStream The stream. Must be in read mode.
603 * @param offRelative The offset to seek to. A negative offset
604 * rewinds and positive one fast forwards the
605 * stream. Will quietly stop at the beginning and
606 * end of the stream.
607 */
608int ScmStreamSeekRelative(PSCMSTREAM pStream, ssize_t offRelative)
609{
610 size_t offAbsolute;
611 if (offRelative >= 0)
612 offAbsolute = pStream->off + offRelative;
613 else if ((size_t)-offRelative <= pStream->off)
614 offAbsolute = pStream->off + offRelative;
615 else
616 offAbsolute = 0;
617 return ScmStreamSeekAbsolute(pStream, offAbsolute);
618}
619
620/**
621 * Seeks to a given line in the stream.
622 *
623 * @returns IPRT status code.
624 *
625 * @param pStream The stream. Must be in read mode.
626 * @param iLine The line to seek to. If this is beyond the end
627 * of the stream, the position is set to the end.
628 */
629int ScmStreamSeekByLine(PSCMSTREAM pStream, size_t iLine)
630{
631 AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
632 if (RT_FAILURE(pStream->rc))
633 return pStream->rc;
634
635 /* Must be fully delineated. (lazy bird) */
636 if (RT_UNLIKELY(!pStream->fFullyLineated))
637 {
638 int rc = scmStreamLineate(pStream);
639 if (RT_FAILURE(rc))
640 return rc;
641 }
642
643 /* Ok, do the job. */
644 if (iLine < pStream->cLines)
645 {
646 pStream->off = pStream->paLines[iLine].off;
647 pStream->iLine = iLine;
648 }
649 else
650 {
651 pStream->off = pStream->cb;
652 pStream->iLine = pStream->cLines;
653 }
654 return VINF_SUCCESS;
655}
656
657/**
658 * Checks if the stream position is at the start of a line.
659 *
660 * @returns @c true if at the start, @c false if not.
661 * @param pStream The stream.
662 */
663bool ScmStreamIsAtStartOfLine(PSCMSTREAM pStream)
664{
665 if ( !pStream->fFullyLineated
666 && !pStream->fWriteOrRead)
667 {
668 int rc = scmStreamLineate(pStream);
669 if (RT_FAILURE(rc))
670 return false;
671 }
672 return pStream->off == pStream->paLines[pStream->iLine].off;
673}
674
675/**
676 * Worker for ScmStreamGetLineByNo and ScmStreamGetLine.
677 *
678 * Works on a fully lineated stream.
679 *
680 * @returns Pointer to the first character in the line, not NULL terminated.
681 * NULL if the end of the stream has been reached or some problem
682 * occurred.
683 *
684 * @param pStream The stream. Must be in read mode.
685 * @param iLine The line to get (0-based).
686 * @param pcchLine The length.
687 * @param penmEol Where to return the end of line type indicator.
688 */
689DECLINLINE(const char *) scmStreamGetLineByNoCommon(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
690{
691 Assert(!pStream->fWriteOrRead);
692 Assert(pStream->fFullyLineated);
693
694 /* Check stream status. */
695 if (RT_SUCCESS(pStream->rc))
696 {
697 /* Not at the end of the stream yet? */
698 if (RT_LIKELY(iLine < pStream->cLines))
699 {
700 /* Get the data. */
701 const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
702 *pcchLine = pStream->paLines[iLine].cch;
703 *penmEol = pStream->paLines[iLine].enmEol;
704
705 /* update the stream position. */
706 pStream->off = pStream->paLines[iLine].off + pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
707 pStream->iLine = iLine + 1;
708 return pchRet;
709 }
710 pStream->off = pStream->cb;
711 pStream->iLine = pStream->cLines;
712 }
713 *pcchLine = 0;
714 *penmEol = SCMEOL_NONE;
715 return NULL;
716}
717
718
719/**
720 * Get a numbered line from the stream (changes the position).
721 *
722 * A line is always delimited by a LF character or the end of the stream. The
723 * delimiter is not included in returned line length, but instead returned via
724 * the @a penmEol indicator.
725 *
726 * @returns Pointer to the first character in the line, not NULL terminated.
727 * NULL if the end of the stream has been reached or some problem
728 * occurred (*pcchLine set to zero and *penmEol to SCMEOL_NONE).
729 *
730 * @param pStream The stream. Must be in read mode.
731 * @param iLine The line to get (0-based).
732 * @param pcchLine The length.
733 * @param penmEol Where to return the end of line type indicator.
734 */
735const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
736{
737 AssertReturn(!pStream->fWriteOrRead, NULL);
738
739 /* Make sure it's fully delineated so we can use the index. */
740 if (RT_LIKELY(pStream->fFullyLineated))
741 return scmStreamGetLineByNoCommon(pStream, iLine, pcchLine, penmEol);
742
743 int rc = pStream->rc;
744 if (RT_SUCCESS(rc))
745 {
746 rc = scmStreamLineate(pStream);
747 if (RT_SUCCESS(rc))
748 return scmStreamGetLineByNoCommon(pStream, iLine, pcchLine, penmEol);
749 }
750
751 *pcchLine = 0;
752 *penmEol = SCMEOL_NONE;
753 return NULL;
754}
755
756/**
757 * Get a line from the stream.
758 *
759 * A line is always delimited by a LF character or the end of the stream. The
760 * delimiter is not included in returned line length, but instead returned via
761 * the @a penmEol indicator.
762 *
763 * @returns Pointer to the first character in the line, not NULL terminated.
764 * NULL if the end of the stream has been reached or some problem
765 * occurred (*pcchLine set to zero and *penmEol to SCMEOL_NONE).
766 *
767 * @param pStream The stream. Must be in read mode.
768 * @param pcchLine The length.
769 * @param penmEol Where to return the end of line type indicator.
770 */
771const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
772{
773 if (RT_LIKELY(pStream->fFullyLineated))
774 {
775 size_t offCur = pStream->off;
776 size_t iCurLine = pStream->iLine;
777 const char *pszLine = scmStreamGetLineByNoCommon(pStream, iCurLine, pcchLine, penmEol);
778 if ( pszLine
779 && offCur > pStream->paLines[iCurLine].off)
780 {
781 offCur -= pStream->paLines[iCurLine].off;
782 Assert(offCur <= pStream->paLines[iCurLine].cch + pStream->paLines[iCurLine].enmEol);
783 if (offCur < pStream->paLines[iCurLine].cch)
784 *pcchLine -= offCur;
785 else
786 *pcchLine = 0;
787 pszLine += offCur;
788 }
789 return pszLine;
790 }
791 return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
792}
793
794/**
795 * Get the current buffer pointer.
796 *
797 * @returns Buffer pointer on success, NULL on failure (asserted).
798 * @param pStream The stream. Must be in read mode.
799 */
800const char *ScmStreamGetCur(PSCMSTREAM pStream)
801{
802 AssertReturn(!pStream->fWriteOrRead, NULL);
803 return pStream->pch + pStream->off;
804}
805
806/**
807 * Gets a character from the stream.
808 *
809 * @returns The next unsigned character in the stream.
810 * ~(unsigned)0 on failure.
811 * @param pStream The stream. Must be in read mode.
812 */
813unsigned ScmStreamGetCh(PSCMSTREAM pStream)
814{
815 /* Check stream state. */
816 AssertReturn(!pStream->fWriteOrRead, ~(unsigned)0);
817 if (RT_FAILURE(pStream->rc))
818 return ~(unsigned)0;
819 if (RT_UNLIKELY(!pStream->fFullyLineated))
820 {
821 int rc = scmStreamLineate(pStream);
822 if (RT_FAILURE(rc))
823 return ~(unsigned)0;
824 }
825
826 /* If there isn't enough stream left, fail already. */
827 if (RT_UNLIKELY(pStream->off >= pStream->cb))
828 return ~(unsigned)0;
829
830 /* Read a character. */
831 char ch = pStream->pch[pStream->off++];
832
833 /* Advance the line indicator. */
834 size_t iLine = pStream->iLine;
835 if (pStream->off >= pStream->paLines[iLine].off + pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol)
836 pStream->iLine++;
837
838 return (unsigned)ch;
839}
840
841
842/**
843 * Peeks at the next character from the stream.
844 *
845 * @returns The next unsigned character in the stream.
846 * ~(unsigned)0 on failure.
847 * @param pStream The stream. Must be in read mode.
848 */
849unsigned ScmStreamPeekCh(PSCMSTREAM pStream)
850{
851 /* Check stream state. */
852 AssertReturn(!pStream->fWriteOrRead, ~(unsigned)0);
853 if (RT_FAILURE(pStream->rc))
854 return ~(unsigned)0;
855 if (RT_UNLIKELY(!pStream->fFullyLineated))
856 {
857 int rc = scmStreamLineate(pStream);
858 if (RT_FAILURE(rc))
859 return ~(unsigned)0;
860 }
861
862 /* If there isn't enough stream left, fail already. */
863 if (RT_UNLIKELY(pStream->off >= pStream->cb))
864 return ~(unsigned)0;
865
866 /* Peek at the next character. */
867 char ch = pStream->pch[pStream->off];
868 return (unsigned)ch;
869}
870
871
872/**
873 * Reads @a cbToRead bytes into @a pvBuf.
874 *
875 * Will fail if end of stream is encountered before the entire read has been
876 * completed.
877 *
878 * @returns IPRT status code.
879 * @retval VERR_EOF if there isn't @a cbToRead bytes left to read. Stream
880 * position will be unchanged.
881 *
882 * @param pStream The stream. Must be in read mode.
883 * @param pvBuf The buffer to read into.
884 * @param cbToRead The number of bytes to read.
885 */
886int ScmStreamRead(PSCMSTREAM pStream, void *pvBuf, size_t cbToRead)
887{
888 AssertReturn(!pStream->fWriteOrRead, VERR_PERMISSION_DENIED);
889 if (RT_FAILURE(pStream->rc))
890 return pStream->rc;
891
892 /* If there isn't enough stream left, fail already. */
893 if (RT_UNLIKELY(pStream->cb - pStream->off < cbToRead))
894 return VERR_EOF;
895
896 /* Copy the data and simply seek to the new stream position. */
897 memcpy(pvBuf, &pStream->pch[pStream->off], cbToRead);
898 return ScmStreamSeekAbsolute(pStream, pStream->off + cbToRead);
899}
900
901
902/**
903 * Checks if the given line is empty or full of white space.
904 *
905 * @returns true if white space only, false if not (or if non-existant).
906 * @param pStream The stream. Must be in read mode.
907 * @param iLine The line in question.
908 */
909bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
910{
911 SCMEOL enmEol;
912 size_t cchLine;
913 const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
914 if (!pchLine)
915 return false;
916 while (cchLine && RT_C_IS_SPACE(*pchLine))
917 pchLine++, cchLine--;
918 return cchLine == 0;
919}
920
921/**
922 * Try figure out the end of line style of the give stream.
923 *
924 * @returns Most likely end of line style.
925 * @param pStream The stream.
926 */
927SCMEOL ScmStreamGetEol(PSCMSTREAM pStream)
928{
929 SCMEOL enmEol;
930 if (pStream->cLines > 0)
931 enmEol = pStream->paLines[0].enmEol;
932 else if (pStream->cb == 0)
933 enmEol = SCMEOL_NONE;
934 else
935 {
936 const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
937 if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
938 enmEol = SCMEOL_CRLF;
939 else
940 enmEol = SCMEOL_LF;
941 }
942
943 if (enmEol == SCMEOL_NONE)
944#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
945 enmEol = SCMEOL_CRLF;
946#else
947 enmEol = SCMEOL_LF;
948#endif
949 return enmEol;
950}
951
952/**
953 * Get the end of line indicator type for a line.
954 *
955 * @returns The EOL indicator. If the line isn't found, the default EOL
956 * indicator is return.
957 * @param pStream The stream.
958 * @param iLine The line (0-base).
959 */
960SCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
961{
962 SCMEOL enmEol;
963 if (iLine < pStream->cLines)
964 enmEol = pStream->paLines[iLine].enmEol;
965 else
966#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
967 enmEol = SCMEOL_CRLF;
968#else
969 enmEol = SCMEOL_LF;
970#endif
971 return enmEol;
972}
973
974/**
975 * Appends a line to the stream.
976 *
977 * @returns IPRT status code.
978 * @param pStream The stream. Must be in write mode.
979 * @param pchLine Pointer to the line.
980 * @param cchLine Line length.
981 * @param enmEol Which end of line indicator to use.
982 */
983int ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
984{
985 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
986 if (RT_FAILURE(pStream->rc))
987 return pStream->rc;
988
989 /*
990 * Make sure the previous line has a new-line indicator.
991 */
992 size_t off = pStream->off;
993 size_t iLine = pStream->iLine;
994 if (RT_UNLIKELY( iLine != 0
995 && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
996 {
997 AssertReturn(pStream->paLines[iLine].cch == 0, VERR_INTERNAL_ERROR_3);
998 SCMEOL enmEol2 = enmEol != SCMEOL_NONE ? enmEol : ScmStreamGetEol(pStream);
999 if (RT_UNLIKELY(off + cchLine + enmEol + enmEol2 > pStream->cbAllocated))
1000 {
1001 int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
1002 if (RT_FAILURE(rc))
1003 return rc;
1004 }
1005 if (enmEol2 == SCMEOL_LF)
1006 pStream->pch[off++] = '\n';
1007 else
1008 {
1009 pStream->pch[off++] = '\r';
1010 pStream->pch[off++] = '\n';
1011 }
1012 pStream->paLines[iLine - 1].enmEol = enmEol2;
1013 pStream->paLines[iLine].off = off;
1014 pStream->off = off;
1015 pStream->cb = off;
1016 }
1017
1018 /*
1019 * Ensure we've got sufficient buffer space.
1020 */
1021 if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
1022 {
1023 int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
1024 if (RT_FAILURE(rc))
1025 return rc;
1026 }
1027
1028 /*
1029 * Add a line record.
1030 */
1031 if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
1032 {
1033 int rc = scmStreamGrowLines(pStream, iLine);
1034 if (RT_FAILURE(rc))
1035 return rc;
1036 }
1037
1038 pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
1039 pStream->paLines[iLine].enmEol = enmEol;
1040
1041 iLine++;
1042 pStream->cLines = iLine;
1043 pStream->iLine = iLine;
1044
1045 /*
1046 * Copy the line
1047 */
1048 memcpy(&pStream->pch[off], pchLine, cchLine);
1049 off += cchLine;
1050 if (enmEol == SCMEOL_LF)
1051 pStream->pch[off++] = '\n';
1052 else if (enmEol == SCMEOL_CRLF)
1053 {
1054 pStream->pch[off++] = '\r';
1055 pStream->pch[off++] = '\n';
1056 }
1057 pStream->off = off;
1058 pStream->cb = off;
1059
1060 /*
1061 * Start a new line.
1062 */
1063 pStream->paLines[iLine].off = off;
1064 pStream->paLines[iLine].cch = 0;
1065 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1066
1067 return VINF_SUCCESS;
1068}
1069
1070/**
1071 * Writes to the stream.
1072 *
1073 * @returns IPRT status code
1074 * @param pStream The stream. Must be in write mode.
1075 * @param pchBuf What to write.
1076 * @param cchBuf How much to write.
1077 */
1078int ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
1079{
1080 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1081 if (RT_FAILURE(pStream->rc))
1082 return pStream->rc;
1083
1084 /*
1085 * Ensure we've got sufficient buffer space.
1086 */
1087 size_t off = pStream->off;
1088 if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
1089 {
1090 int rc = scmStreamGrowBuffer(pStream, cchBuf);
1091 if (RT_FAILURE(rc))
1092 return rc;
1093 }
1094
1095 /*
1096 * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
1097 */
1098 size_t iLine = pStream->iLine;
1099 if (RT_UNLIKELY( iLine > 0
1100 && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
1101 {
1102 iLine--;
1103 pStream->cLines = iLine;
1104 pStream->iLine = iLine;
1105 }
1106
1107 /*
1108 * Deal with lines.
1109 */
1110 const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
1111 if (!pchLF)
1112 pStream->paLines[iLine].cch += cchBuf;
1113 else
1114 {
1115 const char *pchLine = pchBuf;
1116 for (;;)
1117 {
1118 if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
1119 {
1120 int rc = scmStreamGrowLines(pStream, iLine);
1121 if (RT_FAILURE(rc))
1122 {
1123 iLine = pStream->iLine;
1124 pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
1125 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1126 return rc;
1127 }
1128 }
1129
1130 size_t cchLine = pchLF - pchLine;
1131 if ( cchLine
1132 ? pchLF[-1] != '\r'
1133 : !pStream->paLines[iLine].cch
1134 || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
1135 pStream->paLines[iLine].enmEol = SCMEOL_LF;
1136 else
1137 {
1138 pStream->paLines[iLine].enmEol = SCMEOL_CRLF;
1139 cchLine--;
1140 }
1141 pStream->paLines[iLine].cch += cchLine;
1142
1143 iLine++;
1144 size_t offBuf = pchLF + 1 - pchBuf;
1145 pStream->paLines[iLine].off = off + offBuf;
1146 pStream->paLines[iLine].cch = 0;
1147 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1148
1149 size_t cchLeft = cchBuf - offBuf;
1150 pchLine = pchLF + 1;
1151 pchLF = (const char *)memchr(pchLine, '\n', cchLeft);
1152 if (!pchLF)
1153 {
1154 pStream->paLines[iLine].cch = cchLeft;
1155 break;
1156 }
1157 }
1158
1159 pStream->iLine = iLine;
1160 pStream->cLines = iLine;
1161 }
1162
1163 /*
1164 * Copy the data and update position and size.
1165 */
1166 memcpy(&pStream->pch[off], pchBuf, cchBuf);
1167 off += cchBuf;
1168 pStream->off = off;
1169 pStream->cb = off;
1170
1171 return VINF_SUCCESS;
1172}
1173
1174/**
1175 * Write a character to the stream.
1176 *
1177 * @returns IPRT status code
1178 * @param pStream The stream. Must be in write mode.
1179 * @param pchBuf What to write.
1180 * @param cchBuf How much to write.
1181 */
1182int ScmStreamPutCh(PSCMSTREAM pStream, char ch)
1183{
1184 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1185 if (RT_FAILURE(pStream->rc))
1186 return pStream->rc;
1187
1188 /*
1189 * Only deal with the simple cases here, use ScmStreamWrite for the
1190 * annoying stuff.
1191 */
1192 size_t off = pStream->off;
1193 if ( ch == '\n'
1194 || RT_UNLIKELY(off + 1 > pStream->cbAllocated))
1195 return ScmStreamWrite(pStream, &ch, 1);
1196
1197 /*
1198 * Just append it.
1199 */
1200 pStream->pch[off] = ch;
1201 pStream->off = off + 1;
1202 pStream->paLines[pStream->iLine].cch++;
1203
1204 return VINF_SUCCESS;
1205}
1206
1207/**
1208 * Puts an EOL marker to the stream.
1209 *
1210 * @returns IPRt status code.
1211 * @param pStream The stream. Must be in write mode.
1212 * @param enmEol The end-of-line marker to write.
1213 */
1214int ScmStreamPutEol(PSCMSTREAM pStream, SCMEOL enmEol)
1215{
1216 if (enmEol == SCMEOL_LF)
1217 return ScmStreamWrite(pStream, "\n", 1);
1218 if (enmEol == SCMEOL_CRLF)
1219 return ScmStreamWrite(pStream, "\r\n", 2);
1220 if (enmEol == SCMEOL_NONE)
1221 return VINF_SUCCESS;
1222 AssertFailedReturn(VERR_INVALID_PARAMETER);
1223}
1224
1225/**
1226 * Formats a string and writes it to the SCM stream.
1227 *
1228 * @returns The number of bytes written (>= 0). Negative value are IPRT error
1229 * status codes.
1230 * @param pStream The stream to write to.
1231 * @param pszFormat The format string.
1232 * @param va The arguments to format.
1233 */
1234ssize_t ScmStreamPrintfV(PSCMSTREAM pStream, const char *pszFormat, va_list va)
1235{
1236 char *psz;
1237 ssize_t cch = RTStrAPrintfV(&psz, pszFormat, va);
1238 if (cch)
1239 {
1240 int rc = ScmStreamWrite(pStream, psz, cch);
1241 RTStrFree(psz);
1242 if (RT_FAILURE(rc))
1243 cch = rc;
1244 }
1245 return cch;
1246}
1247
1248/**
1249 * Formats a string and writes it to the SCM stream.
1250 *
1251 * @returns The number of bytes written (>= 0). Negative value are IPRT error
1252 * status codes.
1253 * @param pStream The stream to write to.
1254 * @param pszFormat The format string.
1255 * @param ... The arguments to format.
1256 */
1257ssize_t ScmStreamPrintf(PSCMSTREAM pStream, const char *pszFormat, ...)
1258{
1259 va_list va;
1260 va_start(va, pszFormat);
1261 ssize_t cch = ScmStreamPrintfV(pStream, pszFormat, va);
1262 va_end(va);
1263 return cch;
1264}
1265
1266/**
1267 * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
1268 *
1269 * The stream positions will be used and changed in both streams.
1270 *
1271 * @returns IPRT status code.
1272 * @param pDst The destination stream. Must be in write mode.
1273 * @param cLines The number of lines. (0 is accepted.)
1274 * @param pSrc The source stream. Must be in read mode.
1275 */
1276int ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
1277{
1278 AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
1279 if (RT_FAILURE(pDst->rc))
1280 return pDst->rc;
1281
1282 AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
1283 if (RT_FAILURE(pSrc->rc))
1284 return pSrc->rc;
1285
1286 while (cLines-- > 0)
1287 {
1288 SCMEOL enmEol;
1289 size_t cchLine;
1290 const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
1291 if (!pchLine)
1292 return pDst->rc = RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF;
1293
1294 int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
1295 if (RT_FAILURE(rc))
1296 return rc;
1297 }
1298
1299 return VINF_SUCCESS;
1300}
1301
1302
1303/**
1304 * If the given C word is at off - 1, return @c true and skip beyond it,
1305 * otherwise return @c false.
1306 *
1307 * @retval true if the given C-word is at the current position minus one char.
1308 * The stream position changes.
1309 * @retval false if not. The stream position is unchanged.
1310 *
1311 * @param pStream The stream.
1312 * @param cchWord The length of the word.
1313 * @param pszWord The word.
1314 */
1315bool ScmStreamCMatchingWordM1(PSCMSTREAM pStream, const char *pszWord, size_t cchWord)
1316{
1317 /* Check stream state. */
1318 AssertReturn(!pStream->fWriteOrRead, false);
1319 AssertReturn(RT_SUCCESS(pStream->rc), false);
1320 AssertReturn(pStream->fFullyLineated, false);
1321
1322 /* Sufficient chars left on the line? */
1323 size_t const iLine = pStream->iLine;
1324 AssertReturn(pStream->off > pStream->paLines[iLine].off, false);
1325 size_t const cchLeft = pStream->paLines[iLine].cch + pStream->paLines[iLine].off - (pStream->off - 1);
1326 if (cchWord > cchLeft)
1327 return false;
1328
1329 /* Do they match? */
1330 const char *psz = &pStream->pch[pStream->off - 1];
1331 if (memcmp(psz, pszWord, cchWord))
1332 return false;
1333
1334 /* Is it the end of a C word? */
1335 if (cchWord < cchLeft)
1336 {
1337 psz += cchWord;
1338 if (RT_C_IS_ALNUM(*psz) || *psz == '_')
1339 return false;
1340 }
1341
1342 /* Skip ahead. */
1343 pStream->off += cchWord - 1;
1344 return true;
1345}
1346
1347/**
1348 * Get's the C word starting at the current position.
1349 *
1350 * @returns Pointer to the word on success and the stream position advanced to
1351 * the end of it.
1352 * NULL on failure, stream position normally unchanged.
1353 * @param pStream The stream to get the C word from.
1354 * @param pcchWord Where to return the word length.
1355 */
1356const char *ScmStreamCGetWord(PSCMSTREAM pStream, size_t *pcchWord)
1357{
1358 /* Check stream state. */
1359 AssertReturn(!pStream->fWriteOrRead, NULL);
1360 AssertReturn(RT_SUCCESS(pStream->rc), NULL);
1361 AssertReturn(pStream->fFullyLineated, NULL);
1362
1363 /* Get the number of chars left on the line and locate the current char. */
1364 size_t const iLine = pStream->iLine;
1365 size_t const cchLeft = pStream->paLines[iLine].cch + pStream->paLines[iLine].off - pStream->off;
1366 const char *psz = &pStream->pch[pStream->off];
1367
1368 /* Is it a leading C character. */
1369 if (!RT_C_IS_ALPHA(*psz) && *psz != '_')
1370 return NULL;
1371
1372 /* Find the end of the word. */
1373 char ch;
1374 size_t off = 1;
1375 while ( off < cchLeft
1376 && ( (ch = psz[off]) == '_'
1377 || RT_C_IS_ALNUM(ch)))
1378 off++;
1379
1380 pStream->off += off;
1381 *pcchWord = off;
1382 return psz;
1383}
1384
1385
1386/**
1387 * Get's the C word starting at the current position minus one.
1388 *
1389 * @returns Pointer to the word on success and the stream position advanced to
1390 * the end of it.
1391 * NULL on failure, stream position normally unchanged.
1392 * @param pStream The stream to get the C word from.
1393 * @param pcchWord Where to return the word length.
1394 */
1395const char *ScmStreamCGetWordM1(PSCMSTREAM pStream, size_t *pcchWord)
1396{
1397 /* Check stream state. */
1398 AssertReturn(!pStream->fWriteOrRead, NULL);
1399 AssertReturn(RT_SUCCESS(pStream->rc), NULL);
1400 AssertReturn(pStream->fFullyLineated, NULL);
1401
1402 /* Get the number of chars left on the line and locate the current char. */
1403 size_t const iLine = pStream->iLine;
1404 size_t const cchLeft = pStream->paLines[iLine].cch + pStream->paLines[iLine].off - (pStream->off - 1);
1405 const char *psz = &pStream->pch[pStream->off - 1];
1406
1407 /* Is it a leading C character. */
1408 if (!RT_C_IS_ALPHA(*psz) && *psz != '_')
1409 return NULL;
1410
1411 /* Find the end of the word. */
1412 char ch;
1413 size_t off = 1;
1414 while ( off < cchLeft
1415 && ( (ch = psz[off]) == '_'
1416 || RT_C_IS_ALNUM(ch)))
1417 off++;
1418
1419 pStream->off += off - 1;
1420 *pcchWord = off;
1421 return psz;
1422}
1423
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette