VirtualBox

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

Last change on this file since 77232 was 76663, checked in by vboxsync, 6 years ago

scm: nits

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.8 KB
Line 
1/* $Id: scmstream.cpp 76663 2019-01-07 01:00:44Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager Stream Code.
4 */
5
6/*
7 * Copyright (C) 2010-2019 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 if (RT_FAILURE(pStream->rc))
632 return pStream->rc;
633
634 /* Must be fully delineated. (lazy bird) */
635 if (RT_UNLIKELY(!pStream->fFullyLineated))
636 {
637 AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
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->iLine = iLine;
647 pStream->off = pStream->paLines[iLine].off;
648 if (pStream->fWriteOrRead)
649 {
650 pStream->cb = pStream->paLines[iLine].off;
651 pStream->cLines = iLine;
652 pStream->paLines[iLine].cch = 0;
653 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
654 }
655 }
656 else
657 {
658 AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
659 pStream->off = pStream->cb;
660 pStream->iLine = pStream->cLines;
661 }
662 return VINF_SUCCESS;
663}
664
665/**
666 * Checks if the stream position is at the start of a line.
667 *
668 * @returns @c true if at the start, @c false if not.
669 * @param pStream The stream.
670 */
671bool ScmStreamIsAtStartOfLine(PSCMSTREAM pStream)
672{
673 if ( !pStream->fFullyLineated
674 && !pStream->fWriteOrRead)
675 {
676 int rc = scmStreamLineate(pStream);
677 if (RT_FAILURE(rc))
678 return false;
679 }
680 return pStream->off == pStream->paLines[pStream->iLine].off;
681}
682
683/**
684 * Worker for ScmStreamGetLineByNo and ScmStreamGetLine.
685 *
686 * Works on a fully lineated stream.
687 *
688 * @returns Pointer to the first character in the line, not NULL terminated.
689 * NULL if the end of the stream has been reached or some problem
690 * occurred.
691 *
692 * @param pStream The stream. Must be in read mode.
693 * @param iLine The line to get (0-based).
694 * @param pcchLine The length.
695 * @param penmEol Where to return the end of line type indicator.
696 */
697DECLINLINE(const char *) scmStreamGetLineByNoCommon(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
698{
699 Assert(!pStream->fWriteOrRead);
700 Assert(pStream->fFullyLineated);
701
702 /* Check stream status. */
703 if (RT_SUCCESS(pStream->rc))
704 {
705 /* Not at the end of the stream yet? */
706 if (RT_LIKELY(iLine < pStream->cLines))
707 {
708 /* Get the data. */
709 const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
710 *pcchLine = pStream->paLines[iLine].cch;
711 *penmEol = pStream->paLines[iLine].enmEol;
712
713 /* update the stream position. */
714 pStream->off = pStream->paLines[iLine].off + pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
715 pStream->iLine = iLine + 1;
716 return pchRet;
717 }
718 pStream->off = pStream->cb;
719 pStream->iLine = pStream->cLines;
720 }
721 *pcchLine = 0;
722 *penmEol = SCMEOL_NONE;
723 return NULL;
724}
725
726
727/**
728 * Get a numbered line from the stream (changes the position).
729 *
730 * A line is always delimited by a LF character or the end of the stream. The
731 * delimiter is not included in returned line length, but instead returned via
732 * the @a penmEol indicator.
733 *
734 * @returns Pointer to the first character in the line, not NULL terminated.
735 * NULL if the end of the stream has been reached or some problem
736 * occurred (*pcchLine set to zero and *penmEol to SCMEOL_NONE).
737 *
738 * @param pStream The stream. Must be in read mode.
739 * @param iLine The line to get (0-based).
740 * @param pcchLine The length.
741 * @param penmEol Where to return the end of line type indicator.
742 */
743const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
744{
745 AssertReturn(!pStream->fWriteOrRead, NULL);
746
747 /* Make sure it's fully delineated so we can use the index. */
748 if (RT_LIKELY(pStream->fFullyLineated))
749 return scmStreamGetLineByNoCommon(pStream, iLine, pcchLine, penmEol);
750
751 int rc = pStream->rc;
752 if (RT_SUCCESS(rc))
753 {
754 rc = scmStreamLineate(pStream);
755 if (RT_SUCCESS(rc))
756 return scmStreamGetLineByNoCommon(pStream, iLine, pcchLine, penmEol);
757 }
758
759 *pcchLine = 0;
760 *penmEol = SCMEOL_NONE;
761 return NULL;
762}
763
764/**
765 * Get a line from the stream.
766 *
767 * A line is always delimited by a LF character or the end of the stream. The
768 * delimiter is not included in returned line length, but instead returned via
769 * the @a penmEol indicator.
770 *
771 * @returns Pointer to the first character in the line, not NULL terminated.
772 * NULL if the end of the stream has been reached or some problem
773 * occurred (*pcchLine set to zero and *penmEol to SCMEOL_NONE).
774 *
775 * @param pStream The stream. Must be in read mode.
776 * @param pcchLine The length.
777 * @param penmEol Where to return the end of line type indicator.
778 */
779const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
780{
781 if (RT_LIKELY(pStream->fFullyLineated))
782 {
783 size_t offCur = pStream->off;
784 size_t iCurLine = pStream->iLine;
785 const char *pszLine = scmStreamGetLineByNoCommon(pStream, iCurLine, pcchLine, penmEol);
786 if ( pszLine
787 && offCur > pStream->paLines[iCurLine].off)
788 {
789 offCur -= pStream->paLines[iCurLine].off;
790 Assert(offCur <= pStream->paLines[iCurLine].cch + pStream->paLines[iCurLine].enmEol);
791 if (offCur < pStream->paLines[iCurLine].cch)
792 *pcchLine -= offCur;
793 else
794 *pcchLine = 0;
795 pszLine += offCur;
796 }
797 return pszLine;
798 }
799 return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
800}
801
802/**
803 * Get the current buffer pointer.
804 *
805 * @returns Buffer pointer on success, NULL on failure (asserted).
806 * @param pStream The stream. Must be in read mode.
807 */
808const char *ScmStreamGetCur(PSCMSTREAM pStream)
809{
810 AssertReturn(!pStream->fWriteOrRead, NULL);
811 return pStream->pch + pStream->off;
812}
813
814/**
815 * Gets a character from the stream.
816 *
817 * @returns The next unsigned character in the stream.
818 * ~(unsigned)0 on failure.
819 * @param pStream The stream. Must be in read mode.
820 */
821unsigned ScmStreamGetCh(PSCMSTREAM pStream)
822{
823 /* Check stream state. */
824 AssertReturn(!pStream->fWriteOrRead, ~(unsigned)0);
825 if (RT_FAILURE(pStream->rc))
826 return ~(unsigned)0;
827 if (RT_UNLIKELY(!pStream->fFullyLineated))
828 {
829 int rc = scmStreamLineate(pStream);
830 if (RT_FAILURE(rc))
831 return ~(unsigned)0;
832 }
833
834 /* If there isn't enough stream left, fail already. */
835 if (RT_UNLIKELY(pStream->off >= pStream->cb))
836 return ~(unsigned)0;
837
838 /* Read a character. */
839 char ch = pStream->pch[pStream->off++];
840
841 /* Advance the line indicator. */
842 size_t iLine = pStream->iLine;
843 if (pStream->off >= pStream->paLines[iLine].off + pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol)
844 pStream->iLine++;
845
846 return (unsigned)ch;
847}
848
849
850/**
851 * Peeks at the next character from the stream.
852 *
853 * @returns The next unsigned character in the stream.
854 * ~(unsigned)0 on failure.
855 * @param pStream The stream. Must be in read mode.
856 */
857unsigned ScmStreamPeekCh(PSCMSTREAM pStream)
858{
859 /* Check stream state. */
860 AssertReturn(!pStream->fWriteOrRead, ~(unsigned)0);
861 if (RT_FAILURE(pStream->rc))
862 return ~(unsigned)0;
863 if (RT_UNLIKELY(!pStream->fFullyLineated))
864 {
865 int rc = scmStreamLineate(pStream);
866 if (RT_FAILURE(rc))
867 return ~(unsigned)0;
868 }
869
870 /* If there isn't enough stream left, fail already. */
871 if (RT_UNLIKELY(pStream->off >= pStream->cb))
872 return ~(unsigned)0;
873
874 /* Peek at the next character. */
875 char ch = pStream->pch[pStream->off];
876 return (unsigned)ch;
877}
878
879
880/**
881 * Reads @a cbToRead bytes into @a pvBuf.
882 *
883 * Will fail if end of stream is encountered before the entire read has been
884 * completed.
885 *
886 * @returns IPRT status code.
887 * @retval VERR_EOF if there isn't @a cbToRead bytes left to read. Stream
888 * position will be unchanged.
889 *
890 * @param pStream The stream. Must be in read mode.
891 * @param pvBuf The buffer to read into.
892 * @param cbToRead The number of bytes to read.
893 */
894int ScmStreamRead(PSCMSTREAM pStream, void *pvBuf, size_t cbToRead)
895{
896 AssertReturn(!pStream->fWriteOrRead, VERR_PERMISSION_DENIED);
897 if (RT_FAILURE(pStream->rc))
898 return pStream->rc;
899
900 /* If there isn't enough stream left, fail already. */
901 if (RT_UNLIKELY(pStream->cb - pStream->off < cbToRead))
902 return VERR_EOF;
903
904 /* Copy the data and simply seek to the new stream position. */
905 memcpy(pvBuf, &pStream->pch[pStream->off], cbToRead);
906 return ScmStreamSeekAbsolute(pStream, pStream->off + cbToRead);
907}
908
909
910/**
911 * Checks if the given line is empty or full of white space.
912 *
913 * @returns true if white space only, false if not (or if non-existant).
914 * @param pStream The stream. Must be in read mode.
915 * @param iLine The line in question.
916 */
917bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
918{
919 SCMEOL enmEol;
920 size_t cchLine;
921 const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
922 if (!pchLine)
923 return false;
924 while (cchLine && RT_C_IS_SPACE(*pchLine))
925 pchLine++, cchLine--;
926 return cchLine == 0;
927}
928
929
930/**
931 * Try figure out the end of line style of the give stream.
932 *
933 * @returns Most likely end of line style.
934 * @param pStream The stream.
935 */
936SCMEOL ScmStreamGetEol(PSCMSTREAM pStream)
937{
938 SCMEOL enmEol;
939 if (pStream->cLines > 0)
940 enmEol = pStream->paLines[0].enmEol;
941 else if (pStream->cb == 0)
942 enmEol = SCMEOL_NONE;
943 else
944 {
945 const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
946 if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
947 enmEol = SCMEOL_CRLF;
948 else
949 enmEol = SCMEOL_LF;
950 }
951
952 if (enmEol == SCMEOL_NONE)
953#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
954 enmEol = SCMEOL_CRLF;
955#else
956 enmEol = SCMEOL_LF;
957#endif
958 return enmEol;
959}
960
961
962/**
963 * Get the end of line indicator type for a line.
964 *
965 * @returns The EOL indicator. If the line isn't found, the default EOL
966 * indicator is return.
967 * @param pStream The stream.
968 * @param iLine The line (0-base).
969 */
970SCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
971{
972 SCMEOL enmEol;
973 if (iLine < pStream->cLines)
974 enmEol = pStream->paLines[iLine].enmEol;
975 else
976#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
977 enmEol = SCMEOL_CRLF;
978#else
979 enmEol = SCMEOL_LF;
980#endif
981 return enmEol;
982}
983
984
985/**
986 * Appends a line to the stream.
987 *
988 * @returns IPRT status code.
989 * @param pStream The stream. Must be in write mode.
990 * @param pchLine Pointer to the line.
991 * @param cchLine Line length.
992 * @param enmEol Which end of line indicator to use.
993 */
994int ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
995{
996 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
997 if (RT_FAILURE(pStream->rc))
998 return pStream->rc;
999
1000 /*
1001 * Make sure the previous line has a new-line indicator.
1002 */
1003 size_t off = pStream->off;
1004 size_t iLine = pStream->iLine;
1005 if (RT_UNLIKELY( iLine != 0
1006 && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
1007 {
1008 AssertReturn(pStream->paLines[iLine].cch == 0, VERR_INTERNAL_ERROR_3);
1009 SCMEOL enmEol2 = enmEol != SCMEOL_NONE ? enmEol : ScmStreamGetEol(pStream);
1010 if (RT_UNLIKELY(off + cchLine + enmEol + enmEol2 > pStream->cbAllocated))
1011 {
1012 int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
1013 if (RT_FAILURE(rc))
1014 return rc;
1015 }
1016 if (enmEol2 == SCMEOL_LF)
1017 pStream->pch[off++] = '\n';
1018 else
1019 {
1020 pStream->pch[off++] = '\r';
1021 pStream->pch[off++] = '\n';
1022 }
1023 pStream->paLines[iLine - 1].enmEol = enmEol2;
1024 pStream->paLines[iLine].off = off;
1025 pStream->off = off;
1026 pStream->cb = off;
1027 }
1028
1029 /*
1030 * Ensure we've got sufficient buffer space.
1031 */
1032 if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
1033 {
1034 int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
1035 if (RT_FAILURE(rc))
1036 return rc;
1037 }
1038
1039 /*
1040 * Add a line record.
1041 */
1042 if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
1043 {
1044 int rc = scmStreamGrowLines(pStream, iLine);
1045 if (RT_FAILURE(rc))
1046 return rc;
1047 }
1048
1049 pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
1050 pStream->paLines[iLine].enmEol = enmEol;
1051
1052 iLine++;
1053 pStream->cLines = iLine;
1054 pStream->iLine = iLine;
1055
1056 /*
1057 * Copy the line
1058 */
1059 memcpy(&pStream->pch[off], pchLine, cchLine);
1060 off += cchLine;
1061 if (enmEol == SCMEOL_LF)
1062 pStream->pch[off++] = '\n';
1063 else if (enmEol == SCMEOL_CRLF)
1064 {
1065 pStream->pch[off++] = '\r';
1066 pStream->pch[off++] = '\n';
1067 }
1068 pStream->off = off;
1069 pStream->cb = off;
1070
1071 /*
1072 * Start a new line.
1073 */
1074 pStream->paLines[iLine].off = off;
1075 pStream->paLines[iLine].cch = 0;
1076 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1077
1078 return VINF_SUCCESS;
1079}
1080
1081/**
1082 * Writes to the stream.
1083 *
1084 * @returns IPRT status code
1085 * @param pStream The stream. Must be in write mode.
1086 * @param pchBuf What to write.
1087 * @param cchBuf How much to write.
1088 */
1089int ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
1090{
1091 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1092 if (RT_FAILURE(pStream->rc))
1093 return pStream->rc;
1094
1095 /*
1096 * Ensure we've got sufficient buffer space.
1097 */
1098 size_t off = pStream->off;
1099 if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
1100 {
1101 int rc = scmStreamGrowBuffer(pStream, cchBuf);
1102 if (RT_FAILURE(rc))
1103 return rc;
1104 }
1105
1106 /*
1107 * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
1108 */
1109 size_t iLine = pStream->iLine;
1110 if (RT_UNLIKELY( iLine > 0
1111 && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
1112 {
1113 iLine--;
1114 pStream->cLines = iLine;
1115 pStream->iLine = iLine;
1116 }
1117
1118 /*
1119 * Deal with lines.
1120 */
1121 const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
1122 if (!pchLF)
1123 pStream->paLines[iLine].cch += cchBuf;
1124 else
1125 {
1126 const char *pchLine = pchBuf;
1127 for (;;)
1128 {
1129 if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
1130 {
1131 int rc = scmStreamGrowLines(pStream, iLine);
1132 if (RT_FAILURE(rc))
1133 {
1134 iLine = pStream->iLine;
1135 pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
1136 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1137 return rc;
1138 }
1139 }
1140
1141 size_t cchLine = pchLF - pchLine;
1142 if ( cchLine
1143 ? pchLF[-1] != '\r'
1144 : !pStream->paLines[iLine].cch
1145 || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
1146 pStream->paLines[iLine].enmEol = SCMEOL_LF;
1147 else
1148 {
1149 pStream->paLines[iLine].enmEol = SCMEOL_CRLF;
1150 cchLine--;
1151 }
1152 pStream->paLines[iLine].cch += cchLine;
1153
1154 iLine++;
1155 size_t offBuf = pchLF + 1 - pchBuf;
1156 pStream->paLines[iLine].off = off + offBuf;
1157 pStream->paLines[iLine].cch = 0;
1158 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1159
1160 size_t cchLeft = cchBuf - offBuf;
1161 pchLine = pchLF + 1;
1162 pchLF = (const char *)memchr(pchLine, '\n', cchLeft);
1163 if (!pchLF)
1164 {
1165 pStream->paLines[iLine].cch = cchLeft;
1166 break;
1167 }
1168 }
1169
1170 pStream->iLine = iLine;
1171 pStream->cLines = iLine;
1172 }
1173
1174 /*
1175 * Copy the data and update position and size.
1176 */
1177 memcpy(&pStream->pch[off], pchBuf, cchBuf);
1178 off += cchBuf;
1179 pStream->off = off;
1180 pStream->cb = off;
1181
1182 return VINF_SUCCESS;
1183}
1184
1185/**
1186 * Write a character to the stream.
1187 *
1188 * @returns IPRT status code
1189 * @param pStream The stream. Must be in write mode.
1190 * @param pchBuf What to write.
1191 * @param cchBuf How much to write.
1192 */
1193int ScmStreamPutCh(PSCMSTREAM pStream, char ch)
1194{
1195 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1196 if (RT_FAILURE(pStream->rc))
1197 return pStream->rc;
1198
1199 /*
1200 * Only deal with the simple cases here, use ScmStreamWrite for the
1201 * annoying stuff.
1202 */
1203 size_t off = pStream->off;
1204 if ( ch == '\n'
1205 || RT_UNLIKELY(off + 1 > pStream->cbAllocated))
1206 return ScmStreamWrite(pStream, &ch, 1);
1207
1208 /*
1209 * Just append it.
1210 */
1211 pStream->pch[off] = ch;
1212 pStream->off = off + 1;
1213 pStream->paLines[pStream->iLine].cch++;
1214
1215 return VINF_SUCCESS;
1216}
1217
1218/**
1219 * Puts an EOL marker to the stream.
1220 *
1221 * @returns IPRt status code.
1222 * @param pStream The stream. Must be in write mode.
1223 * @param enmEol The end-of-line marker to write.
1224 */
1225int ScmStreamPutEol(PSCMSTREAM pStream, SCMEOL enmEol)
1226{
1227 if (enmEol == SCMEOL_LF)
1228 return ScmStreamWrite(pStream, "\n", 1);
1229 if (enmEol == SCMEOL_CRLF)
1230 return ScmStreamWrite(pStream, "\r\n", 2);
1231 if (enmEol == SCMEOL_NONE)
1232 return VINF_SUCCESS;
1233 AssertFailedReturn(VERR_INVALID_PARAMETER);
1234}
1235
1236/**
1237 * Formats a string and writes it to the SCM stream.
1238 *
1239 * @returns The number of bytes written (>= 0). Negative value are IPRT error
1240 * status codes.
1241 * @param pStream The stream to write to.
1242 * @param pszFormat The format string.
1243 * @param va The arguments to format.
1244 */
1245ssize_t ScmStreamPrintfV(PSCMSTREAM pStream, const char *pszFormat, va_list va)
1246{
1247 char *psz;
1248 ssize_t cch = RTStrAPrintfV(&psz, pszFormat, va);
1249 if (cch)
1250 {
1251 int rc = ScmStreamWrite(pStream, psz, cch);
1252 RTStrFree(psz);
1253 if (RT_FAILURE(rc))
1254 cch = rc;
1255 }
1256 return cch;
1257}
1258
1259/**
1260 * Formats a string and writes it to the SCM stream.
1261 *
1262 * @returns The number of bytes written (>= 0). Negative value are IPRT error
1263 * status codes.
1264 * @param pStream The stream to write to.
1265 * @param pszFormat The format string.
1266 * @param ... The arguments to format.
1267 */
1268ssize_t ScmStreamPrintf(PSCMSTREAM pStream, const char *pszFormat, ...)
1269{
1270 va_list va;
1271 va_start(va, pszFormat);
1272 ssize_t cch = ScmStreamPrintfV(pStream, pszFormat, va);
1273 va_end(va);
1274 return cch;
1275}
1276
1277/**
1278 * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
1279 *
1280 * The stream positions will be used and changed in both streams.
1281 *
1282 * @returns IPRT status code.
1283 * @param pDst The destination stream. Must be in write mode.
1284 * @param cLines The number of lines. (0 is accepted.)
1285 * @param pSrc The source stream. Must be in read mode.
1286 */
1287int ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
1288{
1289 AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
1290 if (RT_FAILURE(pDst->rc))
1291 return pDst->rc;
1292
1293 AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
1294 if (RT_FAILURE(pSrc->rc))
1295 return pSrc->rc;
1296
1297 while (cLines-- > 0)
1298 {
1299 SCMEOL enmEol;
1300 size_t cchLine;
1301 const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
1302 if (!pchLine)
1303 return pDst->rc = RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF;
1304
1305 int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
1306 if (RT_FAILURE(rc))
1307 return rc;
1308 }
1309
1310 return VINF_SUCCESS;
1311}
1312
1313
1314/**
1315 * If the given C word is at off - 1, return @c true and skip beyond it,
1316 * otherwise return @c false.
1317 *
1318 * @retval true if the given C-word is at the current position minus one char.
1319 * The stream position changes.
1320 * @retval false if not. The stream position is unchanged.
1321 *
1322 * @param pStream The stream.
1323 * @param cchWord The length of the word.
1324 * @param pszWord The word.
1325 */
1326bool ScmStreamCMatchingWordM1(PSCMSTREAM pStream, const char *pszWord, size_t cchWord)
1327{
1328 /* Check stream state. */
1329 AssertReturn(!pStream->fWriteOrRead, false);
1330 AssertReturn(RT_SUCCESS(pStream->rc), false);
1331 AssertReturn(pStream->fFullyLineated, false);
1332
1333 /* Sufficient chars left on the line? */
1334 size_t const iLine = pStream->iLine;
1335 AssertReturn(pStream->off > pStream->paLines[iLine].off, false);
1336 size_t const cchLeft = pStream->paLines[iLine].cch + pStream->paLines[iLine].off - (pStream->off - 1);
1337 if (cchWord > cchLeft)
1338 return false;
1339
1340 /* Do they match? */
1341 const char *psz = &pStream->pch[pStream->off - 1];
1342 if (memcmp(psz, pszWord, cchWord))
1343 return false;
1344
1345 /* Is it the end of a C word? */
1346 if (cchWord < cchLeft)
1347 {
1348 psz += cchWord;
1349 if (RT_C_IS_ALNUM(*psz) || *psz == '_')
1350 return false;
1351 }
1352
1353 /* Skip ahead. */
1354 pStream->off += cchWord - 1;
1355 return true;
1356}
1357
1358
1359/**
1360 * Get's the C word starting at the current position.
1361 *
1362 * @returns Pointer to the word on success and the stream position advanced to
1363 * the end of it.
1364 * NULL on failure, stream position normally unchanged.
1365 * @param pStream The stream to get the C word from.
1366 * @param pcchWord Where to return the word length.
1367 */
1368const char *ScmStreamCGetWord(PSCMSTREAM pStream, size_t *pcchWord)
1369{
1370 /* Check stream state. */
1371 AssertReturn(!pStream->fWriteOrRead, NULL);
1372 AssertReturn(RT_SUCCESS(pStream->rc), NULL);
1373 AssertReturn(pStream->fFullyLineated, NULL);
1374
1375 /* Get the number of chars left on the line and locate the current char. */
1376 size_t const iLine = pStream->iLine;
1377 size_t const cchLeft = pStream->paLines[iLine].cch + pStream->paLines[iLine].off - pStream->off;
1378 const char *psz = &pStream->pch[pStream->off];
1379
1380 /* Is it a leading C character. */
1381 if (!RT_C_IS_ALPHA(*psz) && *psz != '_')
1382 return NULL;
1383
1384 /* Find the end of the word. */
1385 char ch;
1386 size_t off = 1;
1387 while ( off < cchLeft
1388 && ( (ch = psz[off]) == '_'
1389 || RT_C_IS_ALNUM(ch)))
1390 off++;
1391
1392 pStream->off += off;
1393 *pcchWord = off;
1394 return psz;
1395}
1396
1397
1398/**
1399 * Get's the C word starting at the current position minus one.
1400 *
1401 * @returns Pointer to the word on success and the stream position advanced to
1402 * the end of it.
1403 * NULL on failure, stream position normally unchanged.
1404 * @param pStream The stream to get the C word from.
1405 * @param pcchWord Where to return the word length.
1406 */
1407const char *ScmStreamCGetWordM1(PSCMSTREAM pStream, size_t *pcchWord)
1408{
1409 /* Check stream state. */
1410 AssertReturn(!pStream->fWriteOrRead, NULL);
1411 AssertReturn(RT_SUCCESS(pStream->rc), NULL);
1412 AssertReturn(pStream->fFullyLineated, NULL);
1413
1414 /* Get the number of chars left on the line and locate the current char. */
1415 size_t const iLine = pStream->iLine;
1416 size_t const cchLeft = pStream->paLines[iLine].cch + pStream->paLines[iLine].off - (pStream->off - 1);
1417 const char *psz = &pStream->pch[pStream->off - 1];
1418
1419 /* Is it a leading C character. */
1420 if (!RT_C_IS_ALPHA(*psz) && *psz != '_')
1421 return NULL;
1422
1423 /* Find the end of the word. */
1424 char ch;
1425 size_t off = 1;
1426 while ( off < cchLeft
1427 && ( (ch = psz[off]) == '_'
1428 || RT_C_IS_ALNUM(ch)))
1429 off++;
1430
1431 pStream->off += off - 1;
1432 *pcchWord = off;
1433 return psz;
1434}
1435
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