koffi 2.7.0 → 2.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -249,14 +249,6 @@ extern "C" void AssertMessage(const char *filename, int line, const char *cond);
249
249
  #define RG_UNREACHABLE() __assume(0)
250
250
  #endif
251
251
 
252
- #if defined(__EMSCRIPTEN__)
253
- #define RG_EXPORT EMSCRIPTEN_KEEPALIVE
254
- #elif defined(_WIN32)
255
- #define RG_EXPORT __declspec(dllexport)
256
- #else
257
- #define RG_EXPORT __attribute__((visibility("default")))
258
- #endif
259
-
260
252
  #define RG_DELETE_COPY(Cls) \
261
253
  Cls(const Cls&) = delete; \
262
254
  Cls &operator=(const Cls&) = delete;
@@ -1220,11 +1212,18 @@ public:
1220
1212
  {
1221
1213
  RG_ASSERT(len <= N - count);
1222
1214
 
1223
- T *it = data + len;
1224
- *it = {};
1225
- len += count;
1215
+ T *first = data + len;
1216
+ if constexpr(!std::is_trivial<T>::value) {
1217
+ for (Size i = 0; i < count; i++) {
1218
+ new (data + len) T();
1219
+ len++;
1220
+ }
1221
+ } else {
1222
+ memset_safe(first, 0, count * RG_SIZE(T));
1223
+ len += count;
1224
+ }
1226
1225
 
1227
- return it;
1226
+ return first;
1228
1227
  }
1229
1228
 
1230
1229
  T *Append(const T &value)
@@ -1312,11 +1311,7 @@ public:
1312
1311
  {
1313
1312
  RemoveFrom(0);
1314
1313
  Grow(other.capacity);
1315
- #if __cplusplus >= 201703L
1316
1314
  if constexpr(!std::is_trivial<T>::value) {
1317
- #else
1318
- if (true) {
1319
- #endif
1320
1315
  for (Size i = 0; i < other.len; i++) {
1321
1316
  ptr[i] = other.ptr[i];
1322
1317
  }
@@ -1421,11 +1416,7 @@ public:
1421
1416
  Grow(count);
1422
1417
 
1423
1418
  T *first = ptr + len;
1424
- #if __cplusplus >= 201703L
1425
1419
  if constexpr(!std::is_trivial<T>::value) {
1426
- #else
1427
- if (true) {
1428
- #endif
1429
1420
  for (Size i = 0; i < count; i++) {
1430
1421
  new (ptr + len) T();
1431
1422
  len++;
@@ -1434,6 +1425,7 @@ public:
1434
1425
  memset_safe(first, 0, count * RG_SIZE(T));
1435
1426
  len += count;
1436
1427
  }
1428
+
1437
1429
  return first;
1438
1430
  }
1439
1431
 
@@ -1442,11 +1434,7 @@ public:
1442
1434
  Grow();
1443
1435
 
1444
1436
  T *first = ptr + len;
1445
- #if __cplusplus >= 201703L
1446
1437
  if constexpr(!std::is_trivial<T>::value) {
1447
- #else
1448
- if (true) {
1449
- #endif
1450
1438
  new (ptr + len) T;
1451
1439
  }
1452
1440
  ptr[len++] = value;
@@ -1458,11 +1446,7 @@ public:
1458
1446
 
1459
1447
  T *first = ptr + len;
1460
1448
  for (const T &value: values) {
1461
- #if __cplusplus >= 201703L
1462
1449
  if constexpr(!std::is_trivial<T>::value) {
1463
- #else
1464
- if (true) {
1465
- #endif
1466
1450
  new (ptr + len) T;
1467
1451
  }
1468
1452
  ptr[len++] = value;
@@ -1474,11 +1458,7 @@ public:
1474
1458
  {
1475
1459
  RG_ASSERT(first >= 0 && first <= len);
1476
1460
 
1477
- #if __cplusplus >= 201703L
1478
1461
  if constexpr(!std::is_trivial<T>::value) {
1479
- #else
1480
- if (true) {
1481
- #endif
1482
1462
  for (Size i = first; i < len; i++) {
1483
1463
  ptr[i].~T();
1484
1464
  }
@@ -1784,11 +1764,7 @@ private:
1784
1764
  void DeleteValues([[maybe_unused]] iterator_type begin,
1785
1765
  [[maybe_unused]] iterator_type end)
1786
1766
  {
1787
- #if __cplusplus >= 201703L
1788
1767
  if constexpr(!std::is_trivial<T>::value) {
1789
- #else
1790
- if (true) {
1791
- #endif
1792
1768
  for (iterator_type it = begin; it != end; ++it) {
1793
1769
  it->~T();
1794
1770
  }
@@ -2065,11 +2041,7 @@ public:
2065
2041
  }
2066
2042
  ~HashTable()
2067
2043
  {
2068
- #if __cplusplus >= 201703L
2069
2044
  if constexpr(std::is_trivial<ValueType>::value) {
2070
- #else
2071
- if (false) {
2072
- #endif
2073
2045
  count = 0;
2074
2046
  Rehash(0);
2075
2047
  } else {
@@ -2266,11 +2238,7 @@ private:
2266
2238
  template <typename T = KeyType>
2267
2239
  const ValueType *Find(Size *idx, const T &key) const
2268
2240
  {
2269
- #if __cplusplus >= 201703L
2270
2241
  if constexpr(std::is_pointer<ValueType>::value) {
2271
- #else
2272
- if (false) {
2273
- #endif
2274
2242
  while (data[*idx]) {
2275
2243
  const KeyType &it_key = Handler::GetKey(data[*idx]);
2276
2244
  if (Handler::TestKeys(it_key, key))
@@ -3172,18 +3140,22 @@ static inline void Log(LogLevel level, const char *ctx, const char *fmt, Args...
3172
3140
 
3173
3141
  // Shortcut log functions
3174
3142
  #ifdef RG_DEBUG
3175
- template <typename... Args>
3176
- static inline void LogDebug(Args... args) { Log(LogLevel::Debug, "Debug", args...); }
3143
+ const char *DebugLogContext(const char *filename, int line);
3144
+
3145
+ #define LogDebug(...) Log(LogLevel::Debug, DebugLogContext(__FILE__, __LINE__) __VA_OPT__(,) __VA_ARGS__)
3146
+ #define LogInfo(...) Log(LogLevel::Info, nullptr __VA_OPT__(,) __VA_ARGS__)
3147
+ #define LogWarning(...) Log(LogLevel::Warning, DebugLogContext(__FILE__, __LINE__) __VA_OPT__(,) __VA_ARGS__)
3148
+ #define LogError(...) Log(LogLevel::Error, DebugLogContext(__FILE__, __LINE__) __VA_OPT__(,) __VA_ARGS__)
3177
3149
  #else
3178
- template <typename... Args>
3179
- static inline void LogDebug(Args...) {}
3150
+ template <typename... Args>
3151
+ static inline void LogDebug(Args...) {}
3152
+ template <typename... Args>
3153
+ static inline void LogInfo(Args... args) { Log(LogLevel::Info, nullptr, args...); }
3154
+ template <typename... Args>
3155
+ static inline void LogWarning(Args... args) { Log(LogLevel::Warning, "Warning: ", args...); }
3156
+ template <typename... Args>
3157
+ static inline void LogError(Args... args) { Log(LogLevel::Error, "Error: ", args...); }
3180
3158
  #endif
3181
- template <typename... Args>
3182
- static inline void LogInfo(Args... args) { Log(LogLevel::Info, nullptr, args...); }
3183
- template <typename... Args>
3184
- static inline void LogWarning(Args... args) { Log(LogLevel::Warning, "Warning", args...); }
3185
- template <typename... Args>
3186
- static inline void LogError(Args... args) { Log(LogLevel::Error, "Error", args...); }
3187
3159
 
3188
3160
  void SetLogHandler(const std::function<LogFunc> &func);
3189
3161
  void DefaultLogHandler(LogLevel level, const char *ctx, const char *msg);
@@ -3289,7 +3261,7 @@ static inline bool TestStrI(Span<const char> str1, const char *str2)
3289
3261
  return (i == str1.len) && !str2[i];
3290
3262
  }
3291
3263
  static inline bool TestStrI(const char *str1, Span<const char> str2)
3292
- { return TestStr(str2, str1); }
3264
+ { return TestStrI(str2, str1); }
3293
3265
  static inline bool TestStrI(const char *str1, const char *str2)
3294
3266
  {
3295
3267
  Size i = 0;
@@ -4332,8 +4304,8 @@ enum class CompressionSpeed {
4332
4304
  Fast
4333
4305
  };
4334
4306
 
4335
- class StreamDecompressor;
4336
- class StreamCompressor;
4307
+ class StreamDecoder;
4308
+ class StreamEncoder;
4337
4309
 
4338
4310
  class StreamReader {
4339
4311
  RG_DELETE_COPY(StreamReader)
@@ -4371,8 +4343,7 @@ class StreamReader {
4371
4343
  bool eof = false;
4372
4344
  } source;
4373
4345
 
4374
- CompressionType compression_type = CompressionType::None;
4375
- StreamDecompressor *decompressor = nullptr;
4346
+ StreamDecoder *decoder = nullptr;
4376
4347
 
4377
4348
  int64_t raw_len = -1;
4378
4349
  Size raw_read = 0;
@@ -4396,6 +4367,9 @@ public:
4396
4367
  : StreamReader() { Open(func, filename, compression_type); }
4397
4368
  ~StreamReader() { Close(true); }
4398
4369
 
4370
+ // Call before Open!
4371
+ void SetDecoder(StreamDecoder *decoder);
4372
+
4399
4373
  bool Open(Span<const uint8_t> buf, const char *filename = nullptr,
4400
4374
  CompressionType compression_type = CompressionType::None);
4401
4375
  bool Open(FILE *fp, const char *filename,
@@ -4409,7 +4383,6 @@ public:
4409
4383
  bool Rewind();
4410
4384
 
4411
4385
  const char *GetFileName() const { return filename; }
4412
- CompressionType GetCompressionType() const { return compression_type; }
4413
4386
  int64_t GetReadLimit() { return read_max; }
4414
4387
  bool IsValid() const { return filename && !error; }
4415
4388
  bool IsEOF() const { return eof; }
@@ -4437,62 +4410,38 @@ private:
4437
4410
 
4438
4411
  Size ReadRaw(Size max_len, void *out_buf);
4439
4412
 
4440
- friend class StreamDecompressor;
4413
+ friend class StreamDecoder;
4441
4414
  };
4442
4415
 
4443
- static inline Size ReadFile(const char *filename, CompressionType compression_type, Span<uint8_t> out_buf)
4444
- {
4445
- StreamReader st(filename, compression_type);
4446
- return st.Read(out_buf);
4447
- }
4448
4416
  static inline Size ReadFile(const char *filename, Span<uint8_t> out_buf)
4449
4417
  {
4450
4418
  StreamReader st(filename);
4451
4419
  return st.Read(out_buf);
4452
4420
  }
4453
- static inline Size ReadFile(const char *filename, CompressionType compression_type, Span<char> out_buf)
4454
- {
4455
- StreamReader st(filename, compression_type);
4456
- return st.Read(out_buf);
4457
- }
4458
4421
  static inline Size ReadFile(const char *filename, Span<char> out_buf)
4459
4422
  {
4460
4423
  StreamReader st(filename);
4461
4424
  return st.Read(out_buf);
4462
4425
  }
4463
-
4464
- static inline Size ReadFile(const char *filename, Size max_len, CompressionType compression_type,
4465
- HeapArray<uint8_t> *out_buf)
4466
- {
4467
- StreamReader st(filename, compression_type);
4468
- return st.ReadAll(max_len, out_buf);
4469
- }
4470
4426
  static inline Size ReadFile(const char *filename, Size max_len, HeapArray<uint8_t> *out_buf)
4471
4427
  {
4472
4428
  StreamReader st(filename);
4473
4429
  return st.ReadAll(max_len, out_buf);
4474
4430
  }
4475
- static inline Size ReadFile(const char *filename, Size max_len, CompressionType compression_type,
4476
- HeapArray<char> *out_buf)
4477
- {
4478
- StreamReader st(filename, compression_type);
4479
- return st.ReadAll(max_len, out_buf);
4480
- }
4481
4431
  static inline Size ReadFile(const char *filename, Size max_len, HeapArray<char> *out_buf)
4482
4432
  {
4483
4433
  StreamReader st(filename);
4484
4434
  return st.ReadAll(max_len, out_buf);
4485
4435
  }
4486
4436
 
4487
- class StreamDecompressor {
4437
+ class StreamDecoder {
4488
4438
  protected:
4489
4439
  StreamReader *reader;
4490
4440
 
4491
4441
  public:
4492
- StreamDecompressor(StreamReader *reader) : reader(reader) {}
4493
- virtual ~StreamDecompressor() {}
4442
+ StreamDecoder(StreamReader *reader) : reader(reader) {}
4443
+ virtual ~StreamDecoder() {}
4494
4444
 
4495
- virtual bool Init(CompressionType type) = 0;
4496
4445
  virtual Size Read(Size max_len, void *out_buf) = 0;
4497
4446
 
4498
4447
  protected:
@@ -4505,7 +4454,7 @@ protected:
4505
4454
  void SetEOF(bool eof) { reader->eof = eof; }
4506
4455
  };
4507
4456
 
4508
- typedef StreamDecompressor *CreateDecompressorFunc(StreamReader *reader);
4457
+ typedef StreamDecoder *CreateDecompressorFunc(StreamReader *reader, CompressionType type);
4509
4458
 
4510
4459
  class StreamDecompressorHelper {
4511
4460
  public:
@@ -4513,9 +4462,9 @@ public:
4513
4462
  };
4514
4463
 
4515
4464
  #define RG_REGISTER_DECOMPRESSOR(Type, Cls) \
4516
- static StreamDecompressor *RG_UNIQUE_NAME(CreateDecompressor)(StreamReader *reader) \
4465
+ static StreamDecoder *RG_UNIQUE_NAME(CreateDecompressor)(StreamReader *reader, CompressionType type) \
4517
4466
  { \
4518
- StreamDecompressor *decompressor = new Cls(reader); \
4467
+ StreamDecoder *decompressor = new Cls(reader, type); \
4519
4468
  return decompressor; \
4520
4469
  } \
4521
4470
  static StreamDecompressorHelper RG_UNIQUE_NAME(CreateDecompressorHelper)((Type), RG_UNIQUE_NAME(CreateDecompressor))
@@ -4589,9 +4538,7 @@ class StreamWriter {
4589
4538
  bool vt100;
4590
4539
  } dest;
4591
4540
 
4592
- CompressionType compression_type = CompressionType::None;
4593
- CompressionSpeed compression_speed = CompressionSpeed::Default;
4594
- StreamCompressor *compressor = nullptr;
4541
+ StreamEncoder *encoder = nullptr;
4595
4542
 
4596
4543
  int64_t raw_written = 0;
4597
4544
 
@@ -4621,6 +4568,9 @@ public:
4621
4568
  : StreamWriter() { Open(func, filename, compression_type, compression_speed); }
4622
4569
  ~StreamWriter() { Close(true); }
4623
4570
 
4571
+ // Call before Open!
4572
+ void SetEncoder(StreamEncoder *encoder);
4573
+
4624
4574
  bool Open(HeapArray<uint8_t> *mem, const char *filename = nullptr,
4625
4575
  CompressionType compression_type = CompressionType::None,
4626
4576
  CompressionSpeed compression_speed = CompressionSpeed::Default);
@@ -4643,8 +4593,6 @@ public:
4643
4593
  bool Flush();
4644
4594
 
4645
4595
  const char *GetFileName() const { return filename; }
4646
- CompressionType GetCompressionType() const { return compression_type; }
4647
- CompressionSpeed GetCompressionSpeed() const { return compression_speed; }
4648
4596
  bool IsVt100() const { return dest.vt100; }
4649
4597
  bool IsValid() const { return filename && !error; }
4650
4598
 
@@ -4665,33 +4613,30 @@ private:
4665
4613
 
4666
4614
  bool WriteRaw(Span<const uint8_t> buf);
4667
4615
 
4668
- friend class StreamCompressor;
4616
+ friend class StreamEncoder;
4669
4617
  };
4670
4618
 
4671
- static inline bool WriteFile(Span<const uint8_t> buf, const char *filename, unsigned int flags = 0,
4672
- CompressionType compression_type = CompressionType::None)
4619
+ static inline bool WriteFile(Span<const uint8_t> buf, const char *filename, unsigned int flags = 0)
4673
4620
  {
4674
- StreamWriter st(filename, flags, compression_type);
4621
+ StreamWriter st(filename, flags);
4675
4622
  st.Write(buf);
4676
4623
  return st.Close();
4677
4624
  }
4678
- static inline bool WriteFile(Span<const char> buf, const char *filename, unsigned int flags = 0,
4679
- CompressionType compression_type = CompressionType::None)
4625
+ static inline bool WriteFile(Span<const char> buf, const char *filename, unsigned int flags = 0)
4680
4626
  {
4681
- StreamWriter st(filename, flags, compression_type);
4627
+ StreamWriter st(filename, flags);
4682
4628
  st.Write(buf);
4683
4629
  return st.Close();
4684
4630
  }
4685
4631
 
4686
- class StreamCompressor {
4632
+ class StreamEncoder {
4687
4633
  protected:
4688
4634
  StreamWriter *writer;
4689
4635
 
4690
4636
  public:
4691
- StreamCompressor(StreamWriter *writer) : writer(writer) {}
4692
- virtual ~StreamCompressor() {}
4637
+ StreamEncoder(StreamWriter *writer) : writer(writer) {}
4638
+ virtual ~StreamEncoder() {}
4693
4639
 
4694
- virtual bool Init(CompressionType type, CompressionSpeed speed) = 0;
4695
4640
  virtual bool Write(Span<const uint8_t> buf) = 0;
4696
4641
  virtual bool Finalize() = 0;
4697
4642
 
@@ -4702,7 +4647,7 @@ protected:
4702
4647
  bool WriteRaw(Span<const uint8_t> buf) { return writer->WriteRaw(buf); }
4703
4648
  };
4704
4649
 
4705
- typedef StreamCompressor *CreateCompressorFunc(StreamWriter *writer);
4650
+ typedef StreamEncoder *CreateCompressorFunc(StreamWriter *writer, CompressionType type, CompressionSpeed speed);
4706
4651
 
4707
4652
  class StreamCompressorHelper {
4708
4653
  public:
@@ -4710,9 +4655,9 @@ public:
4710
4655
  };
4711
4656
 
4712
4657
  #define RG_REGISTER_COMPRESSOR(Type, Cls) \
4713
- static StreamCompressor *RG_UNIQUE_NAME(CreateCompressor)(StreamWriter *writer) \
4658
+ static StreamEncoder *RG_UNIQUE_NAME(CreateCompressor)(StreamWriter *writer, CompressionType type, CompressionSpeed speed) \
4714
4659
  { \
4715
- StreamCompressor *compressor = new Cls(writer); \
4660
+ StreamEncoder *compressor = new Cls(writer, type, speed); \
4716
4661
  return compressor; \
4717
4662
  } \
4718
4663
  static StreamCompressorHelper RG_UNIQUE_NAME(CreateCompressorHelper)((Type), RG_UNIQUE_NAME(CreateCompressor))
@@ -4887,7 +4832,9 @@ public:
4887
4832
  { return Test(test1, nullptr, type); }
4888
4833
 
4889
4834
  bool TestHasFailed() const { return test_failed; }
4835
+
4890
4836
  void LogUnknownError() const;
4837
+ void LogUnusedArguments() const;
4891
4838
  };
4892
4839
 
4893
4840
  template <typename T>
@@ -4920,6 +4867,36 @@ bool OptionToEnum(Span<const OptionDesc> options, Span<const char> str, T *out_v
4920
4867
  return false;
4921
4868
  }
4922
4869
 
4870
+ template <typename T>
4871
+ bool OptionToEnumI(Span<const char *const> options, Span<const char> str, T *out_value)
4872
+ {
4873
+ for (Size i = 0; i < options.len; i++) {
4874
+ const char *opt = options[i];
4875
+
4876
+ if (TestStrI(opt, str)) {
4877
+ *out_value = (T)i;
4878
+ return true;
4879
+ }
4880
+ }
4881
+
4882
+ return false;
4883
+ }
4884
+
4885
+ template <typename T>
4886
+ bool OptionToEnumI(Span<const OptionDesc> options, Span<const char> str, T *out_value)
4887
+ {
4888
+ for (Size i = 0; i < options.len; i++) {
4889
+ const OptionDesc &desc = options[i];
4890
+
4891
+ if (TestStrI(desc.name, str)) {
4892
+ *out_value = (T)i;
4893
+ return true;
4894
+ }
4895
+ }
4896
+
4897
+ return false;
4898
+ }
4899
+
4923
4900
  template <typename T>
4924
4901
  bool OptionToFlag(Span<const char *const> options, Span<const char> str, T *out_flags, bool enable = true)
4925
4902
  {
@@ -4950,6 +4927,36 @@ bool OptionToFlag(Span<const OptionDesc> options, Span<const char> str, T *out_f
4950
4927
  return false;
4951
4928
  }
4952
4929
 
4930
+ template <typename T>
4931
+ bool OptionToFlagI(Span<const char *const> options, Span<const char> str, T *out_flags, bool enable = true)
4932
+ {
4933
+ for (Size i = 0; i < options.len; i++) {
4934
+ const char *opt = options[i];
4935
+
4936
+ if (TestStrI(opt, str)) {
4937
+ *out_flags = ApplyMask(*out_flags, 1u << i, enable);
4938
+ return true;
4939
+ }
4940
+ }
4941
+
4942
+ return false;
4943
+ }
4944
+
4945
+ template <typename T>
4946
+ bool OptionToFlagI(Span<const OptionDesc> options, Span<const char> str, T *out_flags, bool enable = true)
4947
+ {
4948
+ for (Size i = 0; i < options.len; i++) {
4949
+ const OptionDesc &desc = options[i];
4950
+
4951
+ if (TestStrI(desc.name, str)) {
4952
+ *out_flags = ApplyMask(*out_flags, 1u << i, enable);
4953
+ return true;
4954
+ }
4955
+ }
4956
+
4957
+ return false;
4958
+ }
4959
+
4953
4960
  // ------------------------------------------------------------------------
4954
4961
  // Console prompter (simplified readline)
4955
4962
  // ------------------------------------------------------------------------
@@ -27,7 +27,7 @@
27
27
 
28
28
  namespace RG {
29
29
 
30
- class LZ4Decompressor: public StreamDecompressor {
30
+ class LZ4Decompressor: public StreamDecoder {
31
31
  LZ4F_dctx *decoder = nullptr;
32
32
  bool done = false;
33
33
 
@@ -39,25 +39,23 @@ class LZ4Decompressor: public StreamDecompressor {
39
39
  Size out_len = 0;
40
40
 
41
41
  public:
42
- LZ4Decompressor(StreamReader *reader) : StreamDecompressor(reader) {}
42
+ LZ4Decompressor(StreamReader *reader, CompressionType type);
43
43
  ~LZ4Decompressor();
44
44
 
45
- bool Init(CompressionType type) override;
46
45
  Size Read(Size max_len, void *out_buf) override;
47
46
  };
48
47
 
49
- LZ4Decompressor::~LZ4Decompressor()
50
- {
51
- LZ4F_freeDecompressionContext(decoder);
52
- }
53
-
54
- bool LZ4Decompressor::Init(CompressionType)
48
+ LZ4Decompressor::LZ4Decompressor(StreamReader *reader, CompressionType)
49
+ : StreamDecoder(reader)
55
50
  {
56
51
  LZ4F_errorCode_t err = LZ4F_createDecompressionContext(&decoder, LZ4F_VERSION);
57
52
  if (LZ4F_isError(err))
58
53
  throw std::bad_alloc();
54
+ }
59
55
 
60
- return true;
56
+ LZ4Decompressor::~LZ4Decompressor()
57
+ {
58
+ LZ4F_freeDecompressionContext(decoder);
61
59
  }
62
60
 
63
61
  Size LZ4Decompressor::Read(Size max_len, void *user_buf)
@@ -106,27 +104,22 @@ Size LZ4Decompressor::Read(Size max_len, void *user_buf)
106
104
  RG_UNREACHABLE();
107
105
  }
108
106
 
109
- class LZ4Compressor: public StreamCompressor {
107
+ class LZ4Compressor: public StreamEncoder {
110
108
  LZ4F_cctx *encoder = nullptr;
111
109
  LZ4F_preferences_t prefs = {};
112
110
 
113
111
  HeapArray<uint8_t> dynamic_buf;
114
112
 
115
113
  public:
116
- LZ4Compressor(StreamWriter *writer) : StreamCompressor(writer) {}
114
+ LZ4Compressor(StreamWriter *writer, CompressionType type, CompressionSpeed speed);
117
115
  ~LZ4Compressor();
118
116
 
119
- bool Init(CompressionType type, CompressionSpeed speed) override;
120
117
  bool Write(Span<const uint8_t> buf) override;
121
118
  bool Finalize() override;
122
119
  };
123
120
 
124
- LZ4Compressor::~LZ4Compressor()
125
- {
126
- LZ4F_freeCompressionContext(encoder);
127
- }
128
-
129
- bool LZ4Compressor::Init(CompressionType, CompressionSpeed speed)
121
+ LZ4Compressor::LZ4Compressor(StreamWriter *writer, CompressionType, CompressionSpeed speed)
122
+ : StreamEncoder(writer)
130
123
  {
131
124
  LZ4F_errorCode_t err = LZ4F_createCompressionContext(&encoder, LZ4F_VERSION);
132
125
  if (LZ4F_isError(err))
@@ -141,15 +134,15 @@ bool LZ4Compressor::Init(CompressionType, CompressionSpeed speed)
141
134
  dynamic_buf.Grow(LZ4F_HEADER_SIZE_MAX);
142
135
 
143
136
  size_t ret = LZ4F_compressBegin(encoder, dynamic_buf.end(), dynamic_buf.capacity - dynamic_buf.len, &prefs);
144
-
145
- if (LZ4F_isError(ret)) {
146
- LogError("Failed to start LZ4 stream for '%1': %2", GetFileName(), LZ4F_getErrorName(ret));
147
- return false;
148
- }
137
+ if (LZ4F_isError(ret))
138
+ throw std::bad_alloc();
149
139
 
150
140
  dynamic_buf.len += ret;
141
+ }
151
142
 
152
- return true;
143
+ LZ4Compressor::~LZ4Compressor()
144
+ {
145
+ LZ4F_freeCompressionContext(encoder);
153
146
  }
154
147
 
155
148
  bool LZ4Compressor::Write(Span<const uint8_t> buf)
@@ -26,7 +26,7 @@
26
26
 
27
27
  namespace RG {
28
28
 
29
- class MinizDecompressor: public StreamDecompressor {
29
+ class MinizDecompressor: public StreamDecoder {
30
30
  tinfl_decompressor inflator;
31
31
  bool done = false;
32
32
 
@@ -45,21 +45,19 @@ class MinizDecompressor: public StreamDecompressor {
45
45
  Size uncompressed_size = 0;
46
46
 
47
47
  public:
48
- MinizDecompressor(StreamReader *reader) : StreamDecompressor(reader) {}
48
+ MinizDecompressor(StreamReader *reader, CompressionType type);
49
49
  ~MinizDecompressor() {}
50
50
 
51
- bool Init(CompressionType type) override;
52
51
  Size Read(Size max_len, void *out_buf) override;
53
52
  };
54
53
 
55
- bool MinizDecompressor::Init(CompressionType type)
54
+ MinizDecompressor::MinizDecompressor(StreamReader *reader, CompressionType type)
55
+ : StreamDecoder(reader)
56
56
  {
57
57
  static_assert(RG_SIZE(out_buf) >= TINFL_LZ_DICT_SIZE);
58
58
 
59
59
  tinfl_init(&inflator);
60
60
  is_gzip = (type == CompressionType::Gzip);
61
-
62
- return true;
63
61
  }
64
62
 
65
63
  Size MinizDecompressor::Read(Size max_len, void *user_buf)
@@ -217,7 +215,7 @@ truncated_error:
217
215
  return -1;
218
216
  }
219
217
 
220
- class MinizCompressor: public StreamCompressor {
218
+ class MinizCompressor: public StreamEncoder {
221
219
  tdefl_compressor deflator;
222
220
 
223
221
  // Gzip support
@@ -229,10 +227,9 @@ class MinizCompressor: public StreamCompressor {
229
227
  LocalArray<uint8_t, 1024> small_buf;
230
228
 
231
229
  public:
232
- MinizCompressor(StreamWriter *writer) : StreamCompressor(writer) {}
230
+ MinizCompressor(StreamWriter *writer, CompressionType type, CompressionSpeed speed);
233
231
  ~MinizCompressor() {}
234
232
 
235
- bool Init(CompressionType type, CompressionSpeed speed) override;
236
233
  bool Write(Span<const uint8_t> buf) override;
237
234
  bool Finalize() override;
238
235
 
@@ -240,7 +237,8 @@ private:
240
237
  bool WriteDeflate(Span<const uint8_t> buf);
241
238
  };
242
239
 
243
- bool MinizCompressor::Init(CompressionType type, CompressionSpeed speed)
240
+ MinizCompressor::MinizCompressor(StreamWriter *writer, CompressionType type, CompressionSpeed speed)
241
+ : StreamEncoder(writer)
244
242
  {
245
243
  is_gzip = (type == CompressionType::Gzip);
246
244
 
@@ -256,10 +254,7 @@ bool MinizCompressor::Init(CompressionType type, CompressionSpeed speed)
256
254
  MinizCompressor *compressor = (MinizCompressor *)udata;
257
255
  return (int)compressor->WriteRaw(MakeSpan((uint8_t *)buf, len));
258
256
  }, this, flags);
259
- if (status != TDEFL_STATUS_OKAY) {
260
- LogError("Failed to initialize Deflate compression for '%1'", GetFileName());
261
- return false;
262
- }
257
+ RG_ASSERT(status == TDEFL_STATUS_OKAY);
263
258
 
264
259
  if (is_gzip) {
265
260
  static uint8_t gzip_header[] = {
@@ -271,11 +266,8 @@ bool MinizCompressor::Init(CompressionType type, CompressionSpeed speed)
271
266
  0 // OS
272
267
  };
273
268
 
274
- if (!WriteRaw(gzip_header))
275
- return false;
269
+ WriteRaw(gzip_header);
276
270
  }
277
-
278
- return true;
279
271
  }
280
272
 
281
273
  bool MinizCompressor::Write(Span<const uint8_t> buf)
@@ -144,6 +144,10 @@ const TypeInfo *PrototypeParser::ParseType(int *out_directions)
144
144
  while (++offset < tokens.len && (tokens[offset] == '*' ||
145
145
  tokens[offset] == '!' ||
146
146
  tokens[offset] == "const"));
147
+ if (offset < tokens.len && tokens[offset] == "[") [[unlikely]] {
148
+ MarkError("Array types decay to pointers in prototypes (C standard), use pointers");
149
+ return instance->void_type;
150
+ }
147
151
  offset--;
148
152
 
149
153
  while (offset >= start) {