382 lines
13 KiB
C
382 lines
13 KiB
C
/* Generated by zi2c from file.zi. */
|
|
#define ZIRAN_FILE_H_SOURCE 1
|
|
#include <stdbool.h>
|
|
#include "zir_bounds.h"
|
|
#include "zir_string.h"
|
|
#include "zir_vec.h"
|
|
#include "file.h"
|
|
#include "result.h"
|
|
|
|
bool
|
|
file_FilePathText(String text)
|
|
{
|
|
int64_t at = 0LL;
|
|
while (at < (int64_t)(text).length) {
|
|
uint8_t first = (uint8_t)ZIRAN_INDEX(text.data, text.length, at);
|
|
if (first < 128) {
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
continue;
|
|
}
|
|
int64_t count = 0LL;
|
|
uint32_t codepoint = 0;
|
|
uint32_t minimum = 0;
|
|
if (first >= 194 && first <= 223) {
|
|
count = 1LL;
|
|
codepoint = (uint32_t)(((uint8_t)((uint32_t)first & 31u)));
|
|
minimum = 128;
|
|
} else if (first >= 224 && first <= 239) {
|
|
count = 2LL;
|
|
codepoint = (uint32_t)(((uint8_t)((uint32_t)first & 15u)));
|
|
minimum = 2048;
|
|
} else if (first >= 240 && first <= 244) {
|
|
count = 3LL;
|
|
codepoint = (uint32_t)(((uint8_t)((uint32_t)first & 7u)));
|
|
minimum = 65536;
|
|
} else {
|
|
return false;
|
|
}
|
|
if ((int64_t)((uint64_t)at + (uint64_t)count) >= (int64_t)(text).length) {
|
|
return false;
|
|
}
|
|
int64_t index = 1LL;
|
|
while (index <= count) {
|
|
uint8_t part = (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)at + (uint64_t)index)));
|
|
if (part < 128 || part > 191) {
|
|
return false;
|
|
}
|
|
codepoint = (uint32_t)(((uint32_t)codepoint << 6) | (uint32_t)(((uint8_t)((uint32_t)part & 63u))));
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
if (codepoint < minimum || codepoint > 1114111 || (codepoint >= 55296 && codepoint <= 57343)) {
|
|
return false;
|
|
}
|
|
at = (int64_t)((uint64_t)at + ((uint64_t)count + UINT64_C(1)));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
__type_d6f9b1b7c4ee2d51
|
|
ReadEntireFileCapped(String path, int64_t maximum)
|
|
{
|
|
__type_d6f9b1b7c4ee2d51 result = {0};
|
|
if (maximum < 0LL) {
|
|
FileReadFailure* value_0 = &(result.error);
|
|
(*value_0) = FileReadFailure_InvalidLimit;
|
|
return result;
|
|
}
|
|
bool value_1 = (int64_t)(path).length == 0LL || (int64_t)(path).length >= 4096LL;
|
|
if (!value_1) {
|
|
bool value_2 = file_FilePathText(path);
|
|
value_1 = !value_2;
|
|
}
|
|
bool invalid_path = value_1;
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(path).length; it_index++) {
|
|
uint8_t byte = (uint8_t)path.data[it_index];
|
|
if (byte == 0) {
|
|
invalid_path = true;
|
|
}
|
|
}
|
|
if (invalid_path) {
|
|
FileReadFailure* value_3 = &(result.error);
|
|
(*value_3) = FileReadFailure_InvalidPath;
|
|
return result;
|
|
}
|
|
FileHandle file = OpenRead(path);
|
|
if (!file.valid) {
|
|
FileReadFailure* value_4 = &(result.error);
|
|
bool value_5 = PathExists(path);
|
|
FileReadFailure value_6 = (FileReadFailure)(0);
|
|
if (value_5) {
|
|
value_6 = FileReadFailure_ReadError;
|
|
} else {
|
|
value_6 = FileReadFailure_NotFound;
|
|
}
|
|
(*value_4) = value_6;
|
|
return result;
|
|
}
|
|
__type_838711075b90279a bytes = {0};
|
|
__type_838711075b90279a* value_7 = &bytes;
|
|
uint8_t chunk[4096] = {0};
|
|
int64_t offset = 0LL;
|
|
while (true) {
|
|
int64_t allowed = (int64_t)((uint64_t)maximum - (uint64_t)offset);
|
|
Slice value_8 = {chunk, 4096};
|
|
int64_t value_9 = 0;
|
|
if (allowed < 4096LL) {
|
|
value_9 = (int64_t)((uint64_t)allowed + UINT64_C(1));
|
|
} else {
|
|
value_9 = 4096LL;
|
|
}
|
|
int64_t count = ReadAt(file, SliceRange(value_8, (int64_t)0LL, (int64_t)value_9, sizeof(uint8_t)), offset);
|
|
if (count < 0LL) {
|
|
CloseFile(file);
|
|
free((bytes).data);
|
|
(bytes).data = NULL;
|
|
bytes.capacity = 0;
|
|
bytes.count = 0;
|
|
FileReadFailure* value_10 = &(result.error);
|
|
(*value_10) = FileReadFailure_ReadError;
|
|
__type_d6f9b1b7c4ee2d51 value_11 = result;
|
|
free((*value_7).data);
|
|
(*value_7).data = NULL;
|
|
(*value_7).capacity = 0;
|
|
(*value_7).count = 0;
|
|
return value_11;
|
|
}
|
|
if (count == 0LL) {
|
|
break;
|
|
}
|
|
if (count > allowed) {
|
|
CloseFile(file);
|
|
free((bytes).data);
|
|
(bytes).data = NULL;
|
|
bytes.capacity = 0;
|
|
bytes.count = 0;
|
|
FileReadFailure* value_12 = &(result.error);
|
|
(*value_12) = FileReadFailure_TooLarge;
|
|
__type_d6f9b1b7c4ee2d51 value_13 = result;
|
|
free((*value_7).data);
|
|
(*value_7).data = NULL;
|
|
(*value_7).capacity = 0;
|
|
(*value_7).count = 0;
|
|
return value_13;
|
|
}
|
|
int64_t i = 0LL;
|
|
while (i < count) {
|
|
uint8_t value_14 = ZIRAN_INDEX(chunk, 4096, i);
|
|
bool value_15 = false;
|
|
void *value_16 = ZirVecGrow((void *)(bytes).data, &(bytes).capacity, (bytes).count, sizeof(*(bytes).data));
|
|
if (value_16 != NULL) {
|
|
(bytes).data = (uint8_t *)value_16;
|
|
(bytes).data[(bytes).count++] = value_14;
|
|
value_15 = true;
|
|
}
|
|
(void)value_15;
|
|
i = (int64_t)((uint64_t)i + UINT64_C(1));
|
|
}
|
|
offset = (int64_t)((uint64_t)offset + (uint64_t)count);
|
|
}
|
|
bool value_17 = CloseFile(file);
|
|
if (!value_17) {
|
|
free((bytes).data);
|
|
(bytes).data = NULL;
|
|
bytes.capacity = 0;
|
|
bytes.count = 0;
|
|
FileReadFailure* value_18 = &(result.error);
|
|
(*value_18) = FileReadFailure_ReadError;
|
|
__type_d6f9b1b7c4ee2d51 value_19 = result;
|
|
free((*value_7).data);
|
|
(*value_7).data = NULL;
|
|
(*value_7).capacity = 0;
|
|
(*value_7).count = 0;
|
|
return value_19;
|
|
}
|
|
bool* value_20 = &(result.is_ok);
|
|
(*value_20) = true;
|
|
String* value_21 = &(result.value);
|
|
String value_22 = {NULL, 0};
|
|
value_22.data = (bytes).count > 0 ? (const char *)(bytes).data : "";
|
|
value_22.length = (size_t)(bytes).count;
|
|
if ((bytes).count == 0) free((bytes).data);
|
|
(bytes).data = NULL;
|
|
bytes.capacity = 0;
|
|
bytes.count = 0;
|
|
(*value_21) = value_22;
|
|
__type_d6f9b1b7c4ee2d51 value_23 = result;
|
|
free((*value_7).data);
|
|
(*value_7).data = NULL;
|
|
(*value_7).capacity = 0;
|
|
(*value_7).count = 0;
|
|
return value_23;
|
|
}
|
|
|
|
__type_86107481102e856b
|
|
ReadFileIntoCapped(String path, int64_t maximum, __type_838711075b90279a* bytes)
|
|
{
|
|
__type_86107481102e856b result = {0};
|
|
if (bytes == ((void *)0)) {
|
|
FileReadFailure* value_0 = &(result.error);
|
|
(*value_0) = FileReadFailure_ReadError;
|
|
return result;
|
|
}
|
|
__type_d6f9b1b7c4ee2d51 read = ReadEntireFileCapped(path, maximum);
|
|
if (!read.is_ok) {
|
|
FileReadFailure* value_1 = &(result.error);
|
|
(*value_1) = (FileReadFailure)(read.error);
|
|
return result;
|
|
}
|
|
int64_t start = (*(bytes)).count;
|
|
{
|
|
String value_2 = read.value;
|
|
String loop_view_11 = StringRange(value_2, (int64_t)0, (int64_t)value_2.length);
|
|
int64_t loop_count_11 = (int64_t)(loop_view_11).length;
|
|
int64_t loop_cursor_11 = 0LL;
|
|
while (loop_cursor_11 < loop_count_11) {
|
|
int64_t loop_index_11 = loop_cursor_11;
|
|
uint8_t byte = (uint8_t)loop_view_11.data[loop_index_11];
|
|
uint8_t value_3 = byte;
|
|
bool value_4 = false;
|
|
void *value_5 = ZirVecGrow((void *)((*(bytes))).data, &((*(bytes))).capacity, ((*(bytes))).count, sizeof(*((*(bytes))).data));
|
|
if (value_5 != NULL) {
|
|
((*(bytes))).data = (uint8_t *)value_5;
|
|
((*(bytes))).data[((*(bytes))).count++] = value_3;
|
|
value_4 = true;
|
|
}
|
|
if (!value_4) {
|
|
int64_t value_7 = start;
|
|
bool value_8 = false;
|
|
if (value_7 >= 0) {
|
|
value_8 = value_7 <= ((*(bytes))).capacity;
|
|
if (!value_8) {
|
|
void *value_9 = ZirVecReserve((void *)((*(bytes))).data, &((*(bytes))).capacity, ((*(bytes))).count, value_7 - ((*(bytes))).count, sizeof(*((*(bytes))).data));
|
|
if (value_9 != NULL) { ((*(bytes))).data = (uint8_t *)value_9; value_8 = true; }
|
|
}
|
|
if (value_8) {
|
|
if (value_7 > ((*(bytes))).count) memset(((*(bytes))).data + ((*(bytes))).count, 0, (size_t)(value_7 - ((*(bytes))).count) * sizeof(*((*(bytes))).data));
|
|
((*(bytes))).count = value_7;
|
|
}
|
|
}
|
|
(void)value_8;
|
|
FileReadFailure* value_11 = &(result.error);
|
|
(*value_11) = FileReadFailure_ReadError;
|
|
return result;
|
|
}
|
|
loop_cursor_11 = (int64_t)((uint64_t)loop_cursor_11 + UINT64_C(1));
|
|
}
|
|
}
|
|
bool* value_12 = &(result.is_ok);
|
|
(*value_12) = true;
|
|
int64_t* value_13 = &(result.value);
|
|
(*value_13) = (int64_t)(read.value).length;
|
|
return result;
|
|
}
|
|
|
|
zir_970088bc989040e5_Results__string__bool
|
|
file_ReadEntireFile(String path)
|
|
{
|
|
FileHandle file = OpenRead(path);
|
|
if (!file.valid) {
|
|
return (zir_970088bc989040e5_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
|
|
}
|
|
__type_838711075b90279a bytes = {0};
|
|
__type_838711075b90279a* value_2 = &bytes;
|
|
uint8_t chunk[4096] = {0};
|
|
int64_t offset = 0LL;
|
|
while (true) {
|
|
Slice value_3 = {chunk, 4096};
|
|
int64_t count = ReadAt(file, value_3, offset);
|
|
if (count < 0LL) {
|
|
CloseFile(file);
|
|
free((bytes).data);
|
|
(bytes).data = NULL;
|
|
bytes.capacity = 0;
|
|
bytes.count = 0;
|
|
zir_970088bc989040e5_Results__string__bool value_4 = (zir_970088bc989040e5_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
|
|
free((*value_2).data);
|
|
(*value_2).data = NULL;
|
|
(*value_2).capacity = 0;
|
|
(*value_2).count = 0;
|
|
return value_4;
|
|
}
|
|
if (count == 0LL) {
|
|
break;
|
|
}
|
|
int64_t i = 0LL;
|
|
while (i < count) {
|
|
uint8_t value_5 = ZIRAN_INDEX(chunk, 4096, i);
|
|
bool value_6 = false;
|
|
void *value_7 = ZirVecGrow((void *)(bytes).data, &(bytes).capacity, (bytes).count, sizeof(*(bytes).data));
|
|
if (value_7 != NULL) {
|
|
(bytes).data = (uint8_t *)value_7;
|
|
(bytes).data[(bytes).count++] = value_5;
|
|
value_6 = true;
|
|
}
|
|
(void)value_6;
|
|
i = (int64_t)((uint64_t)i + UINT64_C(1));
|
|
}
|
|
offset = (int64_t)((uint64_t)offset + (uint64_t)count);
|
|
}
|
|
CloseFile(file);
|
|
zir_970088bc989040e5_Results__string__bool value_8 = {0};
|
|
String value_9 = {NULL, 0};
|
|
value_9.data = (bytes).count > 0 ? (const char *)(bytes).data : "";
|
|
value_9.length = (size_t)(bytes).count;
|
|
if ((bytes).count == 0) free((bytes).data);
|
|
(bytes).data = NULL;
|
|
bytes.capacity = 0;
|
|
bytes.count = 0;
|
|
value_8.value_0 = value_9;
|
|
value_8.value_1 = true;
|
|
zir_970088bc989040e5_Results__string__bool value_10 = value_8;
|
|
free((*value_2).data);
|
|
(*value_2).data = NULL;
|
|
(*value_2).capacity = 0;
|
|
(*value_2).count = 0;
|
|
return value_10;
|
|
}
|
|
|
|
bool
|
|
file_ReadFileInto(String path, __type_838711075b90279a* bytes)
|
|
{
|
|
FileHandle file = OpenRead(path);
|
|
if (!file.valid) {
|
|
return false;
|
|
}
|
|
uint8_t chunk[4096] = {0};
|
|
int64_t offset = 0LL;
|
|
while (true) {
|
|
Slice value_0 = {chunk, 4096};
|
|
int64_t count = ReadAt(file, value_0, offset);
|
|
if (count < 0LL) {
|
|
CloseFile(file);
|
|
return false;
|
|
}
|
|
if (count == 0LL) {
|
|
break;
|
|
}
|
|
int64_t i = 0LL;
|
|
while (i < count) {
|
|
uint8_t value_1 = ZIRAN_INDEX(chunk, 4096, i);
|
|
bool value_2 = false;
|
|
void *value_3 = ZirVecGrow((void *)((*(bytes))).data, &((*(bytes))).capacity, ((*(bytes))).count, sizeof(*((*(bytes))).data));
|
|
if (value_3 != NULL) {
|
|
((*(bytes))).data = (uint8_t *)value_3;
|
|
((*(bytes))).data[((*(bytes))).count++] = value_1;
|
|
value_2 = true;
|
|
}
|
|
(void)value_2;
|
|
i = (int64_t)((uint64_t)i + UINT64_C(1));
|
|
}
|
|
offset = (int64_t)((uint64_t)offset + (uint64_t)count);
|
|
}
|
|
CloseFile(file);
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
file_WriteEntireFile(String path, String text)
|
|
{
|
|
FileHandle file = OpenReplace(path);
|
|
if (!file.valid) {
|
|
return false;
|
|
}
|
|
uint8_t chunk[4096] = {0};
|
|
int64_t offset = 0LL;
|
|
while (offset < (int64_t)(text).length) {
|
|
int64_t count = 0LL;
|
|
while (count < 4096LL && (int64_t)((uint64_t)offset + (uint64_t)count) < (int64_t)(text).length) {
|
|
uint8_t* value_0 = &(chunk[count]);
|
|
(*value_0) = (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)offset + (uint64_t)count)));
|
|
count = (int64_t)((uint64_t)count + UINT64_C(1));
|
|
}
|
|
Slice value_1 = {chunk, 4096};
|
|
int64_t value_2 = WriteAt(file, SliceRange(value_1, (int64_t)0LL, (int64_t)count, sizeof(uint8_t)), offset);
|
|
if (value_2 != count) {
|
|
CloseFile(file);
|
|
return false;
|
|
}
|
|
offset = (int64_t)((uint64_t)offset + (uint64_t)count);
|
|
}
|
|
return CloseFile(file);
|
|
}
|