better error handling: lots of little changes.
added some error codes, tried to eliminate unspecific return -1 where it made sense. This was SVN commit r1436.
This commit is contained in:
parent
6c2f455547
commit
f1dac9c626
@ -119,16 +119,53 @@ STMT(\
|
||||
|
||||
enum LibError
|
||||
{
|
||||
ERR_INVALID_HANDLE = -1000,
|
||||
ERR_NO_MEM = -1001,
|
||||
ERR_EOF = -1002, // attempted to read beyond EOF
|
||||
ERR_INVALID_PARAM = -1003,
|
||||
ERR_FILE_NOT_FOUND = -1004,
|
||||
ERR_PATH_NOT_FOUND = -1005,
|
||||
ERR_PATH_LENGTH = -1006,
|
||||
ERR_AGAIN = -1007, // try again later
|
||||
//
|
||||
// lib + res
|
||||
//
|
||||
|
||||
ERR_INVALID_PARAM = -1000,
|
||||
ERR_INVALID_HANDLE = -1001,
|
||||
ERR_NO_MEM = -1002,
|
||||
|
||||
// try again later
|
||||
ERR_AGAIN = -1003,
|
||||
|
||||
// fixed limit exceeded
|
||||
ERR_LIMIT = -1004,
|
||||
|
||||
// system doesn't support required API(s)
|
||||
ERR_NO_SYS = -1005,
|
||||
|
||||
// feature not currently implemented (will probably change)
|
||||
ERR_NOT_IMPLEMENTED = -1006,
|
||||
|
||||
// feature won't be supported
|
||||
ERR_NOT_SUPPORTED = -1007,
|
||||
|
||||
// file contents are damaged
|
||||
ERR_CORRUPTED = -1008,
|
||||
|
||||
ERR_UNKNOWN_FORMAT = -1009,
|
||||
|
||||
//
|
||||
// file + vfs
|
||||
//
|
||||
|
||||
ERR_FILE_NOT_FOUND = -1100,
|
||||
ERR_PATH_NOT_FOUND = -1101,
|
||||
ERR_DIR_END = -1102,
|
||||
|
||||
// started
|
||||
ERR_EOF = -1103,
|
||||
|
||||
ERR_PATH_LENGTH = -1104,
|
||||
ERR_NOT_FILE = -1105,
|
||||
ERR_FILE_ACCESS = -1106,
|
||||
ERR_IO = -1107,
|
||||
|
||||
|
||||
ERR_TEX_FMT_INVALID = -1200,
|
||||
|
||||
ERR_VFS_DIR_END = -1008,
|
||||
|
||||
ERR_LAST
|
||||
};
|
||||
|
@ -119,12 +119,7 @@ static int Cursor_reload(Cursor* c, const char* name, Handle)
|
||||
sprintf(filename, "art/textures/cursors/%s.png", name);
|
||||
|
||||
Handle tex = tex_load(filename);
|
||||
if (tex <= 0)
|
||||
{
|
||||
// TODO: Handle errors
|
||||
assert(! "Error loading cursor texture");
|
||||
return -1;
|
||||
}
|
||||
CHECK_ERR(tex);
|
||||
|
||||
#ifdef _WIN32
|
||||
if (c->type == 1)
|
||||
@ -219,12 +214,7 @@ static int Cursor_reload(Cursor* c, const char* name, Handle)
|
||||
{
|
||||
|
||||
int err = tex_upload(tex);
|
||||
if (err < 0)
|
||||
{
|
||||
// TODO: Handle errors
|
||||
assert(! "Error uploading cursor texture");
|
||||
return -1;
|
||||
}
|
||||
CHECK_ERR(err);
|
||||
|
||||
c->cursor = new struct ogl_cursor;
|
||||
|
||||
|
@ -284,7 +284,7 @@ int file_enum(const char* const dir, const FileCB cb, const uintptr_t user)
|
||||
|
||||
DIR* const os_dir = opendir(n_path);
|
||||
if(!os_dir)
|
||||
return -1;
|
||||
return ERR_PATH_NOT_FOUND;
|
||||
|
||||
// will append file names here
|
||||
const size_t n_path_len = strlen(n_path);
|
||||
@ -473,9 +473,9 @@ int file_open(const char* const p_fn, const uint flags, File* const f)
|
||||
{
|
||||
struct stat s;
|
||||
if(stat(n_fn, &s) < 0)
|
||||
return -1;
|
||||
return ERR_FILE_NOT_FOUND;
|
||||
if(!S_ISREG(s.st_mode))
|
||||
return -1;
|
||||
return ERR_NOT_FILE;
|
||||
size = s.st_size;
|
||||
}
|
||||
|
||||
@ -485,7 +485,7 @@ int file_open(const char* const p_fn, const uint flags, File* const f)
|
||||
|
||||
int fd = open(n_fn, oflag, S_IRWXO|S_IRWXU|S_IRWXG);
|
||||
if(fd < 0)
|
||||
return -1;
|
||||
return ERR_FILE_ACCESS;
|
||||
|
||||
#ifdef PARANOIA
|
||||
f->magic = FILE_MAGIC;
|
||||
@ -674,8 +674,9 @@ debug_out("file_wait_io: hio=%p\n", io);
|
||||
debug_out("file_wait_io: bytes_transferred=%d aio_nbytes=%d\n",
|
||||
bytes_transferred, cb->aio_nbytes);
|
||||
#endif
|
||||
// (size was clipped to EOF in file_io => this is an actual IO error)
|
||||
if(bytes_transferred < (ssize_t)cb->aio_nbytes)
|
||||
return -1;
|
||||
return ERR_IO;
|
||||
|
||||
p = (void*)cb->aio_buf; // cast from volatile void*
|
||||
size = bytes_transferred;
|
||||
@ -881,7 +882,7 @@ debug_out("file_io fd=%d size=%d ofs=%d\n", f->fd, data_size, data_ofs);
|
||||
// cut data_size off at EOF
|
||||
const ssize_t bytes_left = f->size - data_ofs;
|
||||
if(bytes_left < 0)
|
||||
return -1;
|
||||
return ERR_EOF;
|
||||
data_size = MIN(data_size, (size_t)bytes_left);
|
||||
}
|
||||
|
||||
|
@ -280,7 +280,7 @@ static int alloc_idx(i32& idx, HDATA*& hd)
|
||||
if(last_in_use >= hdata_cap)
|
||||
{
|
||||
assert(!"alloc_idx: too many open handles (increase IDX_BITS)");
|
||||
return -1;
|
||||
return ERR_LIMIT;
|
||||
}
|
||||
idx = last_in_use+1; // just incrementing idx would start it at 1
|
||||
hd = h_data_from_idx(idx);
|
||||
|
@ -76,7 +76,7 @@ size_t read_func(void* ptr, size_t elements, size_t el_size, void* datasource)
|
||||
|
||||
int seek_func(void* datasource, ogg_int64_t offset, int whence)
|
||||
{
|
||||
return -1;
|
||||
return -1; // libvorbisfile: indicate "not implemented"
|
||||
}
|
||||
|
||||
int close_func(void* datasource)
|
||||
|
@ -39,18 +39,22 @@ static int get_gl_fmt(TexInfo* ti, GLenum* fmt)
|
||||
*fmt = GL_LUMINANCE_ALPHA;
|
||||
return 0;
|
||||
case 24:
|
||||
if(alpha)
|
||||
return -1;
|
||||
*fmt = bgr? GL_BGR : GL_RGB;
|
||||
return 0;
|
||||
case 32:
|
||||
if(!alpha)
|
||||
return -1;
|
||||
*fmt = bgr? GL_BGRA : GL_RGBA;
|
||||
return 0;
|
||||
{
|
||||
*fmt = bgr? GL_BGR : GL_RGB;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case 32:
|
||||
if(alpha)
|
||||
{
|
||||
*fmt = bgr? GL_BGRA : GL_RGBA;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return -1;
|
||||
return ERR_TEX_FMT_INVALID;
|
||||
}
|
||||
|
||||
|
||||
|
@ -530,7 +530,7 @@ static const char* devs;
|
||||
int snd_dev_prepare_enum()
|
||||
{
|
||||
if(alcIsExtensionPresent(0, (ALubyte*)"ALC_ENUMERATION_EXT") != AL_TRUE)
|
||||
return -1;
|
||||
return ERR_NO_SYS;
|
||||
|
||||
devs = (const char*)alcGetString(0, ALC_DEVICE_SPECIFIER);
|
||||
return 0;
|
||||
@ -921,7 +921,7 @@ static int SndData_reload(SndData* sd, const char* fn, Handle hsd)
|
||||
if(ogg_supported == -1)
|
||||
ogg_supported = alIsExtensionPresent((ALubyte*)"AL_EXT_vorbis")? 1 : 0;
|
||||
if(!ogg_supported)
|
||||
return -1;
|
||||
return ERR_NO_SYS
|
||||
|
||||
sd->al_fmt = AL_FORMAT_VORBIS_EXT;
|
||||
sd->al_freq = 0;
|
||||
@ -934,7 +934,7 @@ static int SndData_reload(SndData* sd, const char* fn, Handle hsd)
|
||||
file_type = FT_WAV;
|
||||
// .. unknown extension
|
||||
else
|
||||
return -1;
|
||||
return ERR_UNKNOWN_FORMAT;
|
||||
|
||||
|
||||
if(sd->is_stream)
|
||||
@ -1447,8 +1447,7 @@ static int VSrc_reload(VSrc* vs, const char* fn, Handle hvs)
|
||||
|
||||
bool is_stream = (vs->flags & VS_IS_STREAM) != 0;
|
||||
vs->hsd = snd_data_load(snd_fn, is_stream);
|
||||
if(vs->hsd <= 0)
|
||||
return -1;
|
||||
CHECK_ERR(vs->hsd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1497,9 +1496,8 @@ int snd_play(Handle hs, float static_pri)
|
||||
{
|
||||
H_DEREF(hs, VSrc, vs);
|
||||
|
||||
// sound data wasn't loaded successfully - bail
|
||||
if(vs->hsd <= 0)
|
||||
return -1;
|
||||
// note: vs->hsd is valid, otherwise snd_open would have failed
|
||||
// and returned an invalid handle (caught above).
|
||||
|
||||
vs->static_pri = static_pri;
|
||||
list_add(vs);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
$Id: unifont.cpp,v 1.20 2004/11/24 23:47:48 gee Exp $
|
||||
$Id$
|
||||
|
||||
Unicode OpenGL texture font
|
||||
|
||||
@ -76,8 +76,8 @@ static int UniFont_reload(UniFont* f, const char* fn, Handle UNUSEDPARAM(h))
|
||||
const char* fnt_fn = FilenameFnt.c_str();
|
||||
|
||||
// Fail quietly if the file simply doesn't exist
|
||||
if (! vfs_exists(fnt_fn))
|
||||
return -1;
|
||||
// // if (! vfs_exists(fnt_fn))
|
||||
// // return ERR_FILE_NOT_FOUND;
|
||||
|
||||
Handle fh = vfs_load(fnt_fn, RawFNT, FNTSize);
|
||||
CHECK_ERR(fh);
|
||||
|
@ -1031,7 +1031,7 @@ int vfs_close_dir(Handle& hd)
|
||||
|
||||
|
||||
// retrieve the next dir entry (in alphabetical order) matching <filter>.
|
||||
// return 0 on success, ERR_VFS_DIR_END if no matching entry was found,
|
||||
// return 0 on success, ERR_DIR_END if no matching entry was found,
|
||||
// or a negative error code on failure.
|
||||
// filter values:
|
||||
// - 0: any file;
|
||||
@ -1053,7 +1053,7 @@ int vfs_next_dirent(const Handle hd, vfsDirEnt* ent, const char* const filter)
|
||||
if(filter && filter[0] == '/' && filter[1] == '\0')
|
||||
{
|
||||
if(vd->subdir_it == vd->subdirs->end())
|
||||
return ERR_VFS_DIR_END;
|
||||
return ERR_DIR_END;
|
||||
ent->name = vd->subdir_it->first.c_str();
|
||||
++vd->subdir_it;
|
||||
return 0;
|
||||
@ -1063,7 +1063,7 @@ int vfs_next_dirent(const Handle hd, vfsDirEnt* ent, const char* const filter)
|
||||
for(;;)
|
||||
{
|
||||
if(vd->file_it == vd->files->end())
|
||||
return ERR_VFS_DIR_END;
|
||||
return ERR_DIR_END;
|
||||
ent->name = vd->file_it->first.c_str();
|
||||
++vd->file_it;
|
||||
|
||||
|
@ -87,7 +87,7 @@ extern Handle vfs_open_dir(const char* dir);
|
||||
extern int vfs_close_dir(Handle& hd);
|
||||
|
||||
// retrieve the next dir entry (in alphabetical order) matching <filter>.
|
||||
// return 0 on success, ERR_VFS_DIR_END if no matching entry was found,
|
||||
// return 0 on success, ERR_DIR_END if no matching entry was found,
|
||||
// or a negative error code on failure.
|
||||
// filter values:
|
||||
// - 0: any file;
|
||||
|
@ -107,7 +107,7 @@ static inline int z_validate(const void* const file, const size_t size)
|
||||
// make sure it's big enough to check the header and for
|
||||
// z_find_ecdr to succeed (if smaller, it's definitely bogus).
|
||||
if(size < ECDR_SIZE)
|
||||
return -1;
|
||||
return ERR_CORRUPTED;
|
||||
|
||||
// check "header" (first LFH) signature
|
||||
return (*(u32*)file == *(u32*)&lfh_id)? 0 : -1;
|
||||
@ -144,7 +144,7 @@ static int z_find_ecdr(const void* const file, const size_t size, const u8*& ecd
|
||||
}
|
||||
|
||||
// reached EOF and still haven't found the ECDR identifier.
|
||||
return -1;
|
||||
return ERR_CORRUPTED;
|
||||
|
||||
}
|
||||
|
||||
@ -165,23 +165,19 @@ static int z_verify_lfh(const void* const file, const off_t lfh_ofs, const off_t
|
||||
|
||||
const u8* lfh = (const u8*)file + lfh_ofs;
|
||||
|
||||
// LFH signature doesn't match
|
||||
if(*(u32*)lfh != *(u32*)lfh_id)
|
||||
{
|
||||
debug_warn("z_verify_lfh: LFH signature not found");
|
||||
return -1;
|
||||
}
|
||||
return ERR_CORRUPTED;
|
||||
|
||||
const u16 lfh_fn_len = read_le16(lfh+26);
|
||||
const u16 lfh_e_len = read_le16(lfh+28);
|
||||
|
||||
const off_t lfh_file_ofs = lfh_ofs + LFH_SIZE + lfh_fn_len + lfh_e_len;
|
||||
|
||||
// CDFH and LFH are inconsistent =>
|
||||
// normal builds would return incorrect offsets.
|
||||
if(file_ofs != lfh_file_ofs)
|
||||
{
|
||||
debug_warn("z_verify_lfh: CDFH and LFH data differ! normal builds will"\
|
||||
"return incorrect file offsets. check Zip file!");
|
||||
return -1;
|
||||
}
|
||||
return ERR_CORRUPTED;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -296,7 +292,7 @@ static int z_extract_cdfh(const u8* cdfh, const ssize_t bytes_left, const char*&
|
||||
if(method & ~8)
|
||||
{
|
||||
debug_warn("z_extract_cdfh: unknown compression method");
|
||||
return -1;
|
||||
return ERR_NOT_SUPPORTED;
|
||||
}
|
||||
// .. this is a directory entry; we only want files.
|
||||
if(!csize_ && !ucsize_)
|
||||
|
@ -248,7 +248,7 @@ fail:
|
||||
int dir_cancel_watch(const intptr_t reqnum)
|
||||
{
|
||||
if(reqnum < 0)
|
||||
return -1;
|
||||
return ERR_INVALID_PARAM;
|
||||
|
||||
Watch* w = watches[reqnum];
|
||||
if(!w)
|
||||
@ -336,12 +336,12 @@ static int get_packet()
|
||||
|
||||
int dir_get_changed_file(char* fn)
|
||||
{
|
||||
// slight optimization: only call get_packet if queue is empty
|
||||
if(pending_events.empty())
|
||||
get_packet();
|
||||
// queue one or more events, or return 1 if none pending.
|
||||
CHECK_ERR(get_packet());
|
||||
|
||||
// nothing to return; call again later.
|
||||
if(pending_events.empty())
|
||||
return -1;
|
||||
return ERR_AGAIN;
|
||||
|
||||
const std::string& fn_s = pending_events.front();
|
||||
strcpy(fn, fn_s.c_str());
|
||||
|
@ -568,9 +568,6 @@ int munmap(void* const start, const size_t len)
|
||||
|
||||
int uname(struct utsname* un)
|
||||
{
|
||||
if(!un)
|
||||
return -1;
|
||||
|
||||
static OSVERSIONINFO vi;
|
||||
vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
||||
GetVersionEx(&vi);
|
||||
@ -653,9 +650,8 @@ long sysconf(int name)
|
||||
GetSystemInfo(&si); // can't fail => page_size always > 0.
|
||||
page_size = si.dwPageSize;
|
||||
|
||||
// import GlobalMemoryStatusEx
|
||||
// (used by _SC_*_PAGES if available -
|
||||
// it's not defined in the VC6 Platform SDK)
|
||||
// import GlobalMemoryStatusEx - it's not defined by the VC6 PSDK.
|
||||
// used by _SC_*_PAGES if available (provides better results).
|
||||
const HMODULE hKernel32Dll = LoadLibrary("kernel32.dll");
|
||||
*(void**)&pGlobalMemoryStatusEx = GetProcAddress(hKernel32Dll, "GlobalMemoryStatusEx");
|
||||
FreeLibrary(hKernel32Dll);
|
||||
@ -688,14 +684,13 @@ long sysconf(int name)
|
||||
// newer API is available: use it to report correct results
|
||||
// (no overflow or wraparound) on systems with > 4 GB of memory.
|
||||
MEMORYSTATUSEX mse = { sizeof(mse) };
|
||||
if(pGlobalMemoryStatusEx(&mse))
|
||||
if(pGlobalMemoryStatusEx && pGlobalMemoryStatusEx(&mse))
|
||||
{
|
||||
total_phys_mem = mse.ullTotalPhys;
|
||||
avail_phys_mem = mse.ullAvailPhys;
|
||||
}
|
||||
else
|
||||
// no matter though, we have the results from GlobalMemoryStatus.
|
||||
debug_warn("GlobalMemoryStatusEx failed - why?");
|
||||
// else: not an error, since this isn't available before Win2k / XP.
|
||||
// we have results from GlobalMemoryStatus anyway.
|
||||
|
||||
if(name == _SC_PHYS_PAGES)
|
||||
return (long)(round_up((uintptr_t)total_phys_mem, 2*MB) / page_size);
|
||||
|
@ -709,13 +709,7 @@ static void sleep_ns(i64 ns)
|
||||
|
||||
int clock_gettime(clockid_t clock, struct timespec* t)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
if(clock != CLOCK_REALTIME || !t)
|
||||
{
|
||||
debug_warn("clock_gettime: invalid clock or t param");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
assert(clock == CLOCK_REALTIME);
|
||||
|
||||
const i64 ns = time_ns();
|
||||
t->tv_sec = (time_t)(ns / _1e9);
|
||||
@ -726,13 +720,7 @@ int clock_gettime(clockid_t clock, struct timespec* t)
|
||||
|
||||
int clock_getres(clockid_t clock, struct timespec* ts)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
if(clock != CLOCK_REALTIME || !ts)
|
||||
{
|
||||
debug_warn("clock_getres: invalid clock or res param");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
assert(clock == CLOCK_REALTIME);
|
||||
|
||||
HRTImpl impl;
|
||||
double nominal_freq, res;
|
||||
@ -758,14 +746,6 @@ int gettimeofday(struct timeval* tv, void* tzp)
|
||||
{
|
||||
UNUSED(tzp);
|
||||
|
||||
#ifndef NDEBUG
|
||||
if(!tv)
|
||||
{
|
||||
debug_warn("gettimeofday: invalid t param");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
const long us = (long)(time_ns() / 1000);
|
||||
tv->tv_sec = (time_t) (us / _1e6);
|
||||
tv->tv_usec = (suseconds_t)(us % _1e6);
|
||||
|
@ -143,7 +143,7 @@ const wchar_t* HardcodedErrorString(int err)
|
||||
E(ERR_INVALID_PARAM)
|
||||
E(ERR_EOF)
|
||||
E(ERR_PATH_NOT_FOUND)
|
||||
E(ERR_VFS_PATH_LENGTH)
|
||||
E(ERR_PATH_LENGTH)
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
@ -205,7 +205,7 @@ static std::string SplitExts(const char *exts)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int WriteSysInfo()
|
||||
static void WriteSysInfo()
|
||||
{
|
||||
double t1 = get_time();
|
||||
get_gfx_info();
|
||||
@ -213,7 +213,7 @@ double t1 = get_time();
|
||||
get_snd_info();
|
||||
get_mem_status();
|
||||
double t2 = get_time();
|
||||
debug_out("INT TIME %g\n\n", t2-t1);
|
||||
debug_out("SYS DETECT TIME %g\n\n", t2-t1);
|
||||
|
||||
|
||||
struct utsname un;
|
||||
@ -221,7 +221,7 @@ debug_out("INT TIME %g\n\n", t2-t1);
|
||||
|
||||
FILE* const f = fopen("../logs/system_info.txt", "w");
|
||||
if(!f)
|
||||
return -1;
|
||||
return;
|
||||
|
||||
// .. OS
|
||||
fprintf(f, "%s %s (%s)\n", un.sysname, un.release, un.version);
|
||||
@ -276,7 +276,6 @@ debug_out("INT TIME %g\n\n", t2-t1);
|
||||
fprintf(f, "\nSupported extensions: \n%s\n", SplitExts(exts).c_str());
|
||||
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -27,7 +27,7 @@ bool VFSUtil::FindFiles (CStr dirname, const char* filter, FileList& files)
|
||||
files.push_back(dirname+"/"+entry.name);
|
||||
}
|
||||
|
||||
if (err != ERR_VFS_DIR_END)
|
||||
if (err != ERR_DIR_END)
|
||||
{
|
||||
LOG(ERROR, LOG_CATEGORY, "Error reading files from directory '%s' (%d)", dirname.c_str(), err);
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user