VirtualBox

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

Last change on this file since 102157 was 100618, checked in by vboxsync, 16 months ago

VBoxCPP: Some fun over the weekend.

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