void Err_add_frame(Err *self, const char *file, int line, const char *func) { if (CB_Ends_With_Str(self->mess, "\n", 1)) { CB_Chop(self->mess, 1); } if (func != NULL) { CB_catf(self->mess, "\n\t%s at %s line %i32\n", func, file, (int32_t)line); } else { CB_catf(self->mess, "\n\tat %s line %i32\n", file, (int32_t)line); } }
static void S_vcat_mess(CharBuf *message, const char *file, int line, const char *func, const char *pattern, va_list args) { size_t guess_len = strlen(file) + func ? strlen(func) : 0 + strlen(pattern) + 30; CB_Grow(message, guess_len); CB_VCatF(message, pattern, args); if (func != NULL) CB_catf(message, " at %s:%i32 %s ", file, (i32_t)line, func); else CB_catf(message, " at %s:%i32", file, (i32_t)line); }
static void S_fill(InStream *self, int64_t amount) { InStreamIVARS *const ivars = InStream_IVARS(self); FileWindow *const window = ivars->window; const int64_t virtual_file_pos = SI_tell(self); const int64_t real_file_pos = virtual_file_pos + ivars->offset; const int64_t remaining = ivars->len - virtual_file_pos; // Throw an error if the requested amount would take us beyond EOF. if (amount > remaining) { THROW(ERR, "Read past EOF of %o (pos: %u64 len: %u64 request: %u64)", ivars->filename, virtual_file_pos, ivars->len, amount); } // Make the request. if (FH_Window(ivars->file_handle, window, real_file_pos, amount)) { char *fw_buf = FileWindow_Get_Buf(window); int64_t fw_offset = FileWindow_Get_Offset(window); int64_t fw_len = FileWindow_Get_Len(window); char *const window_limit = fw_buf + fw_len; ivars->buf = fw_buf - fw_offset // theoretical start of real file + ivars->offset // top of virtual file + virtual_file_pos; // position within virtual file ivars->limit = window_limit - ivars->buf > remaining ? ivars->buf + remaining : window_limit; } else { Err *error = Err_get_error(); CB_catf(Err_Get_Mess(error), " (%o)", ivars->filename); RETHROW(INCREF(error)); } }
void ErrMsg_set_with_errno(const char *fmt, ...) { int cur_errno = errno; CharBuf *buf = CB_new(0); va_list args; va_start(args, fmt); CB_VCatF(buf, fmt, args); va_end(args); CB_Cat_Trusted_Utf8(buf, ": ", 2); const char *msg = ErrMsg_strerror(cur_errno); if (msg != NULL) { CB_Cat_Trusted_Utf8(buf, msg, strlen(msg)); } else { CB_catf(buf, "Unknown error: %i32", (int32_t)cur_errno); } Err_set_error(Err_new(CB_Yield_String(buf))); DECREF(buf); }
static void S_fill(InStream *self, int64_t amount) { FileWindow *const window = self->window; const int64_t virtual_file_pos = SI_tell(self); const int64_t real_file_pos = virtual_file_pos + self->offset; const int64_t remaining = self->len - virtual_file_pos; // Throw an error if the requested amount would take us beyond EOF. if (amount > remaining) { THROW(ERR, "Read past EOF of %o (pos: %u64 len: %u64 request: %u64)", self->filename, virtual_file_pos, self->len, amount); } // Make the request. if (FH_Window(self->file_handle, window, real_file_pos, amount)) { char *const window_limit = window->buf + window->len; self->buf = window->buf - window->offset // theoretical start of real file + self->offset // top of virtual file + virtual_file_pos; // position within virtual file self->limit = window_limit - self->buf > remaining ? self->buf + remaining : window_limit; } else { Err *error = Err_get_error(); CB_catf(Err_Get_Mess(error), " (%o)", self->filename); RETHROW(INCREF(error)); } }
static void vcatf_s(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar bizzle baz"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %s baz", "bizzle"); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void vcatf_null_obj(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar [NULL] baz"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %o baz", NULL); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void test_vcatf_null_obj(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar [NULL] baz"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %o baz", NULL); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%o NULL"); DECREF(wanted); DECREF(got); }
static void vcatf_i8(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar -3 baz"); i8_t num = -3; CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %i8 baz", num); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void vcatf_i32(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar -100000 baz"); i32_t num = -100000; CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %i32 baz", num); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void vcatf_u64(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar 5000000000 baz"); u64_t num = U64_C(5000000000); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %u64 baz", num); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void test_vcatf_s(TestBatchRunner *runner) { String *wanted = S_get_str("foo bar bizzle baz"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %s baz", "bizzle"); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%s"); DECREF(wanted); DECREF(got); }
static void test_vcatf_s(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar bizzle baz"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %s baz", "bizzle"); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%s"); DECREF(wanted); DECREF(got); }
static void test_vcatf_null_obj(TestBatchRunner *runner) { String *wanted = S_get_str("foo bar [NULL] baz"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %o baz", NULL); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%o NULL"); DECREF(wanted); DECREF(got); }
static void test_vcatf_u8(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar 3 baz"); uint8_t num = 3; CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %u8 baz", num); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%u8"); DECREF(wanted); DECREF(got); }
static void test_vcatf_u32(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar 100000 baz"); uint32_t num = 100000; CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %u32 baz", num); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%u32"); DECREF(wanted); DECREF(got); }
static void test_vcatf_u32(TestBatchRunner *runner) { String *wanted = S_get_str("foo bar 100000 baz"); uint32_t num = 100000; CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %u32 baz", num); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%u32"); DECREF(wanted); DECREF(got); }
static void test_vcatf_u64(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar 5000000000 baz"); uint64_t num = U64_C(5000000000); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %u64 baz", num); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%u64"); DECREF(wanted); DECREF(got); }
static String* S_vmake_mess(const char *file, int line, const char *func, const char *pattern, va_list args) { size_t guess_len = strlen(file) + (func ? strlen(func) : 0) + strlen(pattern) + 30; CharBuf *buf = CB_new(guess_len); CB_VCatF(buf, pattern, args); if (func != NULL) { CB_catf(buf, "\n\t%s at %s line %i32\n", func, file, (int32_t)line); } else { CB_catf(buf, "\n\t%s line %i32\n", file, (int32_t)line); } String *message = CB_Yield_String(buf); DECREF(buf); return message; }
static void test_vcatf_i8(TestBatchRunner *runner) { String *wanted = S_get_str("foo bar -3 baz"); int8_t num = -3; CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %i8 baz", num); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%i8"); DECREF(wanted); DECREF(got); }
static void vcatf_obj(VArray *tests) { CharBuf *wanted = S_get_cb("ooga content:FOO booga"); LeafQuery *leaf_query = TestUtils_make_leaf_query("content", "FOO"); CharBuf *got = S_get_cb("ooga"); CB_catf(got, " %o booga", leaf_query); DECREF(leaf_query); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void test_vcatf_u64(TestBatchRunner *runner) { String *wanted = S_get_str("foo bar 5000000000 baz"); uint64_t num = UINT64_C(5000000000); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %u64 baz", num); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%u64"); DECREF(wanted); DECREF(got); }
static void vcatf_cb(VArray *tests) { CharBuf *wanted = S_get_cb("foo bar ZEKE baz"); CharBuf *catworthy = S_get_cb("ZEKE"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %o baz", catworthy); DECREF(catworthy); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }
static void test_vcatf_cb(TestBatch *batch) { CharBuf *wanted = S_get_cb("foo bar ZEKE baz"); CharBuf *catworthy = S_get_cb("ZEKE"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %o baz", catworthy); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%o CharBuf"); DECREF(catworthy); DECREF(wanted); DECREF(got); }
static void test_vcatf_obj(TestBatch *batch) { CharBuf *wanted = S_get_cb("ooga 20 booga"); Integer32 *i32 = Int32_new(20); CharBuf *got = S_get_cb("ooga"); CB_catf(got, " %o booga", i32); TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%o Obj"); DECREF(i32); DECREF(wanted); DECREF(got); }
static void test_vcatf_str(TestBatchRunner *runner) { String *wanted = S_get_str("foo bar ZEKE baz"); String *catworthy = S_get_str("ZEKE"); CharBuf *got = S_get_cb("foo "); CB_catf(got, "bar %o baz", catworthy); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%o CharBuf"); DECREF(catworthy); DECREF(wanted); DECREF(got); }
static void test_vcatf_obj(TestBatchRunner *runner) { String *wanted = S_get_str("ooga 20 booga"); Integer32 *i32 = Int32_new(20); CharBuf *got = S_get_cb("ooga"); CB_catf(got, " %o booga", i32); TEST_TRUE(runner, S_cb_equals(got, wanted), "%%o Obj"); DECREF(i32); DECREF(wanted); DECREF(got); }
void Err_Add_Frame_IMP(Err *self, const char *file, int line, const char *func) { CharBuf *buf = CB_new(0); CB_Cat(buf, self->mess); if (!Str_Ends_With_Utf8(self->mess, "\n", 1)) { CB_Cat_Char(buf, '\n'); } if (func != NULL) { CB_catf(buf, "\t%s at %s line %i32\n", func, file, (int32_t)line); } else { CB_catf(buf, "\tat %s line %i32\n", file, (int32_t)line); } DECREF(self->mess); self->mess = CB_Yield_String(buf); DECREF(buf); }
static String* S_encode_entities(String *text, CharBuf *buf) { StringIterator *iter = Str_Top(text); size_t space = 0; const int MAX_ENTITY_BYTES = 9; // &#dddddd; // Scan first so that we only allocate once. int32_t code_point; while (STRITER_DONE != (code_point = StrIter_Next(iter))) { if (code_point > 127 || (!isgraph(code_point) && !isspace(code_point)) || code_point == '<' || code_point == '>' || code_point == '&' || code_point == '"' ) { space += MAX_ENTITY_BYTES; } else { space += 1; } } CB_Grow(buf, space); CB_Set_Size(buf, 0); DECREF(iter); iter = Str_Top(text); while (STRITER_DONE != (code_point = StrIter_Next(iter))) { if (code_point > 127 || (!isgraph(code_point) && !isspace(code_point)) ) { CB_catf(buf, "&#%u32;", code_point); } else if (code_point == '<') { CB_Cat_Trusted_Utf8(buf, "<", 4); } else if (code_point == '>') { CB_Cat_Trusted_Utf8(buf, ">", 4); } else if (code_point == '&') { CB_Cat_Trusted_Utf8(buf, "&", 5); } else if (code_point == '"') { CB_Cat_Trusted_Utf8(buf, """, 6); } else { CB_Cat_Char(buf, code_point); } } DECREF(iter); return CB_To_String(buf); }
static void vcatf_f64(VArray *tests) { CharBuf *wanted; char buf[64]; float num = 1.3f; CharBuf *got = S_get_cb("foo "); sprintf(buf, "foo bar %g baz", num); wanted = CB_new_from_trusted_utf8(buf, strlen(buf)); CB_catf(got, "bar %f64 baz", num); VA_Push(tests, (Obj*)TestCB_new(wanted, got)); }