1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
#include "slang-stream.h"
#ifdef _WIN32
#include <share.h>
#endif
#include "slang-io.h"
#include "slang-process.h"
#include <stdio.h>
#include <thread>
namespace Slang
{
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FileStream !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SlangResult Stream::readExactly(void* buffer, size_t length)
{
size_t readBytes;
SLANG_RETURN_ON_FAIL(read(buffer, length, readBytes));
return (readBytes == length) ? SLANG_OK : SLANG_FAIL;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FileStream !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FileStream::FileStream()
: m_handle(nullptr), m_fileAccess(FileAccess::None), m_endReached(false)
{
}
SlangResult FileStream::init(const String& fileName, FileMode fileMode)
{
const FileAccess access = (fileMode == FileMode::Open) ? FileAccess::Read : FileAccess::Write;
return _init(fileName, fileMode, access, FileShare::None);
}
SlangResult FileStream::init(
const String& fileName,
FileMode fileMode,
FileAccess access,
FileShare share)
{
return _init(fileName, fileMode, access, share);
}
SlangResult FileStream::_init(
const String& fileName,
FileMode fileMode,
FileAccess access,
// Only used on Windows
[[maybe_unused]] FileShare share)
{
// Make sure it's closed to start with
close();
if (access == FileAccess::None)
{
SLANG_ASSERT(!"FileAccess::None not valid to create a FileStream.");
return SLANG_E_INVALID_ARG;
}
const char* mode = "rt";
switch (fileMode)
{
case FileMode::Create:
if (access == FileAccess::Read)
{
SLANG_ASSERT(!"Read-only access is incompatible with Create mode.");
return SLANG_E_INVALID_ARG;
}
else if (access == FileAccess::ReadWrite)
{
mode = "w+b";
}
else
{
mode = "wb";
}
break;
case FileMode::Open:
if (access == FileAccess::Read)
{
mode = "rb";
}
else if (access == FileAccess::ReadWrite)
{
mode = "r+b";
}
else
{
mode = "wb";
}
break;
case FileMode::CreateNew:
if (File::exists(fileName))
{
return SLANG_E_CANNOT_OPEN;
}
if (access == FileAccess::Read)
{
SLANG_ASSERT(!"Read-only access is incompatible with Create mode.");
return SLANG_E_INVALID_ARG;
}
else if (access == FileAccess::ReadWrite)
{
mode = "w+b";
}
else
{
mode = "wb";
}
break;
case FileMode::Append:
if (access == FileAccess::Read)
{
SLANG_ASSERT(!"Read-only access is incompatible with Append mode.");
return SLANG_E_INVALID_ARG;
}
else if (access == FileAccess::ReadWrite)
{
mode = "a+b";
}
else
{
mode = "ab";
}
break;
default:
break;
}
#ifdef _WIN32
// NOTE! This works because we know all the characters in the mode
// are encoded directly as the same value in a wchar_t.
//
// Work out the length *including* terminating 0
const Index modeLength = Index(::strlen(mode)) + 1;
wchar_t wideMode[8];
SLANG_ASSERT(modeLength <= SLANG_COUNT_OF(wideMode));
// Copy to wchar_t
for (Index i = 0; i < modeLength; ++i)
{
wideMode[i] = wchar_t(mode[i]);
}
int shFlag = _SH_DENYRW;
switch (share)
{
case FileShare::None:
shFlag = _SH_DENYRW;
break;
case FileShare::ReadOnly:
shFlag = _SH_DENYWR;
break;
case FileShare::WriteOnly:
shFlag = _SH_DENYRD;
break;
case FileShare::ReadWrite:
shFlag = _SH_DENYNO;
break;
default:
SLANG_ASSERT(!"Invalid file share mode.");
return SLANG_FAIL;
}
if (share == FileShare::None)
#pragma warning(suppress : 4996)
m_handle = _wfopen(fileName.toWString(), wideMode);
else
m_handle = _wfsopen(fileName.toWString(), wideMode, shFlag);
#else
m_handle = fopen(fileName.getBuffer(), mode);
#endif
if (!m_handle)
{
return SLANG_E_CANNOT_OPEN;
}
// Just set the access specified
m_fileAccess = access;
return SLANG_OK;
}
FileStream::~FileStream()
{
close();
}
Int64 FileStream::getPosition()
{
#if defined(_WIN32) || defined(__CYGWIN__)
fpos_t pos;
fgetpos(m_handle, &pos);
return pos;
#elif defined(__APPLE__)
return ftell(m_handle);
#else
fpos64_t pos;
fgetpos64(m_handle, &pos);
return *(Int64*)(&pos);
#endif
}
SlangResult FileStream::seek(SeekOrigin seekOrigin, Int64 offset)
{
int fseekOrigin;
switch (seekOrigin)
{
case SeekOrigin::Start:
fseekOrigin = SEEK_SET;
break;
case SeekOrigin::End:
fseekOrigin = SEEK_END;
break;
case SeekOrigin::Current:
fseekOrigin = SEEK_CUR;
break;
default:
SLANG_ASSERT(!"Unsupported seek origin.");
return SLANG_FAIL;
}
// If endReached is intended to be like feof - then doing a seek will reset it
m_endReached = false;
#ifdef _WIN32
int rs = _fseeki64(m_handle, offset, fseekOrigin);
#else
int rs = fseek(m_handle, (long int)offset, fseekOrigin);
#endif
// If rs != 0 then the the seek failed
SLANG_ASSERT(rs == 0);
return (rs == 0) ? SLANG_OK : SLANG_FAIL;
}
SlangResult FileStream::read(void* buffer, size_t length, size_t& outBytesRead)
{
auto bytesRead = fread_s(buffer, length, 1, length, m_handle);
outBytesRead = bytesRead;
if (bytesRead == 0 && length > 0)
{
// If we have reached the end, then reading nothing is ok.
if (!m_endReached)
{
// If we are not at the end of the file we should be able to read some bytes
if (!feof(m_handle))
{
return SLANG_FAIL;
}
m_endReached = true;
}
}
return SLANG_OK;
}
SlangResult FileStream::write(const void* buffer, size_t length)
{
auto bytesWritten = fwrite(buffer, 1, length, m_handle);
return (bytesWritten == length) ? SLANG_OK : SLANG_FAIL;
}
SlangResult FileStream::flush()
{
if (m_handle && canWrite())
{
fflush(m_handle);
return SLANG_OK;
}
return SLANG_E_NOT_AVAILABLE;
}
bool FileStream::canRead()
{
return ((int)m_fileAccess & (int)FileAccess::Read) != 0;
}
bool FileStream::canWrite()
{
return ((int)m_fileAccess & (int)FileAccess::Write) != 0;
}
void FileStream::close()
{
if (m_handle)
{
fclose(m_handle);
m_handle = nullptr;
// If closed, can neither read or write
m_fileAccess = FileAccess::None;
}
}
bool FileStream::isEnd()
{
return m_endReached;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MemoryStreamBase !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SlangResult MemoryStreamBase::seek(SeekOrigin origin, Int64 offset)
{
Int64 pos = 0;
switch (origin)
{
case SeekOrigin::Start:
pos = offset;
break;
case SeekOrigin::End:
pos = Int64(m_contentsSize) + offset;
break;
case SeekOrigin::Current:
pos = Int64(m_position) + offset;
break;
default:
SLANG_ASSERT(!"Unsupported seek origin.");
return SLANG_E_NOT_IMPLEMENTED;
}
m_atEnd = false;
// Clamp to the valid range
pos = (pos < 0) ? 0 : pos;
pos = (pos > Int64(m_contentsSize)) ? Int64(m_contentsSize) : pos;
m_position = ptrdiff_t(pos);
return SLANG_OK;
}
SlangResult MemoryStreamBase::read(void* buffer, size_t length, size_t& outReadBytes)
{
outReadBytes = 0;
if (!canRead())
{
SLANG_ASSERT(!"Cannot read this stream.");
return SLANG_FAIL;
}
const size_t maxRead = size_t(m_contentsSize - m_position);
if (maxRead == 0 && length > 0)
{
// At end of stream
m_atEnd = true;
return SLANG_OK;
}
length = length > maxRead ? maxRead : length;
::memcpy(buffer, m_contents + m_position, length);
m_position += ptrdiff_t(length);
outReadBytes = length;
return SLANG_OK;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! OwnedMemoryStream !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SlangResult OwnedMemoryStream::write(const void* buffer, size_t length)
{
if (!canWrite())
{
SLANG_ASSERT(!"Cannot write this stream.");
return SLANG_FAIL;
}
if (m_position == m_ownedContents.getCount())
{
m_ownedContents.addRange((const uint8_t*)buffer, Index(length));
}
else
{
m_ownedContents.insertRange(m_position, (const uint8_t*)buffer, Index(length));
}
m_contents = m_ownedContents.getBuffer();
m_contentsSize = ptrdiff_t(m_ownedContents.getCount());
m_atEnd = false;
m_position += ptrdiff_t(length);
return SLANG_OK;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! BufferedReadStream !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void BufferedReadStream::consume(Index byteCount)
{
SLANG_ASSERT(Index(getCount()) >= byteCount && byteCount >= 0);
m_startIndex += byteCount;
if (getCount() == 0)
{
_resetBuffer();
}
}
Int64 BufferedReadStream::getPosition()
{
return m_stream ? (m_stream->getPosition() - getCount()) : 0;
}
SlangResult BufferedReadStream::seek(SeekOrigin origin, Int64 offset)
{
if (!m_stream)
{
return SLANG_FAIL;
}
// As it currently stands the data behind m_startIndex is the previous data.
// So we could seek backwards up to -m_startIndex.
// We don't worry about this here, for simplicity sake.
if (origin == SeekOrigin::End || origin == SeekOrigin::Start || offset < 0 ||
offset >= Int64(getCount()))
{
// Empty the buffer
_resetBuffer();
// Seek on underlying stream
return m_stream->seek(origin, offset);
}
// We can just seek on the buffered data
consume(Index(offset));
return SLANG_OK;
}
SlangResult BufferedReadStream::read(void* inBuffer, size_t length, size_t& outReadBytes)
{
// If the buffer has no data and the read size is larger than the default read size - may as
// well just read directly into the output buffer
if (getCount() == 0 && length > m_defaultReadSize)
{
return m_stream->read(inBuffer, length, outReadBytes);
}
Byte* buffer = (Byte*)inBuffer;
size_t totalReadBytes = 0;
outReadBytes = 0;
// Do a read to fill the buffer.
SLANG_RETURN_ON_FAIL(update());
while (length > 0)
{
const size_t bufferCount = size_t(getCount());
if (bufferCount)
{
const size_t readCount = (bufferCount < length) ? bufferCount : length;
::memcpy(buffer, getBuffer(), readCount);
consume(Index(readCount));
buffer += readCount;
length -= readCount;
totalReadBytes += readCount;
}
else
{
if (m_stream == nullptr)
{
break;
}
// Read from underlying buffer
size_t readBytes;
SlangResult res = m_stream->read(buffer, length, readBytes);
outReadBytes = totalReadBytes + readBytes;
return res;
}
}
outReadBytes = totalReadBytes;
return SLANG_OK;
}
SlangResult BufferedReadStream::write(const void* buffer, size_t length)
{
SLANG_UNUSED(buffer);
SLANG_UNUSED(length);
return SLANG_E_NOT_AVAILABLE;
}
bool BufferedReadStream::canRead()
{
return getCount() > 0 || (m_stream && m_stream->canRead());
}
bool BufferedReadStream::canWrite()
{
return false;
}
void BufferedReadStream::close()
{
if (m_stream)
{
m_stream->close();
m_stream.setNull();
}
}
bool BufferedReadStream::isEnd()
{
return getCount() == 0 && (m_stream == nullptr || m_stream->isEnd());
}
SlangResult BufferedReadStream::flush()
{
return SLANG_E_NOT_AVAILABLE;
}
SlangResult BufferedReadStream::update()
{
if (m_stream == nullptr)
{
// Should this return an error?
return SLANG_OK;
}
// Repeat until we have enough space
for (;;)
{
// How much buffer space do we have. We need at least m_defaultReadSize
const size_t remainingCount = size_t(m_buffer.getCapacity() - m_buffer.getCount());
if (remainingCount >= m_defaultReadSize)
{
break;
}
// If there is anything in the buffer shift it all down
if (m_startIndex > 0)
{
Byte* buffer = m_buffer.getBuffer();
const Index count = getCount();
if (count > 0)
{
::memmove(buffer, buffer + m_startIndex, count);
}
m_buffer.setCount(count);
m_startIndex = 0;
}
else
{
// Make sure we have the space
const Index prevCount = m_buffer.getCount();
m_buffer.setCount(prevCount + m_defaultReadSize);
m_buffer.setCount(prevCount);
}
}
{
const Index prevCount = m_buffer.getCount();
m_buffer.setCount(prevCount + m_defaultReadSize);
size_t readBytes = 0;
const SlangResult res =
m_stream->read(m_buffer.getBuffer() + prevCount, m_defaultReadSize, readBytes);
m_buffer.setCount(prevCount + Index(readBytes));
return res;
}
}
SlangResult BufferedReadStream::readUntilContains(size_t size)
{
while (true)
{
if (size_t(getCount()) >= size)
{
return SLANG_OK;
}
const size_t preCount = size_t(getCount());
// Update buffer
SLANG_RETURN_ON_FAIL(update());
// If nothing was read yield
if (preCount == getCount())
{
Process::sleepCurrentThread(0);
}
}
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! StreamUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SlangResult StreamUtil::readAndWrite(
Stream* writeStream,
ArrayView<Byte> bytesToWrite,
Stream* readStream,
List<Byte>& outReadBytes,
Stream* errStream,
List<Byte>& outErrBytes)
{
std::thread writeThread(
[&]()
{
writeStream->write(bytesToWrite.getBuffer(), (size_t)bytesToWrite.getCount());
writeStream->close();
});
SlangResult readResult = SLANG_OK;
std::thread readThread([&]() { readResult = readAll(readStream, 1024, outReadBytes); });
std::thread readErrThread([&]() { readAll(errStream, 1024, outErrBytes); });
writeThread.join();
readThread.join();
readErrThread.join();
return readResult;
}
/* static */ SlangResult StreamUtil::readAll(Stream* stream, size_t readSize, List<Byte>& ioBytes)
{
while (!stream->isEnd())
{
SLANG_RETURN_ON_FAIL(read(stream, readSize, ioBytes));
}
return SLANG_OK;
}
/* static */ SlangResult StreamUtil::read(Stream* stream, size_t readSize, List<Byte>& ioBytes)
{
readSize = (readSize <= 0) ? 1024 : readSize;
while (true)
{
const Index prevCount = ioBytes.getCount();
ioBytes.setCount(prevCount + readSize);
size_t readBytesCount;
SLANG_RETURN_ON_FAIL(
stream->read(ioBytes.getBuffer() + prevCount, readSize, readBytesCount));
ioBytes.setCount(prevCount + Index(readBytesCount));
if (readBytesCount == 0)
{
return SLANG_OK;
}
}
}
/* static */ SlangResult StreamUtil::discard(Stream* stream)
{
Byte buf[1024];
const Index bufSize = SLANG_COUNT_OF(buf);
while (true)
{
size_t readBytesCount;
SLANG_RETURN_ON_FAIL(stream->read(buf, bufSize, readBytesCount));
if (readBytesCount == 0)
{
return SLANG_OK;
}
}
}
/* static */ SlangResult StreamUtil::discardAll(Stream* stream)
{
while (!stream->isEnd())
{
SLANG_RETURN_ON_FAIL(discard(stream));
}
return SLANG_OK;
}
/* static */ SlangResult StreamUtil::readOrDiscard(
Stream* stream,
size_t readSize,
List<Byte>* ioBytes)
{
if (ioBytes)
{
return read(stream, readSize, *ioBytes);
}
else
{
return discard(stream);
}
}
/* static */ SlangResult StreamUtil::readOrDiscardAll(
Stream* stream,
size_t readSize,
List<Byte>* ioBytes)
{
if (ioBytes)
{
return readAll(stream, readSize, *ioBytes);
}
else
{
return discardAll(stream);
}
}
static FILE* _getFileFromStdStreamType(StdStreamType stdStream)
{
switch (stdStream)
{
case StdStreamType::ErrorOut:
return stderr;
case StdStreamType::Out:
return stdout;
case StdStreamType::In:
return stdin;
default:
return nullptr;
}
}
static int _getBufferOptions(StreamBufferStyle style)
{
switch (style)
{
case StreamBufferStyle::None:
return _IONBF;
case StreamBufferStyle::Line:
return _IOLBF;
default:
case StreamBufferStyle::Full:
return _IOFBF;
}
}
/* static */ SlangResult StreamUtil::setStreamBufferStyle(
StdStreamType stdStream,
StreamBufferStyle style)
{
FILE* file = _getFileFromStdStreamType(stdStream);
if (file)
{
auto options = _getBufferOptions(style);
// https://www.cplusplus.com/reference/cstdio/setvbuf/
// NOTE! We don't set a buffer here (we pass in nullptr).
// Passing nullptr is fine for 'no buffering' and sets a 'dynamic buffer' for others.
// But it's not clear the behavior is around the buffer size. It seems the size is a
// 'suggestion' so it will set the default but the documentation is unclear.
if (setvbuf(file, nullptr, options, 0) == 0)
{
return SLANG_OK;
}
return SLANG_FAIL;
}
return SLANG_E_NOT_AVAILABLE;
}
} // namespace Slang
|