VirtualBox

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

Last change on this file since 98338 was 98319, checked in by vboxsync, 2 years ago

scm: Started on implement a more thoughout cleanup of kmk makefiles. bugref:10348

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