本文整理匯總了C++中Err_set_error函數的典型用法代碼示例。如果您正苦於以下問題:C++ Err_set_error函數的具體用法?C++ Err_set_error怎麽用?C++ Err_set_error使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Err_set_error函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: test_Open_FileHandle
static void
test_Open_FileHandle(TestBatch *batch)
{
Folder *folder = (Folder*)RAMFolder_new(NULL);
FileHandle *fh;
Folder_MkDir(folder, &foo);
fh = Folder_Open_FileHandle(folder, &boffo, FH_CREATE | FH_WRITE_ONLY);
TEST_TRUE(batch, fh && FH_Is_A(fh, FILEHANDLE), "Open_FileHandle");
DECREF(fh);
fh = Folder_Open_FileHandle(folder, &foo_boffo,
FH_CREATE | FH_WRITE_ONLY);
TEST_TRUE(batch, fh && FH_Is_A(fh, FILEHANDLE),
"Open_FileHandle for nested file");
DECREF(fh);
Err_set_error(NULL);
fh = Folder_Open_FileHandle(folder, &foo, FH_CREATE | FH_WRITE_ONLY);
TEST_TRUE(batch, fh == NULL,
"Open_FileHandle on existing dir path fails");
TEST_TRUE(batch, Err_get_error() != NULL,
"Open_FileHandle on existing dir name sets Err_error");
Err_set_error(NULL);
fh = Folder_Open_FileHandle(folder, &foo_bar_baz_boffo,
FH_CREATE | FH_WRITE_ONLY);
TEST_TRUE(batch, fh == NULL,
"Open_FileHandle for entry within non-existent dir fails");
TEST_TRUE(batch, Err_get_error() != NULL,
"Open_FileHandle for entry within non-existent dir sets Err_error");
DECREF(folder);
}
示例2: test_Local_MkDir
static void
test_Local_MkDir(TestBatchRunner *runner) {
RAMFolder *folder = RAMFolder_new(NULL);
bool result;
result = RAMFolder_Local_MkDir(folder, foo);
TEST_TRUE(runner, result, "Local_MkDir succeeds and returns true");
Err_set_error(NULL);
result = RAMFolder_Local_MkDir(folder, foo);
TEST_FALSE(runner, result,
"Local_MkDir returns false when a dir already exists");
TEST_TRUE(runner, Err_get_error() != NULL,
"Local_MkDir sets Err_error when a dir already exists");
TEST_TRUE(runner, RAMFolder_Exists(folder, foo),
"Existing dir untouched after failed Local_MkDir");
FileHandle *fh = RAMFolder_Open_FileHandle(folder, boffo,
FH_CREATE | FH_WRITE_ONLY);
DECREF(fh);
Err_set_error(NULL);
result = RAMFolder_Local_MkDir(folder, foo);
TEST_FALSE(runner, result,
"Local_MkDir returns false when a file already exists");
TEST_TRUE(runner, Err_get_error() != NULL,
"Local_MkDir sets Err_error when a file already exists");
TEST_TRUE(runner, RAMFolder_Exists(folder, boffo) &&
!RAMFolder_Local_Is_Directory(folder, boffo),
"Existing file untouched after failed Local_MkDir");
DECREF(folder);
}
示例3: test_Open_Dir
static void
test_Open_Dir(TestBatch *batch)
{
Folder *folder = (Folder*)RAMFolder_new(NULL);
DirHandle *dh;
Folder_MkDir(folder, &foo);
Folder_MkDir(folder, &foo_bar);
dh = Folder_Open_Dir(folder, &foo);
TEST_TRUE(batch, dh && DH_Is_A(dh, DIRHANDLE), "Open_Dir");
DECREF(dh);
dh = Folder_Open_Dir(folder, &foo_bar);
TEST_TRUE(batch, dh && DH_Is_A(dh, DIRHANDLE), "Open_Dir nested dir");
DECREF(dh);
Err_set_error(NULL);
dh = Folder_Open_Dir(folder, &bar);
TEST_TRUE(batch, dh == NULL,
"Open_Dir on non-existent entry fails");
TEST_TRUE(batch, Err_get_error() != NULL,
"Open_Dir on non-existent entry sets Err_error");
Err_set_error(NULL);
dh = Folder_Open_Dir(folder, &foo_foo);
TEST_TRUE(batch, dh == NULL,
"Open_Dir on non-existent nested entry fails");
TEST_TRUE(batch, Err_get_error() != NULL,
"Open_Dir on non-existent nested entry sets Err_error");
DECREF(folder);
}
示例4: FSFH_read
bool_t
FSFH_read(FSFileHandle *self, char *dest, int64_t offset, size_t len) {
int64_t check_val;
// Sanity check.
if (offset < 0) {
Err_set_error(Err_new(CB_newf("Can't read from an offset less than 0 (%i64)",
offset)));
return false;
}
// Read.
check_val = pread64(self->fd, dest, len, offset);
if (check_val != (int64_t)len) {
if (check_val == -1) {
Err_set_error(Err_new(CB_newf("Tried to read %u64 bytes, got %i64: %s",
(uint64_t)len, check_val, strerror(errno))));
}
else {
Err_set_error(Err_new(CB_newf("Tried to read %u64 bytes, got %i64",
(uint64_t)len, check_val)));
}
return false;
}
return true;
}
示例5: SI_close_win_handles
static INLINE bool_t
SI_close_win_handles(FSFileHandle *self) {
// Close both standard handle and mapping handle.
if (self->win_maphandle) {
if (!CloseHandle(self->win_maphandle)) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf("Failed to close file mapping handle: %s",
win_error)));
FREEMEM(win_error);
return false;
}
self->win_maphandle = NULL;
}
if (self->win_fhandle) {
if (!CloseHandle(self->win_fhandle)) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf("Failed to close file handle: %s",
win_error)));
FREEMEM(win_error);
return false;
}
self->win_fhandle = NULL;
}
return true;
}
示例6: test_Read_Write
static void
test_Read_Write(TestBatchRunner *runner) {
FSFileHandle *fh;
const char *foo = "foo";
const char *bar = "bar";
char buffer[12];
char *buf = buffer;
String *test_filename = SSTR_WRAP_C("_fstest");
S_remove(test_filename);
fh = FSFH_open(test_filename,
FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE);
TEST_TRUE(runner, FSFH_Length(fh) == INT64_C(0), "Length initially 0");
TEST_TRUE(runner, FSFH_Write(fh, foo, 3), "Write returns success");
TEST_TRUE(runner, FSFH_Length(fh) == INT64_C(3), "Length after Write");
TEST_TRUE(runner, FSFH_Write(fh, bar, 3), "Write returns success");
TEST_TRUE(runner, FSFH_Length(fh) == INT64_C(6), "Length after 2 Writes");
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Read(fh, buf, 0, 2),
"Reading from a write-only handle returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Reading from a write-only handle sets error");
if (!FSFH_Close(fh)) { RETHROW(INCREF(Err_get_error())); }
DECREF(fh);
// Reopen for reading.
Err_set_error(NULL);
fh = FSFH_open(test_filename, FH_READ_ONLY);
TEST_TRUE(runner, FSFH_Length(fh) == INT64_C(6), "Length on Read");
TEST_TRUE(runner, FSFH_Read(fh, buf, 0, 6), "Read returns success");
TEST_TRUE(runner, strncmp(buf, "foobar", 6) == 0, "Read/Write");
TEST_TRUE(runner, FSFH_Read(fh, buf, 2, 3), "Read returns success");
TEST_TRUE(runner, strncmp(buf, "oba", 3) == 0, "Read with offset");
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Read(fh, buf, -1, 4),
"Read() with a negative offset returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Read() with a negative offset sets error");
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Read(fh, buf, 6, 1),
"Read() past EOF returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Read() past EOF sets error");
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Write(fh, foo, 3),
"Writing to a read-only handle returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Writing to a read-only handle sets error");
DECREF(fh);
S_remove(test_filename);
}
示例7: SI_init_read_only
static INLINE bool_t
SI_init_read_only(FSFileHandle *self)
{
LARGE_INTEGER large_int;
char *filepath = (char*)CB_Get_Ptr8(self->path);
SYSTEM_INFO sys_info;
// Get system page size.
GetSystemInfo(&sys_info);
self->page_size = sys_info.dwAllocationGranularity;
// Open.
self->win_fhandle = CreateFile(
filepath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY | FILE_FLAG_OVERLAPPED,
NULL
);
if (self->win_fhandle == INVALID_HANDLE_VALUE) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf("CreateFile for %o failed: %s",
self->path, win_error)));
FREEMEM(win_error);
return false;
}
// Derive len.
GetFileSizeEx(self->win_fhandle, &large_int);
self->len = large_int.QuadPart;
if (self->len < 0) {
Err_set_error(Err_new(CB_newf(
"GetFileSizeEx for %o returned a negative length: '%i64'",
self->path, self->len)));
return false;
}
// Init mapping handle.
self->buf = NULL;
if (self->len) {
self->win_maphandle = CreateFileMapping(self->win_fhandle, NULL,
PAGE_READONLY, 0, 0, NULL);
if (self->win_maphandle == NULL) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf(
"CreateFileMapping for %o failed: %s",
self->path, win_error)));
FREEMEM(win_error);
return false;
}
}
return true;
}
示例8: SI_init_read_only
static INLINE bool
SI_init_read_only(FSFileHandle *self, FSFileHandleIVARS *ivars) {
char *filepath = (char*)CB_Get_Ptr8(ivars->path);
SYSTEM_INFO sys_info;
// Get system page size.
GetSystemInfo(&sys_info);
ivars->page_size = sys_info.dwAllocationGranularity;
// Open.
ivars->win_fhandle = CreateFile(
filepath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY | FILE_FLAG_OVERLAPPED,
NULL
);
if (ivars->win_fhandle == INVALID_HANDLE_VALUE) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf("CreateFile for %o failed: %s",
ivars->path, win_error)));
FREEMEM(win_error);
return false;
}
// Derive len.
DWORD file_size_hi;
DWORD file_size_lo = GetFileSize(ivars->win_fhandle, &file_size_hi);
if (file_size_lo == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
Err_set_error(Err_new(CB_newf("GetFileSize for %o failed",
ivars->path)));
return false;
}
ivars->len = ((uint64_t)file_size_hi << 32) | file_size_lo;
// Init mapping handle.
ivars->buf = NULL;
if (ivars->len) {
ivars->win_maphandle = CreateFileMapping(ivars->win_fhandle, NULL,
PAGE_READONLY, 0, 0, NULL);
if (ivars->win_maphandle == NULL) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf("CreateFileMapping for %o failed: %s",
ivars->path, win_error)));
FREEMEM(win_error);
return false;
}
}
return true;
}
示例9: test_Window
static void
test_Window(TestBatchRunner *runner) {
String *test_filename = SSTR_WRAP_C("_fstest");
FSFileHandle *fh;
FileWindow *window = FileWindow_new();
uint32_t i;
S_remove(test_filename);
fh = FSFH_open(test_filename,
FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE);
for (i = 0; i < 1024; i++) {
FSFH_Write(fh, "foo ", 4);
}
if (!FSFH_Close(fh)) { RETHROW(INCREF(Err_get_error())); }
// Reopen for reading.
DECREF(fh);
fh = FSFH_open(test_filename, FH_READ_ONLY);
if (!fh) { RETHROW(INCREF(Err_get_error())); }
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Window(fh, window, -1, 4),
"Window() with a negative offset returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Window() with a negative offset sets error");
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Window(fh, window, 4000, 1000),
"Window() past EOF returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Window() past EOF sets error");
TEST_TRUE(runner, FSFH_Window(fh, window, 1021, 2),
"Window() returns true");
const char *buf = FileWindow_Get_Buf(window);
int64_t offset = FileWindow_Get_Offset(window);
TEST_TRUE(runner,
strncmp(buf - offset + 1021, "oo", 2) == 0,
"Window()");
TEST_TRUE(runner, FSFH_Release_Window(fh, window),
"Release_Window() returns true");
TEST_TRUE(runner, FileWindow_Get_Buf(window) == NULL,
"Release_Window() resets buf");
TEST_INT_EQ(runner, FileWindow_Get_Offset(window), 0,
"Release_Window() resets offset");
TEST_INT_EQ(runner, FileWindow_Get_Len(window), 0,
"Release_Window() resets len");
DECREF(window);
DECREF(fh);
S_remove(test_filename);
}
示例10: test_Window
static void
test_Window(TestBatchRunner *runner) {
String *test_filename = (String*)SSTR_WRAP_UTF8("_fstest", 7);
FSFileHandle *fh;
FileWindow *window = FileWindow_new();
FileWindowIVARS *const window_ivars = FileWindow_IVARS(window);
uint32_t i;
remove(Str_Get_Ptr8(test_filename));
fh = FSFH_open(test_filename,
FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE);
for (i = 0; i < 1024; i++) {
FSFH_Write(fh, "foo ", 4);
}
if (!FSFH_Close(fh)) { RETHROW(INCREF(Err_get_error())); }
// Reopen for reading.
DECREF(fh);
fh = FSFH_open(test_filename, FH_READ_ONLY);
if (!fh) { RETHROW(INCREF(Err_get_error())); }
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Window(fh, window, -1, 4),
"Window() with a negative offset returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Window() with a negative offset sets error");
Err_set_error(NULL);
TEST_FALSE(runner, FSFH_Window(fh, window, 4000, 1000),
"Window() past EOF returns false");
TEST_TRUE(runner, Err_get_error() != NULL,
"Window() past EOF sets error");
TEST_TRUE(runner, FSFH_Window(fh, window, 1021, 2),
"Window() returns true");
TEST_TRUE(runner,
strncmp(window_ivars->buf - window_ivars->offset + 1021, "oo", 2) == 0,
"Window()");
TEST_TRUE(runner, FSFH_Release_Window(fh, window),
"Release_Window() returns true");
TEST_TRUE(runner, window_ivars->buf == NULL, "Release_Window() resets buf");
TEST_TRUE(runner, window_ivars->offset == 0, "Release_Window() resets offset");
TEST_TRUE(runner, window_ivars->len == 0, "Release_Window() resets len");
DECREF(window);
DECREF(fh);
remove(Str_Get_Ptr8(test_filename));
}
示例11: FSFH_read
bool_t
FSFH_read(FSFileHandle *self, char *dest, int64_t offset, size_t len)
{
BOOL check_val;
DWORD got;
OVERLAPPED read_op_state;
uint64_t offs = (uint64_t)offset;
read_op_state.hEvent = NULL;
read_op_state.OffsetHigh = offs >> 32;
read_op_state.Offset = offs & 0xFFFFFFFF;
// Sanity check.
if (offset < 0) {
Err_set_error(Err_new(CB_newf(
"Can't read from an offset less than 0 (%i64)", offset)));
return false;
}
// ReadFile() takes a DWORD (unsigned 32-bit integer) as a length
// argument, so throw a sensible error rather than wrap around.
if (len > U32_MAX) {
Err_set_error(Err_new(CB_newf(
"Can't read more than 4 GB (%u64)", (uint64_t)len)));
return false;
}
// Read.
check_val = ReadFile(self->win_fhandle, dest, len, &got, &read_op_state);
if (!check_val && GetLastError() == ERROR_IO_PENDING) {
// Read has been queued by the OS and will soon complete. Wait for
// it, since this is a blocking IO call from the point of the rest of
// the library.
check_val = GetOverlappedResult(self->win_fhandle, &read_op_state,
&got, TRUE);
}
// Verify that the read has succeeded by now.
if (!check_val) {
char *win_error = Err_win_error();
Err_set_error(Err_new(CB_newf("Failed to read %u64 bytes: %s",
(uint64_t)len, win_error)));
FREEMEM(win_error);
return false;
}
return true;
}
示例12: test_spew_and_slurp
static void
test_spew_and_slurp(TestBatch *batch) {
Obj *dump = S_make_dump();
Folder *folder = (Folder*)RAMFolder_new(NULL);
CharBuf *foo = (CharBuf*)ZCB_WRAP_STR("foo", 3);
bool_t result = Json_spew_json(dump, folder, foo);
TEST_TRUE(batch, result, "spew_json returns true on success");
TEST_TRUE(batch, Folder_Exists(folder, foo),
"spew_json wrote file");
Obj *got = Json_slurp_json(folder, foo);
TEST_TRUE(batch, got && Obj_Equals(dump, got),
"Round trip through spew_json and slurp_json");
DECREF(got);
Err_set_error(NULL);
result = Json_spew_json(dump, folder, foo);
TEST_FALSE(batch, result, "Can't spew_json when file exists");
TEST_TRUE(batch, Err_get_error() != NULL,
"Failed spew_json sets Err_error");
Err_set_error(NULL);
CharBuf *bar = (CharBuf*)ZCB_WRAP_STR("bar", 3);
got = Json_slurp_json(folder, bar);
TEST_TRUE(batch, got == NULL,
"slurp_json returns NULL when file doesn't exist");
TEST_TRUE(batch, Err_get_error() != NULL,
"Failed slurp_json sets Err_error");
CharBuf *boffo = (CharBuf*)ZCB_WRAP_STR("boffo", 5);
FileHandle *fh
= Folder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY);
FH_Write(fh, "garbage", 7);
DECREF(fh);
Err_set_error(NULL);
got = Json_slurp_json(folder, boffo);
TEST_TRUE(batch, got == NULL,
"slurp_json returns NULL when file doesn't contain valid JSON");
TEST_TRUE(batch, Err_get_error() != NULL,
"Failed slurp_json sets Err_error");
DECREF(got);
DECREF(dump);
DECREF(folder);
}
示例13: ErrMsg_set_with_errno
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);
}
示例14: Json_to_json
CharBuf*
Json_to_json(Obj *dump) {
// Validate object type, only allowing hashes and arrays per JSON spec.
if (!dump || !(Obj_Is_A(dump, HASH) || Obj_Is_A(dump, VARRAY))) {
if (!tolerant) {
CharBuf *class_name = dump ? Obj_Get_Class_Name(dump) : NULL;
CharBuf *mess = MAKE_MESS("Illegal top-level object type: %o",
class_name);
Err_set_error(Err_new(mess));
return NULL;
}
}
// Encode.
CharBuf *json = CB_new(31);
if (!S_to_json(dump, json, 0)) {
DECREF(json);
ERR_ADD_FRAME(Err_get_error());
json = NULL;
}
else {
// Append newline.
CB_Cat_Trusted_Str(json, "\n", 1);
}
return json;
}
示例15: CFReader_local_open_in
InStream*
CFReader_local_open_in(CompoundFileReader *self, const CharBuf *name) {
Hash *entry = (Hash*)Hash_Fetch(self->records, (Obj*)name);
if (!entry) {
InStream *instream = Folder_Local_Open_In(self->real_folder, name);
if (!instream) {
ERR_ADD_FRAME(Err_get_error());
}
return instream;
}
else {
Obj *len = Hash_Fetch_Str(entry, "length", 6);
Obj *offset = Hash_Fetch_Str(entry, "offset", 6);
if (!len || !offset) {
Err_set_error(Err_new(CB_newf("Malformed entry for '%o' in '%o'",
name, Folder_Get_Path(self->real_folder))));
return NULL;
}
else if (CB_Get_Size(self->path)) {
CharBuf *fullpath = CB_newf("%o/%o", self->path, name);
InStream *instream = InStream_Reopen(self->instream, fullpath,
Obj_To_I64(offset), Obj_To_I64(len));
DECREF(fullpath);
return instream;
}
else {
return InStream_Reopen(self->instream, name, Obj_To_I64(offset),
Obj_To_I64(len));
}
}
}