| 1 | #include "mycpp/gc_mylib.h"
 | 
| 2 | 
 | 
| 3 | #include <errno.h>
 | 
| 4 | #include <stdio.h>
 | 
| 5 | #include <unistd.h>  // isatty
 | 
| 6 | 
 | 
| 7 | namespace mylib {
 | 
| 8 | 
 | 
| 9 | void InitCppOnly() {
 | 
| 10 |   // We don't seem need this now that we have ctx_FlushStdout().
 | 
| 11 |   // setvbuf(stdout, 0, _IONBF, 0);
 | 
| 12 | 
 | 
| 13 |   // Arbitrary threshold of 50K objects based on eyeballing
 | 
| 14 |   // benchmarks/osh-runtime 10K or 100K aren't too bad either.
 | 
| 15 |   gHeap.Init(50000);
 | 
| 16 | }
 | 
| 17 | 
 | 
| 18 | void print_stderr(BigStr* s) {
 | 
| 19 |   fputs(s->data_, stderr);  // prints until first NUL
 | 
| 20 |   fputc('\n', stderr);
 | 
| 21 | }
 | 
| 22 | 
 | 
| 23 | #if 0
 | 
| 24 | void writeln(BigStr* s, int fd) {
 | 
| 25 |   // TODO: handle errors and write in a loop, like posix::write().  If possible,
 | 
| 26 |   // use posix::write directly, but that introduces some dependency problems.
 | 
| 27 | 
 | 
| 28 |   if (write(fd, s->data_, len(s)) < 0) {
 | 
| 29 |     assert(0);
 | 
| 30 |   }
 | 
| 31 |   if (write(fd, "\n", 1) < 0) {
 | 
| 32 |     assert(0);
 | 
| 33 |   }
 | 
| 34 | }
 | 
| 35 | #endif
 | 
| 36 | 
 | 
| 37 | BigStr* JoinBytes(List<int>* byte_list) {
 | 
| 38 |   int n = len(byte_list);
 | 
| 39 |   BigStr* result = NewStr(n);
 | 
| 40 |   for (int i = 0; i < n; ++i) {
 | 
| 41 |     result->data_[i] = byte_list->at(i);
 | 
| 42 |   }
 | 
| 43 |   return result;
 | 
| 44 | }
 | 
| 45 | 
 | 
| 46 | // For BashArray
 | 
| 47 | void BigIntSort(List<mops::BigInt>* keys) {
 | 
| 48 |   keys->sort();
 | 
| 49 | }
 | 
| 50 | 
 | 
| 51 | class MutableStr : public BigStr {};
 | 
| 52 | 
 | 
| 53 | MutableStr* NewMutableStr(int n) {
 | 
| 54 |   // In order for everything to work, MutableStr must be identical in layout to
 | 
| 55 |   // BigStr. One easy way to achieve this is for MutableStr to have no members
 | 
| 56 |   // and to inherit from BigStr.
 | 
| 57 |   static_assert(sizeof(MutableStr) == sizeof(BigStr),
 | 
| 58 |                 "BigStr and MutableStr must have same size");
 | 
| 59 |   return reinterpret_cast<MutableStr*>(NewStr(n));
 | 
| 60 | }
 | 
| 61 | 
 | 
| 62 | Tuple2<BigStr*, BigStr*> split_once(BigStr* s, BigStr* delim) {
 | 
| 63 |   DCHECK(len(delim) == 1);
 | 
| 64 | 
 | 
| 65 |   const char* start = s->data_;  // note: this pointer may move
 | 
| 66 |   char c = delim->data_[0];
 | 
| 67 |   int length = len(s);
 | 
| 68 | 
 | 
| 69 |   const char* p = static_cast<const char*>(memchr(start, c, length));
 | 
| 70 | 
 | 
| 71 |   if (p) {
 | 
| 72 |     int len1 = p - start;
 | 
| 73 |     int len2 = length - len1 - 1;  // -1 for delim
 | 
| 74 | 
 | 
| 75 |     BigStr* s1 = nullptr;
 | 
| 76 |     BigStr* s2 = nullptr;
 | 
| 77 |     // Allocate together to avoid 's' moving in between
 | 
| 78 |     s1 = NewStr(len1);
 | 
| 79 |     s2 = NewStr(len2);
 | 
| 80 | 
 | 
| 81 |     memcpy(s1->data_, s->data_, len1);
 | 
| 82 |     memcpy(s2->data_, s->data_ + len1 + 1, len2);
 | 
| 83 | 
 | 
| 84 |     return Tuple2<BigStr*, BigStr*>(s1, s2);
 | 
| 85 |   } else {
 | 
| 86 |     return Tuple2<BigStr*, BigStr*>(s, nullptr);
 | 
| 87 |   }
 | 
| 88 | }
 | 
| 89 | 
 | 
| 90 | LineReader* gStdin;
 | 
| 91 | 
 | 
| 92 | LineReader* open(BigStr* path) {
 | 
| 93 |   // TODO: Don't use C I/O; use POSIX I/O!
 | 
| 94 |   FILE* f = fopen(path->data_, "r");
 | 
| 95 |   if (f == nullptr) {
 | 
| 96 |     throw Alloc<IOError>(errno);
 | 
| 97 |   }
 | 
| 98 | 
 | 
| 99 |   return reinterpret_cast<LineReader*>(Alloc<CFile>(f));
 | 
| 100 | }
 | 
| 101 | 
 | 
| 102 | BigStr* CFile::readline() {
 | 
| 103 |   char* line = nullptr;
 | 
| 104 |   size_t allocated_size = 0;  // unused
 | 
| 105 | 
 | 
| 106 |   // Reset errno because we turn the EOF error into empty string (like Python).
 | 
| 107 |   errno = 0;
 | 
| 108 |   ssize_t len = getline(&line, &allocated_size, f_);
 | 
| 109 |   if (len < 0) {
 | 
| 110 |     // man page says the buffer should be freed even if getline fails
 | 
| 111 |     free(line);
 | 
| 112 |     if (errno != 0) {  // Unexpected error
 | 
| 113 |       log("getline() error: %s", strerror(errno));
 | 
| 114 |       throw Alloc<IOError>(errno);
 | 
| 115 |     }
 | 
| 116 |     return kEmptyString;  // EOF indicated by by empty string, like Python
 | 
| 117 |   }
 | 
| 118 | 
 | 
| 119 |   // Note: getline() NUL-terminates the buffer
 | 
| 120 |   BigStr* result = ::StrFromC(line, len);
 | 
| 121 |   free(line);
 | 
| 122 |   return result;
 | 
| 123 | }
 | 
| 124 | 
 | 
| 125 | bool CFile::isatty() {
 | 
| 126 |   return ::isatty(fileno(f_));
 | 
| 127 | }
 | 
| 128 | 
 | 
| 129 | // Problem: most BigStr methods like index() and slice() COPY so they have a
 | 
| 130 | // NUL terminator.
 | 
| 131 | // log("%s") falls back on sprintf, so it expects a NUL terminator.
 | 
| 132 | // It would be easier for us to just share.
 | 
| 133 | BigStr* BufLineReader::readline() {
 | 
| 134 |   BigStr* line = nullptr;
 | 
| 135 | 
 | 
| 136 |   int str_len = len(s_);
 | 
| 137 |   if (pos_ == str_len) {
 | 
| 138 |     return kEmptyString;
 | 
| 139 |   }
 | 
| 140 | 
 | 
| 141 |   int orig_pos = pos_;
 | 
| 142 |   const char* p = strchr(s_->data_ + pos_, '\n');
 | 
| 143 |   // log("pos_ = %s", pos_);
 | 
| 144 |   int line_len;
 | 
| 145 |   if (p) {
 | 
| 146 |     int new_pos = p - s_->data_;
 | 
| 147 |     line_len = new_pos - pos_ + 1;  // past newline char
 | 
| 148 |     pos_ = new_pos + 1;
 | 
| 149 |   } else {             // leftover line
 | 
| 150 |     if (pos_ == 0) {   // The string has no newlines at all -- just return it
 | 
| 151 |       pos_ = str_len;  // advance to the end
 | 
| 152 |       return s_;
 | 
| 153 |     } else {
 | 
| 154 |       line_len = str_len - pos_;
 | 
| 155 |       pos_ = str_len;  // advance to the end
 | 
| 156 |     }
 | 
| 157 |   }
 | 
| 158 | 
 | 
| 159 |   line = NewStr(line_len);
 | 
| 160 |   memcpy(line->data_, s_->data_ + orig_pos, line_len);
 | 
| 161 |   DCHECK(line->data_[line_len] == '\0');
 | 
| 162 |   return line;
 | 
| 163 | }
 | 
| 164 | 
 | 
| 165 | Writer* gStdout;
 | 
| 166 | Writer* gStderr;
 | 
| 167 | 
 | 
| 168 | //
 | 
| 169 | // CFileWriter
 | 
| 170 | //
 | 
| 171 | 
 | 
| 172 | void CFile::write(BigStr* s) {
 | 
| 173 |   // Writes can be short!
 | 
| 174 |   int n = len(s);
 | 
| 175 |   int num_written = ::fwrite(s->data_, sizeof(char), n, f_);
 | 
| 176 |   // Similar to CPython fileobject.c
 | 
| 177 |   if (num_written != n) {
 | 
| 178 |     throw Alloc<IOError>(errno);
 | 
| 179 |   }
 | 
| 180 | }
 | 
| 181 | 
 | 
| 182 | void CFile::flush() {
 | 
| 183 |   if (::fflush(f_) != 0) {
 | 
| 184 |     throw Alloc<IOError>(errno);
 | 
| 185 |   }
 | 
| 186 | }
 | 
| 187 | 
 | 
| 188 | void CFile::close() {
 | 
| 189 |   if (::fclose(f_) != 0) {
 | 
| 190 |     throw Alloc<IOError>(errno);
 | 
| 191 |   }
 | 
| 192 | }
 | 
| 193 | 
 | 
| 194 | //
 | 
| 195 | // BufWriter
 | 
| 196 | //
 | 
| 197 | 
 | 
| 198 | void BufWriter::EnsureMoreSpace(int n) {
 | 
| 199 |   if (str_ == nullptr) {
 | 
| 200 |     // TODO: we could make the default capacity big enough for a line, e.g. 128
 | 
| 201 |     // capacity: 128 -> 256 -> 512
 | 
| 202 |     str_ = NewMutableStr(n);
 | 
| 203 |     return;
 | 
| 204 |   }
 | 
| 205 | 
 | 
| 206 |   int current_cap = len(str_);
 | 
| 207 |   DCHECK(current_cap >= len_);
 | 
| 208 | 
 | 
| 209 |   int new_cap = len_ + n;
 | 
| 210 | 
 | 
| 211 |   if (current_cap < new_cap) {
 | 
| 212 |     auto* s = NewMutableStr(std::max(current_cap * 2, new_cap));
 | 
| 213 |     memcpy(s->data_, str_->data_, len_);
 | 
| 214 |     s->data_[len_] = '\0';
 | 
| 215 |     str_ = s;
 | 
| 216 |   }
 | 
| 217 | }
 | 
| 218 | 
 | 
| 219 | uint8_t* BufWriter::LengthPointer() {
 | 
| 220 |   // start + len
 | 
| 221 |   return reinterpret_cast<uint8_t*>(str_->data_) + len_;
 | 
| 222 | }
 | 
| 223 | 
 | 
| 224 | uint8_t* BufWriter::CapacityPointer() {
 | 
| 225 |   // start + capacity
 | 
| 226 |   return reinterpret_cast<uint8_t*>(str_->data_) + str_->len_;
 | 
| 227 | }
 | 
| 228 | 
 | 
| 229 | void BufWriter::SetLengthFrom(uint8_t* length_ptr) {
 | 
| 230 |   uint8_t* begin = reinterpret_cast<uint8_t*>(str_->data_);
 | 
| 231 |   DCHECK(length_ptr >= begin);  // we should have written some data
 | 
| 232 | 
 | 
| 233 |   // Set the length, e.g. so we know where to resume writing from
 | 
| 234 |   len_ = length_ptr - begin;
 | 
| 235 |   // printf("SET LEN to %d\n", len_);
 | 
| 236 | }
 | 
| 237 | 
 | 
| 238 | void BufWriter::Truncate(int length) {
 | 
| 239 |   len_ = length;
 | 
| 240 | }
 | 
| 241 | 
 | 
| 242 | void BufWriter::WriteRaw(char* s, int n) {
 | 
| 243 |   DCHECK(is_valid_);  // Can't write() after getvalue()
 | 
| 244 | 
 | 
| 245 |   // write('') is a no-op, so don't create Buf if we don't need to
 | 
| 246 |   if (n == 0) {
 | 
| 247 |     return;
 | 
| 248 |   }
 | 
| 249 | 
 | 
| 250 |   EnsureMoreSpace(n);
 | 
| 251 | 
 | 
| 252 |   // Append the contents to the buffer
 | 
| 253 |   memcpy(str_->data_ + len_, s, n);
 | 
| 254 |   len_ += n;
 | 
| 255 |   str_->data_[len_] = '\0';
 | 
| 256 | }
 | 
| 257 | 
 | 
| 258 | void BufWriter::WriteConst(const char* c_string) {
 | 
| 259 |   // meant for short strings like '"'
 | 
| 260 |   WriteRaw(const_cast<char*>(c_string), strlen(c_string));
 | 
| 261 | }
 | 
| 262 | 
 | 
| 263 | void BufWriter::write(BigStr* s) {
 | 
| 264 |   WriteRaw(s->data_, len(s));
 | 
| 265 | }
 | 
| 266 | 
 | 
| 267 | void BufWriter::write_spaces(int n) {
 | 
| 268 |   if (n == 0) {
 | 
| 269 |     return;
 | 
| 270 |   }
 | 
| 271 | 
 | 
| 272 |   EnsureMoreSpace(n);
 | 
| 273 | 
 | 
| 274 |   char* dest = str_->data_ + len_;
 | 
| 275 |   for (int i = 0; i < n; ++i) {
 | 
| 276 |     dest[i] = ' ';
 | 
| 277 |   }
 | 
| 278 |   len_ += n;
 | 
| 279 |   str_->data_[len_] = '\0';
 | 
| 280 | }
 | 
| 281 | 
 | 
| 282 | BigStr* BufWriter::getvalue() {
 | 
| 283 |   DCHECK(is_valid_);  // Check for two INVALID getvalue() in a row
 | 
| 284 |   is_valid_ = false;
 | 
| 285 | 
 | 
| 286 |   if (str_ == nullptr) {  // if no write() methods are called, the result is ""
 | 
| 287 |     return kEmptyString;
 | 
| 288 |   } else {
 | 
| 289 |     BigStr* s = str_;
 | 
| 290 |     s->MaybeShrink(len_);
 | 
| 291 |     str_ = nullptr;
 | 
| 292 |     len_ = -1;
 | 
| 293 |     return s;
 | 
| 294 |   }
 | 
| 295 | }
 | 
| 296 | 
 | 
| 297 | }  // namespace mylib
 |