|
| 1 | +// https://github.com/c3lang/c3c/blob/master/lib/std/io/io.c3 (partial saved here) |
| 2 | +// Copyright (c) 2021-2025 Christoffer Lerno. All rights reserved. |
| 3 | +// Use of this source code is governed by the MIT license |
| 4 | +// a copy of which can be found in the LICENSE_STDLIB file. |
| 5 | +module std::io; |
| 6 | +import libc; |
| 7 | + |
| 8 | +enum Seek |
| 9 | +{ |
| 10 | + SET, |
| 11 | + CURSOR, |
| 12 | + END |
| 13 | +} |
| 14 | + |
| 15 | +faultdef |
| 16 | + ALREADY_EXISTS, |
| 17 | + BUSY, |
| 18 | + CANNOT_READ_DIR, |
| 19 | + DIR_NOT_EMPTY, |
| 20 | + PARENT_DIR_MISSING, |
| 21 | + EOF, |
| 22 | + FILE_CANNOT_DELETE, |
| 23 | + FILE_IS_DIR, |
| 24 | + FILE_IS_PIPE, |
| 25 | + FILE_NOT_DIR, |
| 26 | + FILE_NOT_FOUND, |
| 27 | + FILE_NOT_VALID, |
| 28 | + GENERAL_ERROR, |
| 29 | + ILLEGAL_ARGUMENT, |
| 30 | + INCOMPLETE_WRITE, |
| 31 | + INTERRUPTED, |
| 32 | + INVALID_POSITION, |
| 33 | + INVALID_PUSHBACK, |
| 34 | + NAME_TOO_LONG, |
| 35 | + NOT_SEEKABLE, |
| 36 | + NO_PERMISSION, |
| 37 | + OUT_OF_SPACE, |
| 38 | + OVERFLOW, |
| 39 | + PATH_COULD_NOT_BE_FOUND, |
| 40 | + READ_ONLY, |
| 41 | + SYMLINK_FAILED, |
| 42 | + TOO_MANY_DESCRIPTORS, |
| 43 | + UNEXPECTED_EOF, |
| 44 | + UNKNOWN_ERROR, |
| 45 | + UNSUPPORTED_OPERATION, |
| 46 | + WOULD_BLOCK; |
| 47 | + |
| 48 | + |
| 49 | +<* |
| 50 | + Read from a stream (default is stdin) to the next "\n" |
| 51 | + or to the end of the stream, whatever comes first. |
| 52 | + "\r" will be filtered from the String. |
| 53 | +
|
| 54 | + @param [&inout] allocator : "The allocator used to allocate the read string." |
| 55 | + @param stream : `The stream to read from.` |
| 56 | + @param limit : `Optionally limits the amount of bytes to read in a single line. Will NOT discard the remaining line data.` |
| 57 | + @require @is_not_instream_if_ptr(stream) : "The value for 'stream' should have been passed as a pointer and not as a value, please add '&'." |
| 58 | + @require @is_instream(stream) : `Make sure that the stream is actually an InStream.` |
| 59 | + @param [inout] allocator : `the allocator to use.` |
| 60 | + @return `The string containing the data read.` |
| 61 | +*> |
| 62 | +macro String? readline(Allocator allocator, stream = io::stdin(), usz limit = 0) |
| 63 | +{ |
| 64 | + return readline_impl{$typeof(stream)}(allocator, stream, limit); |
| 65 | +} |
| 66 | + |
| 67 | +<* |
| 68 | + Reads a string, see `readline`, except the it is allocated |
| 69 | + on the temporary allocator and does not need to be freed. |
| 70 | +
|
| 71 | + @param stream : `The stream to read from.` |
| 72 | + @param limit : `Optionally limits the amount of bytes to read in a single line. Will NOT discard the remaining line data.` |
| 73 | + @require @is_not_instream_if_ptr(stream) : "The value for 'stream' should have been passed as a pointer and not as a value, please add '&'." |
| 74 | + @require @is_instream(stream) : `The stream must implement InStream.` |
| 75 | + @return `The temporary string containing the data read.` |
| 76 | +*> |
| 77 | +macro String? treadline(stream = io::stdin(), usz limit = 0) |
| 78 | +{ |
| 79 | + return readline(tmem, stream, limit) @inline; |
| 80 | +} |
| 81 | + |
| 82 | +fn String? readline_impl(Allocator allocator, Stream stream, usz limit) <Stream> @private |
| 83 | +{ |
| 84 | + if (allocator == tmem) |
| 85 | + { |
| 86 | + DString str = dstring::temp_with_capacity(256); |
| 87 | + readline_to_stream(&str, stream, limit)!; |
| 88 | + return str.str_view(); |
| 89 | + } |
| 90 | + @pool() |
| 91 | + { |
| 92 | + DString str = dstring::temp_with_capacity(256); |
| 93 | + readline_to_stream(&str, stream, limit)!; |
| 94 | + return str.copy_str(allocator); |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +<* |
| 99 | + Reads a string, see `readline`, the data is passed to an outstream |
| 100 | +
|
| 101 | + @param out_stream : `The stream to write to` |
| 102 | + @param in_stream : `The stream to read from.` |
| 103 | + @param limit : `Optionally limits the byte-length of the allocated output string.` |
| 104 | + @require @is_not_instream_if_ptr(in_stream) : "The value for 'in_stream' should have been passed as a pointer and not as a value, please add '&'." |
| 105 | + @require @is_not_outstream_if_ptr(out_stream) : "The value for 'out_stream' should have been passed as a pointer and not as a value, please add '&'." |
| 106 | + @require @is_instream(in_stream) : `The in_stream must implement InStream.` |
| 107 | + @require @is_outstream(out_stream) : `The out_stream must implement OutStream.` |
| 108 | + @return `The number of bytes written. When a 'limit' is provided and the return value is equal to it, there may be more to read on the current line.` |
| 109 | +*> |
| 110 | +macro usz? readline_to_stream(out_stream, in_stream = io::stdin(), usz limit = 0) |
| 111 | +{ |
| 112 | + return readline_to_stream_impl{$typeof(in_stream), $typeof(out_stream)}(out_stream, in_stream, limit); |
| 113 | +} |
| 114 | + |
| 115 | +fn usz? readline_to_stream_impl(OStream out_stream, IStream in_stream, usz limit) <IStream, OStream> @private |
| 116 | +{ |
| 117 | + bool $is_stream = IStream == InStream; |
| 118 | + $if $is_stream: |
| 119 | + var func @safeinfer = &in_stream.read_byte; |
| 120 | + char val = func((void*)in_stream)!; |
| 121 | + $else |
| 122 | + char val = in_stream.read_byte()!; |
| 123 | + $endif |
| 124 | + bool $is_out_stream = OStream == OutStream; |
| 125 | + $if $is_out_stream: |
| 126 | + var out_func @safeinfer = &out_stream.write_byte; |
| 127 | + $endif |
| 128 | + |
| 129 | + if (val == '\n') return 0; |
| 130 | + usz len; |
| 131 | + if (val != '\r') |
| 132 | + { |
| 133 | + $if $is_out_stream: |
| 134 | + out_func((void*)out_stream.ptr, val)!; |
| 135 | + $else |
| 136 | + out_stream.write_byte(val)!; |
| 137 | + $endif |
| 138 | + len++; |
| 139 | + } |
| 140 | + while (!limit || len < limit) |
| 141 | + { |
| 142 | + $if $is_stream: |
| 143 | + char? c = func((void*)in_stream); |
| 144 | + $else |
| 145 | + char? c = in_stream.read_byte(); |
| 146 | + $endif |
| 147 | + if (catch err = c) |
| 148 | + { |
| 149 | + if (err == io::EOF) break; |
| 150 | + return err~; |
| 151 | + } |
| 152 | + if (c == '\r') continue; |
| 153 | + if (c == '\n') break; |
| 154 | + $if $is_out_stream: |
| 155 | + out_func((void*)out_stream.ptr, c)!; |
| 156 | + $else |
| 157 | + out_stream.write_byte(c)!; |
| 158 | + $endif |
| 159 | + len++; |
| 160 | + } |
| 161 | + return len; |
| 162 | +} |
| 163 | + |
0 commit comments